gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-31 02:04:51 31 4
gpt4 key购买 nike

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

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/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com