sources:


website:
more info here


screenshot:
studies/grafik/Computergrafik-Code4/Aufgabe9/cgapplication.cpp
download file

  1 // Computergraphik I
  2 // Prof. Dr. Juergen Doellner
  3 // Sommersemester 2001
  4 //
  5 // Rahmenprogramm fuer Aufgabenzettel 3
  6
  7 #include "cgapplication.h"
  8
  9 // Diese Variable enthaelt einen Zeiger auf die aktive Anwendung.
 10 static CGApplication* activeApplication_ = 0;
 11 static CGApplication::MouseButton activeMouseButton_ = CGApplication::LeftMouseButton;
 12
 13 CGApplication::CGApplication() {
 14     assert(!activeApplication_); // Es ist nur eine aktive Anwendung erlaubt!!!
 15     activeApplication_ = this;
 16 }
 17
 18 CGApplication::~CGApplication() {
 19     activeApplication_ = 0;
 20 }
 21
 22 // Auf diese Ereignisse wird default-maessig nicht reagiert:
 23 void CGApplication::onInit() { }
 24 void CGApplication::onButton(MouseButton button, MouseButtonEvent event, int x, int y) { }
 25 void CGApplication::onMove(MouseButton buttoin, int x, int y) { }
 26 void CGApplication::onKey(unsigned char key) { }
 27
 28 // glut-Callback fuer den Zeichenaufruf
 29 static void displayFunc() {
 30     assert(activeApplication_);
 31     activeApplication_->onDraw();
 32 }
 33
 34 // glut-Callback fuer die Aenderung der Fenstergroesse
 35 static void reshapeFunc(int newWidth, int newHeight) {
 36     assert(activeApplication_);
 37     activeApplication_->onSize(newWidth, newHeight);
 38 }
 39
 40 // glut-Callback fuer das Druecken einer Maustaste
 41 static void mouseFunc(int button, int state, int x, int y) {
 42     assert(activeApplication_);
 43     y = glutGet(GLenum(GLUT_WINDOW_HEIGHT)) - y; // Koordinaten-Ursprung: links unten!!!
 44     activeMouseButton_ = ((button == GLUT_LEFT_BUTTON)
 45                           ? CGApplication::LeftMouseButton                           : CGApplication::RightMouseButton)
;
 46     activeApplication_->onButton(activeMouseButton_,
 47                                  ((state == GLUT_DOWN)
 48                                   ? CGApplication::MouseButtonDown                                   : CGApplication::MouseButtonUp)
,
 49                                  x, y)
;
 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(activeMouseButton_, 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(int argc, char* argv[],
 66                           const char* windowTitle,
 67                           unsigned int buffers,
 68                           unsigned long width, unsigned long height)
{
 69     assert(buffers & ColorBuffer);
 70
 71     // Initialisiert Glut
 72     glutInit(&argc, argv);
 73
 74     // Fordert ein OpenGL-Fenster im RGB-Format mit oder ohne Double-Buffering an:
 75     glutInitDisplayMode(GLUT_RGB | ((buffers & DoubleBuffer) ? GLUT_DOUBLE : GLUT_SINGLE) );
 76
 77     // Legt die Fenstergroesse fest:
 78     glutInitWindowSize(width, height);
 79
 80     // Erzeugt das OpenGL-Fenster mit dem entsprechenden Namen:
 81     glutCreateWindow(windowTitle);
 82
 83     // Registriere die oben definierten Callbacks fuer die
 84     // entsprechenden glut-Ereignisse:
 85     glutDisplayFunc(displayFunc);
 86     glutReshapeFunc(reshapeFunc);
 87     glutMouseFunc(mouseFunc);
 88     glutMotionFunc(motionFunc);
 89     glutKeyboardFunc(keyboardFunc);
 90
 91     // Zeige das OpenGL-Fenster auf dem Bildschirm an
 92     glutShowWindow();
 93
 94     // Rufe die Methode zur Initialisierung von OpenGL auf:
 95     onInit();
 96
 97     // Starte die Verarbeitung der glut-Ereignisse:
 98     glutMainLoop();
 99 }
100
101 void CGApplication::swapBuffers() {
102     glutSwapBuffers();
103 }
104
105