# Shader error and Identity Matrix

• 04-28-2013, 12:30 AM
Alecto
Hi. I have a simple shader line gl_Position = u_MVPMatrix *(u_InverseBindMatrix) *(a_Position); I have u_inversebindmatrix set to Identity matrix using Matrix.setidentityM(); If I render without the line *u_InverseBindMatrix, then my cube shows up. If I leave it in, however, nothing shows up. I thought the identity matrix left equations unchanged. Any ideas?
• 04-28-2013, 02:10 AM
Alfonse Reinheart
You might want to consider that you're not setting the matrix properly. Of course, we can't actually know that since you didn't show us your code...
• 04-28-2013, 07:06 AM
Alecto
This is the shader and the draw code.
Code :

``` protected String getVertexShader() { // TODO: Explain why we normalize the vectors, explain some of the vector math behind it all. Explain what is eye space. final String vertexShader = "uniform mat4 u_MVPMatrix; \n" // A constant representing the combined model/view/projection matrix. + "uniform mat4 u_MVMatrix; \n" // A constant representing the combined model/view matrix. + "uniform vec3 u_LightPos; \n" // The position of the light in eye space. +" uniform mat4 u_BindShapeMatrix; \n" +" uniform mat4 u_InverseBindMatrix; \n" +"uniform mat4 a_Bone; \n" //bone matrix + "attribute vec4 a_Position; \n" // Per-vertex position information we will pass in. + "attribute vec4 a_Color; \n" // Per-vertex color information we will pass in. + "attribute vec3 a_Normal; \n" // Per-vertex normal information we will pass in.   + "varying vec4 v_Color; \n" // This will be passed into the fragment shader.   + "void main() \n" // The entry point for our vertex shader. + "{ \n" // Transform the vertex into eye space. + " vec3 modelViewVertex = vec3(u_MVMatrix * a_Position); \n" // Transform the normal's orientation into eye space. + " vec3 modelViewNormal = vec3(u_MVMatrix * vec4(a_Normal, 0.0)); \n" // Will be used for attenuation. + " float distance = length(u_LightPos - modelViewVertex); \n" // Get a lighting direction vector from the light to the vertex. + " vec3 lightVector = normalize(u_LightPos - modelViewVertex); \n" // Calculate the dot product of the light vector and vertex normal. If the normal and light vector are // pointing in the same direction then it will get max illumination. + " float diffuse = max(dot(modelViewNormal, lightVector), 0.1); \n" // Attenuate the light based on distance. //+ " diffuse = diffuse * (1.0 / (1.0 + (0.25 * distance * distance))) ; \n" // Multiply the color by the illumination level. It will be interpolated across the triangle. + " v_Color = a_Color * diffuse; \n" // gl_Position is a special variable used to store the final position. // Multiply the vertex by the matrix to get the final point in normalized screen coordinates. + " gl_Position = u_MVPMatrix *u_InverseBindMatrix *a_Position; \n" + "} \n";   return vertexShader; }```
Code :

