3D to 2D projection

2019-04-14 06:58发布

I'm trying to write a game in Java (Processing, actually) with some cool 3D effects.

I have a choice of two 3D renderers, but neither have the quality or flexibility of the default renderer. I was thinking that if I could get a function to proje

So say I have a set of coordinates (x, y, z) floating in 3D space. How would I get where on the 2D screen that point should be drawn (perspective)?

Just to clarify, I need only the bare minimum (not taking the position of the camera into account, I can get that effect just by offsetting the points) - I'm not re-writing OpenGL.

And yes, I see that there are many other questions on this - but none of them seem to really have a definitive answer.

标签: java 3d
2条回答
一夜七次
2楼-- · 2019-04-14 07:37

Here is your bare minimum.

column = X*focal/Z + width/2
row = -Y*focal/Z  + height/2

The notation is:

X, Y, Z are 3D coordinates in distance units such as mm or meters;

focal is a focal length of the camera in pixels (focal= 500 for VGA resolution is a reasonable choice since it will generate a field of view about 60 deg; if you have a larger image size scale your focal length proportionally); note that physically focal~1cm << Z, which simplifies formulas presented in the previous answer.

height and width are the dimensions of the image or sensor in pixels;

row, column - are image pixels coordinates (note: row starts on top and goes down, while Y goes up). This is a standard set of coordinate systems centered on camera center (for X, Y, Z) and on the upper-left image corner (for row, column, see green lines). Coordinate systems

You don't need to use OpenGL indeed since these formulas are easy to implement. But there will be some side-effects such as whenever your object has several surfaces they won't display correctly since you have no way to simulate occlusions. Thus you can add a depth buffer which is a simple 2D array with float Z values that keeps track which pixels are closer and which are further; if there is an attempt to write more than once at the same projected location, the closer pixel always wins. Good luck.

查看更多
兄弟一词,经得起流年.
3楼-- · 2019-04-14 07:37

Look into Pin hole camera model

http://en.wikipedia.org/wiki/Pinhole_camera_model

ProjectedX = WorldX * D / ( D + worldZ )

ProjectedY = WorldY * D / ( D + worldZ )

where D is the distance between the projection plane and eye

查看更多
登录 后发表回答