// // Computergraphik II // Prof. Dr. Juergen Doellner // Wintersemester 2001/02 // // Rahmenprogramm zu Aufgabenzettel 4 // #include "material.h" CGMaterial::CGMaterial() { run_ = false; zoom_ = 0.8; } CGMaterial::~CGMaterial( ) { } void CGMaterial::setLight(int x, int y) { // Hier werden die Lichtquellen definiert } void CGMaterial::setMaterial(int x, int y) { // Hier werden die Materialeigenschaften festgelegt // ambient float ambient[4]; ambient[0] = 0.5; ambient[1] = ambient[2] = ambient[3] = 0; glMaterialfv(GL_FRONT, GL_AMBIENT, ambient); // diffuse float diffuse[4]; diffuse[0] = x/(XWindows-1.0); diffuse[1] = diffuse[2] = diffuse[3] = 0; glMaterialfv(GL_FRONT, GL_DIFFUSE, diffuse); // specular static const float specular[] = { 0.5, 0.5, 0.5, 0.5 }; glMaterialfv(GL_FRONT, GL_SPECULAR, specular); glMaterialf(GL_FRONT, GL_SHININESS, 16*pow(2,y)); } void CGMaterial::setSmallViewport(int x, int y) { int sizeX = (width_/XWindows); int sizeY = (height_/YWindows); glViewport(x*sizeX, y*sizeY, sizeX - 1, sizeY - 1); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(36.0,float(sizeX)/float(sizeY),2.0, 100.0); glMatrixMode(GL_MODELVIEW); } void CGMaterial::drawScene() { glPushMatrix(); glScaled(zoom_, zoom_, zoom_); glRotatef(70.0, 1., 0., 0.); glColor4f(1,0,0,0); static GLuint cache = 0; if (cache == 0) { cache = glGenLists(1); glNewList(cache, GL_COMPILE_AND_EXECUTE); // Hinweis: Falls das Programm zu langsam läuft, kann man die Tessellation // des Torus durch Verringern des dritten und vierten Parameters vergröbern. // Dadurch leidet aber die Darstellungsqualität. glutSolidTorus(0.5, 1.0, 40, 40); //glutSolidTeapot(1.0); glEndList(); } else { glCallList(cache); } glPopMatrix(); } void CGMaterial::onInit() { // automatische Normalisierung glEnable(GL_NORMALIZE); // Tiefen Test aktivieren glEnable(GL_DEPTH_TEST); // Beleuchtung aktivieren glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); static const float lightPosition0[] = { -3, 3, 0, 0 }; glLightfv(GL_LIGHT0, GL_POSITION, lightPosition0); // glEnable(GL_COLOR_MATERIAL); // glColorMaterial(GL_FRONT,GL_AMBIENT_AND_DIFFUSE); // Smooth Schattierung aktivieren glShadeModel(GL_SMOOTH); // 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 CGMaterial::onSize(unsigned int newWidth,unsigned int newHeight) { width_ = newWidth; height_ = newHeight; glViewport(0, 0, width_ - 1, height_ - 1); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(40.0,float(width_)/float(height_),2.0, 100.0); glMatrixMode(GL_MODELVIEW); } void CGMaterial::onKey(unsigned char key) { switch (key) { case 27: { exit(0); break; } case '+': { zoom_*= 1.1; break; } case '-': { zoom_*= 0.9; break; } case ' ': { run_ = !run_; break; } } onDraw(); } void CGMaterial::onIdle() { if (run_) { glRotatef(5,0.1,1,0.2); onDraw(); } } void CGMaterial::onDraw() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); for (int x=0; x