我正在使用 learnopengl 教程和转换章节学习 OpenGL。我理解他所做的一切及其背后的理论(数学)。但是,虽然尝试练习我的对象并没有显示我复制了他的代码并粘贴它,但仍然没有任何改变!
这是我的顶点着色器:
#version 330 core
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec2 aTexCoord;
out vec2 TexCoord;
uniform mat4 transform;
void main()
{
gl_Position = transform * vec4(aPos, 1.0);
TexCoord = vec2(aTexCoord.x, aTexCoord.y);
}
我的渲染:
// bind textures on corresponding texture units
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture1);
// create transformations
glm::mat4 transform;
transform = glm::rotate(transform, glm::radians((float)glfwGetTime()), glm::vec3(0.0f, 0.0f, 1.0f));
// get matrix's uniform location and set matrix
ourShader.use();
unsigned int transformLoc = glGetUniformLocation(ourShader.ID, "transform");
glUniformMatrix4fv(transformLoc, 1, GL_FALSE, glm::value_ptr(transform));
// render container
glBindVertexArray(VAO);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
当我从顶点着色器中的乘法中删除变换时,一切正常
最佳答案
您必须初始化矩阵变量glm::mat4转换
。
glm API documentation指The OpenGL Shading Language specification 4.20 .
5.4.2 Vector and Matrix Constructors
If there is a single scalar parameter to a vector constructor, it is used to initialize all components of the constructed vector to that scalar’s value. If there is a single scalar parameter to a matrix constructor, it is used to initialize all the components on the matrix’s diagonal, with the remaining components initialized to 0.0.
这意味着单位矩阵可以通过单个参数 1.0 来初始化:
glm::mat4 transform(1.0f);
关于c++ - OpenGL 转换无法使用 GLM 矩阵,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52901400/