1

I am trying to rotate a quad in a 3D space. The following code shows the vertex shader utilized to draw the quad:

#version 330 core
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aColor;

out vec3 ourColor;

uniform mat4 transform;
uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;

void main()
{
    gl_Position = transform*(projection*view*model*vec4(aPos, 1.0f));
    ourColor = aColor;
}



The quad is displayed when transform is not multiplied to projection*view*model*vec4(aPos,1.0f) but is not displayed when it is multiplied as above.

The code for transformation:

trans=glm::rotate(trans,(float)(glfwGetTime()),glm::vec3(0.0,0.0,1.0));
float scaleAmount = sin(j*0.3);j=j+0.035;
trans=glm::scale(trans,glm::vec3(scaleAmount,scaleAmount,scaleAmount));
unsigned int transformLoc = glGetUniformLocation(shaderProgram, "transform");
glUniformMatrix4fv(transformLoc, 1, GL_FALSE, glm::value_ptr(trans));
glBindVertexArray(VAO);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);


I have set the uniform present in the vertex shader as well.
Why is it not rotating and scaling, or even appearing when I multiply transform with (projection*view*model*vec4(aPos,1.0f)) ?
Edit: I figured out that the problem is with scaling, since the code works with rotation only. The code does not work with scaling only.

genpfault
  • 51,148
  • 11
  • 85
  • 139
mahesh Rao
  • 375
  • 1
  • 4
  • 16
  • Try `gl_Position = projection * view * transform * model * vec4(aPos,1.0f)` – Ripi2 May 09 '18 at 19:21
  • @Ripi2 did not work. – mahesh Rao May 09 '18 at 19:24
  • Do you understand "transformations"? If not, take a look at [this](https://stackoverflow.com/a/850239/3871028). – Ripi2 May 09 '18 at 19:27
  • @Rabbid76 first comment. Also, I've found that the problem has something to do with scaling. – mahesh Rao May 09 '18 at 19:32
  • 1
    I've got it ! I initialized `j` variable as an integer. So, for the scale function, `j=j+0.035` was always 0. Hence, the quad didn't appear.I'm sorry for all the trouble and thank you for helping me. – mahesh Rao May 09 '18 at 19:50

2 Answers2

1

Let's think only in 2D.

The quad is defined in "world" coordinates. To rotate it around some point move the quad to that point, then rotate and scale it and then move it back.

Doing this with matrices is the same as transform * model where transform is something like

transform = moveback * scale * rotate * movetopoint
Ripi2
  • 7,031
  • 1
  • 17
  • 33
  • I tried it. I've noticed that the code works with rotation only and doesn't with scaling only. It has something to do with the scaling of the quad. – mahesh Rao May 09 '18 at 19:42
0

If scaleAmount == 0.0:

glm::mat4 trans( 1.0f );
float scaleAmount = 0.0f;
trans=glm::scale(trans,glm::vec3(scaleAmount,scaleAmount,scaleAmount));

then this would cause that trans is

{{0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 1}}

Since sin(0.0) == 0.0 it has to be ensured that in case of sin(j*0.3);, j is not equal 0.0.

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • Yup, that was it. I had initialized j as an integer. So j+0.035, did not make a difference and j was still 0. – mahesh Rao May 09 '18 at 19:58