2013-02-21 80 views
-1

我正在使用GLM来管理我的矩阵,但我遇到了一些对我没有意义的问题。当我将投影矩阵设置为单位矩阵以外的任何东西时,我看不到我想要绘制的正方形。如果它是一个身份,它将起作用。我的视图矩阵出现了类似的情况。如果我尝试翻译过-1或+1的方块会消失,否则它看起来没有效果。OpenGL 3.2麻烦设置矩阵

没有OpenGL错误,GLSL链接器/编译器错误,并且glGetUniformLocation返回有效位置。着色器程序也正确使用。

另外我测试了着色器,看它是否得到传递给每个矩阵的正确值(通过改变正方形的颜色,如果该值是正确的)。

下面是如何设置的投影矩阵:

projectionMatrix = glm::perspective(60.0f, (float)windowWidth/(float)windowHeight, 0.1f, 100.0f); 

这是我的绘制函数:

void OpenGLContext::render(void) { 
glViewport(0, 0, windowWidth, windowHeight); // Set the viewport size to fill the window 
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); // Clear required buffers 

//Set up matrices 
viewMatrix = glm::translate(glm::mat4(1.0f), glm::vec3(0.0f, 0.0f, -5.0f)); 
modelMatrix = glm::scale(glm::mat4(1.0f), glm::vec3(.5f)); 

shader->bind(); 

int projectionMatrixLocation = glGetUniformLocation(shader->id(), "projectionMatrix"); 
int viewMatrixLocation = glGetUniformLocation(shader->id(), "viewMatrix"); 
int modelMatrixLocation = glGetUniformLocation(shader->id(), "modelMatrix"); 

glUniformMatrix4fv(projectionMatrixLocation, 1, GL_FALSE, &projectionMatrix[0][0]); 
glUniformMatrix4fv(viewMatrixLocation, 1, GL_FALSE, &viewMatrix[0][0]); 
glUniformMatrix4fv(modelMatrixLocation, 1, GL_FALSE, &modelMatrix[0][0]); 

glBindVertexArray(vaoID[0]); 
glDrawArrays(GL_TRIANGLES, 0, 6); 
glBindVertexArray(0); 

shader->unbind(); 

SwapBuffers(hdc); 

}

这里的shader.vert

#version 150 core 

uniform mat4 projectionMatrix; 
uniform mat4 viewMatrix; 
uniform mat4 modelMatrix; 

in vec3 in_Position; 
in vec3 in_Color; 
out vec3 pass_Color; 

void main(void) 
{ 
    gl_Position = projectionMatrix * viewMatrix * modelMatrix * vec4(in_Position, 1.0); 
    pass_Color = in_Color; 
} 

这里是shader.frag

#version 150 core 

uniform mat4 projectionMatrix; 
uniform mat4 viewMatrix; 
uniform mat4 modelMatrix; 

in vec3 pass_Color; 
out vec4 out_Color; 

void main(void) 
{ 
    out_Color = vec4(pass_Color, 1.0); 
} 

对不起忘了什么我画:

void OpenGLContext::createSquare(void) 
{ 
float* vertices = new float[18]; 

vertices[0] = -0.5; vertices[1] = -0.5; vertices[2] = 0.0; // Bottom left corner 
vertices[3] = -0.5; vertices[4] = 0.5; vertices[5] = 0.0; // Top left corner 
vertices[6] = 0.5; vertices[7] = 0.5; vertices[8] = 0.0; // Top Right corner 

vertices[9] = 0.5; vertices[10] = -0.5; vertices[11] = 0.0; // Bottom right corner 
vertices[12] = -0.5; vertices[13] = -0.5; vertices[14] = 0.0; // Bottom left corner 
vertices[15] = 0.5; vertices[16] = 0.5; vertices[17] = 0.0; // Top Right corner 

glGenVertexArrays(1, &vaoID[0]); 
glBindVertexArray(vaoID[0]); 

glGenBuffers(1, vboID); 
glBindBuffer(GL_ARRAY_BUFFER, vboID[0]); 
glBufferData(GL_ARRAY_BUFFER, 18 * sizeof(GLfloat), vertices, GL_STATIC_DRAW); 

glVertexAttribPointer((GLuint) 0, 3, GL_FLOAT, GL_FALSE, 0, 0); 

glEnableVertexAttribArray(0); // Disable our Vertex Array Object 
glBindVertexArray(0); 

delete [] vertices; 
} 

设置我的矩阵这样的结果是什么在屏幕上被绘制。就像我说过的,如果我将投影和视图矩阵设置为一个标识,它将起作用。 modelMatrix上的缩放似乎总是可以工作的。

+0

如果不知道自己想要渲染什么,就很难回答这个问题。 – 2013-02-21 00:39:54

+0

你能描述你如何改变投影矩阵,你期望发生什么,以及实际发生了什么?图片将有极大的帮助。 – user1118321 2013-02-21 02:58:44

+0

哇,我不知道如何,但当我去去截图显示你们这刚刚开始正常工作?我不是抱怨只是困惑哈哈。我在调试时必须意外修复它? – RaptorIV 2013-02-21 03:51:04

回答

0

位置1没有任何属性(in_Color)。如果你只是将它从这个问题中解决出来,那么问题就出在你没有在着色器中定义的位置上。我从来没有真正地测试它没有位置部分,但我认为它是必要的,至少对于多个​​值:你应该使用例如layout(location = 0) in in_Position

+0

跨度为0指定了紧凑的数据,并且非常好。 – 2016-09-06 20:56:55

+0

什么?我一直认为0意味着每个组件的大小为0,而sizeof(float)* 3意味着它们是三个浮点数宽度 – jv110 2016-09-06 21:18:57

+0

不,'0'是一个特殊值。请参阅[文档](https://www.opengl.org/sdk/docs/man/html/glVertexAttribPointer.xhtml)。 – 2016-09-06 22:57:04