c++ - OpenGL 转换无法使用 GLM 矩阵

标签 c++ opengl glsl

我正在使用 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 documentationThe 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/

相关文章:

opengl - 分析图形着色器

c# - 如何使用 OpenGL 制作自定义顶点格式

c++ - 带指针的 ostream_iterator

c++ - 使用 C++ 的 Oracle 扩展存储过程

c++ - C++中的调用函数

html - 在 windows 下,WebGL 使用 DirectX 运行时或 OpenGL 运行时?

编译时的OpenGL问题

c++ - 为什么当 bool = true 时补码运算符不起作用?

c++ - 尝试将使用 X11 的基于 linux 的开源 C++ 项目移植到 OSX 中。我已经安装了 XQuartz : now what?

opengl - OpenGL 中 3D 坐标系的透视问题