OpenGL 3.2设置矩阵时出现问题

OpenGL 3.2 trouble setting up matrices

本文关键字:问题 设置 OpenGL      更新时间:2023-10-16

我正在使用GLM来管理我的矩阵,但我遇到了一些对我来说毫无意义的问题。当我将投影矩阵设置为单位矩阵以外的任何矩阵时,我看不到我试图绘制的正方形。如果这是一种身份,它就会起作用。我的视图矩阵也发生了类似的情况。如果我试着翻译过去的-1或+1,方块就会消失,否则似乎没有效果。

没有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); 

}

这是阴影垂直

#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上的缩放似乎总是同样有效。

位置1(in_Color)上没有属性。如果您只是将其排除在这个问题之外,那么问题在于位置,而您没有在着色器中定义这些位置。我从来没有在没有位置部分的情况下测试过它,但我认为这是必要的,至少对于多个值:你应该使用例如layout(location = 0) in in_Position