2

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

Omarito
  • 577
  • 6
  • 22
  • The code is intended to provide a rotating shape. By "works fine" you mean you can see that shape but not moving, right? This means the transform matrix when included is not correct. Try a simpler one. – bcperth Oct 20 '18 at 02:10
  • 1
    You have to initialize `transform`: `glm::mat4 transform(1.0f);` - See [Why is the sprite not rendering in OpenGL?](https://stackoverflow.com/questions/49651388/why-is-the-sprite-not-rendering-in-opengl/49652092#49652092). – Rabbid76 Oct 20 '18 at 06:57
  • 1
    Possible duplicate of [Why is the sprite not rendering in OpenGL?](https://stackoverflow.com/questions/49651388/why-is-the-sprite-not-rendering-in-opengl) – Rabbid76 Oct 20 '18 at 06:58
  • try step-by-step stuff.Start without texture then add it .Also `glDrawElements` is a bad choice for learning purpose code. – Алексей Неудачин Oct 20 '18 at 09:33

1 Answers1

2

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);
Community
  • 1
  • 1
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • A life saver thanks! I already searched, what glm::mat4 default constructor do and i found that it init the mat to the identity. but thanks anyway – Omarito Oct 20 '18 at 13:38