基本呈现3D透视投影到二维的屏幕,摄像头(无OpenGL的) [英] Basic render 3D perspective projection onto 2D screen with camera (without opengl)

查看:244
本文介绍了基本呈现3D透视投影到二维的屏幕,摄像头(无OpenGL的)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们说我有类似如下的数据结构:

Let's say I have a data structure like the following:

Camera {
   double x, y, z

   /** ideally the camera angle is positioned to aim at the 0,0,0 point */
   double angleX, angleY, angleZ;
}

SomePointIn3DSpace {
   double x, y, z
}

ScreenData {
   /** Convert from some point 3d space to 2d space, end up with x, y */
   int x_screenPositionOfPt, y_screenPositionOfPt

   double zFar = 100;

   int width=640, height=480
}

...

如果没有屏幕剪辑或别的东西也不多,我将如何计算屏幕的x,有些点y位置给定的空间中一些3D点。我想预测,3D点到2D画面。

Without screen clipping or much of anything else, how would I calculate the screen x,y position of some point given some 3d point in space. I want to project that 3d point onto the 2d screen.

Camera.x = 0
Camera.y = 10;
Camera.z = -10;


/** ideally, I want the camera to point at the ground at 3d space 0,0,0 */
Camera.angleX = ???;
Camera.angleY = ????
Camera.angleZ = ????;

SomePointIn3DSpace.x = 5;
SomePointIn3DSpace.y = 5;
SomePointIn3DSpace.z = 5;

ScreenData.x和y是在空间中的三维点的屏幕x位置。如何计算这些价值?

ScreenData.x and y is the screen x position of the 3d point in space. How do I calculate those values?

我可能用在这里发现的公式,但我不知道如何在屏幕宽/高进场。另外,我不明白,在维基条目是VERS摄像头位置的观众的位置。

I could possibly use the equations found here, but I don't understand how the screen width/height comes into play. Also, I don't understand in the wiki entry what is the viewer's position vers the camera position.

http://en.wikipedia.org/wiki/3D_projection

推荐答案

在'的方式,它的完成'就是用同质转换和坐标。你把一个点在空间和

The 'way it's done' is to use homogenous transformations and coordinates. You take a point in space and:

  • 的位置是相对于使用模型矩阵相机。
  • 项目,要么正射或使用投影矩阵中的观点。
  • 应用视trnasformation将其放置在屏幕上。

这得到pretty的模糊,但我会尽力并覆盖重要的位,并留下一些给你。我假设你了解矩阵数学的基础知识:。)

This gets pretty vague, but I'll try and cover the important bits and leave some of it to you. I assume you understand the basics of matrix math :).

同质载体,积分,转换

在三维,均质点可以是以下形式的一列矩阵[X,Y,Z,1]。最后一个组件是'W',一个比例系数,这对于载体是0:这有你不能转化载体,这是数学上正确的效果。我们不会去那里,我们的谈话要点。

In 3D, a homogenous point would be a column matrix of the form [x, y, z, 1]. The final component is 'w', a scaling factor, which for vectors is 0: this has the effect that you can't translate vectors, which is mathematically correct. We won't go there, we're talking points.

同质转化为4X4矩阵,因为它们允许翻译被重新psented为矩阵乘法,而不是加法,这是很好的,快速为你的显示卡$ P $。又方便,因为我们可以通过它们相乘再present连续变换。我们将转换应用于点通过执行转换*点。

Homogenous transformations are 4x4 matrices, used because they allow translation to be represented as a matrix multiplication, rather than an addition, which is nice and quick for your videocard. Also convenient because we can represent successive transformations by multiplying them together. We apply transformations to points by performing transformation * point.

有3个主要同质转变:

  • Translation,
  • Rotation, and
  • Scaling.

有其他人,特别是'看'的转型,这是值得探讨的。不过,我只是想给一个简短的清单和几个环节。移动,缩放和旋转应用于点的连续应用是集体的模型变换矩阵,并将它们放置在场景中,相对于摄像机。重要的是要意识到我们正在做的是类似于走动的摄像头,而不是其他方式的对象。

There are others, notably the 'look at' transformation, which are worth exploring. However, I just wanted to give a brief list and a few links. Successive application of moving, scaling and rotating applied to points is collectively the model transformation matrix, and places them in the scene, relative to the camera. It's important to realise what we're doing is akin to moving objects around the camera, not the other way around.

正交和透视

从世界坐标转换为屏幕坐标,你会首先使用的投影矩阵,其中常见的有两种形式:

To transform from world coordinates into screen coordinates, you would first use a projection matrix, which commonly, come in two flavors:

  • 在正投影,通常用于2D和CAD。
  • 视角,良好的游戏和3D环境。

这是正投影矩阵的结构如下:

An orthographic projection matrix is constructed as follows:

