Last active
December 11, 2015 12:29
-
-
Save aib/4600974 to your computer and use it in GitHub Desktop.
GLUT [almost-]skeletal code
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
#include <GL/glut.h> | |
#include <time.h> | |
void reshape(int width, int height) | |
{ | |
double aspect; | |
if (height == 0) height = 1; | |
aspect = width / height; | |
glViewport(0, 0, width, height); | |
glMatrixMode(GL_PROJECTION); | |
glLoadIdentity(); | |
gluPerspective(45, aspect, 1.0, 1000.0); //fov, aspect, near, far | |
glMatrixMode(GL_MODELVIEW); | |
} | |
void update_display(void) | |
{ | |
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); | |
glLoadIdentity(); | |
gluLookAt( | |
2.0, 2.0, 10.0, //eye | |
0.0, 0.0, 0.0, //target | |
0.0, 1.0, 0.0 //eyeUp | |
); | |
glPushMatrix(); | |
glTranslatef(+1.0f, +1.0f, -0.5f); //let's move up, right and back a bit | |
glBegin(GL_TRIANGLES); | |
glColor3f(1.0f, 0.0f, 0.0f); | |
glVertex3f( 0.0f, +0.6f, 0.0f); | |
glVertex3f(-0.4f, 0.0f, 0.0f); | |
glVertex3f(+0.4f, 0.0f, 0.0f); | |
glEnd(); | |
glPopMatrix(); | |
glBegin(GL_LINES); //axes | |
//X | |
glColor3f(1.0f, 0.0f, 0.0f); | |
glVertex3f(-100000.0f, 0.0f, 0.0f); | |
glVertex3f(+100000.0f, 0.0f, 0.0f); | |
//Y | |
glColor3f(0.0f, 1.0f, 0.0f); | |
glVertex3f(0.0f, -100000.0f, 0.0f); | |
glVertex3f(0.0f, +100000.0f, 0.0f); | |
//Z | |
glColor3f(0.0f, 0.0f, 1.0f); | |
glVertex3f(0.0f, 0.0f, -100000.0f); | |
glVertex3f(0.0f, 0.0f, +100000.0f); | |
glEnd(); | |
glutSwapBuffers(); | |
} | |
void doUpdate(double elapsedSeconds) | |
{ | |
} | |
void update_idle(void) | |
{ | |
static clock_t lastUpdate = 0; | |
clock_t now; | |
double elapsed; | |
if (lastUpdate == 0) lastUpdate = clock(); | |
now = clock(); | |
elapsed = (now - lastUpdate) / (double) CLOCKS_PER_SEC; | |
lastUpdate = now; | |
doUpdate(elapsed); | |
update_display(); | |
} | |
void process_keys(unsigned char key, int x, int y) | |
{ | |
switch (key) { | |
case '\033': //ESC | |
exit(EXIT_SUCCESS); //crude | |
break; | |
} | |
} | |
int main(int argc, char* argv[]) | |
{ | |
glutInit(&argc, argv); | |
glutInitWindowSize(640, 480); | |
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA); | |
glutCreateWindow("aibglutTest"); | |
glutReshapeFunc(reshape); | |
glutDisplayFunc(update_display); | |
glutIdleFunc(update_idle); | |
glutKeyboardFunc(process_keys); | |
glEnable(GL_DEPTH_TEST); | |
glutMainLoop(); | |
return EXIT_SUCCESS; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment