OpenGL 3.2设置矩阵时出现问题
OpenGL 3.2 trouble setting up matrices
我正在使用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
。
相关文章:
- 将十六进制值设置为用于填充的字符变量时出现问题
- std::p 设置或多组的修订问题
- 在Visual "Microsoft studio 2019"上设置OpenCV 4.1.1时遇到问题?(非法指令。
- 嵌套的类成员变量不可访问.链表设置.V、下一个和新的问题
- C / C++链接问题与非常简单的设置
- 通过比较字符设置字符串时出现问题
- 在 c++ 中为我的类设置/获取方法时遇到问题
- 在设置 if-else 语句以删除特定货币并保持更新时遇到问题
- Coin-CLP设置的问题
- C 无序的设置问题与结构哈希
- 为 C++ 开发设置 GTK+ 时遇到问题
- 可视C++ 设置串行 COM 端口时出现问题
- 在将结构插入设置C++时遇到问题
- 使用设置精度在C++中舍入小数点时出现问题
- 未在另一个函数中设置变量引用的问题
- 设置Ncurses和C++时出现问题
- C awesomium设置了听众问题
- 为英特尔C++编辑器"GCC not found"设置环境变量时出现问题
- 问题:设置IP
- Eclipse c++ IDE,问题设置