0
votes

I am developing now a 3d game engine (Very simple, just cubes and maybe few other simple 3d objects). I am using C++, and for the graphics SFML, it's 2d graphics library for C++.

I already have the formula for the perspective projection:

S will be the position of the point on the screen. P will be the position of the point in the 3d space. E is the position of the eye (camera).

Sx = ((Ez * (Px - Ex)) / (Ez + Pz)) + Ex

Sy = ((Ez * (Py - Ey)) / (Ez + Pz)) + Ey

Now, that is working great! what I want to do now is rotating the camera itself. I already thought about rotating everything else around the camera, because I can do that quickly with my current code, which includes a "rotateAroundPoint" function of a 3d point.

But is there anything faster and simpler then that? any addition to my formula/ a new one that allows me to rotate the camera itself?

Thanks for any help and hope someone has a solution ;D Arad Arble.

1

1 Answers

1
votes

Making my comments an answer.

I'd suggest you look into the pipeline that major graphics libraries use for rendering. (OpenGL, for example.) SFML uses OpenGL under the hood, so you don't need to reinvent the wheel. Here's another example with OpenGL.

Also look into existing matrix libraries like GLM that provide the transformations you need. Your render pipeline will be much faster if you can condense the necessary transformations into a few matrices that OpenGL can use. For example, the equations in your question would typically be split into the model matrix (to transform the position of each vertex from model/local space -> world space), the view matrix (to transform world space -> camera space, making vertices appear as if from the camera's point of view), and a perspective projection matrix (to transform from camera space -> screen space, and to define things like the field of view). When all of these matrices are multiplied together, you end up with a final matrix that, when multiplied by a position vector, gives you a position in screen space. So this final matrix is applied to each vertex in your scene, which includes model transforms, camera transforms, perspective division, clipping, etc. to give the final result of drawing that vertex to the screen.

To answer your original question, traditionally the "camera" is represented by the combination of the view matrix and the projection matrix. To rotate the camera, all you have to do is apply a rotation transform (or possibly a "lookAt" transform; it's hard to tell from your question) to your view matrix, and the math will carry through to the following operations in the pipeline.