openGL rotate camera around itself?

Hi, I'm trying to get started with 3D in OpenGL, but my camera only rotates around the coordinate origin! Here's my code: For each model, each frame, I call:

 models[i])->draw(currentShader, projectionMatrix * camera.getTransformation().mat()

In the draw function of each model is then:

 shader.setUniform<Matrix4>(shader.getUniformLocation("MVP"), VP * transformation.mat());

Now I do the cursorPositionCallBack function:

 enviroment.camera.getTransformation().rotate(Transformation::Y, movedX);

So that it always rotates by the given distance!

However, it's about the world-origin. Although I thought I could solve it with this code: (Excerpt from the "Transformation" class:

 Transformation & rotate(Axes axes, float angel, bool degree = true) { Vector3 curT = this->getTranslation(); setTranslation({0, 0, 0}); matrix = glm::rotate(matrix, degree ? glm::radians(angel) : angel, Vector3(axes == X ? 1 : 0, axes == Y ? 1 : 0, axes == Z ? 1 : 0)); setTranslation(curT); return *this; } Transformation & setTranslation(Vector3 values) { matrix[3][0] = values.x; matrix[3][1] = values.y; matrix[3][2] = values.z; return *this; }

The getTranslation method uses the same fields as setTranslation.

In the shader, simply set gl_Position = MVP * vec4 (position); I'm really confused, and not even gpt-4 can tell me the problem for sure!

(1 votes)
Loading...

Similar Posts

Subscribe
Notify of
3 Answers
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Destranix
1 year ago

You need the inverse transformation matrix of the camera. You want to transform the points from the Global Space into the camera space.

Your rotation of the camera should take place correctly around the pivot of the object. But I am not sure,w as glm::rotate does exactly.
Evtl. print some interim results and see if you can identify the problem.

Tyldu
1 year ago

gives tutorials on the thema still and nöcher…

https://learnopengl.com/Getting-started/Camera