// Computergrafik I // Prof. Dr. Juergen Doellner // Sommersemester 2001 // // Programmrahmen fuer Aufgabe 4 // include class interface #include "cgapplication.h" // to use asserts #include // Static variable pointing to the current application to allow only one application. static CGApplication* activeApplication_ = 0; // C'tor CGApplication::CGApplication() { assert(!activeApplication_); // only one application allowed!!! activeApplication_ = this; } // Destructor CGApplication::~CGApplication() { activeApplication_ = 0; } // No specific implementation provided. // This method may be overwritten in a derived class. void CGApplication::onInit() { } // No specific implementation provided for the following event. // These methods may be overwritten in a derived class. void CGApplication::onButton(MouseButton button, int x, int y) { } void CGApplication::onMove(int x, int y) { } void CGApplication::onKey(unsigned char key) { } // // glut-callback functions // These are not class functions! // glut-callback function is called whenever the content of the window needs to be redrawn. static void displayFunc() { assert(activeApplication_); activeApplication_->onDraw(); // forward call } // glut-callback function is called whenever the content of the window needs to be redrawn. // The newWidth and newHeight callback parameters indicate the new dimensions of the window. static void reshapeFunc(int newWidth, int newHeight) { assert(activeApplication_); activeApplication_->onSize(newWidth, newHeight); } // glut-callback function is called whenever the mouse button is pressed or released. // The button callback parameter is either GLUT_LEFT_BUTTON, GLUT_RIGHT_BUTTON // or GLUT_MIDDLE_BUTTON indicating the button. The state callback parameter is // either GLUT_UP or GLUT_DOWN indicating whether the mouse button is pressed // or released. The x and y parameters indicate the location of the mouse // when the mouse event occurred. static void mouseFunc(int button, int state, int x, int y) { assert(activeApplication_); if(state == GLUT_DOWN) { // mouse button pressed y = glutGet(GLenum(GLUT_WINDOW_HEIGHT)) - y; // Koordinaten-Ursprung: links unten!!! if(button == GLUT_LEFT_BUTTON) { // left mouse button activeApplication_->onButton(CGApplication::LeftMouseButton, x, y); // forward call } else { // right mouse button activeApplication_->onButton(CGApplication::RightMouseButton, x, y); // forward call } } } // glut-callback function is called whenever the mouse pointer moves within the window while // a mouse button is pressed. The x and y parameters indicate the location of the mouse // when the mouse event occurred. static void motionFunc(int x, int y) { assert(activeApplication_); y = glutGet(GLenum(GLUT_WINDOW_HEIGHT)) - y; // origin in the lower left corner!!! activeApplication_->onMove(x, y); // forward call } // glut-callback function is called whenever a key is pressed. The x and y parameters // indicate the location of the mouse when the key event occurred. static void keyboardFunc(unsigned char key, int x, int y) { assert(activeApplication_); activeApplication_->onKey(key); } // Initialization void CGApplication::start(const char* windowTitle, bool doubleBuffering, int width, int height) { // Specifies a RGB display mode using double buffering (GLUT_DOUBLE) // or not (GLUT_SINGLE). glutInitDisplayMode(GLUT_RGB | (doubleBuffering ? GLUT_DOUBLE : GLUT_SINGLE)); // Defines the dimensions of the window. glutInitWindowSize(width, height); // Generate an OpenGL window due to its specifications. glutCreateWindow(windowTitle); // Register glut-callback functions to capture glut events. glutDisplayFunc(displayFunc); glutReshapeFunc(reshapeFunc); glutMouseFunc(mouseFunc); glutMotionFunc(motionFunc); glutKeyboardFunc(keyboardFunc); // onInit-method call to initialize OpenGL settings. onInit(); // Start processing the event loop to capture glut events and // display the OpenGL window for drawing/rendering. glutMainLoop(); } // Swapping buffers. void CGApplication::swapBuffers() { glutSwapBuffers(); }