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

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

问题描述

我想在一个句子中做什么



我试图在Java中实现一个查看管道,所以我只使用Java AWT在画布上绘制2D Polygons,完全没有OpenGL的帮助。



这篇文章的目的



I我想和你一起测试我的结果,因为这是一个很难的话题,我可能误解了一些观点。因此,让我们来描述一下我的设置......

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

  8 //顶点数
0 0 0 // list of顶点索引...
1 0 0
1 1 0
0 1 0
0 0 1
1 0 1
1 1 1
0 1 1
12 //多边形的数量
0 1 //第一个多边形坐标索引(所以第一个多边形是0 0 0到1 0 0)
1 2 //第二个多边形是1 0 0
2 3 //等等
3 0
4 5
5 6
6 7
7 0
0 4
1 5
2 6
3 7

以上实际上是12条应该在屏幕上形成3D多维数据集的多段线。



以下是相机配置:

 位置0.5 0.5 1 //相机的位置
LookAt 0.5 0.5 0.5 //看点
向上0 1 0 //向上向量
窗口-1 1 - 1 1 //窗口大小,(-1,-1)到(1,1)包括窗口
中的所有上述多边形800 600 600 //视口大小,不太相关



我预计会发生的事情



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


究竟发生了什么 h2>



我为了解决这个问题而接近我。

为了找出对角线来自哪里,我潜入我的代码中,并意识到尽管我期望坐标(0,0,0)和(0,0,1)在2D中落在相同的点上(特别是当我们与立方体完全垂直时),它们彼此是对角的。

更详细:

将(0,0,0)坐标转换为vi (-0,0,-0.5,-1),而(0,0,1)转换为(-0.5,-0.5,0)。

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



我实际需要弄清楚什么



为什么会发生这种情况?

看到一个方块,或者我很好,这可能是一个计算错误?你是否得到和我一样的结果?



如果有人想知道,这里是我用来转换成查看坐标的计算: p>



然后投射到二维空间上:





我计算出d,就好像它是距离照相机的视点,但在seonde的心中,我可能是错的。这是我的来源(我认为我误解了,但不知道d是什么):



编辑:



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



为什么z = 1上的对角线会出现这样的结果? 解决方案

首先,您的投影中心是在立方体的背面。因此,这张脸上的点被投射到无穷远处。将投影中心稍微移动一点(例如,将z坐标设置为2)。其次,您的视图变换和投影变换似乎不匹配。使用以下变换可以得到合理的值:

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

通常, d 设置为1,但设置合理的窗口大小并不重要。



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

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?

解决方案

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.

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

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