# [Stage3D] Mixing Ortho and Perspective Projection

This topic is 1735 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## 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 on other sites

How are you calculating the position of the labels from the object positions?

##### 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 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 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 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 on other sites

I have try to implement these steps, but i'm having two problems.  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" +
"mov op, vt2\n" +

Edited by Stimpy83

##### 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.

• 10
• 13
• 14
• 18
• 15
{"extraction_info": {"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 1, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "math_score": 0.430663526058197, "perplexity": 945.8956656215711}, "config": {"markdown_headings": true, "markdown_code": true, "boilerplate_config": {"ratio_threshold": 0.18, "absolute_threshold": 10, "end_threshold": 15, "enable": true}, "remove_buttons": true, "remove_image_figures": true, "remove_link_clusters": true, "table_config": {"min_rows": 2, "min_cols": 3, "format": "plain"}, "remove_chinese": true, "remove_edit_buttons": true, "extract_latex": true}, "warc_path": "s3://commoncrawl/crawl-data/CC-MAIN-2018-22/segments/1526794863277.18/warc/CC-MAIN-20180520092830-20180520112830-00274.warc.gz"}