sources:


website:
more info here


screenshot:
studies/grafik/Computergrafik-Code1/Aufgabe2/cgmalen.cpp
download file

  1 // Computergraphik I
  2 // Prof. Dr. Juergen Doellner
  3 // Sommersemester 2001
  4 //
  5 // Rahmenprogramm fuer Aufgabenzettel 1
  6
  7
  8 // Stephan Brumme, 702544
  9 // last changes: May 01, 2001
 10
 11 #include "cgmalen.h"
 12
 13 CGMalen::CGMalen() : m_x(-10), m_y(-10) {
 14 }
 15
 16 CGMalen::~CGMalen() {
 17 }
 18
 19 void CGMalen::onInit() {
 20     //***************************************
 21     // Setze die Hintergrundfarbe auf grau:
 22     //***************************************
 23
 24     // gray background with no alpha
 25     glClearColor(0.75, 0.75, 0.75, 0.0);
 26
 27     //***************************************
 28     // Loesche den Farbspeicher (wird mit der Hintergrundfarbe
 29     // gefuellt):
 30     //***************************************
 31
 32     // needs to be done twice (back- and front buffer)
 33     glClear(GL_COLOR_BUFFER_BIT);
 34     swapBuffers();
 35     glClear(GL_COLOR_BUFFER_BIT);
 36
 37     //***************************************
 38     // Setze die Zeichenfarbe
 39     //***************************************
 40
 41     // blue is initially set color
 42     glColor3f(0.0, 0.0, 1.0);
 43 }
 44
 45 void CGMalen::onDraw() {   
 46
 47     //***************************************
 48     // Zeichne das Rechteck an der gespeicherten Position:
 49     //***************************************
 50
 51     // needs to be done twice (back- and front buffer)
 52     glRecti(m_x, m_y, m_x+5, m_y+5);
 53     swapBuffers();
 54     glRecti(m_x, m_y, m_x+5, m_y+5);
 55
 56     // Nicht vergessen! Front- und Back-Buffer tauschen:
 57     swapBuffers();
 58 }
 59
 60 void CGMalen::onSize(unsigned int newWidth, unsigned int newHeight) {
 61     if((newWidth > 0) && (newHeight > 0)) {
 62         // Passe den OpenGL-Viewport an die neue Fenstergroesse an:
 63         glViewport(0, 0, newWidth - 1, newHeight - 1);
 64
 65         // Passe die OpenGL-Projektionsmatrix an die neue
 66         // Fenstergroesse an:
 67         glMatrixMode(GL_PROJECTION);
 68         glLoadIdentity();
 69         gluOrtho2D(0.0, newWidth - 1, 0.0, newHeight - 1);
 70        
 71         // Schalte zurueck auf die Modelview-Matrix und
 72         // initialisiere sie mit der Einheitsmatrix:
 73         glMatrixMode(GL_MODELVIEW);
 74         glLoadIdentity();
 75
 76 // glutPostRedisplay();
 77     }
 78 }
 79
 80 void CGMalen::onButton(MouseButton button, int x, int y) {
 81     //***************************************
 82     // Speichere die aktuelle Mausposition:
 83     //***************************************
 84
 85     // save mouse cursor position
 86     m_x = x;
 87     m_y = y;
 88
 89     // save mouse button for onMove
 90     m_mbLastPressed = button;
 91
 92     // force redraw
 93     glutPostRedisplay();
 94 }
 95
 96 void CGMalen::onMove(int x, int y) {
 97     // pass current position to onButton
 98     onButton(m_mbLastPressed, x, y);
 99 }
100
101 void CGMalen::onKey(unsigned char key) {
102     //***************************************
103     // reagiere auf Tasteneingabe
104     //***************************************
105
106     switch (key)
107     {
108     // change current color
109     // red
110     case 'r': glColor3f(1.0, 0.0, 0.0);
111               break;
112     // green
113     case 'g': glColor3f(0.0, 1.0, 0.0);
114               break;
115     // blue
116     case 'b': glColor3f(0.0, 0.0, 1.0);
117               break;
118     // black
119     case 's': glColor3f(0.0, 0.0, 0.0);
120               break;
121     // white
122     case 'w': glColor3f(1.0, 1.0, 1.0);
123               break;
124
125     // clear view (clear both buffers !!!)
126     case 'c': glClear(GL_COLOR_BUFFER_BIT);
127               swapBuffers();
128               glClear(GL_COLOR_BUFFER_BIT);
129               break;
130
131     // quit program
132     case 'q': exit(0);
133     }
134 }
135
136 // Hauptprogramm
137 int main(int argc, char* argv[]) {
138     // Erzeuge eine Instanz der Beispiel-Anwendung:
139     CGMalen sample;
140
141     // Starte die Beispiel-Anwendung:
142     sample.start("CGMalen, Stephan Brumme, 702544");
143     return(0);
144 }
145
146