calculating z coordinate of camera
I have a 1680x1050 2d texture that I'm showing on a plane. My aspect ratio is set to 1680/1050. My camera look at is set to 0,0,-1 and is staring directly into the middle of my 2d texture. My window size is 800x600. What I want to do is to set my camera position to 0,0,z where z is the z position of the camera so that the 1680x1050 texture takes up the entire window.
I have absolutely no idea where to start with this. Any help would be greatly appreciated.
Thanks
Aren't you already storing the 3d coordinates of your camera somewhere? if so why can't you just directly use that coordinate?
What graphics api are you using and how do you currently represent your camera?
Games/Projects Currently In Development:
Discord RPG Bot | D++ - The Lightweight C++ Discord API Library | TriviaBot Discord Trivia Bot
If this approach doesn't work for you, you can use the tangent
/|
/ |
/ |
/ | h/2
/a |
/-----|
z
a = fieldOfView / 2
h = height of texture
z = distance of camera
tan(a) = (h / 2) / z
solving for z givesz = (h / 2) / tan(a)
Hmm, there is a caveat. In the following I assume that pixels/texels are squares.
I have a 1680x1050 2d texture that I'm showing on a plane. [...] What I want to do is to set my camera position to 0,0,z where z is the z position of the camera so that the 1680x1050 texture takes up the entire window.
(Bold by me) It is not the texture in itself that should be shown in the window, it is the projected textured area of the plane that should be shown. This is important because you can use a 1x1 texture as well to cover the same area of the plane. So assuming that the range of the textured area of the plane is [-w/2,+w/2] by [-h/2,+h/2] in world space (in which you specify the camera/view matrix as well) then with this h you can use HappyCoder's formula.
As an optional but pleasing condition you want to keep the aspect ratio, so that the plane does not distort the texture: w/h := 1680/1050 = 16/10 = 1.6
Only if the aspect ratio of the textured plane matches that of the window you can cover the window perfect entirely: w/h := 800/600 = 4/3 = 1.333 (approx.)
Unfortunately, you have now 2 conditions which cannot be met both, because 1.6 != 1.333. So you have to decide ...
1.) Do you want to show the texture undisturbed (i.e. no non-uniform scale) but entirely, which means a letter/pillar box?
2.) Do you want to show the texture undisturbed (i.e. no non-uniform scale) and it is okay to clip a little, but the main thing is that the window is filled entirely?
3.) Is it okay to show the texture disturbed (i.e. with non-uniform scale), but the main thing is that the window is filled entirely with the entire texture?
I want to then create the camera so the first thing the player sees is the texture at 0,0,0 perfectly taking up the entire window. I know x and y will be 0 but I don't know how to calculate the cameras initial z.
Does this make more sense?
Thanks.
Number 3 is ideal.
This means that the aspect ratio of the plane needs to met the aspect ratio of the camera's view volume. The aspect ratio of the view volume is chosen to be 1680/1050 as you told in the OP. The aspect ratio of the plane need to be the same. With the absolute extent of the plane being w by h (you don't told us real values) and the plane being centered at (0,0,0), the plane's co-ordinates are the aforementioned [-w/2,+w/2] by [-h/2,+h/2]. Then the distance can be computed by HappyCoder's formula (with the FOV being the vertical angle). The viewport settings then scales this non-uniformly to fit into the 800 by 600 pixels window.
BTW: As HappyCoder already mentioned: Is it necessary to use a perspective camera here?
I don't see how just drawing a fullscreen quad (with simple hardcoded screen space coordinates in a shader) and sampling from the texture wouldn't be enough.
"I AM ZE EMPRAH OPENGL 3.3 THE CORE, I DEMAND FROM THEE ZE SHADERZ AND MATRIXEZ"
My journals: dustArtemis ECS framework and Making a Terrain Generator
I know how to calculate fov verticle from my 90 degree horizontal so that is great.
You say that I didn't provide you with w and h but isn't that my 1680 and 1050 or am I missing a step?
Also doesn't happycoders give me the z distance in pixels and not translated to z axis?