其中的参数包括:

  • 热门:在Y的可视空间的顶边坐标
  • :在Y可视空间的底部边缘坐标
  • :可见空间的左边缘的X坐标
  • 右键:在X可视空间的右边缘坐标
  • Top: The Y coordinate of the top edge of visible space.
  • Bottom: The Y coordinate of the bottom edge of the visible space.
  • Left: The X coordinate of the left edge of the visible space.
  • Right: The X coordinate of the right edge of the visible space.

我认为这是pretty的简单。你确定是空间,是要显示在屏幕上,你可以对夹在一个区域。它的简单这里,由于空间可视区域为长方形。裁剪的角度来看是比较复杂的,因为它出现在屏幕上或视体的区域,是一个平截

I think that's pretty simple. What you establish is an area of space that is going to appear on the screen, which you can clip against. It's simple here, because the area of space visible is a rectangle. Clipping in perspective is more complicated because the area which appears on screen or the viewing volume, is a frustrum.

如果你有一个很难与透视投影维基百科,这里的code构建一个合适的基质,的礼貌geeks3D

If you're having a hard time with the wikipedia on perspective projection, Here's the code to build a suitable matrix, courtesy of geeks3D

void BuildPerspProjMat(float *m, float fov, float aspect,
float znear, float zfar)
{
  float xymax = znear * tan(fov * PI_OVER_360);
  float ymin = -xymax;
  float xmin = -xymax;

  float width = xymax - xmin;
  float height = xymax - ymin;

  float depth = zfar - znear;
  float q = -(zfar + znear) / depth;
  float qn = -2 * (zfar * znear) / depth;

  float w = 2 * znear / width;
  w = w / aspect;
  float h = 2 * znear / height;

  m[0]  = w;
  m[1]  = 0;
  m[2]  = 0;
  m[3]  = 0;

  m[4]  = 0;
  m[5]  = h;
  m[6]  = 0;
  m[7]  = 0;

  m[8]  = 0;
  m[9]  = 0;
  m[10] = q;
  m[11] = -1;

  m[12] = 0;
  m[13] = 0;
  m[14] = qn;
  m[15] = 0;
}

变量是:

  • FOV :视场,pi / 4弧度一个很好的价值
  • 方面:高度比率宽度
  • znear,​​zfar :用于裁剪,我会忽略这些
  • fov: Field of view, pi/4 radians is a good value.
  • aspect: Ratio of height to width.
  • znear, zfar: used for clipping, I'll ignore these.

和生成的矩阵是列优先,收录在上面code如下:

and the matrix generated is column major, indexed as follows in the above code:

0   4   8  12
1   5   9  13
2   6  10  14
3   7  11  15

视口转换,屏幕坐标

这两个转变的要求另一个矩阵的矩阵把事情的屏幕坐标,称为视口变换。 这是这里描述的,我不会掩盖它(这是死的简单)

Both of these transformations require another matrix matrix to put things in screen coordinates, called the viewport transformation. That's described here, I won't cover it (it's dead simple).

因此​​,对于一个点p,我们将:

Thus, for a point p, we would:

  • 执行模型变换矩阵* P,导致分。
  • 执行投影矩阵*时许,致使第
  • 在裁剪PP对视景体。
  • 执行视口变换矩阵*页,是造成PS:在屏幕上点

摘要

我希望,涵盖了大部分。有上述漏洞,它是模糊的地方,张贴任何以下问题。这个题目通常是值得一本教科书一整章的,我做我的最好的蒸馏过程中,希望你的优势!

I hope that covers most of it. There are holes in the above and it's vague in places, post any questions below. This subject is usually worthy of a whole chapter in a textbook, I've done my best to distill the process, hopefully to your advantage!

我联系到这个上面,但我强烈建议你阅读这一点,并下载二进制文件。这是一个很好的工具,以确保为您的论文变革的理解,以及如何获取屏幕上的点:

I linked to this above, but I strongly suggest you read this, and download the binary. It's an excellent tool to further your understanding of theses transformations and how it gets points on the screen:

<一个href="http://www.songho.ca/opengl/gl_transform.html">http://www.songho.ca/opengl/gl_transform.html

至于实际的工作中,您将需要实施均质转换的4x4矩阵类以及均匀的点类,你可以乘反对申请转换(记住,[X,Y,Z,1]) 。您需要产生的转化,并用与链接描述。一旦你理解了程序这不是那么困难。祝您好运。)

As far as actual work, you'll need to implement a 4x4 matrix class for homogeneous transformations as well as a homogeneous point class you can multiply against it to apply transformations (remember, [x, y, z, 1]). You'll need to generate the transformations as described above and in the links. It's not all that difficult once you understand the procedure. Best of luck :).

这篇关于基本呈现3D透视投影到二维的屏幕,摄像头(无OpenGL的)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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