2013-02-21 164 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