这个问题在这里已经有了答案:
无法在OpenGL着色器 1答案中 设置统一值我有一个简单的顶点数组,可以在屏幕上绘制四边形。
glViewport(0, 0, screenWidth, screenHeight); // Load shaders ... GLfloat vertices[] = { 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f }; GLuint vao, vbo; glGenVertexArrays(1, &vao); glGenBuffers(1, &vbo); glBindBuffer(GL_ARRAY_BUFFER, vbo); glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); glBindVertexArray(vao); glEnableVertexAttribArray(0); glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(GLfloat), (GLvoid*)0); glBindBuffer(GL_ARRAY_BUFFER, 0); glBindVertexArray(0); ... glUseProgram(program); glUniform3f(glGetUniformLocation(program, "spriteColour"), colour.x, colour.y, colour.z); glBindVertexArray(vao); glDrawArrays(GL_TRIANGLES, 0, 6); glBindVertexArray(0);使用下面的简单顶点和片段着色器。
#version 330 core layout (location = 0) in vec2 vertex; void main() { gl_Position = vec4(vertex.xy, 0.0, 1.0); } #version 330 core out vec4 color; uniform vec3 spriteColour; void main() { color = vec4(spriteColour, 1.0); }这完全符合我的预期。 它在窗口的右上角呈现一个矩形。
现在我想添加一个简单的模型和投影矩阵。 我正在使用正交投影矩阵,我的模型矩阵只是将四边形缩放到100 x 100。
glm::mat4 projection = glm::ortho(0.0f, (GLfloat)screenWidth, (GLfloat)screenHeight, 0.0f, -1.0f, 1.0f); glUniformMatrix4fv(glGetUniformLocation(shader, "projection"), 1, GL_FALSE, glm::value_ptr(projection)); ... glm::mat4 model; model = glm::scale(model, glm::vec3(100.0f, 100.0f, 1.0f)); glUniformMatrix4fv(glGetUniformLocation(program, "model"), 1, GL_FALSE, glm::value_ptr(model)); ... // Update vertex shader #version 330 core layout (location = 0) in vec2 vertex; uniform mat4 model; uniform mat4 projection; void main() { gl_Position = projection * model * vec4(vertex.xy, 0.0, 1.0); }我希望这会在屏幕的左上角渲染一个100 x 100的四边形但是我没有看到任何东西。
我只能假设我的变换以某种方式导致四边形从屏幕上被绘制并被剪裁? 我是Open GL的新手,所以我不完全确定这里有什么问题。 我已经多次使用它并根据我使用的教程( http://learnopengl.com/ )看起来是正确的。
有没有人对我做错了什么有任何想法?
This question already has an answer here:
Can't set uniform value in OpenGL shader 1 answerI have a simple vertex array that draws a quad to the screen.
glViewport(0, 0, screenWidth, screenHeight); // Load shaders ... GLfloat vertices[] = { 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f }; GLuint vao, vbo; glGenVertexArrays(1, &vao); glGenBuffers(1, &vbo); glBindBuffer(GL_ARRAY_BUFFER, vbo); glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); glBindVertexArray(vao); glEnableVertexAttribArray(0); glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(GLfloat), (GLvoid*)0); glBindBuffer(GL_ARRAY_BUFFER, 0); glBindVertexArray(0); ... glUseProgram(program); glUniform3f(glGetUniformLocation(program, "spriteColour"), colour.x, colour.y, colour.z); glBindVertexArray(vao); glDrawArrays(GL_TRIANGLES, 0, 6); glBindVertexArray(0);Using the below simple vertex and fragment shaders.
#version 330 core layout (location = 0) in vec2 vertex; void main() { gl_Position = vec4(vertex.xy, 0.0, 1.0); } #version 330 core out vec4 color; uniform vec3 spriteColour; void main() { color = vec4(spriteColour, 1.0); }This works exactly how I expect. It renders a rectangle to the upper right corner of the window.
Now I want to add a simple model and projection matrix. I am using an orthographic projection matrix and my model matrix just scales the quad to 100 x 100.
glm::mat4 projection = glm::ortho(0.0f, (GLfloat)screenWidth, (GLfloat)screenHeight, 0.0f, -1.0f, 1.0f); glUniformMatrix4fv(glGetUniformLocation(shader, "projection"), 1, GL_FALSE, glm::value_ptr(projection)); ... glm::mat4 model; model = glm::scale(model, glm::vec3(100.0f, 100.0f, 1.0f)); glUniformMatrix4fv(glGetUniformLocation(program, "model"), 1, GL_FALSE, glm::value_ptr(model)); ... // Update vertex shader #version 330 core layout (location = 0) in vec2 vertex; uniform mat4 model; uniform mat4 projection; void main() { gl_Position = projection * model * vec4(vertex.xy, 0.0, 1.0); }I would expect this to render a 100 x 100 quad at the top left of the screen however I don't see anything.
I can only assume that somehow my transformations are causing the quad to be drawn off screen and clipped? I am pretty new at Open GL so I am not entirely sure what is wrong here. I've been over it numerous times and based on the tutorials I am using (http://learnopengl.com/) it seems correct.
Does anyone have any ideas as to what I've done wrong?
更多推荐
发布评论