Passing a 4x4 uniform matrix to a vertex shader program

I am trying to learn OpenGL in the following way: http://www.opengl-tutorial.org/beginners-tutorials/tutorial-3-matrices/

Until they begin to transfer matrices to the vertex shader to translate the triangle in which we drew the drawing.

This is a shader program in which it starts to make mistakes:

#version 330 core layout(location = 0) in vec3 vertexPosition_modelspace; uniform mat4 MVP; void main(){ vec4 v = vec4(vertexPosition_modelspace,1); // Transform an homogeneous 4D vector gl_Position = MVP * v; //mat4 M = mat4( // vec4(1.0, 0.0, 0.0, 0.0), // vec4(0.0, 1.0, 0.0, 0.0), // vec4(0.0, 0.0, 1.0, 0.0), // vec4(0.0, 0.0, 0.0, 1.0) //); //gl_Position = M * v; } 

If I use the code with comments instead of the line gl_Position = MVP * v; , everything works. A triangle is drawn on the screen. I can also change to M matrix to move the triangle around and scale it.

To simplify things as much as possible, I just pass the vertex shader of the identity matrix into the MVP field. The code is as follows:

 mat4x4 MVP; mat4x4_identity(MVP); int i,j; for(i=0; i<4; ++i) { for(j=0; j<4; ++j) printf("%f, ", MVP[i][j]); printf("\n"); } GLuint MatrixID = glGetUniformLocation(programID, "MVP"); // Send our transformation to the currently bound shader, // in the "MVP" uniform // For each model you render, since the MVP will be different (at least the M part) glUniformMatrix4fv(MatrixID, 1, GL_FALSE, &MVP[0][0]); 

I use linmath.h instead of glm ( https://github.com/datenwolf/linmath.h ). The cycle prints:

 1.000000, 0.000000, 0.000000, 0.000000, 0.000000, 1.000000, 0.000000, 0.000000, 0.000000, 0.000000, 1.000000, 0.000000, 0.000000, 0.000000, 0.000000, 1.000000, 

And it confirms that MVP is indeed a unit matrix. However, when I run the program, I do not see a triangle on the screen. Background only (blue).

Here you can see a little more of the main program: https://gist.github.com/avwhite/68580376ddf9a7ec9cb7

If necessary, I can also provide all the source code for the main program, vertex and fragment shader.

I think this has something to do with how the MVP matrix is ​​passed to the shader program, but I'm completely new to OpenGL, so I really don't know what is going on.

+5
source share
1 answer

glUniform*() calls the given values ​​for the current program. You need to call glUseProgram() before calling glUniform*() . In this example:

 glUseProgram(programID); glUniformMatrix4fv(MatrixID, 1, GL_FALSE, &MVP[0][0]); 
+11
source

Source: https://habr.com/ru/post/1200482/


All Articles