Omarito
Omarito

Reputation: 577

OpenGL Transformations not working using GLM matrix

I'm learning the OpenGL using the learnopengl tutorials, and in the transformations chapter. 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

Upvotes: 2

Views: 1550

Answers (1)

Rabbid76
Rabbid76

Reputation: 210877

You have to initialize the matrix variable glm::mat4 transform.

The glm API documentation refers to 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.

This means, that an identity matrix can be initialized by the single parameter 1.0:

glm::mat4 transform(1.0f);

Upvotes: 2

Related Questions