查看管道技术示例 [英] Viewing pipeline technical example

查看:39
本文介绍了查看管道技术示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试自己用 Java 实现一个查看管道,所以我只使用 Java AWT 在画布上绘制 2D 多边形,完全没有 OpenGL 的帮助.

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.

我想和你一起测试我的结果,因为这是一个很难的话题,而且我可能误解了一些观点.

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

我制作了一些原始数据文件用于测试.这是一个表示 3D 立方体的文件:

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

以上实际上是 12 条折线,它们应该在屏幕上形成 3D 立方体.

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

这是相机配置:

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

我期望发生的事情

当我绘制上述多边形时,因为我的相机看着立方体的中心,并且正好位于它的前面(距离 1,所以我可以看到一些东西),我希望在屏幕上只看到一个普通的正方形(因为所有 3D 边缘都正好位于正方形面的后面).

我深入我的代码以找出对角线的来源,并意识到尽管我希望坐标 (0,0,0) 和 (0,0,1) 落在同一个位置在 2D 中(特别是当我们与立方体完全垂直时),它们彼此成对角线.

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.

更详细:

将 (0,0,0) 坐标转换为视图坐标会导致 (-0.5,-0.5,-1),而 (0,0,1) 会转换为 (-0.5,-0.5,0).

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

现在,当将每个投影到 2D 时,(0,0,0) 变为 (0.5,0.5),(0,0,1) 变为 (-0.5,-0.5).这就是为什么它们看起来是对角线的,而绘制多边形 0 4(基本上是它们之间的一条线)就是一条对角线.

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.

为什么会这样?

我认为我应该看到一个正方形的假设是否错误,或者我一切都很好,这可能是计算中的错误?你得到的结果和我的一样吗?

如果有人想知道,以下是我用来转换为查看坐标的计算:

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

并在 2D 空间上进行投影:

And to project on 2D space:

我计算 d 好像它是从相机到观察点的距离,但在我看来,我可能是错的.这是我的来源(我认为我误解了但无法弄清楚什么是 d):

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):

根据 Nico 的回答,现在我设置 d = 1(并且将方程保留为/z+d).我还将第 8 个多边形从 7 0 更改为 7 4,这更有意义.这就是我得到的,数字是相机位置的变化:

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:

为什么 z=1 上的对角线看起来像那样?

Why the diagonals on z=1 appear like that?

推荐答案

首先,你的投影中心在立方体的背面.因此,该面上的点被投影到无穷远处.进一步移动投影中心(例如将 z 坐标设置为 2).

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

通常情况下,d 设置为 1,但是否设置了合理的窗口大小并不重要.

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

请记住,这是一个透视变换.因此,点 (0, 0, 0)(0, 0, 1) 不会投影到同一点上.

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

这篇关于查看管道技术示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