// Computergraphik I // Prof. Dr. Juergen Doellner // Sommersemester 2001 // // Rahmenprogramm fuer Aufgabenzettel 1 // Stephan Brumme, 702544 // last changes: May 01, 2001 #include "cgmalen.h" CGMalen::CGMalen() : m_x(-10), m_y(-10) { } CGMalen::~CGMalen() { } void CGMalen::onInit() { //*************************************** // Setze die Hintergrundfarbe auf grau: //*************************************** // gray background with no alpha glClearColor(0.75, 0.75, 0.75, 0.0); //*************************************** // Loesche den Farbspeicher (wird mit der Hintergrundfarbe // gefuellt): //*************************************** // needs to be done twice (back- and front buffer) glClear(GL_COLOR_BUFFER_BIT); swapBuffers(); glClear(GL_COLOR_BUFFER_BIT); //*************************************** // Setze die Zeichenfarbe //*************************************** // blue is initially set color glColor3f(0.0, 0.0, 1.0); } void CGMalen::onDraw() { //*************************************** // Zeichne das Rechteck an der gespeicherten Position: //*************************************** // needs to be done twice (back- and front buffer) glRecti(m_x, m_y, m_x+5, m_y+5); swapBuffers(); glRecti(m_x, m_y, m_x+5, m_y+5); // Nicht vergessen! Front- und Back-Buffer tauschen: swapBuffers(); } void CGMalen::onSize(unsigned int newWidth, unsigned int newHeight) { if((newWidth > 0) && (newHeight > 0)) { // Passe den OpenGL-Viewport an die neue Fenstergroesse an: glViewport(0, 0, newWidth - 1, newHeight - 1); // Passe die OpenGL-Projektionsmatrix an die neue // Fenstergroesse an: glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(0.0, newWidth - 1, 0.0, newHeight - 1); // Schalte zurueck auf die Modelview-Matrix und // initialisiere sie mit der Einheitsmatrix: glMatrixMode(GL_MODELVIEW); glLoadIdentity(); // glutPostRedisplay(); } } void CGMalen::onButton(MouseButton button, int x, int y) { //*************************************** // Speichere die aktuelle Mausposition: //*************************************** // save mouse cursor position m_x = x; m_y = y; // save mouse button for onMove m_mbLastPressed = button; // force redraw glutPostRedisplay(); } void CGMalen::onMove(int x, int y) { // pass current position to onButton onButton(m_mbLastPressed, x, y); } void CGMalen::onKey(unsigned char key) { //*************************************** // reagiere auf Tasteneingabe //*************************************** switch (key) { // change current color // red case 'r': glColor3f(1.0, 0.0, 0.0); break; // green case 'g': glColor3f(0.0, 1.0, 0.0); break; // blue case 'b': glColor3f(0.0, 0.0, 1.0); break; // black case 's': glColor3f(0.0, 0.0, 0.0); break; // white case 'w': glColor3f(1.0, 1.0, 1.0); break; // clear view (clear both buffers !!!) case 'c': glClear(GL_COLOR_BUFFER_BIT); swapBuffers(); glClear(GL_COLOR_BUFFER_BIT); break; // quit program case 'q': exit(0); } } // Hauptprogramm int main(int argc, char* argv[]) { // Erzeuge eine Instanz der Beispiel-Anwendung: CGMalen sample; // Starte die Beispiel-Anwendung: sample.start("CGMalen, Stephan Brumme, 702544"); return(0); }