开发者

How to change 3d point to 2d pixel location?

Assume that the camera is located at (0,0,1) point looking into the origin. -z direction is going into the screen. Objects beyond z = -100 are not visible ( far viewing plane). At the far viewing plane x and y are clipped at 100 at right and -100 at left for x-axis and likewise for y-axis.

The viewport window is 600 pixel wide and 300 pixel height. On the 2d pixel coordinates (x2d, y2d), (0,0) is the top left corner, x2d increases to right and y2d increases going downward.

Given above parameters, what are the formulas that calculates (x2d,y2d) given a point (x,y,z)? Each 3d point maps to a pixel unless 3d point is clipped ( not visible in the viewport). Assume perspective projection.

Please don't provide a link that talks about the theory of 3d projections. I am looking for specific solution to this specific problem with the given parameter开发者_Go百科s.


If all you want to do is find 2D screen locations from 3D locations its easy if you know similar triangles. No triggers are required. just make up some variable that is about the distance in pixels out of the screen to your eyes - call that the focal_length. You can adjust that figure to make it look more realistic (too low looks really stretchy)

This solution is for looking forward in x, looking backwards just needs and extra line to negative the z which is just needless complexity in my humble opinion but i don't know your purpose.

first clip out pixels outside of your range (just use a couple of if statements. I'm not gonna pseudocode that out)

then position yourself in the 3D world:

x0 = x - yourx
y0 = y - youry
z0 = z - yourz

(in your example yourx = 0, youry = 0, yourz = -1)

Then project away:

x2d = focal_length * x0 / z0
y2d = focal_length * y0 / z0

Simple huh? I just made this up by thinking about train tracks and am now trying to make a basic 3d game ;)

If you want to get into the math of 3D rotation or hiding faces etc... that's when things gets ... tricky.

By the way depending on how your screen coordinates are, you might need negative signs on one or the other or both of x2d or y2d equations. Meh you figure it out.


This should explain everything:

Field of view is the limits of the screen. In Minecraft, the FOV is 70 degrees across the entire screen.

The angle of how far over the point is in either the y or z plane (if you look down the x-axis) and is the arctangent of x/y or x/z for whichever plane you calculate for. After that, simply adjust the position of the point to be relative to the eye position and direction. Adjust for the relative position from eye direction by using sines of the angles and doing some math. Drawing it out is always helpful. A good diagram from the Wikipedia article on 3D projection:

How to change 3d point to 2d pixel location?

I am not giving you the code for this, but this should give you a good start.


I suggest you start here:

http://msdn.microsoft.com/en-us/library/bb206269(v=vs.85).aspx

Read through and it will give you a good idea of how all the coordinate transforms work. Its a good explanation :)

Edit: The specific question you ask is HUGE. That link DOES provide you with an explanation of how to get yourself to projection space. You might also want to look at viewports ( http://msdn.microsoft.com/en-us/library/bb206341(v=VS.85).aspx ). Once you have the coordinate transformed perspective projection to a 2d plane is simply a matter of dividing x, y and z by the resulting w coordinate.

Sorry if thats not good enough for you but its a very complex piece of maths. While I do understand it it will take me hours to explain it all to you in person and through the medium of stack overflow its unlikely I'll get my point across well. The MSDN links above will give you a full understanding of the 3D transformation pipeline if you read them.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