Drawing 2 triangles using transformations

Hi guys,

I’m fairly new to OpenGL and have been trying to learn it by following the textbook : “Learn OpenGL” by Joe DeVries. I am stuck on one of the exercises they provided, i.e:

I have taken a look at their solution (https://learnopengl.com/code_viewer.php?code=getting-started/transformations-exercise2) however have not been successful in drawing 2 triangles with 2 different transformations.

Apart from a few differences (I do not use a separate ‘shader class’ and I have not included textures) my code appears to be very similar to the solution, however the result I get is that of a stationary triangle that has rotated once but does not continue to rotate (which I assume it should since it is in a while loop).

void MyOpenGL::render()
	glClearColor(0.f, 0.f, 0.f, 1.f);
	glm::mat4 trans;
	trans = glm::rotate(trans, glm::radians(20.f),glm::vec3(0.5f, 0.f, 0.f));
	GLuint transformLoc = glGetUniformLocation(shaderProgram, "transform");
	glUniformMatrix4fv(transformLoc, 1, GL_FALSE, &trans[0][0]);
	glDrawArrays(GL_TRIANGLES, 0, 3);
	trans = glm::mat4();
	//trans = glm::translate(trans, glm::vec3(0.01f, 0.f, 0.f));
	trans = glm::rotate(trans, glm::radians(10.f), glm::vec3(0.f, 0.f, 1.0f));
	glUniformMatrix4fv(transformLoc, 1, GL_FALSE, &trans[0][0]);
	glDrawArrays(GL_TRIANGLES, 0, 3);

//main loop
int main()
	MyOpenGL one;
	while (!one.shouldWindowClose())

	return 0;

//my vertex shader 
#version 330 core
in layout (location = 0) vec2 position;
in layout (location = 1) vec3 vertexColour;

out vec3 theColour;

uniform mat4 transform;
void main()
	gl_Position = transform * vec4(position, 0.0f, 1.0f);
	theColour = vertexColour;

I have noticed that if I put the “glm::mat4 trans” as a private variable of MyOpenGL class and comment trans = glm::mat4();, then I will get a single triangle that rotates on the x and z axis. But what I am trying to achieve is having 2 triangles drawn: one that rotates on the x axis and the other rotating on the z axis.

I am using version 3.3 of OpenGL.

Thank you for your time :slight_smile:

This sets the [var]transform[/var] matrix to a constant rotation of 20 degrees about the X axis. If you want it to rotate at a constant rate, you either need to make the angle a variable which increments each frame, or store the transformation (e.g. in a member variable) between frames and apply an incremental rotation each frame.

That looks wrong, but may not be an issue. If you only have one set of geometry to draw, you can bind the VAO during the setup and just keep it bound thereafter. Otherwise, the VAO needs to be bound before the draw call.

This is similar to the previous one, except the fixed rotation is of 10 degrees about the Z axis. Again, you can either rotate by a constantly-incrementing angle, or you can store the transformation between frames (the X-axis and Z-axis rotations need to be two different variables).

Yes, a big one is binding your vertex array object, before you call the drawing command. First you have your program initialization (not in the main draw loop yet), which probably has the proper VAO bound. Then, you enter your draw (render()) loop, which does not explicitly bind the VAO again, but rather goes straight to a draw call, then you bind a VAO, then another draw call (with the just bound VAO), then you unbind the VAO. The next iteration of your render() loop, will start with no VAO bound before the next draw call, so you should just get one triangle after the initial first frame.

I hope that makes sense, keep it going!