Created
November 21, 2017 09:51
-
-
Save kevinmungai/7ec64de113cf754adad56d4028bc5a24 to your computer and use it in GitHub Desktop.
Animation in OpenGL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// We will be doing animation in OpenGL | |
// key concepts are: | |
// 1. the idle function - idle() will post a replaint and this will lead to activation of the display() function | |
// 2. Also we will be doing double buffering like this: glutInitDisplayMode(GLUT_DOUBLE); | |
// In addition we will be introducing other functions such as glPushMatrix, glPopMatrix and glSwapBuffers | |
#include <windows.h> | |
#include <GL/glut.h> | |
#include <GL/gl.h> | |
GLfloat angle = 0.0f; | |
void initGL() { // Set "clearing" or background color | |
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Black and opaque | |
} | |
void idle() { | |
glutPostRedisplay(); // Post a re-paint request to activate display() | |
} | |
void display() { | |
glClear(GL_COLOR_BUFFER_BIT); // Clear the color buffer | |
glMatrixMode(GL_MODELVIEW); // To operate on Model-View matrix | |
glLoadIdentity(); // Reset the model-view matrix | |
glPushMatrix(); // Save model-view matrix setting | |
//glTranslatef(-0.5f, 0.4f, 0.0f); // Translate | |
glRotatef(angle, 0.0f, 0.0f, 1.0f); // rotate by angle in degrees | |
glBegin(GL_QUADS); // Each set of 4 vertices form a quad | |
glColor3f(1.0f, 0.0f, 0.0f); // Red | |
glVertex2f(-0.3f, -0.3f); | |
glColor3f(0.0, 1.0, 0.0); | |
glVertex2f( 0.3f, -0.3f); | |
glColor3f(0.0, 0.0, 1.0); | |
glVertex2f( 0.3f, 0.3f); | |
glColor3f(1.0, 1.0, 0.0); | |
glVertex2f(-0.3f, 0.3f); | |
glEnd(); | |
glPopMatrix(); | |
glPushMatrix(); | |
glutSwapBuffers(); // Double buffered - swap the front and back buffers | |
angle += 20.0f; // Change the rotational angle after each display() | |
} | |
int main(int argc, char** argv) { | |
glutInit(&argc, argv); // Initialize GLUT | |
glutInitDisplayMode(GLUT_DOUBLE); // Enable double buffered mode | |
glutInitWindowSize(640, 480); // Set the window's initial width & height - non-square | |
glutInitWindowPosition(50, 50); // Position the window's initial top-left corner | |
glutCreateWindow("Animation via Idle Function"); // Create window with the given title | |
glutDisplayFunc(display); // Register callback handler for window re-paint event | |
//glutReshapeFunc(reshape); // Register callback handler for window re-size event | |
glutIdleFunc(idle); // Register callback handler if no other event | |
initGL(); // Our own OpenGL initialization | |
glutMainLoop(); // Enter the infinite event-processing loop | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment