1

I have been working on skeletal animation in the game engine I am creating in lwjgl. I can render entities that are not animated but animated entities simply will not draw, which makes it very hard to debug using the methods suggested here. How can I debug a vertex shader when nothing is drawing?


Here is the vertex shader:

Animated Vertex Shader

#version 330 core

in vec3 position;
in vec2 textureCoordinates;
in vec3 normal;
in vec4 bones;
in vec4 weights;

out vec2 pass_textureCoordinates;
out vec3 surfaceNormal;
out vec3 toLightVector;
out vec3 toCameraVector;

uniform mat4 transformationMatrix;
uniform mat4 projectionMatrix;
uniform mat4 viewMatrix;
uniform vec3 lightPosition;

uniform mat4 joints[100];

void main(void){

    vec4 originalPosition = vec4(position, 1.0);

    int index = int(bones.x);
    vec4 animatedPosition = (joints[index] * originalPosition) * weights.x;
    vec4 animatedNormal = (joints[index] * vec4(normal, 0.0)) * weights.x;

    index = int(bones.y);
    animatedPosition = (joints[index] * originalPosition) * weights.y;
    animatedNormal = (joints[index] * vec4(normal, 0.0)) * weights.y;

    index = int(bones.z);
    animatedPosition = (joints[index] * originalPosition) * weights.z;
    animatedNormal = (joints[index] * vec4(position,1.0)) * weights.z;

    index = int(bones.w);
    animatedPosition = (joints[index] * originalPosition) * weights.w;
    animatedNormal = (joints[index] * vec4(normal, 0.0)) * weights.w;

    vec4 worldPosition = transformationMatrix * vec4(animatedPosition.xyz,1.0);
    gl_Position = projectionMatrix * viewMatrix * worldPosition;
    pass_textureCoordinates = textureCoordinates;
    surfaceNormal = (transformationMatrix * vec4(animatedNormal.x,animatedNormal.y,animatedNormal.z,0.0)).xyz;
    toLightVector = lightPosition - worldPosition.xyz;
    toCameraVector = (inverse(viewMatrix) * vec4(0.0,0.0,0.0,1.0)).xyz - worldPosition.xyz;
}

EDIT:

So there is a possibility the error is in uploading the joints matrix array. Just in case, here is my loadMatrices method:

Loading Matrices

protected void loadMatrices(int location, Matrix4f matrices[]){
    FloatBuffer matrixBuffer = BufferUtils.createFloatBuffer(matrices.length * 16);

    for(int i = 0; i < matrices.length; i++){
        if(matrices[i] != null){
            matrices[i].store(matrixBuffer);
        }
    }
    matrixBuffer.flip();
    GL20.glUniformMatrix4(location, false, matrixBuffer);
}

UPDATE:

Using the advice from jozxyqk, simplified my shader to make sure it is running. All I did was change this:

vec4 worldPosition = transformationMatrix * vec4(animatedPosition.xyz,1.0);

To this:

vec4 worldPosition = transformationMatrix * vec4(position.xyz,1.0);

Now it renders the entity perfectly fine (not using armature of course) So now it will be a lot easier for me to debug. Unfortunately I will be able to start debugging right now but I will be in the car for a long time tomorrow so I will debug then and I will be posting updates. Thanks so much jozxyqk!

UPDATE:

I got it working, the problem was that I forgot to activate some attribute arrays. Thanks!

Community
  • 1
  • 1
Derek Lesho
  • 318
  • 1
  • 3
  • 10
  • possible duplicate of [How to debug a GLSL shader?](http://stackoverflow.com/questions/2508818/how-to-debug-a-glsl-shader) – jozxyqk Jun 14 '15 at 03:13
  • This is not duplicate, in the other article which I have reread over 9000 times they say to output info as color when I cant because it is not drawing and i am using lwjgl so glsldevil doesnt work – Derek Lesho Jun 14 '15 at 03:53
  • OK, I've edit the question. It's on the border of "why doesn't this code work", but I think a question regarding debugging transforms is quite valid. I'd start by drawing x/y/z axes for all the joints to make sure the matrices are correct. Then apply only one join transform to your vertices, without weights. Work from there. In general, strip down the shader until it *has* to work. Then add features back in one at a time. – jozxyqk Jun 14 '15 at 04:14
  • One problem in the code is that you're only applying the last of the 4 joint transformations. The result of the first 3 assignments to `animatedPosition` is never used. – Reto Koradi Jun 14 '15 at 19:16
  • Thanks, I will change that! – Derek Lesho Jun 14 '15 at 19:21

0 Answers0