Created
February 26, 2012 18:42
-
-
Save Ricket/1918222 to your computer and use it in GitHub Desktop.
OpenGL Spinning Square
This file contains hidden or 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
// spinningsquare.c | |
// compiles on OSX with: | |
// gcc spinningsquare.c -framework glut -framework opengl -o spinningsquare | |
#include <stdlib.h> | |
#ifdef __APPLE__ | |
# include <GLUT/glut.h> | |
#else | |
# include <GL/glut.h> | |
#endif | |
#include <stdio.h> | |
static void init(void) | |
{ | |
glMatrixMode(GL_PROJECTION); | |
glOrtho(0.0f, 10.0f, 0.0f, 10.0f, -1.0f, 1.0f); | |
glMatrixMode(GL_MODELVIEW); | |
} | |
static float rotDeg = 0.0f; | |
static int lastTime = 0; | |
static void idle_func(void) | |
{ | |
int thisTime; | |
thisTime = glutGet(GLUT_ELAPSED_TIME); | |
rotDeg += (float)(thisTime - lastTime) * 0.1f; | |
lastTime = thisTime; | |
glutPostRedisplay(); | |
} | |
static void render(void) | |
{ | |
glClearColor(1.0f, 1.0f, 1.0f, 1.0f); | |
glClear(GL_COLOR_BUFFER_BIT); | |
glLoadIdentity(); | |
glTranslatef(5.0f, 5.0f, 0.0f); | |
glRotatef(rotDeg, 0.0f, 0.0f, 1.0f); | |
glColor3f(1.0f, 0.0f, 0.0f); | |
glBegin(GL_TRIANGLES); | |
{ | |
glVertex2f(-0.5f, -0.5f); | |
glVertex2f(0.5f, -0.5f); | |
glVertex2f(0.5f, 0.5f); | |
glVertex2f(-0.5f, -0.5f); | |
glVertex2f(0.5f, 0.5f); | |
glVertex2f(-0.5f, 0.5f); | |
} | |
glEnd(); | |
glutSwapBuffers(); | |
} | |
int main(int argc, char** argv) | |
{ | |
glutInit(&argc, argv); | |
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE); | |
glutInitWindowSize(400, 300); | |
glutCreateWindow("Spinning Square"); | |
glutIdleFunc(&idle_func); | |
glutDisplayFunc(&render); | |
init(); | |
glutMainLoop(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment