透视投影 - 我怎么项目,该项目背后“照相机”点? [英] Perspective projection - how do I project points which are behind 'camera'?

查看:169
本文介绍了透视投影 - 我怎么项目,该项目背后“照相机”点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用Java编写自己的软件光栅,我跑了与它的一些麻烦......看看样本图像,请:

I'm writing my own software rasterizer in Java, and I ran into some trouble with it... take a look at a sample image, please:

图片

此示例只是画简单的方形网格在一个平面上。一切工作正常,直到我将相机足够接近一些点来移动它后面。在此之后,他们不再正确投影,你可以看到(垂直线 - 投影到屏幕上方点应该是后面相机)。

This sample just draw simple square grid on a plane. Everything works fine until I move camera close enough for some points to move behind it. After that, they're no longer projected correctly, as you can see (vertical lines - points that should be behind camera are projected on top of the screen).

我的转换矩阵和向量相同的DirectX那些使用(PerspectiveFovLH投影,LookAtLH相机)。

My transformation matrices and vectors are same ones DirectX is using (PerspectiveFovLH for projection, LookAtLH for camera).

我用下面的方法改造项目三维点:

I'm using following transformation method to project 3D point:

  1. 3D矢量进行改造创建。
  2. 矢量乘以ViewProjection矩阵。
  3. 在此之后,点转换使用下面的方法来筛选:

  1. 3D vector to be transformed is created.
  2. Vector is multiplied by ViewProjection matrix.
  3. After that, point is transformed to screen using following method:

// 'vector' is input vector in projection space
// projection to screen
double vX = vector.x / vector.z;        
double vY = vector.y / vector.z;


//translate
//surfaceW is width and surfaceH is height of the rendering window.
vX = (( vX + 1.0f) / 2.0f) * surfaceW;
vY = ((-vY + 1.0f) / 2.0f) * surfaceH;


return new Vector3(vX, vY, vector.z);

正如我刚才所说,它工作正常,直到后面的摄像头点移动。事实是,我可以计算出,当点的背后是相机(通过测试它的Z值后的最终变换),但因为我是画线和其他线路基础的对象,我不能只跳过了这一点。

As I said earlier, it works fine until point moves behind camera. The fact is, I can figure out when the point is behind camera (by testing it's Z value after final transform), but since I'm drawing lines and other line based objects, I can't just skip that point.

然后我试图根据设定我的转换管道的 Direct3D的转换管道的文章在MSDN上。

Then I tried setting my transformation pipeline according to The Direct3D Transformation Pipeline article on MSDN.

不幸的是,我还没有得到任何运气的同时(相同的结果),所以任何帮助将是非常美联社preciated,因为我有点坚持这一点。

Unfortunately, I haven't had any luck with that as well (same results), so any help would be highly appreciated, since I'm a bit stuck on this one.

感谢你。

最好的问候, 亚历克斯

Best Regards, Alex

推荐答案

您需要相交在三维空间中前剪裁平面的线与截断线,所以你只画线段这是明显的:

You need to intersect the line with the front clipping plane in 3d space and truncate the line so you only draw the line segment that's visible:

             |
             |
             |
x------------+-----------o
             |
             |
             |   * - camera
             |
             |
             |
       clipping plane

您已经有了一个行 XO ,其中 X 的裁剪平面的前面, 0 后面。这相交线与裁剪平面生成点 + 。你知道哪些 X 0 可见,所以吸取 X中的行 +

You've got a line xo where x in front of the clipping plane and o behind it. Intersect this line with the clipping plane to generate the point +. You know which of x and o is visible so draw the line from x to +.

这样你不投射点,这是背后的摄像头。

This way you're not projecting points which are behind the camera.

这篇关于透视投影 - 我怎么项目,该项目背后“照相机”点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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