// // Computergraphik I // Prof. Dr. Juergen Doellner // Sommersemester 2001 // // Rahmenprogramm zu Aufgabenzettel 7 // #include "cgmultiview.h" #include "vector.h" #include #include CGMultiview::CGMultiview() { run_ = false; mainfilled_ = true; childfilled_ = false; mainzoom_ = 1; zoomxy_ = zoomxz_ = zoomyz_ = 1; rotate_ = rotate2_ = 0; } CGMultiview::~CGMultiview( ) { } void CGMultiview::drawScene() { glRotatef(rotate_,-1,1,0); glRotatef(rotate2_,0,1,0); static GLuint cache = 0; if (cache == 0) { cache = glGenLists(1); glNewList(cache,GL_COMPILE_AND_EXECUTE); glMatrixMode(GL_MODELVIEW); glPushMatrix(); glRotated(-90,1,0,0); ifstream s("big.txt"); // fuer den Porsche //ifstream s("small.txt"); char buf[100]; int nTriangleCount = 0; 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()); nTriangleCount++; } if (s.rdstate() & ios::failbit) { s.clear(s.rdstate() & ~ios::failbit); } } glEnd(); glPopMatrix(); glEndList(); cout << "triangles: " << nTriangleCount << endl; } else { glCallList(cache); } } void CGMultiview::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.95,0.95,0.95,1); } void CGMultiview::onSize(unsigned int newWidth,unsigned int newHeight) { width_ = newWidth; height_ = newHeight; glutPostRedisplay(); } void CGMultiview::onKey(unsigned char key) { switch (key) { case 27: { exit(0); break; } case ' ': { run_ = !run_; break; } case '+': { mainzoom_*= 1.1; break; } case '-': { mainzoom_*= 0.9; break; } case '1': { zoomxy_*= 1.1; break; } case '2': { zoomxy_*= 0.9; break; } case '3': { zoomxz_*= 1.1; break; } case '4': { zoomxz_*= 0.9; break; } case '5': { zoomyz_*= 1.1; break; } case '6': { zoomyz_*= 0.9; break; } case 'l': { childfilled_ = !childfilled_; break; } case 'L': { mainfilled_ = !mainfilled_; break; } } glutPostRedisplay(); } void CGMultiview::onIdle() { if (run_) { rotate_ += 1; glutPostRedisplay(); } } void CGMultiview::onDrag(double dx, double dy) { rotate2_ += dx*20; // glRotatef(-dy*20, 1, 0, 0); glutPostRedisplay(); } void CGMultiview::onDraw() { // Loesche den Farb- und Tiefenspeicher glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Fentergröße int maxx = width_; int maxy = height_; int orthosize = maxy/3; // zeichne perspektivische Hauptansicht glViewport(0,0, maxx-1, maxy-1); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(45/mainzoom_, (float)maxx/maxy, 0.1, 100.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(0,0,10, 0,0,0, 0,1,0); // Füllmodus glPolygonMode(GL_FRONT_AND_BACK, mainfilled_ ? GL_FILL:GL_LINE); glColor3f(1,1,1); drawScene(); glPolygonMode(GL_FRONT_AND_BACK, childfilled_ ? GL_FILL:GL_LINE); // zeichne Ansicht x,y glViewport(maxx-orthosize, 2*orthosize, orthosize, orthosize); glMatrixMode(GL_PROJECTION); // orthogonale Sicht glLoadIdentity(); glOrtho(-1/zoomxy_, 1/zoomxy_, -1/zoomxy_, 1/zoomxy_, 0.1, 100); // entlang z-Achse glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(0,0,10, 0,0,0, 0,1,0); glColor3f(0,0,1); drawScene(); // zeichne Ansicht x,z glViewport(maxx-orthosize, orthosize, orthosize, orthosize); glMatrixMode(GL_PROJECTION); // orthogonale Sicht glLoadIdentity(); glOrtho(-1/zoomxz_, 1/zoomxz_, -1/zoomxz_, 1/zoomxz_, 0.1, 100); // entlang y-Achse glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(0,10,0, 0,0,0, -1,0,0); glColor3f(0,1,0); drawScene(); // zeichne Ansicht y,z glViewport(maxx-orthosize, 0, orthosize, orthosize); glMatrixMode(GL_PROJECTION); // orthogonale Sicht glLoadIdentity(); glOrtho(-1/zoomyz_, 1/zoomyz_, -1/zoomyz_, 1/zoomyz_, 0.1, 100); // entlang x-Achse glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(10,0,0, 0,0,0, 0,1,0); glColor3f(1,0,0); drawScene(); // Nicht vergessen! Front- und Back-Buffer tauschen: swapBuffers(); } // Hauptprogramm int main(int argc, char* argv[]) { // Erzeuge eine Instanz der Beispiel-Anwendung: CGMultiview sample; // Starte die Beispiel-Anwendung: sample.start("CGMultiview, Stephan Brumme, 702544"); return(0); }