grimfang4 / sdl-gpu

A library for high-performance, modern 2D graphics with SDL written in C.
MIT License
1.17k stars 123 forks source link

Camera and matrix transforms don't seem to mix intuitively #26

Open dhiahassen opened 7 years ago

dhiahassen commented 7 years ago

i want to get the current projection matrix and store it to a variable so that later i can reuse it , i can try this :

float * matrix;
GPU_PushMatrix();
matrix = GPU_GetProjection();
...
//here i want to set the projection matrix to "matrix"

how to do that ? i mean i looking for an equivalent to "glLoadMatrixf(matrix);"

grimfang4 commented 7 years ago

GPU_GetProjection() returns a pointer to the current projection matrix. If SDL_gpu changes that matrix, it'll affect your pointer. If you really want to store the matrix for later, copy it into your own 16-element float array. GPU_MatrixCopy() will help. To restore it, you can copy it back onto the result of GPU_GetProjection(). GPU_MatrixCopy() is a more generic equivalent to glLoadMatrix(). You might also look at GPU_LoadIdentity() and GPU_MultMatrix().

Most of the time, however, you are temporarily changing the matrix and want to reset it back to how it was before. It might be cleaner and clearer if you use GPU_PushMatrix() to store the matrix, then GPU_PopMatrix() to restore it. It works very similarly to glPushMatrix() and glPopMatrix().

dhiahassen commented 7 years ago

thanks , im asking because i have a problem mixing sdl_gpu camera with GPU_Translate() ill tell you in short what it is : I am using sdl_gpu camera to navigate on the level's world and follow an object , but when i try to apply rotation on the camera , it appears that the rotaion center is not the screen's center point as expected but it is the top left cornner of the screen , one possible solution is to rotate first then translate ( as i did in allegro 5 ) , but that is impossible with sdl_gpu , because it handles the camera control at once using GPU_SetCamera() and it does it in it's one only own way . i left the camera methods and i thought that i can use SDL_Translate() , it is working as expected with some exceptions :

grimfang4 commented 7 years ago

This sounds like something that needs to be fixed, but I don't have the time right now to dig into it. If you do look into it further, you can send a pull request or a patch and I'll take it in.

dhiahassen commented 7 years ago

i think i will make some changes on sdl gpu for the engine im working on , ill back to this later when i need a camera rotation , whenever i add something or fix something ill let you know , im sure that after few months ill have to back to work on the engine's guts and basics but for the moment i don't have to loose time i prefer to move on , don't close this