How to debug transforms in glsl vertex shaders in lwjgl

355 views Asked by At

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!

0

There are 0 answers