3
$\begingroup$

I am fairly new to graphics programming and I am trying to pass a custom Matrix4 (non-glm) to my vertex shader by using a uniform.

    Matrix44 transform;  //creates a 4x4 identity matrix
    transform.SetTranslation(Vector3(0.5f, 0.3f, 0.0f));
    GLint transformLocation = glGetUniformLocation(ourShader.Program, "transformation");
    glUniformMatrix4fv(transformLocation, sizeof(transform), GL_FALSE, &transform.m[0][0]);

Above is the code which I am using to send the matrix to the shaders.

(the .m allows me to access the matrix as a 2D array of 4x4 floats);

This is how my vertex shader looks like:

#version 430 core

layout (location = 0) in vec3 position;

uniform mat4 transform;

void main()
{                   
   gl_Position = transform*vec4(position,1.0f);     
}

When I run it, I don't get anything drawn on my screen, except the background color.

Below is the matrix which I send to the shader. I am using column major, so as far as I've read this should be the translation part of the 4x4. I've also tried transposing the matrix with no luck whatsoever.

enter image description here

The last clue I have is that the values which I am sending are 0s. I came to this conclusion because I tried playing with the fragment shader and I set the color to be equal to the last column from the uniform mat4:

#version 430 core
out vec4 color;

uniform mat4 transform;

void main()
{
 color = vec4(transform[0][3],transform[1][3], transform[2][3], transform[3][3]);   
} 

Which gives me a black triangle i.e. the color vec4 is filled with 0s.

enter image description here

I apologize in advance if the question is too noob-ish but I've been struggling with this for several days and I could not find a way to run it :(

Any advise would be appreciated!

$\endgroup$

1 Answer 1

3
$\begingroup$
glUniformMatrix4fv(transformLocation, sizeof(transform), GL_FALSE, &transform.m[0][0]);

The second parameter to glUniformMatrix is not the number of bytes in the data. OpenGL can figure that out from the 4fv part of the command (a 4x4 matrix of GLfloats). The second parameter is the number of matrices you're sending to that uniform, in case it is an array of mat4s.

So that value ought to be 1. You almost certainly got a GL_INVALID_OPERATION error from calling it with sizeof(transform).

$\endgroup$
1
  • $\begingroup$ Thank you for your reply Nicol. I`ve fixed the argument mistake and found out the root cause. I have specified 'transformation' as my uniform value and was trying to use 'transform'.... It works llike a charm now. $\endgroup$
    – Jigo
    Commented Jan 21, 2017 at 0:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.