But why does the rotation happen evenly and not increase with the timer
Because you're starting the the transform from "zero" (identity) each time through.
If you want to rotate faster & faster with time you need to accumulate the angle:
float angle = 0.0f;
while( shouldDraw() )
{
...
angle += static_cast< float >( glfwGetTime() );
glm::mat4 transform(1.0f);
transform = glm::rotate(transform, angle, glm::vec3(0.0f, 0.0f, 1.0f));
...
}
All together, comparing both approaches:
// g++ main.cpp `pkg-config --cflags --libs glfw3 gl`
#include <GLFW/glfw3.h>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
void DrawTriangle()
{
glBegin( GL_TRIANGLES );
glColor3ub( 255, 0, 0 );
glVertex2f( 0, 1 );
glColor3ub( 0, 255, 0 );
glVertex2f( -1, -1 );
glColor3ub( 0, 0, 255 );
glVertex2f( 1, -1 );
glEnd();
}
int main( int, char** )
{
glfwInit();
GLFWwindow* window = glfwCreateWindow( 600, 600, "GLFW", NULL, NULL );
glfwMakeContextCurrent( window );
float angle2 = 0.0f;
while( !glfwWindowShouldClose( window ) )
{
glfwPollEvents();
int w, h;
glfwGetFramebufferSize( window, &w, &h );
glViewport( 0, 0, w, h );
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
glOrtho( -5, 5, -5, 5, -1, 1 );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
const float time = static_cast< float >( glfwGetTime() );
float angle1 = time;
angle2 += time;
glPushMatrix();
{
glTranslatef( 2.5f, 2.5f, 0.0f );
glm::mat4 transform(1.0f);
transform = glm::rotate(transform, angle1, glm::vec3(0.0f, 0.0f, 1.0f));
glMultMatrixf( glm::value_ptr( transform ) );
DrawTriangle();
}
glPopMatrix();
glPushMatrix();
{
glTranslatef( -2.5f, -2.5f, 0.0f );
glm::mat4 transform(1.0f);
transform = glm::rotate(transform, angle2, glm::vec3(0.0f, 0.0f, 1.0f));
glMultMatrixf( glm::value_ptr( transform ) );
DrawTriangle();
}
glPopMatrix();
glfwSwapBuffers( window );
}
glfwTerminate();
}
glm::rotatehas to be set in radians. - Rabbid76