#include #include #include // Zustandsvariablen // Mausbewewung int mousemotion; int mousex, mousey; int draw_type = 1; // Initialisierung Modellorientierung GLfloat xangle = 4; /* in Drehung um */ GLfloat yangle = 120; //Modellposition GLfloat posx = 0, posy = 0, posz = -400; //Callback Funktion: Reaktion auf Mausclicks void mouse(int button, int state, int x, int y) { if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) { mousemotion = 1; mousex = x; mousey = y; } if (button == GLUT_LEFT_BUTTON && state == GLUT_UP) { mousemotion = 0; } } //Callback Funktion: Reaktion auf Mausbewegung void motion(int x, int y) { if (mousemotion) { xangle = xangle - (y - mousey); yangle = yangle - (x - mousex); mousex = x; mousey = y; // Szene neu zeichnen glutPostRedisplay(); } } //Callback Funktion: Reaktion auf Tastendruck void keyb(unsigned char keyPressed, int x, int y) { switch (keyPressed) { case '1': draw_type = 1; break; case '2': draw_type = 2; break; case '3': draw_type = 3; break; case '4': draw_type = 4; break; case '5': draw_type = 5; break; case '6': draw_type = 6; break; case '7': draw_type = 7; break; case '8': draw_type = 8; break; case '9': draw_type = 9; break; } } //neue Modellposition berechnen void recalcModelPos(void) { glLoadIdentity(); glTranslatef(posx, posy, posz); glRotatef(xangle, 1.0, 0.0, 0.0); glRotatef(yangle, 0.0, 1.0, 0.0); } void DrawScene(void) { // Puffer löschen glClearColor(0.0, 0.0, 0.0, 0.0); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // neue Modellkoordinaten berechnen recalcModelPos(); // Verschiedene Objekte zeichnen switch (draw_type) { case 1: // ZWEI PUNKTE glBegin(GL_POINTS); // 1. PUNKT glColor3f(1.0f, 0.0f, 0.0f); glVertex3f(-100.0f, 0.0f, -100.0f); // 2. PUNKT glColor3f(0.0f, 1.0f, 0.0f); glVertex3f(-100.0f, 100.0f, -100.0f); glEnd(); break; case 2: // EINE LINIE glBegin(GL_LINES); glColor3f(1.0f, 0.0f, 0.0f); glVertex3f(-100.0f, 0.0f, -100.0f); glColor3f(0.0f, 1.0f, 0.0f); glVertex3f(-100.0f, 100.0f, -100.0f); glEnd(); break; case 3: // ZWEI LINIEN glBegin(GL_LINE_STRIP); glColor3f(1.0f, 0.0f, 0.0f); glVertex3f(-100.0f, 0.0f, -100.0f); glColor3f(0.0f, 1.0f, 0.0f); glVertex3f(-100.0f, 100.0f, -100.0f); glColor3f(0.0f, 0.0f, 1.0f); glVertex3f(0.0f, 100.0f, -100.0f); glEnd(); break; case 4: // EIN LINIENZUG glBegin(GL_LINE_LOOP); glColor3f(1.0f, 0.0f, 0.0f); glVertex3f(-100.0f, 0.0f, -100.0f); glColor3f(0.0f, 1.0f, 0.0f); glVertex3f(-100.0f, 100.0f, -100.0f); glColor3f(0.0f, 0.0f, 1.0f); glVertex3f(0.0f, 100.0f, -100.0f); glEnd(); break; case 5: // EIN DREICK glBegin(GL_TRIANGLES); glColor3f(1.0f, 0.0f, 0.0f); glVertex3f(-100.0f, 0.0f, -100.0f); glVertex3f(-100.0f, 100.0f, -100.0f); glVertex3f(0.0f, 100.0f, -100.0f); glEnd(); break; case 6: // ZWEI DREIECKE glBegin(GL_TRIANGLE_STRIP); glColor3f(1.0f, 0.0f, 0.0f); glVertex3f(-100.0f, 0.0f, -100.0f); glVertex3f(-100.0f, 100.0f, -100.0f); glColor3f(0.0f, 0.0f, 1.0f); glVertex3f(0.0f, 0.0f, -100.0f); glVertex3f(0.0f, 100.0f, -100.0f); glEnd(); break; case 7: // ZWEI DREIECKE glBegin(GL_TRIANGLE_FAN); glColor3f(1.0f, 0.0f, 0.0f); glVertex3f(0.0f, 0.0f, -100.0f); glColor3f(1.0f, 0.03, 0.0f); glVertex3f(-100.0f, 0.0f, -100.0f); glColor3f(.0f, 1.0f, 0.0f); glVertex3f(-100.0f, 100.0f, -100.0f); glColor3f(.0f, 0.0f, 1.0f); glVertex3f(50.0f, 50.0f, -100.0f); glEnd(); break; case 8: // EIN VIERECK glBegin(GL_QUADS); glColor3f(1.0f, 0.0f, 0.0f); glVertex3f(0.0f, 0.0f, -100.0f); glVertex3f(100.0f, 0.0f, -100.0f); glVertex3f(100.0f, 100.0f, -100.0f); glVertex3f(0.0f, 100.0f, -100.0f); glEnd(); break; case 9: // EIN FÜNFECK glBegin(GL_POLYGON); glColor3f(1.0f, 0.0f, 0.0f); glVertex3f(0.0f, 0.0f, -100.0f); glVertex3f(100.0f, 0.0f, -100.0f); glVertex3f(50.0f, 100.0f, -100.0f); glVertex3f(100.0f, 100.0f, -100.0f); glVertex3f(0.0f, 100.0f, -100.0f); glEnd(); break; } // Szene neu zeichnen glutPostRedisplay(); // Vordergrund- und Hintergrundpuffer wechseln glutSwapBuffers(); } //eigene Initialisierungsfunktion void myinit() { GLfloat light_position[] = { 0.0, 0.0, -1.0, 1.0 }; //erstes GL-Light setzen glLightfv(GL_LIGHT0, GL_POSITION, light_position); glEnable(GL_LIGHT0); //Z-Puffer aktivieren glDepthFunc(GL_LEQUAL); glEnable(GL_DEPTH_TEST); // Matritzen setzen glMatrixMode(GL_PROJECTION); // Sichtpyramide erstellen gluPerspective(60.0, 1.0, 1.0, 1000.0); glMatrixMode(GL_MODELVIEW); gluLookAt(0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.); } int main(int argc, char *argv[]) { //GLUT initialisieren, Fenster setzen glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); glutInitWindowSize(500, 500); glutInitWindowPosition(100, 100); glutCreateWindow(argv[0]); // eigene Initialisierungen myinit(); //Callbacks setzen: Reaktion auf Mausklicks und -bewegungen, Tastaturaktivitäten glutMouseFunc(mouse); glutMotionFunc(motion); glutKeyboardFunc(keyb); //Callback zum Zeichnen der GL-Funktion glutDisplayFunc(DrawScene); glutMainLoop(); return EXIT_SUCCESS; }