# Problems finding bounds of window in world coordinates.

Hi everyone,

I’m having a little trouble again.
Currently I want to find the bounds of my window in world coordinates. I know that unprojection can be useful for that, but I’m doing it wrong and can’t find my mistake.
I’m trying to get the right end of my window this way:

``````
viewMatrix = glm::translate(glm::mat4(1.0f), glm::vec3(0.0f, 0.0f, -5.f)); //View matrix is translated 5 units back.
projectionMatrix = glm::perspective(60.0f, 640.f / 480.f, 0.1f, 100.f); //FOV is 60, window aspect is 640/480, near plane 0.1 and far plane is 100.
glm::vec3 position(639.f, 479.f, 0.f);
glm::vec3 v = glm::unProject(position, viewMatrix, projectionMatrix, glm::vec4(0, 0, 640.f, 480.f));
``````

The goal is that v.x contains the right bound of my window in world coordinates. This value should be around 4.
I don’t know what value I can use for the z-value in position. If I use 1 then v.x is ~67. With z = 0 it is ~0.067.

Currently I’m not using the z-axis for any of my game objects. It’s 2D but not orthogonal.

Is there a way to calculate z for position or any other way to get the window bounds in world coordinates?

The goal is that v.x contains the right bound of my window in world coordinates

In perspective mode the right bound is not a fixed value, it is a plane. When z = 0, it is a vertical line where the near plane and right plane intersect (hence a fixed value for x).
When z = 1, it is a vertcal line where the far plane and right plane intersect.

I know that the right bound is not fixed. That’s why I need to figure out z.
How can I calculate z when my world space z is 0?
If I have near plane = 0.1 and far plane 100 and have my viewMatrix at -5 my near plane world coordinate z is 4.9 and my far plane z is -95. There must be a way to figure it out but I can’t find it. It’s probably something pretty easy…

[QUOTE=Skalli;1248880]…I want to find the bounds of my window in world coordinates.

``````
viewMatrix = glm::translate(glm::mat4(1.0f), glm::vec3(0.0f, 0.0f, -5.f)); //View matrix is translated 5 units back.
projectionMatrix = glm::perspective(60.0f, 640.f / 480.f, 0.1f, 100.f); //FOV is 60, window aspect is 640/480, near plane 0.1 and far plane is 100.
glm::vec3 position(639.f, 479.f, 0.f);
glm::vec3 v = glm::unProject(position, viewMatrix, projectionMatrix, glm::vec4(0, 0, 640.f, 480.f));

``````

The goal is that v.x contains the right bound of my window in world coordinates.[/QUOTE]

There are a number of ways to do this. If you use the projection matrix an GLM’s gluUnProject work-alike like you are, then window-space Z runs from 0…1 as you go from near clip plane to far clip plane. The vector you’re feeding into glm::unProject() doesn’t make sense to me. I’d feed in the following values, to successive calls, to get the points on the near clip plane: (0,0,0,1),(640,0,0,1),(640,480,0,1),(0,480,0,1). For far clip, use Z=1 instead of 0.

However, you don’t even need to mess with the projection matrix and glm::unproject if you just start with eye-space coords. That is, you can compute the glFrustum left/right/bottom/top args from your gluPerspective args (gluPerspective), and then back those through the inverse viewing matrix. Namely, back these eye-space points through to get the near clip plane corners: (l,b,-n,1),(r,b,-n,1),(r,t,-n,1),(l,t,-n,1). And use Z=-f instead of -n for the far plane.

Hi Dark Photon.
The values I’m passing to glm::unProject should be ok (except for the z component in position).
If you look at my last post from yesterday you will see that I’m struggeling with the plane where my game objects are.
I’ve also tried to find the bounds with triangle equations. When my near plane is at 4.9 I can use that to calculate the right bound:

``````(4.9f * glm::sin(60.f * 3.14159 / 180))/glm::sin(90.f * 3.14159 / 180);
``````

The result is close, but not correct. :-/
Last but not least I’ve set up my scene in blender to get a better orientation of my space, but the coordinates in blender are a bit different than in my scene…

Greetings
Skalli