sources:


website:
more info here


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

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