# Trandforming a 3D point to the 2D screen given the view direction

## Recommended Posts

I have a DXF file that I want to render onto the screen as an orthographic 2D image.

The view direction vector is given in the following format:

-0.720495631362985, -1.49503251161485, 0.495745735636187

I want to translate a point onto the screen, given the x, y and z coordinates in the form screen-x, screen-y. Google mostly delivers opengl solutions.

If the View direction is looking straight down (ie. the z-coordinate is removed), then the vector is as follows:

0,0,1

In which case ((int)x, (int)y)==(screen-x,screen-y)

I have asked this question before, but it would seem that that was before the board was upgraded.

##### Share on other sites

What actually is your question? (I don't mean to be snarky, but you said "I have asked this question before"... except, unless I'm missing something, you didn't ask a question)

Edited by masskonfuzion

##### Share on other sites
7 hours ago, dgcoventry said:

I want to translate a point onto the screen, given the x, y and z coordinates in the form screen-x, screen-y. Google mostly delivers opengl solutions.

How do I translate a point onto the screen?

I want to get the screen-x and screen-y, given that the viewing direction is x=-0.720495631362985,y= -1.49503251161485, z=0.495745735636187 and the view is orthographic.

If a line is from (0,0,0) to (100,100,100), how is it represented on the screen, given the viewing direction above? I hope this is clearer.

I've had this conversation before on here, but it was years ago and I can't remember how it was resolved.

##### Share on other sites

The typically way to do this would be to build a 3x3 rotation matrix matching the direction of the viewing vector you have been given, and then multiply each point by that matrix. You'll probably need to multiply that matrix by a scaling matrix to convert the output into window coordinates, but you get the general idea...

What API are you planning to use to draw the points on screen?

##### Share on other sites

Thanks.

I'll have a look at the link.

I want to render portions of the file as an SVG to be loaded into the browser, so I just need to know the 2D screen projection points in the event of the veiw being anything other than in plan. Hence OpenGL solutions are not really applicable.

##### Share on other sites

How can I derive the transformation matrix from the view direction?

It strikes me that the values given for the view vector have already been reduced to be plugged into a formula to calculate the screen coordinates. Or do I still need to find the cosines and sines and angles in 3d?

(Thanks for the assistance, by the way)

##### Share on other sites

The view vector defines the direction of the one axis of your camera system which passes through your center of the view. You typically don't want the camera to be tilted, so the viewing vector and the up vector is all you need to calculate the orientation matrix of the camera. Sines and cosines are not needed for this step; instead the cross product of vectors is what you need. Several threads in this forums discuss that topic; possible keyword for a search is "look-at matrix".

The fact that the projection is orthographic means that all projector line directions are identical to the view vector, so no opening angle is needed. However, also orthographic projection requires a camera position since 2 of its 3 dimensions are not irrelevant (also the 3rd is not irrelevant as soon as you use front-/backplane clipping). You further need to have a scaling, because there is a mapping of units in which the world is defined and units of the screen size. (swiftcoder already mentioned this above.) May be you want to use something like "the entire drawing should be seen" and "looking at the center of the bounding box of the entire drawing" for resolving such values.

Only if you have all this stuff, a view transform can be computed. The principles for this kind of math are not really different with respect to different graphic APIs.

##### Share on other sites

Thanks.

I have been searching Google using the terms "lookat" and "matrix", but I'm not terribly familiar with the nomenclature used. I've found something here, but again, I am unsure of how it applies to my proposed implementation.

As I explained above, I'm planning to generate a SVG file. Most views of the file will be in the plan view (just the x and y coordinates), but I want to make sure that the 3D capability is preserved if needed.

##### Share on other sites

A single view vector is not enough to describe the orientation of the camera, the camera can roll around the view direction. But as haegarr points out, you generally don't want your camera to be tilted, so you can probably get away with using (0, 0, 1) as your up direction.

In order to build your camera matrix you simply need 3 orthogonal vectors: right, up, view. These vectors are the columns of your matrix, in that order. To get your vectors you can use the cross product:

right = Cross(view, (0, 0, 1));
up    = Cross(right, view);

The reason we recalculate the up vector is because (0, 0, 1) is only orthogonal if view.z == 0. Now if your camera looks in the same direction or in the exact opposite of (0, 0, 1) the cross product becomes 0, which means this will not work, so keep that in mind.

##### Share on other sites

I'm afraid my difficulties with matrices continues.

Supposing I have a point (100,100,50)

My view direction is defined by the vector (-0.720495631362985, -1.49503251161485, 0.495745735636187)

Up is in the direction (0,0,1)

The cross product of (-0.720495631362985, -1.49503251161485, 0.495745735636187) and (0,0,1) is (0,0.720495631362985,-1.49503251161485). Is this right?

If I cross this with (100,100,50), this gives (72.0495631362985,149.503251161485,-185.528032729634).

So my screenx=72 and screeny=150?

## Create an account

Register a new account

1. 1
2. 2
3. 3
Rutin
19
4. 4
5. 5

• 11
• 21
• 9
• 31
• 16
• ### Forum Statistics

• Total Topics
632618
• Total Posts
3007465
• ### Who's Online (See full list)

There are no registered users currently online

×