将统一的4x4矩阵传递给顶点着色器程序 [英] Passing uniform 4x4 matrix to vertex shader program
问题描述
我正在尝试学习OpenGL,并遵循以下步骤: http://www.opengl-tutorial.org/beginners-tutorials/tutorial-3-matrices/
I am trying to learn OpenGL and following this: http://www.opengl-tutorial.org/beginners-tutorials/tutorial-3-matrices/
直到他们开始将矩阵传递到顶点着色器以平移它们所遵循的三角形为止.
Up until the point where they started passing matrices to the vertex shader to translate the triangle they where drawing I was following along.
这是着色程序开始出错的地方:
This is the shader program where it starts to go wrong:
#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;
}
如果我使用注释掉的代码而不是行gl_Position = MVP * v;
,则一切正常.屏幕上将绘制一个三角形.我还可以更改为M
矩阵,以移动三角形并缩放三角形.
If I use the commented out code instead of the line gl_Position = MVP * v;
, everything works. A triangle is drawn to the screen. I can also change to M
matrix to move the triangle around and scale it.
为了使事情尽可能简单,我只是在MVP字段中向顶点着色器传递了一个单位矩阵.代码如下:
In order to keep things as simple as possible I am just passing the vertex shader an identity matrix in the MVP field. The code looks like this:
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]);
我使用的是linmath.h而不是glm( https://github.com/datenwolf/linmath. h ).循环显示:
I am using linmath.h instead of glm (https://github.com/datenwolf/linmath.h). The loop 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,
并确认MVP确实是一个单位矩阵.但是,当我运行该程序时,在屏幕上看不到三角形.只有背景(深蓝色顺便说一句).
And confirms that MVP is indeed an identity matrix. However when I run the program, I see no triangle on the screen. Only the background (which is dark blue btw).
您可以在这里看到更多的主要C程序: https://gist.github.com/avwhite/68580376ddf9a7ec9cb7
You can see slightly more of the main C program here: https://gist.github.com/avwhite/68580376ddf9a7ec9cb7
如果需要,我还可以提供主程序,顶点着色器和片段着色器的完整源代码.
If needed I can also provide the whole source code for the main program, the vertex-, and the fragment shader.
我认为这与将MVP矩阵传递给着色器程序有关,但是我是OpenGL的新手,所以我真的不知道发生了什么.
I am thinking this has something to do with how the MVP matrix is passed to the shader program, but I am completely new to OpenGL, so I really have no idea what is going on.
推荐答案
glUniform*()
调用当前程序的设置值.您需要先glUseProgram()
呼叫glUniform*()
.在此示例中:
glUniform*()
calls set values for the current program. You need to call glUseProgram()
before the glUniform*()
call. In this example:
glUseProgram(programID);
glUniformMatrix4fv(MatrixID, 1, GL_FALSE, &MVP[0][0]);
这篇关于将统一的4x4矩阵传递给顶点着色器程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!