OpenGL | Rotation
-
Also erstmal: Knüppel einpacken und nett sein, danke

Ich sage das, weil diese Frage bestimmt andauernd auftaucht. Ich finde bloß nirgends (nirgends = nicht bei SuFu und Google) eine Lösung. Nachdem ich mich mal wieder an die Grafikprogrammierung gemacht habe, habe ich mir erstmal einen kleinen Würfel gebaut und das in eine Methode gepackt:struct POSITION { float fPosition[3]; }; //... void GAMESCENES::CreateCube (POSITION pPos, float iLength) { glTranslatef (pPos.fPosition[0], pPos.fPosition[1], pPos.fPosition[2]); glBegin (GL_QUADS); glVertex3f (pPos.fPosition[0] - (iLength / 2), pPos.fPosition[1] - (iLength / 2), pPos.fPosition[2] - (iLength / 2)); glVertex3f (pPos.fPosition[0] - (iLength / 2), pPos.fPosition[1] - (iLength / 2), pPos.fPosition[2] + (iLength / 2)); glVertex3f (pPos.fPosition[0] + (iLength / 2), pPos.fPosition[1] - (iLength / 2), pPos.fPosition[2] + (iLength / 2)); glVertex3f (pPos.fPosition[0] + (iLength / 2), pPos.fPosition[1] - (iLength / 2), pPos.fPosition[2] - (iLength / 2)); glVertex3f (pPos.fPosition[0] - (iLength / 2), pPos.fPosition[1] + (iLength / 2), pPos.fPosition[2] - (iLength / 2)); glVertex3f (pPos.fPosition[0] - (iLength / 2), pPos.fPosition[1] + (iLength / 2), pPos.fPosition[2] + (iLength / 2)); glVertex3f (pPos.fPosition[0] + (iLength / 2), pPos.fPosition[1] + (iLength / 2), pPos.fPosition[2] + (iLength / 2)); glVertex3f (pPos.fPosition[0] + (iLength / 2), pPos.fPosition[1] + (iLength / 2), pPos.fPosition[2] - (iLength / 2)); glVertex3f (pPos.fPosition[0] - (iLength / 2), pPos.fPosition[1] + (iLength / 2), pPos.fPosition[2] - (iLength / 2)); glVertex3f (pPos.fPosition[0] - (iLength / 2), pPos.fPosition[1] + (iLength / 2), pPos.fPosition[2] + (iLength / 2)); glVertex3f (pPos.fPosition[0] - (iLength / 2), pPos.fPosition[1] - (iLength / 2), pPos.fPosition[2] + (iLength / 2)); glVertex3f (pPos.fPosition[0] - (iLength / 2), pPos.fPosition[1] - (iLength / 2), pPos.fPosition[2] - (iLength / 2)); glVertex3f (pPos.fPosition[0] + (iLength / 2), pPos.fPosition[1] + (iLength / 2), pPos.fPosition[2] - (iLength / 2)); glVertex3f (pPos.fPosition[0] + (iLength / 2), pPos.fPosition[1] + (iLength / 2), pPos.fPosition[2] + (iLength / 2)); glVertex3f (pPos.fPosition[0] + (iLength / 2), pPos.fPosition[1] - (iLength / 2), pPos.fPosition[2] + (iLength / 2)); glVertex3f (pPos.fPosition[0] + (iLength / 2), pPos.fPosition[1] - (iLength / 2), pPos.fPosition[2] - (iLength / 2)); glVertex3f (pPos.fPosition[0] - (iLength / 2), pPos.fPosition[1] - (iLength / 2), pPos.fPosition[2] - (iLength / 2)); glVertex3f (pPos.fPosition[0] - (iLength / 2), pPos.fPosition[1] + (iLength / 2), pPos.fPosition[2] - (iLength / 2)); glVertex3f (pPos.fPosition[0] + (iLength / 2), pPos.fPosition[1] + (iLength / 2), pPos.fPosition[2] - (iLength / 2)); glVertex3f (pPos.fPosition[0] + (iLength / 2), pPos.fPosition[1] - (iLength / 2), pPos.fPosition[2] - (iLength / 2)); glVertex3f (pPos.fPosition[0] - (iLength / 2), pPos.fPosition[1] - (iLength / 2), pPos.fPosition[2] + (iLength / 2)); glVertex3f (pPos.fPosition[0] - (iLength / 2), pPos.fPosition[1] + (iLength / 2), pPos.fPosition[2] + (iLength / 2)); glVertex3f (pPos.fPosition[0] + (iLength / 2), pPos.fPosition[1] + (iLength / 2), pPos.fPosition[2] + (iLength / 2)); glVertex3f (pPos.fPosition[0] + (iLength / 2), pPos.fPosition[1] - (iLength / 2), pPos.fPosition[2] + (iLength / 2)); glEnd (); }Soweit gibt es noch kein Problem. Ich rufe die Methode dann in einer anderen auf, in der eben alle Zeichenoperationen ausgeführt werden sollen. Hier mal die Methode DrawGameScene, damit keine Fragen dazu offen bleiben:
bool GAMESCENES::DrawGameScene () { static float fAngelRotate; glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity (); POSITION position = {-1.5f, 0.0f, -10.0f}; glRotatef (fAngelRotate, 1, 0, 0); CreateCube (position, 3.0f); fAngelRotate -= 1.0f; return true; }Nochmal nebenbei: An und für sich ist das alles auf den NeHe-Tutorials aufgebaut, ich habe das bloß alles in eine Klasse gepackt.
Nun eben zum Problem: Ich möchte, dass sich der Würfel um seine eigene Achse dreht. Zurzeit dreht er sich einfach im Raum, aber eben nicht um seine eigene Achse. Nun dann die Frage, wie kann ich dem Würfel eine eigene Achse zu weisen bzw. wie kann ich ihn um seine eigene Achse drehen lassen, falls ersteres gar nicht möglich ist (vllt. weil er schon eine eigene hat usw.). Danke schonmal.
-
1. Fixpunkt der Rotation in den Koordinatenursprung des lokalen Koordinatensystems verschieben
2. Rotation des Objekts und seines lokalen Koordinatensystems
3. Rückverschiebung des Fixpunktes
-
Das ist ja alles schön und gut, aber mir fehlen die richtigen Funktionen dafür bzw. die richtige Reihenfolge in der ich diese aufrufen muss. Ich habe danach auch schon gegoogelt und auch die Ergebnisse gefunden, durchgelesen und rumprobiert aber es hat nichts geklappt. Könntest du mir bitte die passenden Funktionen dafür nennen?
http://www.erinacom.de/ogl/themen/matrix01.html
aber das hat mir auch nichts geholfen...EDIT: An und für sich brauche ich die Funktionen glLoadIdentity (), glTranslatef () und glRotatef () oder?
-
Bei OpenGL wird die zuletzt "gesendete" Transforamtaion als erstes ausgeführt:
glRotate(); // als dirttes wieder rotieren... glTranslate(); // als zweites verschieben glRotate(); // als erstes rotierenintern arbeiten die Funktionen etwa so:
void glTranslate3f(float x, y, z) { // A = B * C d. h. erst C dann B g_ModelViewMatrix = g_ModelViewMatrix * CreateTranslationMatrix(x,y,z); }
-
Achso, also erst auf die richtige Position bringen, dann rotieren und dann die Position wieder zurück schieben... also praktisch so wie du es (bzw das tutorial) geschrieben hatte.
Ich bin mir nicht sicher, glaube es aber vorhin schonmal so probiert zu haben...
naja es klappt jetzt danke
official&client=firefox-a