我正在使用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上的縮放似乎總是可以工作的。
如果不知道自己想要渲染什麼,就很難回答這個問題。 – 2013-02-21 00:39:54
你能描述你如何改變投影矩陣,你期望發生什麼,以及實際發生了什麼?圖片將有極大的幫助。 – user1118321 2013-02-21 02:58:44
哇,我不知道如何,但當我去去截圖顯示你們這剛剛開始正常工作?我不是抱怨只是困惑哈哈。我在調試時必須意外修復它? – RaptorIV 2013-02-21 03:51:04