/************************************************************ * File: simple.cc * Author: C.Yap * Date: Sep 7, 2001. * Synopsis: * First OpenGL program (see (Hill, p.40). * Illustrates the initialization for a display window * in GLUT. * ************************************************************/ #include // needed for use of cout #include // needed for math functions exp(x), etc // #include (You need this for Windows) #include ///////////////////////////////////////////////////////////// // Constants ///////////////////////////////////////////////////////////// const int screenWidth = 640; const int screenHeight = 480; ///////////////////////////////////////////////////////////// // Initialization ///////////////////////////////////////////////////////////// void myInit(void) { glClearColor(1.0, 1.0, 1.0, 0.0); // background is white glColor3f(0.0f, 0.0f, 1.0f); // pen is blue glPointSize(2.0); // dot is 2pixels wide glMatrixMode(GL_PROJECTION); // set camera shape glLoadIdentity(); // initialize projection matrix gluOrtho2D(0.0, (GLdouble)screenWidth, 0.0, (GLdouble)screenHeight); // set matrix for 2D graphics }//myInit() ///////////////////////////////////////////////////////////// // callback functions ///////////////////////////////////////////////////////////// void myDisplay(void) { glClear(GL_COLOR_BUFFER_BIT); // clear screen // PLOT FUNCTION: y = exp(-3x) * cos (16 * pi * x) // -- 16*pi gives us 8 full circles over the range of x glBegin(GL_POINTS); for (GLdouble x = 10; x < screenWidth-10; x += 1) { GLdouble y = exp(-3*x/screenWidth) * cos(16 * 3.14159265 * x/screenWidth); y = y*(screenHeight/2) + (screenHeight/2) ; glVertex2d(x, y) ; }//for glEnd(); glFlush(); }//myDisplay() ///////////////////////////////////////////////////////////// // MAIN PROGRAM ///////////////////////////////////////////////////////////// void main(int argc, char** argv) { ///////////////////////////////////////////////////////////// // initialization and create screen window glutInit(&argc, argv); // initialize toolkit glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); // display mode (single buffer) glutInitWindowSize(screenWidth, screenHeight); // window size glutInitWindowPosition(100, 150); // position of top right corner glutCreateWindow("My First Window"); // open it ///////////////////////////////////////////////////////////// // register callback functions glutDisplayFunc(myDisplay); // redraw window event // glutReshapeFunc(myReshape); // reshape window event // glutMouseFunc(myMouse); // mouse button event // glutKeyboardFunc(myKeyboard); // keyboard event myInit(); // special initialization glutMainLoop(); // mandatory final call to glut }//main ///////////////////////////////////////////////////////////// // The end /////////////////////////////////////////////////////////////