sources:
cgapplication.cpp (4.0k)
cgapplication.h (4.8k)
cgtessellator.cpp (5.6k)
cgtessellator.h (1.3k)


binaries:
Release/Aufgabe11.exe (60.0k)


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
 49                           : CGApplication::RightMouseButton)
;
 50     activeApplication_->onButton(activeMouseButton_,
 51                                  ((state == GLUT_DOWN)
 52                                   ? CGApplication::MouseButtonDown
 53                                   : CGApplication::MouseButtonUp)
,
 54                                  x, y)
;
 55 }
 56
 57 // glut-Callback fuer das Ziehen bei gedrueckter Maustaste
 58 static void motionFunc(int x, int y) {
 59     assert(activeApplication_);
 60     y = glutGet(GLenum(GLUT_WINDOW_HEIGHT)) - y; // Koordinaten-Ursprung: links unten!!!
 61     activeApplication_->onMove(activeMouseButton_, x, y);
 62 }
 63
 64 // glut-Callback fuer einen Tastendruck
 65 static void keyboardFunc(unsigned char key, int x, int y) {
 66     assert(activeApplication_);
 67     activeApplication_->onKey(key);
 68 }
 69
 70 // glut-Callback fuer ein Timer-Event
 71 static void timerFunc(int msecs) {
 72     assert(activeApplication_);
 73     activeApplication_->onTimer();
 74     glutTimerFunc(msecs, timerFunc, msecs);
 75 }
 76
 77 void CGApplication::start(int argc, char* argv[],
 78                           const char* windowTitle,
 79                           unsigned int buffers, unsigned int msecsTimer,
 80                           unsigned long width, unsigned long height)
{
 81     assert(buffers & ColorBuffer);
 82
 83     // Initialisiert Glut
 84     glutInit(&argc, argv);
 85
 86     // Fordert ein OpenGL-Fenster im RGB-Format mit oder ohne Double-Buffering an:
 87     glutInitDisplayMode(GLUT_RGB
 88                         | ((buffers & DoubleBuffer) ? GLUT_DOUBLE : GLUT_SINGLE)
 89                         | ((buffers & StencilBuffer) ? GLUT_STENCIL : 0))
;
 90
 91     // Legt die Fenstergroesse fest:
 92     glutInitWindowSize(width, height);
 93
 94     // Erzeugt das OpenGL-Fenster mit dem entsprechenden Namen:
 95     glutCreateWindow(windowTitle);
 96
 97     // Registriere die oben definierten Callbacks fuer die
 98     // entsprechenden glut-Ereignisse:
 99     glutDisplayFunc(displayFunc);
100     glutReshapeFunc(reshapeFunc);
101     glutMouseFunc(mouseFunc);
102     glutMotionFunc(motionFunc);
103     glutKeyboardFunc(keyboardFunc);
104
105     if(msecsTimer > 0) { glutTimerFunc(msecsTimer, timerFunc, msecsTimer); }
106
107     // Zeige das OpenGL-Fenster auf dem Bildschirm an
108     glutShowWindow();
109
110     // Rufe die Methode zur Initialisierung von OpenGL auf:
111     onInit();
112
113     // Starte die Verarbeitung der glut-Ereignisse:
114     glutMainLoop();
115 }
116
117 void CGApplication::needsRedraw() {
118     glutPostRedisplay();
119 }
120
121 void CGApplication::swapBuffers() {
122     glutSwapBuffers();
123 }
124