// // Computergraphik II // Prof. Dr. Juergen Doellner // Wintersemester 2001/02 // // Rahmenprogramm zu Aufgabenzettel 1 // #include "cgtranslucent.h" #include #include #include CGTranslucent::CGTranslucent() { run_ = true; moveX_ = 0.0; dirX_ = 0.005; } CGTranslucent::~CGTranslucent( ) { } void CGTranslucent::onInit() { // OpenGL Lichtquelle static GLfloat light_diffuse[] = {1.0, 1.0, 1.0, 1.0}; /* diffuse light. */ static GLfloat light_position[] = {1.0, 1.0, 1.0, 0.0}; /* Infinite light location. */ glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse); glLightfv(GL_LIGHT0, GL_POSITION, light_position); glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); // automatische Normalisierung glEnable(GL_NORMALIZE); glEnable(GL_COLOR_MATERIAL); glColorMaterial(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE); // Tiefen Test aktivieren glEnable(GL_DEPTH_TEST); // Smooth Schattierung aktivieren glShadeModel(GL_SMOOTH); // Projection glMatrixMode(GL_PROJECTION); gluPerspective(60.0,1.0,2,2000.0); // LookAt glMatrixMode(GL_MODELVIEW); gluLookAt(0.0, 0.0, 4.0, // from (0,0,4) 0.0, 0.0, 0.0, // to (0,0,0) 0.0, 1.0, 0.); // up glClearColor(1.0,1.0,1.0,1.0); } void CGTranslucent::onSize(unsigned int newWidth,unsigned int newHeight) { width_ = newWidth; height_ = newHeight; glMatrixMode(GL_PROJECTION); glViewport(0, 0, width_ - 1, height_ - 1); glLoadIdentity(); gluPerspective(40.0,float(width_)/float(height_),2.0, 100.0); glMatrixMode(GL_MODELVIEW); } void CGTranslucent::onKey(unsigned char key) { switch (key) { case 27: { exit(0); break; } case ' ': { run_ = !run_; break; } } onDraw(); } void CGTranslucent::onIdle() { moveX_+= dirX_; if(moveX_ > 1.4) { moveX_-=(dirX_); dirX_ = -dirX_; } else if(moveX_ < -1.4) { moveX_-=(dirX_); dirX_ = -dirX_; } if (run_) { glRotatef(1,0.1,1,0.2); } onDraw(); } void CGTranslucent::onDraw() { // Mechanismus zur Behandlung opaker Objekte glDepthMask(GL_TRUE); glDisable(GL_BLEND); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glColor4f(1,1,0,1); // nicht Transparent (A=1) glPushMatrix(); glTranslatef(moveX_,0,0); glutSolidSphere(0.5, 32, 32); glPopMatrix(); // Mechanismus zur Behandlung transparenter Objekte glEnable(GL_BLEND); glDepthMask(GL_FALSE); glColor4f(1,0,0,0.4); // Transparent (A=0.4) glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glutSolidSphere(0.8, 32, 32); // Nicht vergessen! Front- und Back-Buffer tauschen: swapBuffers(); } // Hauptprogramm int main(int argc, char* argv[]) { // Erzeuge eine Instanz der Beispiel-Anwendung: CGTranslucent sample; // Starte die Beispiel-Anwendung: sample.start("CGTranslucent", true, 400, 400); return(0); }