#include #include #include using namespace std; const int milliseconds_per_frame = 30; // Time you would LIKE per frame // The actual time can be longer. int frameCt = 0; // This value is incremented just before each frame is drawn, // to allow you to vary the image depending on frame number. int width; // Current width of window; value is maintained by reshape(). int height; // Current height of window; value is maintained by reshape(). /** * Called in the main program to give you a chance to do initialization. */ void init() { } /** * You can call this to output a string. The characters are scaled * to the size specified; the size is given in drawing coordinates. * The x,y parameters give the location of the left endpoint of the * base of the string. */ void drawString(const char *str, double x=0, double y=0, double size=1.0) { glPushMatrix(); glTranslatef(x,y,0); glScalef(size/153.0,size/153.0,1.0); int lineCt = 0; int len = strlen(str); for (int i = 0; i < len; i++) { if (str[i] == '\n') { lineCt++; glPopMatrix(); glPushMatrix(); glTranslatef(x,y-size*1.15*lineCt,0); glScalef(size/153.0,size/153.0,1.0); } else { glutStrokeCharacter(GLUT_STROKE_ROMAN,str[i]); } } glPopMatrix(); } /** * Called in the "display()" function before drawing anything. * Should set up the rand of coords that will be used for drawing. * You should only hav */ void initTransformation() { glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(-1,1,-1,1); // The parameters are xmin, xmax, ymin, ymax; // These specify the ranges of x and y that // are visible in the window. glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } /** * This will be called over and over to drive the animation. * Each time it is called, it triggers a new frame, which in * turn triggers another call to timer(), in an infinte loop. */ void timer(int id) { frameCt++; glutPostRedisplay(); } /** * Called whenever the windows needs to be redrawn. It should * completely redraw the image that is to be displayed in the window. * To force it to be called, call the suboutine glutPostRedisplay(). */ void display() { glutTimerFunc(milliseconds_per_frame,timer,1); // Cause display to be called again after milliseconds_per_frame. glClearColor(1.0,1.0,1.0,1.0); // Set the color to use in glClear. glClear(GL_COLOR_BUFFER_BIT); // Fill the window with the color. initTransformation(); // Setup drawing coords, transformation. glColor3f(0,0,0); drawString("Red\nGreen\nBlue",-0.75,0.75,0.1); glRotatef(frameCt,0,0,1); glBegin(GL_POLYGON); // Draw a picture glColor3f(1,0,0); glVertex2f(0,0.7); glColor3f(0,1,0); glVertex2f(-0.6,-0.6); glColor3f(0,0,1); glVertex2f(0.6,-0.6); glEnd(); glutSwapBuffers(); // Makes the drawing appear on the screen! } /** * Called when the user changes the size of the window. * It should not be necessary to modify this. */ void reshape(int new_width, int new_height) { glViewport(0,0,new_width,new_height); height = new_height; width = new_width; } /** * If you uncomment the glutMouseFunc() call in the the main program, * then this will be called whenever the user presses or releases * any of the buttons on the mouse. */ void mouse(int button, int state, int x, int y) { } /** * If you uncomment the glutMotionFunc() call in the the main program, * then this will be called whenever the user moves the mouse. */ void motion(int x, int y) { } /** * If you uncomment the glutKeyboardFunc() call in the the main program, * then this will be called whenever the user types a character on * the keyboard. */ void keyboard(unsigned char key, int x, int y) { } /** * If you uncomment the glutSpecialFunc() call in the the main program, * then this will be called when the user presses certain special * keys on the keyboard, such as the arrow keys and function keys. */ void special(int key, int x, int y) { } /** * If you uncomment the glutIdleFunc() call in the the main program, * then this will be called whenever the program does not have any * other events to process. It is can be called repeatedly * while the program is idle. */ void idle() { } int main(int argc, char **argv) { glutInit(&argc,argv); glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE); glutInitWindowSize(400,400); // Set the size of the window here. glutInitWindowPosition(150,50); // Upper left corner of window. glutCreateWindow("OpenGL Window"); // Title displayed in window title bar. glutDisplayFunc(display); glutReshapeFunc(reshape); //glutMouseFunc(mouse); // Uncomment to enable mouse handling. //glutMotionFunc(motion); // Uncomment to enable mouse motion handling. //glutKeyboardFunc(keyboard); // Uncomment to enable ASCII key handling. //glutSpecialFunc(special); // Uncomment to enable special key handling. //glutIdleFunc(idle); // Uncomment to enable the idle function. init(); glutMainLoop(); }