// // Computergraphik II // Prof. Dr. Juergen Doellner // Wintersemester 2001/02 // // Rahmenprogramm zu Aufgabenzettel 2 // #include "cglinestyle.h" #include "vector.h" #include #include CGLinestyle::CGLinestyle() { run_ = false; zoom_ = 0.15; culling_ = false; lighting_ = true; mode_ = SEGMENTED; haloewidth_ = 5.0; segmentwidth_ = 1.0; } CGLinestyle::~CGLinestyle( ) { } void CGLinestyle::drawScene() { glPushMatrix(); glScaled(zoom_, zoom_, zoom_); static GLuint cache = 0; if (cache == 0) { cache = glGenLists(1); glNewList(cache, GL_COMPILE_AND_EXECUTE); glMatrixMode(GL_MODELVIEW); glPushMatrix(); //ifstream s("small.txt"); //ifstream s("stegarosaurus.txt"); ifstream s("triceratops.txt"); char buf[100]; glBegin(GL_TRIANGLES); Vector v[3]; while (!s.eof()) { s >> buf; while (s.good()) { s >> v[0] >> v[1] >> v[2]; Vector n = (v[1]-v[0])*(v[2]-v[0]); glNormal3dv(n.rep()); glVertex3dv(v[0].rep()); glVertex3dv(v[1].rep()); glVertex3dv(v[2].rep()); } if (s.rdstate() & ios::failbit) { s.clear(s.rdstate() & ~ios::failbit); } } glEnd(); glPopMatrix(); glEndList(); } else { glCallList(cache); } glPopMatrix(); } void CGLinestyle::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(0.0,0.0,0.0,1.0); } void CGLinestyle::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 CGLinestyle::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; } case 'c': { culling_ = !culling_; break; } case 'l': { lighting_ = !lighting_; break; } case 's': { mode_ = SEGMENTED; break; } case 'h': { mode_ = HALOED; break; } case '1': { haloewidth_*= 0.9; break; } case '2': { haloewidth_*= 1.1; break; } case '3': { segmentwidth_*= 0.9; break; } case '4': { segmentwidth_*= 1.1; break; } } onDraw(); } void CGLinestyle::onIdle() { if (run_) { glRotatef(1,0.1,1,0.2); onDraw(); } } void CGLinestyle::onDraw() { // clear buffers glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); glDepthMask(GL_TRUE); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); if (culling_) glEnable(GL_CULL_FACE); if (!lighting_) glDisable(GL_LIGHTING); if (mode_ == SEGMENTED) { // SEGMENTED // important ! default is GL_LESS glDepthFunc(GL_LESS); // wireframe glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); glLineWidth(segmentwidth_); glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); drawScene(); // filled glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); drawScene(); } else { // HALOED // important ! default is GL_LESS glDepthFunc(GL_LEQUAL); // wireframe I glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); glLineWidth(haloewidth_); glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); drawScene(); // wireframe II glLineWidth(1); glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); drawScene(); } // clean up glEnable(GL_LIGHTING); glDisable(GL_CULL_FACE); // Front- und Back-Buffer tauschen: swapBuffers(); } // Hauptprogramm int main(int argc, char* argv[]) { // Erzeuge eine Instanz der Beispiel-Anwendung: CGLinestyle sample; cout << "Tastenbelegung:" << endl << "ESC Programm beenden" << endl << "Leertaste Objekt drehen" << endl << "+ in die Szene hineinzoomen" << endl << "- aus der Szene herauszoomen" << endl << "c Culling de-/aktivieren" << endl << "l Beleuchtung de-/aktivieren" << endl << "s segmented surfaces" << endl << "h haloed wires" << endl << "1 halo verringern" << endl << "2 halo verstärken" << endl << "3 Abstand der Segmente verringern" << endl << "4 Abstand der Segmente vergrößern" << endl; // Starte die Beispiel-Anwendung: sample.start("Stephan Brumme, 702544", true, 550, 550); return(0); }