Sign in to follow this  
Stimpy83

[Stage3D] Mixing Ortho and Perspective Projection

Recommended Posts

Hi,

 

i have a simple question, because i'm a little bit confused. My scene render in perspective projection well, now i will overlay some Labels and other stuff without perspective. So i create an ortho perspective and render the stuff with this new projection. But the positions on the screen dosn't match. The label is not over the object. Why? I don't understand it.

projection.perspectiveFieldOfViewLH(fov * Math.PI / 180, width / height, zNear, zFar);  
projectionOrtho.orthoLH(width , height, zNear, zFar);  

I'm using an fov of 45 degree. Have somebody some idea why the positions dosn't match?

 

Thanks.

Share this post


Link to post
Share on other sites

I'm using the World Position from the Label/Object(The Positions are the same) and the ModelViewProjection Matrix. I'm render the object with the same Matrix, only the Projection is different from the object rendering. When i render the label with the perspective matrix in the MovelViewProjection it look right, but perspective.

Edited by Stimpy83

Share this post


Link to post
Share on other sites

If the label and the object has the same position, then the different projections will place them at different places on the screen. You need to calculate the corresponding position in the orthographic coordinate system that has the same screen space position as the perspective coordinate system. If the two projections were the same, you wouldn't have to use two different projections in the first place.

 

I don't know what graphics API you use, but most of them are based on the same ideas, although there may be some variations so check the documentation for exact details. What you need to do is find the common point in the two coordinate systems that projects to the same point on the screen:

  1. Take the point of the object and multiply it by the [edit: perspective] projection matrix. This gives you the point in clip space.
  2. Take the point in clip space and normalize its W-component. This is called the perspective division and is done by dividing all four components of the position by the W-component, such that the W-component becomes W=1. This is called the normalized device space because it has the range [-1, 1] along both X and Y-axes (Z-range may vary across APIs, but may not be relevant in your case).
  3. Take the point in normalized device coordinate and multiply it by the viewport transform. This gives you the point in pixel coordinates where it would appear on the screen.

The API may already have a function to do this transform, so you may not have to do it yourself. The task is now to find the point in the orthographic coordinate system which has the same pixel coordinate.

  1. If you have a pixel-based coordinate system, which you seem to have, then this i trivial; the position in your orthographic coordinate system is simply the pixel coordinate from step 3 in the previous list.
Edited by Brother Bob

Share this post


Link to post
Share on other sites

Thank you very much for this info - i will try it today with my code. One thing is not really clear for me - On point 1 i take the world coordinate and multiply it with which projection - with the ortho or the perspective projection matrix? 

 

Thanks.

Share this post


Link to post
Share on other sites

The object is displayed with the perspective matrix, so that is the matrix you want transform the point with because you need to find the corresponding point with the orthographic projection. I should have mentioned that in point 1, and I have added it to make it clear, sorry.

Share this post


Link to post
Share on other sites

I have try to implement these steps, but i'm having two problems. dry.png First @ point 1 i take just the projection? what about the view? My second Problem is that i don't have a pixel based coordinate system. I' using Stage3D - An Flash Based Engine. My Output Values in the Shader must between 0-1. But when i delete step 3 i'm between 0-1 , but the labels still positon wrong. When i take the final vertex and multiply it against the ortho projection i see nothing - something wrong, don't know what.

shaderV = 
"mov vt0 va0\n" +	// Label Vertex
"mov vt1 va1\n" +	// Label Position
// 1) Take the point of the object and multiply it by the [edit: perspective] projection matrix. This gives you the point in clip space.
"m44 vt2 vt1 vc0\n" + 
// 2) Take the point in clip space and normalize its W-component
"div vt2.xyzw vt2.xyzw vt2.wwww \n" +
"add vt2 vt2 vt0\n" +
"mov op, vt2\n" + 
Edited by Stimpy83

Share this post


Link to post
Share on other sites

If you are at the correct range after step 1, then step 1 should be all you need. But you need to multiply by the view matrix as well in step 1, that's correct. I have no idea what Stage3D is or how it works, so I cannot assist on the details regarding that particular API.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this