``` public void onDrawFrame(GL10 glUnused) { GLES20.glClear(GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);     GLES20.glUseProgram(mPerVertexProgramHandle); // Set program handles for cube drawing. mMVPMatrixHandle = GLES20.glGetUniformLocation(mPerVertexProgramHandle, "u_MVPMatrix"); mMVMatrixHandle = GLES20.glGetUniformLocation(mPerVertexProgramHandle, "u_MVMatrix"); mLightPosHandle = GLES20.glGetUniformLocation(mPerVertexProgramHandle, "u_LightPos"); mPositionHandle = GLES20.glGetAttribLocation(mPerVertexProgramHandle, "a_Position"); mColorHandle = GLES20.glGetAttribLocation(mPerVertexProgramHandle, "a_Color"); mNormalHandle = GLES20.glGetAttribLocation(mPerVertexProgramHandle, "a_Normal"); mBoneMatHandle = GLES20.glGetUniformLocation(mPerVertexProgramHandle, "a_Bone"); BindMatrixHandle = GLES20.glGetUniformLocation(mPerVertexProgramHandle, "u_BindShapeMatrix"); IBMHandle = GLES20.glGetUniformLocation(mPerVertexProgramHandle, "u_InverseBindShape"); // public int BindMatrixHandle; //mBoneMat = animations[0];   //mBoneMat ={1, 0, 0, 5, 0, 1, 0, 5, 0 , 0, 1, 0, 0, 0, 0, 1}; long time = SystemClock.uptimeMillis() % 10000L;     LastTime = ThisTime; ThisTime = SystemClock.elapsedRealtime();   // ElapsedTime = ThisTime - LastTime; GameTime += ThisTime;   RealTime = ThisTime/1000;   // Log.d("Time", Float.toString(ThisTime));   // for(int i = 0; i<11; i++) // { for(int j = 0; j<16; j++) { CombinedBone[j] = Bone[j] * animations[animCounter][j];       } animCounter++; if(animCounter == 11) animCounter = 0;   // Log.d("Counter", Integer.toString(animCounter));   //}   // Log.d("Time", Float.toString(GameTime));         float angleInDegrees = (360.0f / 10000.0f) * ((int) time);   // Calculate position of the light. Rotate and then push into the distance. Matrix.setIdentityM(mLightModelMatrix, 0); Matrix.translateM(mLightModelMatrix, 0, 0.0f, 0.0f, -15.0f); Matrix.rotateM(mLightModelMatrix, 0, angleInDegrees, 0.0f, 1.0f, 0.0f); Matrix.translateM(mLightModelMatrix, 0,-15.0f, 0.0f, 2.0f);   Matrix.multiplyMV(mLightPosInWorldSpace, 0, mLightModelMatrix, 0, mLightPosInModelSpace, 0); Matrix.multiplyMV(mLightPosInEyeSpace, 0, mViewMatrix, 0, mLightPosInWorldSpace, 0);       Matrix.setIdentityM(mModelMatrix, 0); drawCube(); } private void drawCube() { mVertices.position(0); GLES20.glVertexAttribPointer(mPositionHandle, mPositionDataSize, GLES20.GL_FLOAT, false, 0, mVertices);   GLES20.glEnableVertexAttribArray(mPositionHandle);   // Pass in the color information mColors.position(0); GLES20.glVertexAttribPointer(mColorHandle, mColorDataSize, GLES20.GL_FLOAT, false, 0, mColors);   GLES20.glEnableVertexAttribArray(mColorHandle);   // Pass in the normal information mNormals.position(0); GLES20.glVertexAttribPointer(mNormalHandle, mNormalDataSize, GLES20.GL_FLOAT, false, 0, mNormals);   GLES20.glEnableVertexAttribArray(mNormalHandle);   // This multiplies the view matrix by the model matrix, and stores the result in the MVP matrix // (which currently contains model * view). Matrix.multiplyMM(mMVPMatrix, 0, mViewMatrix, 0, mModelMatrix, 0);   // Pass in the modelview matrix. GLES20.glUniformMatrix4fv(mMVMatrixHandle, 1, false, mMVPMatrix, 0);   // This multiplies the modelview matrix by the projection matrix, and stores the result in the MVP matrix // (which now contains model * view * projection). Matrix.multiplyMM(mMVPMatrix, 0, mProjectionMatrix, 0, mMVPMatrix, 0);   // Pass in the combined matrix. GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, mMVPMatrix, 0);   //Matrix.multiplyMM(Bone, 0, controllerMat, 0, Bone,0); //Matrix.multiplyMM(Bone, 0, mBindMatrix, 0, Bone, 0); GLES20.glUniformMatrix4fv(mBoneMatHandle, 1, false, Identity, 0);   Matrix.setIdentityM(Identity, 0); GLES20.glUniformMatrix4fv(IBMHandle, 1, false, Identity, 0); GLES20.glUniformMatrix4fv(BindMatrixHandle, 1, false, mBindMatrix, 0); // Pass in the light position in eye space. GLES20.glUniform3f(mLightPosHandle, mLightPosInEyeSpace[0], mLightPosInEyeSpace[1], mLightPosInEyeSpace[2]);   // Draw the cube. GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0,72);       }```
• 04-28-2013, 07:08 AM
Alecto
it wont let me post the full code for some reason. Maybe there is a forbidden word somewhere in it.
• 05-03-2013, 11:35 PM
Alecto
Of course you can multiply by an Identity matrix. Thep roblem was that my handle was looking for a variable in the shader that wasn't there. Took me 2 weeks to find that error, wow. I see why no one could help me who would look at this crap code and catch that. Thanks for the views anyway.