简体   繁体   English

OpenGL 转换无法使用 GLM 矩阵工作

[英]OpenGL Transformations not working using GLM matrix

I'm learning the OpenGL using the learnopengl tutorials, and in the transformations chapter.我正在使用 learnopengl 教程和转换章节学习 OpenGL。 I understood everything he did and the theory (maths) behind it.我理解他所做的一切以及背后的理论(数学)。 But while trying to practice my object isn't showing I copied his code and paste it and still nothing changed!但是在尝试练习时,我的对象没有显示我复制了他的代码并粘贴了它,但仍然没有任何改变!

Here is my vertex shader:这是我的顶点着色器:

#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);
}

My rendering:我的渲染:

// 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);

when I remove the transform from the multiplication in vertex shader everything works fine当我从顶点着色器中的乘法中删除变换时,一切正常

You have to initialize the matrix variable glm::mat4 transform .您必须初始化矩阵变量glm::mat4 transform

The glm API documentation refers to The OpenGL Shading Language specification 4.20 . glm API 文档是指OpenGL 着色语言规范 4.20

5.4.2 Vector and Matrix Constructors 5.4.2 向量和矩阵构造函数

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.如果矩阵构造函数只有一个标量参数,则它用于初始化矩阵对角线上的所有分量,其余分量初始化为 0.0。

This means, that an identity matrix can be initialized by the single parameter 1.0:这意味着,可以通过单个参数 1.0 初始化单位矩阵:

glm::mat4 transform(1.0f);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM