/* Presented by James Madison University Computer Science major Joshua Blake to FALL 2004 CS 480 students on 8/31/2004 UPDATED: 3/1/2004 Feel free to use and modify as needed. Please send me an email if you find this useful: blakejr@jmu.edu */ #include //include gl/glut.h in all GLUT programs #include float myx, myy; //position of the vertex float dx, dy; //velocity of the vertex void renderScene(void) { //draws the 2D scene glClear(GL_COLOR_BUFFER_BIT); //clears the color buffer (default: black) glBegin(GL_TRIANGLES); //begin processing vertices as triangle triples glColor3f(1.0, 0.0, 0.0); //set color to red (R = 1, G = 0, B = 0) glVertex3f(myx, myy, 0.0); //first vertex at (myx, myy, 0) glColor3f(0.0, 1.0, 0.0); //set color to green (R = 0, G = 1, B = 0) glVertex3f(10.0, 0.0, 0.0); //second vertex at (10, 0, 0) glColor3f(0.0, 0.0, 1.0); //set color to blue (R = 0, G = 0, B = 1) glVertex3f(0.0, 10.0, 0.0); //third vertex at (0, 10, 0) /*this completes one triangle further triples of vertices would become separate triangles */ glEnd(); //end vertex processing glFlush(); //flush all primitives to color buffer //NOTE: glFlush() is needed in single buffering, //but is redundant in double buffering because of glutSwapBuffers() glutSwapBuffers(); //swap the front and back buffers in double buffering } void keyboard(unsigned char key, int x, int y) { /*key presses are sent here key is the character pressed, x and y are the mouse position IN WINDOW COORDINATES */ switch (key) { case 'q': //'q' or ESC will exit the program case 27: exit(0); break; case 'h': //'h' will print hi to the console printf("hi\n"); break; case 'w': //'w' increases the y velocity dy += 0.1; break; case 's': //'s' decreases the y velocity dy -= 0.1; break; case 'a': //'a' decreases the x velocity dx -= 0.1; break; case 'd': //'a' increases the x velocity dx += 0.1; break; } glutPostRedisplay(); //tell GLUT it needs to repaint the window } void timer(int num) { //timer is called according to the glutTimerFunc() callback myx += dx; //move the current position by the velocity myy += dy; //in x and y glutPostRedisplay(); //tell GLUT it needs to repaint the window glutTimerFunc(10, timer, 1); //tell GLUT to call this function again in 10 ms with 1 as the parameter } int main(int argc, char **argv) { glutInit(&argc, argv); //initialize GLUT glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA); /* display mode: use DEPTH BUFFERING use DOUBLE BUFFERING use RGBA color scheme */ glutInitWindowPosition(100,100); //initial window position glutInitWindowSize(800,600); //initial window size glutCreateWindow("My first OpenGL program"); //create the window with the specified title glutTimerFunc(10, timer, 1); //set the timer callback to fire in 10 milliseconds glutKeyboardFunc(keyboard); //register the keyboard callback glutDisplayFunc(renderScene); //register the display callback gluOrtho2D(0, 10, 0, 10); //Set up an Orthographic Projection in 2D // with X ranging from 0 to 10 // and Y ranging from 0 to 10 myx = 0.0; //initial vertex position myy = 0.0; glutMainLoop(); //enter the main loop (never returns) return 0; //this is never executed }