Log in or register to post comments

Unable apply lighting

February 19, 2013 - 12:59am #1

Hi guys, I would like to add the lighting to the rendered cube (I got from http://heikobehrens.net/2009/08/27/obj2opengl/)  but when I started the apps, I got black screen. Does anybody know what's matter? here are my snippet codes. Thanks for any helps

 

//CubeShaders.h


static const char* cubeMeshVertexShader = " \
  \
attribute vec4 vertexPosition; \
attribute vec4 vertexNormal; \
attribute vec2 vertexTexCoord; \
attribute vec4 a_color; \
 \
varying vec2 texCoord; \
varying vec4 normal; \
varying vec4 v_color; \
 \
uniform mat4 modelViewProjectionMatrix; \
uniform mat4 u_MVMatrix; \
uniform vec3 u_lightPos; \
 \
void main() \
{ \
 gl_Position = modelViewProjectionMatrix * vertexPosition; \
 vec3 modelViewVertex = vec3 (u_MVMatrix * vertexPostition); \
vec3 modelViewNormal = vec3 (u_MVMatrix * vertexNormal); \
float distance = length(u_lightPos - modelViewVertex); \
vec3 lightVector = normalize(u_lightPos - modelViewVertex); \
float diffuse = max(dot(modelViewNormal, ligthVector), 0.1); \
diffuse = diffuse * (1.0/(0.25*distance*distance))); \
  v_color = a_color * diffuse; \
   normal = vertexNormal; \
   texCoord = vertexTexCoord; \
} ";


static const char* cubeFragmentShader = " \
 \
precision mediump float; \
 \
varying vec2 texCoord; \
varying vec4 normal; \
varying vec4 v_color; \
 \
uniform sampler2D texSampler2D; \
 \
void main() \
{ \
   gl_FragColor = v_color; \
} \
";


//Some handles

 vertexHandle        = glGetAttribLocation(shaderProgramID,
                                                "vertexPosition");
    normalHandle        = glGetAttribLocation(shaderProgramID,
                                                "vertexNormal");
    textureCoordHandle  = glGetAttribLocation(shaderProgramID,
                                                "vertexTexCoord");
    mvpMatrixHandle     = glGetUniformLocation(shaderProgramID,
                                                "modelViewProjectionMatrix");
    texSampler2DHandle  = glGetUniformLocation(shaderProgramID, 
                                                "texSampler2D");
    mMVMatrixHandle = glGetUniformLocation(shaderProgramID, "u_MVMatrix");
    mLightPosHandle = glGetUniformLocation(shaderProgramID, "u_LightPos");
    mColorHandle = glGetAttribLocation(shaderProgramID, "a_Color");

// LightPos

QCAR::Matrix44F  mLightModelMatrix = SampleMath::Matrix44FIdentity();
QCAR::Matrix44F mLightPosInModelSpace = SampleMath::Matrix44FIdentity();

          SampleUtils::translatePoseMatrix(0.5f, 0.5f, -3.0f,
                                                  &mLightModelMatrix.data[0]);
          SampleUtils::multiplyMatrix(&mLightModelMatrix.data[0],
                                             &mLightPosInModelSpace.data[0] ,
                                             &mLightPosInWorldSpace.data[0]);
          SampleUtils::multiplyMatrix(&modelViewMatrix.data[0],
                                                       &mLightPosInWorldSpace.data[0] ,
                                                       &mLightPosInEyeSpace.data[0]);

// Pass in  LightPos to programShader
 glUniform3f(mLightPosHandle, mLightPosInEyeSpace.data[12], mLightPosInEyeSpace.data[13], mLightPosInEyeSpace.data[14]);

 

 

Unable apply lighting

April 8, 2015 - 4:44am #17

I have been looking for the linked tutorial on basic diffuse lighting, but it seems to have been removed.

Is there a new location to access it? I would really appreciate if that content was available, since there are many links to it floating around but they all lead to nothing, it's very frustrating and I have spent days trying to get my diffuse lighting right...

Unable apply lighting

February 25, 2013 - 2:27am #16

Hi, could you create a new Thread for this "animation" topic, otherwise we risk to deviate too much from the original topic.

thank you.

Unable apply lighting

February 25, 2013 - 2:23am #15

Hi, does unity3d deployed  by creating kind of scenario? is it possible  to update my virtual object during run time by using data from the movement of real device like simulating the movement of the robot arm? (animation which is not planned before). thanks

Unable apply lighting

February 24, 2013 - 11:58pm #14

Hi, glad to hear the problem is solved.

Concerning your second question about animations, you have plenty of possitbilities, but that's a very broad subject; the only suggestion I would give here is to consider using Unity3D instead of programming natively in Android, as it will really save you a lot of development time when it comes to 3D animations.

Unable apply lighting

February 24, 2013 - 11:33pm #13

Hi AlessandroB. Thanks for helping me. I figured out my lighting problem and it worked fine after reversing my lightPos. By the way, I'm new to 3D graphic. I want to know what is the easy and best tools to create the animation of 3D object that can be controlled by the user during run time? such as dragging the object by touching the screen , moving the object that configured with the real device (Robotic simulation in real time) and etc.

Thanks

Unable apply lighting

February 22, 2013 - 6:01am #12

Hi, I see two possible reasons:

diffuse = diffuse * (1.0/(0.25*distance*distance)));

In this equation, distance could be potentially very large (because the scene is in millimeters) and as a consequence your diffuse factor might get close to zero (thus giving you black color).

The second possible issue I see is in that you set the light position in eye coordinates (based on the name of the variable that you pass via glUniform), but then you multiply the lightPos once again by the modelViewNormal (which should not be needed if you already expressed your light in eye coordinates).

These are the two things that may go wrong, for the rest the shader looks good.

Other than that, if you cannot sort it out, I would recommend using (and adjusting a bit to your needs) the Diffuse shader that I propose in the Tutorial I was referring to.

 

 

Unable apply lighting

February 22, 2013 - 2:02am #11

hi, the value of second argument of   glVertexAttribPointer() for color cube should be 4 right?  Also,  I have set the color of my cube to be Red and it appeared to be red without lighting effect, but when I applied my ligthing in shader, the color of my cube became black. I also check the logcat and shader get compiled successfully. Do you know what might be the issue? thanks

Unable apply lighting

February 22, 2013 - 12:41am #10

Hi, the problem seems to be in your color array; if you use:

glEnableVertexAttribArray(mColorHandle);

then you should also use (consistently):

glVertexAttribPointer(mColorHandle, 4, GL_FLOAT, GL_FALSE, 0, 
                   (const GLvoid*) &cubeColor[0]);

instead of glVertexAttrib4fv(mColorHandle,   &cubeColor[0]);

Then, make sure that your cubeColor array has 36 elements (i.e. 36 x 4 components R,G,B,A), same as your vertex array and normal array; alternatively, you can also try by removing completely the color array, the mColorHandle and the a_Color and v_color from your shader and just use normals (and use some constant in the shader instead of a_color or v_color, such as vec4(1.0,0.0,0.0,1.0));

Unable apply lighting

February 22, 2013 - 12:31am #9
Yes, indeed because I want to use constant value to set the color. 
here is my rendering code. Do you notice something wrong?
glVertexAttribPointer(vertexHandle, 3, GL_FLOAT, GL_FALSE, 0,
                              (const GLvoid*) &cubeVerts[0]);
        glVertexAttribPointer(normalHandle, 3, GL_FLOAT, GL_FALSE, 0,
                              (const GLvoid*) &cubeNormals[0]);
        glVertexAttrib4fv(mColorHandle,   &cubeColor[0]);
        //glVertexAttribPointer(mColorHandle, 3, GL_FLOAT, GL_FALSE, 0,
                          //         (const GLvoid*) &cubeColor[0]);


        glEnableVertexAttribArray(vertexHandle);
        glEnableVertexAttribArray(normalHandle);
        glEnableVertexAttribArray(mColorHandle);
        //glEnableVertexAttribArray(textureCoordHandle);
        
        //glActiveTexture(GL_TEXTURE0);
        //glBindTexture(GL_TEXTURE_2D, thisTexture->mTextureID);
        glUniform1i(texSampler2DHandle, 0 /*GL_TEXTURE0*/);
        glUniformMatrix4fv(  mvpMatrixHandle , 1, GL_FALSE,
                           (GLfloat*)&modelViewProjection.data[0]);
        glUniformMatrix4fv( mMVMatrixHandle , 1, GL_FALSE,
                                   (GLfloat*)&modelViewMatrix.data[0]);

       // glUniform3f(diffuseMaterialHandle, 1.0f, 1.0f, 1.0f);
        // Pass in the light position in eye space.
       glUniform3f(mLightPosHandle, mLightPosInEyeSpace.data[12], mLightPosInEyeSpace.data[13], mLightPosInEyeSpace.data[14]);

        //glDrawElements(GL_TRIANGLES, NUM_TEAPOT_OBJECT_INDEX, GL_UNSIGNED_SHORT,
                     //  (const GLvoid*) &teapotIndices[0]);


        glDrawArrays(GL_TRIANGLES, 0, cubeNumVerts);

 

Unable apply lighting

February 21, 2013 - 9:36am #8

Hi, the color array is just 4 elements long; if you really want to use the color array, you should make sure that it contains the same number of elements as the vertices and the normals (unless you are not using it);

also, can you share your rendering code (the part which contains the glVertexAttribPointer(), glDrawArrays() or glDrawElements(),  etc...); there might be something wrong there as well (potentially).

 

 

Unable apply lighting

February 21, 2013 - 9:25am #7

Yes, I did. Here is my virtual object data. Is my data okay?

 

unsigned int cubeNumVerts = 36;

float cubeVerts [] = {
  // f  1//2  7//2  5//2
  -0.5, -0.5, -0.5,
  0.5, 0.5, -0.5,
  0.5, -0.5, -0.5,
  // f  1//2  3//2  7//2 
  -0.5, -0.5, -0.5,
  -0.5, 0.5, -0.5,
  0.5, 0.5, -0.5,
  // f  1//6  4//6  3//6 
  -0.5, -0.5, -0.5,
  -0.5, 0.5, 0.5,
  -0.5, 0.5, -0.5,
  // f  1//6  2//6  4//6 
  -0.5, -0.5, -0.5,
  -0.5, -0.5, 0.5,
  -0.5, 0.5, 0.5,
  // f  3//3  8//3  7//3 
  -0.5, 0.5, -0.5,
  0.5, 0.5, 0.5,
  0.5, 0.5, -0.5,
  // f  3//3  4//3  8//3 
  -0.5, 0.5, -0.5,
  -0.5, 0.5, 0.5,
  0.5, 0.5, 0.5,
  // f  5//5  7//5  8//5 
  0.5, -0.5, -0.5,
  0.5, 0.5, -0.5,
  0.5, 0.5, 0.5,
  // f  5//5  8//5  6//5 
  0.5, -0.5, -0.5,
  0.5, 0.5, 0.5,
  0.5, -0.5, 0.5,
  // f  1//4  5//4  6//4 
  -0.5, -0.5, -0.5,
  0.5, -0.5, -0.5,
  0.5, -0.5, 0.5,
  // f  1//4  6//4  2//4 
  -0.5, -0.5, -0.5,
  0.5, -0.5, 0.5,
  -0.5, -0.5, 0.5,
  // f  2//1  6//1  8//1 
  -0.5, -0.5, 0.5,
  0.5, -0.5, 0.5,
  0.5, 0.5, 0.5,
  // f  2//1  8//1  4//1 
  -0.5, -0.5, 0.5,
  0.5, 0.5, 0.5,
  -0.5, 0.5, 0.5,
};

