OpenGL - Anfängerfragenweder
-
Also unsre Aufgabe war es ein Dreieck darzustellen, welches bei Tastendruck verschiedene Sachen tut:
Bei Drücken von <UP>, soll sich das Dreieck in Richtung Pfeilspitze bewegen, eine Pfeilspitze ist zu dem Zweck entweder farblich markiert oder anderweitig erkenntlich gemacht.
Beim Drücken von <LINKS> oder <RECHTS> soll das Dreieck entlang der Z-Achse rotieren (+/- 90°)
nun habe ich eine lösung dafür die mir aber kopfzerbrechen bereitet, besser gesagt ich hab 2 Lösungen, welche ich gerne analysiert hätte
LÖSUNG 1:
////////////////////////////////////////////////////////////////////////////////// // Tastatureingaben // ////////////////////////////////////////////////////////////////////////////////// if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Escape)) App.Close(); if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Left)) glRotatef(30.0, 0.0, 0.0, 1.0); if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Right)) glRotatef(-30.0, 0.0, 0.0, 1.0); if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Up)) { //glRotatef(-30, 0.0, 0.0, 1.0); glTranslatef(0.0, 1.0, 0.0); //glRotatef(30, 0.0, 0.0, 1.0); }
zur eventabfrage.
////////////////////////////////////////////////////// // OPEN GL // ////////////////////////////////////////////////////// glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glBegin(GL_TRIANGLES); glColor3f(1.0, 0., 0.); glVertex3f( 0.0f, 1.0f, 0.0f); // Top glColor3f(0., 0., 1.); glVertex3f(-1.0f,-1.0f, 0.0f); // Bottom Left glColor3f(0.0, 1.0, 0.0); glVertex3f( 1.0f,-1.0f, 0.0f); // Bottom Right glEnd();
zum Zeichnen eines Dreiecks.
Was mir an dieser Lösung nicht gefällt ist dass ich die Ursprüngliche ModelView-Matrix ständig verändere.////////////////
2. LÖSUNGTurtle (also das Dreieck- header)
class Turtle { protected: sf::Vector3f rotation; sf::Vector3f translation; public: Turtle(); //Turtle(sf::Vector3fr, sf::Vector3f t); void translate(sf::Vector3f rot); void translate(float x, float y, float z); void rotate(float angle, sf::Vector3f normal); void rotate(float angle, float x, float y, float z); void rotateAxis(float angle, char axis); void draw(); };
Turtle.cpp
Turtle::Turtle() { rotation.x = rotation.y = rotation.z = 0; translation.x = translation.y = translation.z = 0; } void Turtle::draw() { glMatrixMode(GL_MODELVIEW); glPushMatrix(); glLoadIdentity(); glTranslatef(translation.x, translation.y, translation.z); cout << translation.x << " " << translation.y << " " << translation.z << endl; cout << rotation.z << endl; glRotatef(rotation.x, 1.f, 0.f, 0.f); glRotatef(rotation.y, 0.f, 1.f, 0.f); glRotatef(rotation.z, 0.f, 0.f, 1.f); //glTranslatef(translation.x, translation.y, translation.z); glBegin(GL_TRIANGLES); glVertex3f(-1.0, -1.0, 0.0); glVertex3f( 0.0, 1.0, 0.0); glVertex3f( 1.0, -1.0, 0.0); glEnd(); glPopMatrix(); } void Turtle::translate(float x, float y, float z) { if(rotation.z == 0) { translation.x += x; translation.y += y; translation.z += z; } if(rotation.z == 90) { translation.x += -y; translation.y += 0; translation.z += z; } if(rotation.z == 180) { translation.x += 0; translation.y += -y; translation.z += z; } if(rotation.z == 270) { translation.x += y; translation.y += 0; translation.z += z; } } void Turtle::translate(sf::Vector3f rot) { translation.x += rot.x ; translation.y += rot.y ; translation.z += rot.z ; } void Turtle::rotateAxis(float angle, char axis) { if( axis == 'x' || axis == 'X') { rotation.x += angle; } if( axis == 'y' || axis == 'Y') { rotation.y += angle; } if( axis == 'z' || axis == 'Z') { rotation.z += angle; } while( rotation.x >= 360) { rotation.x -= 360; } while( rotation.y >= 360) { rotation.y -= 360; } while( rotation.z >= 360) { rotation.z -= 360; } while( rotation.x < 0) { rotation.x += 360; } while( rotation.y < 0) { rotation.y += 360; } while( rotation.z < 0) { rotation.z += 360; } }
main.cpp
// irgendeine projektionsmatrix // turtle in sichtbereich schieben Turtle turtle; turtle.translate(0,0, -5.0); //ereignisse abfragen // Left key: rotate 90°, z-axis if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Left)) turtle.rotateAxis(90.0, 'z'); // Right key : rotate -90° , z-axis if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Right)) turtle.rotateAxis(-90.0, 'z'); // Up key : translate something , z-axis if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Up)) { turtle.translate(0, 0.2, 0); } // zeichnen glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); turtle.draw(); App.Display();
der code wurde natürlich reduziert und die rotationsfunktion der 2. lsg ist nur auf die zu-achse abgestimmt.Aber im grunde genommen funktionieren beide varianten, nur welche ist vorzuziehen??
-
ist etwas an der fragestellung unklar oder ist das einfach zu viel code, oder soll ich mal alles komplett posten, so dass man was lauffähiiges bekommt?
-
ravenheart schrieb:
Was mir an dieser Lösung nicht gefällt ist dass ich die Ursprüngliche ModelView-Matrix ständig verändere.
Na dann mach doch ein glPushMatrix() in Deiner Init-Routine auf die ModelView-Matrix.