// // Computergraphik I // Prof. Dr. Juergen Doellner // Sommersemester 2001 // // Rahmenprogramm fuer Aufgabenzettel 6 // #include "cgscooter.h" CGScooter::CGScooter() { rotate_ = true; run_ = false; axis_ = true; } CGScooter::~CGScooter() { } void CGScooter::drawBox() { glutSolidCube(1); } void CGScooter::drawSphere() { static GLUquadricObj* q = gluNewQuadric(); gluSphere(q,1.0,10,10); } void CGScooter::drawTorus(double innerRadius, double outerRadius) { static GLUquadricObj* q = gluNewQuadric(); glutSolidTorus(outerRadius-innerRadius, outerRadius, 18, 20); } void CGScooter::drawCylinder(bool caps) { static GLUquadricObj* q = gluNewQuadric(); static GLUquadricObj* d = gluNewQuadric(); gluCylinder(q,0.5,0.5,1,8,1); if (caps) { glPushMatrix(); gluDisk(d,0,0.5,8,1); glTranslated(0,0,1); gluDisk(d,0,0.5,8,1); glPopMatrix(); } } void CGScooter::drawWheel() { // ein Rad besteht aus Nabe, Speichen, Felge und Bereifung // Durchmesser des Rades: 1 // Breite (Reifen): 0.25 // Ausrichtung in der xy-Ebene, zentriert im Ursprung glPushMatrix(); // Nabe (Durchmesser 0.4, Breite 0.5+0.5) glPushMatrix(); glColor3f(0.9, 0.9, 0.9); glScalef(0.4, 0.4, 1); glTranslatef(0, 0, -0.5); drawCylinder(true); glPopMatrix(); // Speichen (jeweils um den Mittelpunkt) glPushMatrix(); glColor3f(0.9, 0.9, 0.9); static double angle = 0; if (rotate_) angle += 1.0; if (angle > 360) angle -= 360; glRotatef(angle, 0, 0, 1); const int nSpeichen = 10; for (int i=0; i 45) angle_inc = -angle_inc; } glRotatef(angle, 0, 1, 0); // zeichne Lenker drawSteering(); glPopMatrix(); // Hinterrad glPushMatrix(); glTranslatef(0.8, 0, 0); glScalef(0.2, 0.2, 0.2); drawWheel(); glPopMatrix(); // Trittbrett glColor3f(0.5, 0.8, 0.8); glPushMatrix(); glTranslatef(0.4, 0, 0); glScalef(0.25, 0.05, 0.2); // eigentliches Brett drawBox(); glPopMatrix(); // Streben zur Hinterachse glPushMatrix(); glTranslatef(0.5, 0, -0.07); glScalef(0.25, 0.05, 0.05); // auf x-Achse glRotatef(90, 0, 1, 0); drawCylinder(true); glPopMatrix(); glPushMatrix(); glTranslatef(0.5, 0, +0.07); glScalef(0.3, 0.05, 0.05); // auf x-Achse glRotatef(90, 0, 1, 0); drawCylinder(true); glPopMatrix(); // Strebe zum Lenker glPushMatrix(); glTranslatef(0, 0.6, 0); // schräg zum Lenker hoch glRotatef(62, 0, 0, -1); // auf x-Achse glRotatef(90, 0, 1, 0); glScalef(0.1, 0.05, 0.7); drawCylinder(true); glPopMatrix(); // rotes Katzenlicht am Hinterrad glPushMatrix(); glColor3f(1, 0, 0); glBegin(GL_TRIANGLES); glVertex3f(0.84, -0.05, 0.1); glVertex3f(0.84, -0.05, 0.05); glVertex3f(0.84, 0.05,0.075); glEnd(); glPopMatrix(); } void CGScooter::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,0.2,20.0); // LookAt glMatrixMode(GL_MODELVIEW); // gluLookAt(0.0, 0.0, 4.0, // from (0,0,4) gluLookAt(0.0, 1.0, 3.0, // from (1,1,3) 0.0, 0.0, 0.0, // to (0,0,0) 0.0, 1.0, 0.0); // up glClearColor(1,1,1,1); } void CGScooter::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(); gluPerspective(40.0,float(newWidth)/float(newHeight),1.0, 10.0); // Schalte zurueck auf die Modelview-Matrix glMatrixMode(GL_MODELVIEW); } } void CGScooter::onKey(unsigned char key) { switch (key) { case 27 : case 'q': exit(0); break; case 'l': glPolygonMode(GL_FRONT_AND_BACK,GL_LINE); break; case 'f': glPolygonMode(GL_FRONT_AND_BACK,GL_FILL); break; case 'c': glEnable(GL_CULL_FACE); break; case 'n': glDisable(GL_CULL_FACE); break; case ' ': run_ = !run_; break; case 'a': axis_ = !axis_; break; case 'r': rotate_ = !rotate_; break; } glutPostRedisplay(); } void CGScooter::onIdle() { if (run_) glRotatef(1,0,1,0); glutPostRedisplay(); } void CGScooter::onDrag(double dx, double dy) { glRotatef(dx*20, 0, 1, 0); // glRotatef(-dy*20, 1, 0, 0); } void CGScooter::onDraw() { // Loesche den Farb- und Tiefenspeicher glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // draw coordinate system if (axis_) { glBegin(GL_LINES); // x glColor3d(1.0,0,0); glVertex3d(0.0,0,0); glVertex3d(3.0,0,0); // y glColor3d(0,1,0); glVertex3d(0,0,0); glVertex3d(0,3,0); // z glColor3d(0,0,1); glVertex3d(0,0,0); glVertex3d(0,0,3); glEnd(); } // Matrixmodus setzen glMatrixMode(GL_MODELVIEW); // hier den Scooter zeichnen glPushMatrix(); glTranslatef(-0.5, 0, 0); drawScooter(); glPopMatrix(); // Plattform glPushMatrix(); glColor3f(0.5, 0.8, 0.5); glTranslatef(0, -0.3, 0); glScalef(2.5, 0.1, 2); drawBox(); glPopMatrix(); // Nicht vergessen! Front- und Back-Buffer tauschen: swapBuffers(); } // Hauptprogramm int main(int argc, char* argv[]) { // Erzeuge eine Instanz der Beispiel-Anwendung: CGScooter sample; // Starte die Beispiel-Anwendung: sample.start("CGScooter, Stephan Brumme, 702544"); return(0); }