1/* 2 * Draw a triangle with rotation 3 */ 4 5#include <stdio.h> 6#include <stdlib.h> 7#include "glut_wrap.h" 8 9 10static int win; 11static float rot = 0.0f; 12 13 14static void 15Reshape(int width, int height) 16{ 17 glViewport(0, 0, width, height); 18 19 glMatrixMode(GL_PROJECTION); 20 glLoadIdentity(); 21 glMatrixMode(GL_MODELVIEW); 22 glLoadIdentity(); 23} 24 25 26static void 27Key(unsigned char key, int x, int y) 28{ 29 if (key == 27) { 30 glutDestroyWindow(win); 31 exit(0); 32 } 33 else if (key == 'r') { 34 rot += 5.0f; 35 glutPostRedisplay(); 36 } 37 else if (key == 'R') { 38 rot -= 5.0f; 39 glutPostRedisplay(); 40 } 41} 42 43 44static void 45Draw(void) 46{ 47 glClear(GL_COLOR_BUFFER_BIT); 48 49 glPushMatrix(); 50 glRotatef(rot, 0.0, 0.0, 1.0f); 51 52 glBegin(GL_TRIANGLES); 53 glColor3f(1.0f, 0.0f, 0.0f); 54 glVertex3f(-0.8f, -0.8f, 0.0f); 55 glColor3f(0.0f, 1.0f, 0.0f); 56 glVertex3f(0.8f, -0.8f, 0.0f); 57 glColor3f(0.0f, 0.0f, 1.0f); 58 glVertex3f(0.0f, 0.8f, 0.0f); 59 glEnd(); 60 61 glPopMatrix(); 62 63 glutSwapBuffers(); 64} 65 66 67static void 68Init(void) 69{ 70 fprintf(stderr, "GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER)); 71 fprintf(stderr, "GL_VERSION = %s\n", (char *) glGetString(GL_VERSION)); 72 fprintf(stderr, "GL_VENDOR = %s\n", (char *) glGetString(GL_VENDOR)); 73 fflush(stderr); 74 75 glClearColor(0.3, 0.3, 0.7, 0.0); 76} 77 78 79int 80main(int argc, char **argv) 81{ 82 glutInit(&argc, argv); 83 84 if (argc > 1) { 85 rot = atof(argv[1]); 86 printf("Using rotation %g\n", rot); 87 fflush(stdout); 88 } 89 90 glutInitWindowPosition(0, 0); 91 glutInitWindowSize(250, 250); 92 93 glutInitDisplayMode(GLUT_RGB | GLUT_ALPHA | GLUT_DOUBLE); 94 95 win = glutCreateWindow(*argv); 96 if (!win) { 97 exit(1); 98 } 99 100 Init(); 101 102 glutReshapeFunc(Reshape); 103 glutKeyboardFunc(Key); 104 glutDisplayFunc(Draw); 105 glutMainLoop(); 106 return 0; 107} 108