I'm having a bit of an issue when it comes to 3D- and 2D camera(s), in relation to a game's window resolution. I want to let the player choose from different window resolutions, either from a menu or by just resizing the window by dragging the window border, and can't get the scaling of the graphics to work properly. In games I've played, I can go into the options menu, choose a screen resolution and then the window resize itself and the graphic's, menu buttons etc just scales up to the correct sizes.
At the moment, I've tried two different window resolutions; 800x600 and 1280x720 (don't mind the different aspect ratios).
As you can see, more of the game world can be seen (more stone tiles can be seen, if that makes sense) in the window with a resolution of 1280x720 and I want my 3D/2D game(s) to be resolution independent and that the same amount of the game world to be seen, independent of what window resolution the player chooses to use.
My camera class consist of:
- Camera matrix - used to translate and rotate my camera
- View matrix - the inverse of the camera matrix
- Perspective matrix - see code below
- View projection matrix -
view matrix * perspective matrix
- Fov - 45.0f
- Aspect ratio -
camera width / camera height
Note: row-major matrices
Perspective matrix:
const float Tangent = 1.0f / tanf(DEGREES_TO_RADIANS(Fov * 0.5f));
const float NearToFar = FarClip - NearClip;
PerspectiveMatrix(Identity);
PerspectiveMatrix[0] = -Tangent / AspectRatio;
PerspectiveMatrix[5] = Tangent;
PerspectiveMatrix[10] = FarClip / NearToFar;
PerspectiveMatrix[11] = 1.0f;
PerspectiveMatrix[14] = (-NearClip * FarClip) / NearToFar;
PerspectiveMatrix[15] = 0.0f;
At each update, I'm also setting the viewport by calling glViewport(0, 0, CameraWidth, CameraHeight)
.
This might change in the future if I decide to make two cameras in a game, for a splitscreen game for example.
How can I solve the window resolution independence issue?
I have though about creating a framebuffer the size I want, attach it to a quad and then render the quad the size of the window, which will scale up/down the framebuffer if the window is bigger/smaller than the framebuffer's original size, but if it's an easier way of doing it, I would gladly use that instead.