sources:


website:
more info here


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

  1 // Computergrafik I
  2 // Prof. Dr. Juergen Doellner
  3 // Sommersemester 2001
  4 //
  5 // Programmrahmen fuer Aufgabe 5
  6
  7 #include "cgapplication.h"
  8
  9 // Diese Variable enthaelt einen Zeiger auf die aktive Anwendung.
 10 static CGApplication* activeApplication_ = 0;
 11
 12 CGApplication::CGApplication() {
 13     assert(!activeApplication_); // Es ist nur eine aktive Anwendung erlaubt!!!
 14     activeApplication_ = this;
 15 }
 16
 17 CGApplication::~CGApplication() {
 18     activeApplication_ = 0;
 19 }
 20
 21 // Auf diese Ereignisse wird default-maessig nicht reagiert:
 22 void CGApplication::onInit() { }
 23 void CGApplication::onButton(MouseButton button, int x, int y) { }
 24 void CGApplication::onMove(int x, int y) { }
 25 void CGApplication::onKey(unsigned char key) { }
 26
 27 // glut-Callback fuer den Zeichenaufruf
 28 static void displayFunc() {
 29     assert(activeApplication_);
 30     activeApplication_->onDraw();
 31 }
 32
 33 // glut-Callback fuer die Aenderung der Fenstergroesse
 34 static void reshapeFunc(int newWidth, int newHeight) {
 35     assert(activeApplication_);
 36     activeApplication_->onSize(newWidth, newHeight);
 37 }
 38
 39 // glut-Callback fuer das Druecken einer Maustaste
 40 static void mouseFunc(int button, int state, int x, int y) {
 41     assert(activeApplication_);
 42     if(state == GLUT_DOWN) {
 43         y = glutGet(GLenum(GLUT_WINDOW_HEIGHT)) - y; // Koordinaten-Ursprung: links unten!!!
 44         if(button == GLUT_LEFT_BUTTON) {
 45             activeApplication_->onButton(CGApplication::LeftMouseButton, x, y);
 46         } else {
 47             activeApplication_->onButton(CGApplication::RightMouseButton, x, y);
 48         }
 49     }
 50 }
 51
 52 // glut-Callback fuer das Ziehen bei gedrueckter Maustaste
 53 static void motionFunc(int x, int y) {
 54     assert(activeApplication_);
 55     y = glutGet(GLenum(GLUT_WINDOW_HEIGHT)) - y; // Koordinaten-Ursprung: links unten!!!
 56     activeApplication_->onMove(x, y);
 57 }
 58
 59 // glut-Callback fuer einen Tastendruck
 60 static void keyboardFunc(unsigned char key, int x, int y) {
 61     assert(activeApplication_);
 62     activeApplication_->onKey(key);
 63 }
 64
 65 void CGApplication::start(const char* windowTitle, bool doubleBuffering,
 66                           unsigned long width, unsigned long height)
{
 67     // Fordert ein OpenGL-Fenster im RGB-Format mit oder ohne Double-Buffering an:
 68     glutInitDisplayMode(GLUT_RGB | (doubleBuffering ? GLUT_DOUBLE : GLUT_SINGLE));
 69
 70     // Legt die Fenstergroesse fest:
 71     glutInitWindowSize(width, height);
 72
 73     // Erzeugt das OpenGL-Fenster mit dem entsprechenden Namen:
 74     glutCreateWindow(windowTitle);
 75
 76     // Registriere die oben definierten Callbacks fuer die
 77     // entsprechenden glut-Ereignisse:
 78     glutDisplayFunc(displayFunc);
 79     glutReshapeFunc(reshapeFunc);
 80     glutMouseFunc(mouseFunc);
 81     glutMotionFunc(motionFunc);
 82     glutKeyboardFunc(keyboardFunc);
 83
 84     // Rufe die Methode zur Initialisierung von OpenGL auf:
 85     onInit();
 86
 87     // Starte die Verarbeitung der glut-Ereignisse:
 88     glutMainLoop();
 89 }
 90
 91 void CGApplication::swapBuffers() {
 92     glutSwapBuffers();
 93 }
 94
 95