"We offer new support options and therefor the forums are now in read-only mode! Please check out our Support Center for more information." - Vuforia Engine Team

Unable apply lighting

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]);

 

 

AlessandroB

Tue, 02/19/2013 - 09:31

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...) ?

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;

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.

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);

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

glEnableVertexAttribArray(mColorHandle);

then you should also use (consistently):

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.

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?

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