OpenGL revolves around World Genesis when they should be around Local Origin

I implement a simple camera system in OpenGL. I set up gluPerspective under the projection matrix and then used gluLookAt in the ModelView matrix. After that, I have my main rendering cycle, which checks for keyboard events and, if any arrow key is pressed, changes angular and forward speeds (I just rotate through the y axis and move through z (forward)). Then I move the view using the following code (deltaTime is the time elapsed since the last frame was displayed in seconds to separate the movement from the frame rate):

//place our camera  
newTime = RunTime(); //get the time since app start  
deltaTime = newTime - time; //get the time since the last frame was rendered  
time = newTime;  
glRotatef(view.angularSpeed*deltaTime,0,1,0); //rotate  
glTranslatef(0,0,view.forwardSpeed*deltaTime); //move forwards  
//draw our vertices  
draw();  
//swap buffers  
Swap_Buffers();  

Then the code repeats again. My drawing algorithm starts with glPushMatrix()and ends with glPopMatrix().

glRotatef() glTranslatef() .

, , , , (0,0,0 - Z) ( ), , (0,0,0), .

(0,0,0) .

glLoadIdentity() , , Matrix GL_MODELVIEW .

- glLoadIdentity() draw() ( PushMatrix PopMatrix, , , , .

- , , (0,0,0) , ?

+3
3

glRotate() ModelView , , , , , , , .

,

float x, y, z;//point you want to rotate around

glTranslatef(0,0,view.forwardSpeed*deltaTime); //move forwards

glTranslatef(x,y,z); //translate to origin
glRotatef(view.angularSpeed*deltaTime,0,1,0); //rotate
glTranslatef(-x,-y,-z); //translate back
//draw our vertices
draw();
//swap buffers
Swap_Buffers();
+5

:)

- , , , "", , .

+1

I think you first need to move the camera to the point (0,0,0), then rotate it and then translate it back.

0
source

Source: https://habr.com/ru/post/1726848/


All Articles