You are on page 1of 4

// MAIN_CPP //

#include <iostream>
#include <stdlib.h>
using namespace std;

#include <GL/glut.h>
#include <Windows.h>

float fSpeed = 0.0f;


float fZKoordinate = -10.0f;
float fXKoordinate = 0.0f;
float fYKoordinate = 0.0f;
float _angle = 90.0f;
float _camera_angle = 0.0f;

//Die Funktion wird aufgerufen, sobald ein Knopf gedrückt wurde


void handleKeypress(unsigned char key, //Der Knopf der gedrückt wurde
int x, int y) { //Die Momentanen
Maus - Koordinaten
switch (key) {
case VK_ESCAPE:
exit(0);
break;
case 'o':
fSpeed += 2.0f;
break;
case 'l':
fSpeed -= 2.0f;
break;
case 'w':
fZKoordinate += 1.0f;
break;
case 's':
fZKoordinate -= 1.0f;
break;
case 'a':
fXKoordinate += 1.0f;
break;
case 'd':
fXKoordinate -= 1.0f;
break;
case 'u':
fYKoordinate -= 1.0f;
break;
case 'j':
fYKoordinate += 1.0f;
break;
}

//3D - Rendering initialisieren


void initRendering() {
//Rendert die 3D - Objekt - Tiefe ( Ein Objekt vor dem anderem
)
glEnable(GL_DEPTH_TEST);
}

//Di Funktion wird aufgerufen, sobald das Fenster geändert wurde


void handleResize(int w, int h) {
//Anweisung , wie er die Koordinaten in Pixel umrechnen soll
glViewport(0, 0, w, h);

glMatrixMode(GL_PROJECTION); //Wechselt zur Kamera _


Perspektive

//Kamera initialisieren
glLoadIdentity(); //Kamera reseten
gluPerspective(45.0, //Kamera sicht
(double)w / (double)h, //Verhältnis:
Breite - Höhe
1.0, //Information über
Z Koordinate ( NÄHE ) ~> Ab 1.0 NÄHE, wird ein Objekt ausgeblendet
100.0); //Information über
Z Koordinate ( FERNE )~> Ab 200.0 FERNE wird ein Objekt ausgeblendet
}

//3D Scene malen


void drawScene() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

glTranslatef(fXKoordinate, fYKoordinate, fZKoordinate);

glRotatef(_angle, 0.0f, 1.0f, 0.0f);

glBegin( GL_QUADS );

// TRUCK_ANHÄNGER \\

glColor3f( 0.0f, 0.0f, 1.0f );

// Vorne

glVertex3f(-0.7f, -0.5f, 1.7f);


glVertex3f(0.7f, -0.5f, 1.7f);
glVertex3f(0.7f, 0.5f, 1.7f);
glVertex3f(-0.7f, 0.5f, 1.7f);

//Hinten

glVertex3f(-0.7f, -0.5f, -1.7f);


glVertex3f(0.7f, -0.5f, -1.7f);
glVertex3f(0.7f, 0.5f, -1.7f);
glVertex3f(-0.7f, 0.5f, -1.7f);

//Links

glVertex3f(-0.7f, -0.5f, 1.7f);


glVertex3f(-0.7f, -0.5f, -1.7f);
glVertex3f(-0.7f, 0.5f, -1.7f);
glVertex3f(-0.7f, 0.5f, 1.7f);

//Rechts

glVertex3f(0.7f, -0.5f, 1.7f);


glVertex3f(0.7f, -0.5f, -1.7f);
glVertex3f(0.7f, 0.5f, -1.7f);
glVertex3f(0.7f, 0.5f, 1.7f);

//Oben

glVertex3f(-0.7f, 0.5f, 1.7f);


glVertex3f(0.7f, 0.5f, 1.7f);
glVertex3f(0.7f, 0.5f, -1.7f);
glVertex3f(-0.7f, 0.5f, -1.7f);

//Unten

glVertex3f(-0.7f, -0.5f, 1.7f);


glVertex3f(0.7f, -0.5f, 1.7f);
glVertex3f(0.7f, -0.5f, -1.7f);
glVertex3f(-0.7f, -0.5f, -1.7f);

// TRUCK_FÜHRERHAUS

glColor3f( 1.0f, 0.0f, 0.0f );

//Vorne

glVertex3f(-0.7f, -0.5f, 2.7f);


glVertex3f(0.7f, -0.5f, 2.7f);
glVertex3f(0.7f, 1.5f, 2.7f);
glVertex3f(-0.7f, 1.5f, 2.7f);

//Hinten

glVertex3f(-0.7f, -0.5f, 1.7f);


glVertex3f(0.7f, -0.5f, 1.7f);
glVertex3f(0.7f, 1.5f, 1.7f);
glVertex3f(-0.7f, 1.5f, 1.7f);

//Links

glVertex3f(-0.7f, 1.5f, 2.7f);


glVertex3f(-0.7f, 1.5f, 1.7f);
glVertex3f(-0.7f, -0.5f, 1.7f);
glVertex3f(-0.7f, -0.5f, 2.7f);

//Rechts

glVertex3f(0.7f, 1.5f, 2.7f);


glVertex3f(0.7f, 1.5f, 1.7f);
glVertex3f(0.7f, -0.5f, 1.7f);
glVertex3f(0.7f, -0.5f, 2.7f);

//Oben

glVertex3f(-0.7f, 1.5f, 2.7f);


glVertex3f(0.7f, 1.5f, 2.7f);
glVertex3f(0.7f, 1.5f, 1.7f);
glVertex3f(-0.7f, 1.5f, 1.7f);

//Hinten

glVertex3f(-0.7f, -0.5f, 2.7f);


glVertex3f(0.7f, -0.5f, 2.7f);
glVertex3f(0.7f, -0.5f, 1.7f);
glVertex3f(-0.7f, -0.5f, 1.7f);

glEnd();

glPopMatrix(); //Undo the move to the center of the triangle


glutSwapBuffers();
}

void rotate( int Wert ) {

_angle += fSpeed;

if( _angle > 360 ) {


_angle -= 360;
}

glutPostRedisplay();
glutTimerFunc( 25, rotate, 0 );
}

void perspective( int Wert ){

glutPostRedisplay();
glutTimerFunc( 25, perspective, 0 );

int main(int argc, char** argv) {


//GLUT initialisieren
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(600, 400); //Fenster Größe Setzen

//Fenster erstellen
glutCreateWindow("OpenGL _ Practise || Copyright Sterminio
Productions© All rights reserved.");
initRendering(); //Rendering initialisieren

//Funktionen zum handeln von: Keypress, FensterResize, 3D Scene


glutDisplayFunc(drawScene);
glutKeyboardFunc(handleKeypress);
glutReshapeFunc(handleResize);

glutTimerFunc( 25, rotate, 0 );


glutTimerFunc( 25, perspective, 0 );

glutMainLoop(); //EndlosSchleife starten // Exit nur möglich


mit KeyPress ( Siehe Keypress Funktion )
return 0;
}