Viewing pipeline technical example

空扰寡人 提交于 2019-12-24 06:05:22

问题


What I'm trying to do in a sentence

I'm trying to implement a viewing pipeline in Java by myself, so I'm using only Java AWT to draw 2D Polygons on canvas, without the help of OpenGL at all.

Purpose of this post

I'd like to test my results with you, since it's a difficult topic and I might have misunderstood some points.

So let's describe my setup...

I have made some raw data files for testing. Here's a file which represent a 3D Cube:

8 // number of vertices
0 0 0 // list of vertices indices...
1 0 0
1 1 0
0 1 0
0 0 1
1 0 1
1 1 1
0 1 1
12 // number of polygons
0 1 // first polygon coordinates indices (so the first polygon is 0 0 0 to 1 0 0)
1 2 // second polygon is 1 0 0 
2 3 // and so on
3 0
4 5
5 6
6 7
7 0
0 4
1 5
2 6
3 7

The above are actually 12 polylines which should form the 3D cube on screen.

Here's the camera configuration:

Position 0.5 0.5 1 // position of camera
LookAt 0.5 0.5 0.5 // look at point
Up 0 1 0 // up vector
Window -1 1 -1 1 // window size, (-1,-1) to (1,1) includes all the above polygons in window
Viewport 800 600 // viewport size, not so relevant

What I expect to happen

When I draw the above polygons, since my camera looks at the center of the cube, and positioned exactly in front of it (from a distance of 1 so I'll be able to see something), I expect to see just a normal square on the screen (because all the 3D edges would be exactly behind the square face).

What actually happens

How I approached in order to solve the problem...

I dived into my code in order to figure out where the diagonals are from, and realized that although I'd expect the coordinates (0,0,0) and (0,0,1) to fall on the same spot in 2D (in particular when we are perpendicular with the cube exactly), they are diagonal to each other.

More detailed:

Transforming the (0,0,0) coordinate to view coordinate results in (-0.5,-0.5,-1) while the (0,0,1) is transformed to (-0.5,-0.5,0).

Now, when projecting each one to 2D, the (0,0,0) becomes (0.5,0.5) and the (0,0,1) becomes (-0.5,-0.5). That's why they appear diagonal, and drawing the polygon 0 4 (basically a line between them) is a diagonal line.

What I actually need to figure out

Why is this happening?

Am I wrong about my assumption that I should see a square, or I'm all good and it's probably a bug in the calculations? Do you get the same result as mine?

In case anyone was wondering, here are the calculations I've used to transform into viewing coordinates:

And to project on 2D space:

I calculated d as if it was the distance from the camera to the lookat point, but in seond mind I might be wrong. Here's my source (which I think I misunderstood but can't figure out what is d):

Edit:

According to Nico's answer, now I set d = 1 (and left the equation with /z+d, yet). I also changed 8th polygon from 7 0 to 7 4 which makes more sense. This is what I get, the numbers are the changes in camera position:

Why the diagonals on z=1 appear like that?


回答1:


Firstly, your center of projection is in the back face of the cube. Therefore, points on this face are projected into infinity. Move the center of projection a bit further (e.g. set z-coordinate to 2).

Secondly, your view transform and projection transform seem to not match. Use the following transform to get reasonable values:

xp = d * x / z
yp = d * y / z
zp = d

Usually, d is set to 1, but it does not really matter if you set a reasonable window size.

Remember that this is a perspective transform. Therefore, the points (0, 0, 0) and (0, 0, 1) will not project onto the same point.



来源:https://stackoverflow.com/questions/36901491/viewing-pipeline-technical-example

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!