float cubeColor[] = {
		// Red Color
		1.0, 0.0, 0.0, 1.0

};

float cubeNormals [] = {
  // f  1//2  7//2  5//2
  0, 0, -1,
  0, 0, -1,
  0, 0, -1,
  // f  1//2  3//2  7//2 
  0, 0, -1,
  0, 0, -1,
  0, 0, -1,
  // f  1//6  4//6  3//6 
  -1, 0, 0,
  -1, 0, 0,
  -1, 0, 0,
  // f  1//6  2//6  4//6 
  -1, 0, 0,
  -1, 0, 0,
  -1, 0, 0,
  // f  3//3  8//3  7//3 
  0, 1, 0,
  0, 1, 0,
  0, 1, 0,
  // f  3//3  4//3  8//3 
  0, 1, 0,
  0, 1, 0,
  0, 1, 0,
  // f  5//5  7//5  8//5 
  1, 0, 0,
  1, 0, 0,
  1, 0, 0,
  // f  5//5  8//5  6//5 
  1, 0, 0,
  1, 0, 0,
  1, 0, 0,
  // f  1//4  5//4  6//4 
  0, -1, 0,
  0, -1, 0,
  0, -1, 0,
  // f  1//4  6//4  2//4 
  0, -1, 0,
  0, -1, 0,
  0, -1, 0,
  // f  2//1  6//1  8//1 
  0, 0, 1,
  0, 0, 1,
  0, 0, 1,
  // f  2//1  8//1  4//1 
  0, 0, 1,
  0, 0, 1,
  0, 0, 1,
};

the only stuff I saw is just a black cube without lighting effect, although I set it to red color.

Unable apply lighting

February 21, 2013 - 8:53am #6

Hi, does your mesh have normals? That's the only "big" issue I could imagine;

other than that, make sure you call all the necessary glUniform() functions for all the parameters involved in the shader, and then it should work (the little tutorial that I pointed you to, has been tested and it provides a basic, but fairly visible, diffuse lighting effect...).

 

Unable apply lighting

February 21, 2013 - 8:42am #5

Hi, yes it's true. I did mistake in typing shader's component after seeing from logcat. I edited those and my ImageTarget run properly but I did not see any lighting effect on my virtual object. I also give a try to the Lighting example provided by yours, and the got the same result. Do you have any idea of what I might be missing?

Thanks

Unable apply lighting

February 19, 2013 - 8:20am #4

Hi, I did not notice any issue with your shader code;

however sometimes a small unnoticeable "typo" error can be sufficient to make it not work;

I would suggest to check if the shader actually gets compiled (you can check the Logs in the shader creation function in SampleUtils.cpp).

 

Unable apply lighting

February 19, 2013 - 3:03am #3

Hi thanks for replying.
 

Yes, I have them

 

  glUniformMatrix4fv(mvpMatrixHandle, 1, GL_FALSE,
                           (GLfloat*)&modelViewProjection.data[0]);
        glUniformMatrix4fv(mMVMatrixHandle, 1, GL_FALSE,
                                   (GLfloat*)&modelViewMatrix.data[0]);

I have read your tutorial also, but do you notice any mistake of mine? I wonder why it did not work. Thankyou

Unable apply lighting

February 19, 2013 - 1:31am #2

Hi,

in your code I don't see the relevant lines to pass the u_MVMatrix and modelViewProjectionMatrix uniform parameters to the shader (perhaps you have the code but you did not put it in your post...) ?

Other than that, we have a small tutorial explaining step-by-step how to setup basic diffuse lighting in ImageTargets sample: 

https://developer.vuforia.com/resources/dev-guide/enabling-lighting-effects

This has been tested and I know it works (and it is really very similar to what you are doing in your code);

you may want to read that tutorial, implement the steps and the code snippets explained there, or otherwise you can also just compare the code in the tutorial with your own implementation.

You should be able to sort it out.

Note: your mesh must contain normals and texture coordinates of course.

 

Log in or register to post comments