sources:


website:
more info here


screenshot:
studies/grafik/Computergrafik-Code2/Aufgabe4/cgapplication.cpp
download file

  1 // Computergrafik I
  2 // Prof. Dr. Juergen Doellner
  3 // Sommersemester 2001
  4 //
  5 // Programmrahmen fuer Aufgabe 4
  6
  7 // include class interface
  8 #include "cgapplication.h"
  9
 10 // to use asserts
 11 #include <assert.h>
 12
 13 // Static variable pointing to the current application to allow only one application.
 14 static CGApplication* activeApplication_ = 0;
 15
 16 // C'tor
 17 CGApplication::CGApplication() {
 18     assert(!activeApplication_); // only one application allowed!!!
 19     activeApplication_ = this;
 20 }
 21
 22 // Destructor
 23 CGApplication::~CGApplication() {
 24     activeApplication_ = 0;
 25 }
 26
 27 // No specific implementation provided.
 28 // This method may be overwritten in a derived class.
 29 void CGApplication::onInit() { }
 30
 31 // No specific implementation provided for the following event.
 32 // These methods may be overwritten in a derived class.
 33 void CGApplication::onButton(MouseButton button, int x, int y) { }
 34 void CGApplication::onMove(int x, int y) { }
 35 void CGApplication::onKey(unsigned char key) { }
 36
 37 //
 38 // glut-callback functions
 39 // These are not class functions!
 40
 41
 42 // glut-callback function is called whenever the content of the window needs to be redrawn.
 43 static void displayFunc() {
 44     assert(activeApplication_);
 45     activeApplication_->onDraw(); // forward call
 46 }
 47
 48 // glut-callback function is called whenever the content of the window needs to be redrawn.
 49 // The newWidth and newHeight callback parameters indicate the new dimensions of the window.
 50 static void reshapeFunc(int newWidth, int newHeight) {
 51     assert(activeApplication_);
 52     activeApplication_->onSize(newWidth, newHeight);
 53 }
 54
 55 // glut-callback function is called whenever the mouse button is pressed or released.
 56 // The button callback parameter is either GLUT_LEFT_BUTTON, GLUT_RIGHT_BUTTON
 57 // or GLUT_MIDDLE_BUTTON indicating the button. The state callback parameter is
 58 // either GLUT_UP or GLUT_DOWN indicating whether the mouse button is pressed
 59 // or released. The x and y parameters indicate the location of the mouse
 60 // when the mouse event occurred.
 61 static void mouseFunc(int button, int state, int x, int y) {
 62     assert(activeApplication_);
 63
 64     if(state == GLUT_DOWN) {
 65         // mouse button pressed
 66         y = glutGet(GLenum(GLUT_WINDOW_HEIGHT)) - y; // Koordinaten-Ursprung: links unten!!!
 67         if(button == GLUT_LEFT_BUTTON) {
 68             // left mouse button
 69             activeApplication_->onButton(CGApplication::LeftMouseButton, x, y); // forward call
 70         } else {
 71             // right mouse button
 72             activeApplication_->onButton(CGApplication::RightMouseButton, x, y); // forward call
 73         }
 74     }
 75 }
 76
 77 // glut-callback function is called whenever the mouse pointer moves within the window while
 78 // a mouse button is pressed. The x and y parameters indicate the location of the mouse
 79 // when the mouse event occurred.
 80 static void motionFunc(int x, int y) {
 81     assert(activeApplication_);
 82
 83     y = glutGet(GLenum(GLUT_WINDOW_HEIGHT)) - y; // origin in the lower left corner!!!
 84     activeApplication_->onMove(x, y); // forward call
 85 }
 86
 87 // glut-callback function is called whenever a key is pressed. The x and y parameters
 88 // indicate the location of the mouse when the key event occurred.
 89 static void keyboardFunc(unsigned char key, int x, int y) {
 90     assert(activeApplication_);
 91     activeApplication_->onKey(key);
 92 }
 93
 94
 95 // Initialization
 96 void CGApplication::start(const char* windowTitle, bool doubleBuffering,
 97                           int width, int height)
{
 98     // Specifies a RGB display mode using double buffering (GLUT_DOUBLE)
 99     // or not (GLUT_SINGLE).
100     glutInitDisplayMode(GLUT_RGB | (doubleBuffering ? GLUT_DOUBLE : GLUT_SINGLE));
101
102     // Defines the dimensions of the window.
103     glutInitWindowSize(width, height);
104
105     // Generate an OpenGL window due to its specifications.
106     glutCreateWindow(windowTitle);
107
108     // Register glut-callback functions to capture glut events.
109     glutDisplayFunc(displayFunc);
110     glutReshapeFunc(reshapeFunc);
111     glutMouseFunc(mouseFunc);
112     glutMotionFunc(motionFunc);
113     glutKeyboardFunc(keyboardFunc);
114
115     // onInit-method call to initialize OpenGL settings.
116     onInit();
117
118     // Start processing the event loop to capture glut events and
119     // display the OpenGL window for drawing/rendering.
120     glutMainLoop();
121 }
122
123 // Swapping buffers.
124 void CGApplication::swapBuffers() {
125     glutSwapBuffers();
126 }
127