3d到2d点转换 [英] 3d to 2d point conversion

查看:110
本文介绍了3d到2d点转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将3D点绘制到2D表面上(我目前正在将SDL.NET作为我的游戏引擎"使用).表面尺寸为800x400像素,3d坐标范围为-4000至4000.我的视图将始终是自顶向下的视图,并且不会发生任何相机移动.有人可以提供一些本机c#,伪代码或简单的说明将3D空间中的坐标转换为2D表面吗?

I'm trying to plot 3D points onto a 2D surface (I'm currently working with SDL.NET as my 'game engine'). The surface will be 800x400 pixels in size and the 3d coordinates will range from -4000 to 4000. My view will always be a top-down view and there will be no camera movement whatsoever. Could someone provide some native c#, pseudo code or an easy explanation for converting coordinates in the 3-D space to the 2-D surface?

同时订购这本书告诉我,它将解决我的许多数学缺陷....希望:)

Meanwhile Im ordering this book which people tell me it will address a lot of my math shortcomings....hopefully :)

推荐答案

注意:这是一堵巨大的文字墙,我完全了解了许多重要的内容-但我的意图只是一个概述...希望这里的某些术语/概念可以使您更好地谷歌搜索适当的Web块.

NOTE: This is a big wall of text and I completely glaze over a lot of important stuff - but my intention here is just an overview...hopefully some of the terms/concepts here will lead you to better Googling for appropriate chunks on the web.

如果您逐步走过人生要点",它会有所帮助:

It helps if you walk your way through "Life as a point":

这是一个很好的3维点:

Here we are, a nice little 3-dimensional point:

var happyPoint = new Point(0, 0, 0);

这是它的好友,它是根据他的朋友定义的:

And here is its buddy, defined in relation to his friend:

var friendlyPoint = new Point(1, 0, 0);

现在,让我们将这两点称为模型"-我们将使用术语模型空间"来讨论单个三维结构(例如房屋,怪物)中的点.等).

For now, let's call these two points our "model" - we'll use the term "model space" to talk about points within a single three-dimensional structure (like a house, monster, etc).

模型不是生活在真空中,但是...通常更容易将模型空间"和世界空间"分开,以使模型调整更容易(否则,所有模型都需要放在模型中).相同的比例,相同的方向等,再加上尝试在3d建模程序中进行操作,都是不可能的.

Models don't live in a vacuum, however...it's usually easier to separate the "model space" and "world space" to make things easier for model tweaking (otherwise, all your models would need to be in the same scale, have the same orientation, etc, etc, plus trying to work on them in a 3d modelling program would be friggin impossible)

因此,我们将为模型"定义一个世界变换"(好的,有2点是a脚模型,但仍然保留了模型).

So we'll define a "World Transform" for our "Model" (ok, 2 points is a lame model, but a model it remains).

什么是世界变革"?简而言之:

What is a "World Transform"? Simply put:

  • 世界变换 W = T X R X S ,其中
  • T = 平移-即沿X,Y或Z轴滑动
  • R = 旋转-相对于轴旋转模型
  • S = 缩放-沿轴调整模型的大小(保持其中的所有相对点)
  • A world transform W = T X R X S, where
  • T = translation - that is, sliding it along the X, Y, or Z axes
  • R = rotation - turning the model with respect to an axis
  • S = scaling - resizing a model (maintaining all the relative points within) along an axis

我们将在这里轻松进行一下,仅将世界变换定义为 Identity 矩阵-基本上,这意味着我们不希望其平移,旋转或缩放:

We'll take the easy out here, and just define our world transform as the Identity matrix - basically, this means we don't want it to translate, rotate, or scale:

world = [
          1 0 0 0
          0 1 0 0
          0 0 1 0
          0 0 0 1
        ];

我强烈建议您精通Matrix数学,尤其是乘法和Vector-> Matrix操作,它在3D图形中使用了 ALL FREAKING TIME .

I highly recommend you brush up on your Matrix math, especially multiplication and Vector->Matrix operations its used ALL THE FREAKING TIME in 3D graphics.

因此,我巧妙地跳过了实际的矩阵乘法运算,我只是告诉您,将我们的世界变换"与模型点相乘只是以模型点再次结束(尽管在这个有趣的新4维矢量表示中,我不会在这里碰到).

So cleverly skipping over the actual matrix multiplication, I'll just tell you that multiplying our "world transform" and our model points just ends up with our model points again (albeit in this fun new 4-dimensional vector representation, which I won't touch here).

因此,我们已经掌握了要点,并且已经将它们绝对定位在空间"中……现在呢?

So we've got our points, and we've absolutely located them in "space"...now what?

那么,我们从哪里看呢?这导致了 View Transformations Camera Projection 的概念-基本上,这只是另一个矩阵乘法-观察:

Well, where are we looking at it from? This leads to the concept of View Transformations or Camera Projection - basically, it's just another matrix multiplication - observe:

说我们有一个X点,在...哦,(4 2)左右:

Say we've got a point X, at...oh, (4 2) or so:

 |
 |
 |
 |    
 |    X
 |
 ------------------------

从原点(0 0)的角度来看,X 是(em)在(4 2)-但是说我们将摄像机移到右边了?

From the perspective of the origin (0 0), X is at (4 2) - but say we put our camera off to the right?

 |
 |
 |
 |    
 |    X         >-camera
 |
 ------------------------

相对于相机,X的位置"是什么?可能更接近(0 9)或(9 0),这取决于相机的向上"和正确"方向.这就是View转换-将一组3D点映射到另一组3D点,以便从观察者的角度来看它们是正确的".如果您使用的是自上而下的固定摄像机,则观察者将处于天空中的某个固定位置,并且所有模型都将进行相应的转换.

What is the "position" of X, relative to the camera? Probably something closer to either (0 9) or (9 0), depending on what your camera's "up" and "right" directions are. This is what View transformations are - mapping one set of 3D points to another set of 3D points such that they are "correct" from the perspective of an observer. In your case of a top-down fixed camera, your observer would be some fixed position in the sky, and all the models would be transformed accordingly.

所以我们来画画吧!

不幸的是,我们的屏幕还不是3D的,因此我们首先需要将该点投影"到2D曲面上.投影是...好吧,它基本上是一个映射,如下所示:

Unfortunately, our screen isn't 3D (yet), so first we need to "project" this point onto a 2D surface. Projection is...well, its basically a mapping that looks like:

(x, y, z) => (x, y)

可能的投影数量几乎是无限的:例如,我们可以将 X Y 坐标移动 Z :

The number of possible projections is nigh-infinite: for example, we could just shift over the X and Y coordinates by Z:

func(x, y, z) => new point2d(x + z, y + z);

通常,您希望此投影模仿人类视网膜在查看3D场景时所做的投影,因此,我们引入了查看投影的概念.有几种不同的视图投影,例如正交 YawPitchRoll定义透视/FOV定义;每一个都有一些关键数据,您需要正确构建投影.

Usually, you want this projection to mimic the projection the human retina does when looking at 3D scenes, however, so we bring in the concepts of a View Projection. There are a few different view projections, like Orthographic, YawPitchRoll-defined, and Perspective/FOV-defined; each of these has a couple of key bits of data you need to properly build the projection.

例如,基于透视/FOV的投影需要:

A Perspective/FOV based projection, for example, needs:

  • 眼球"(即屏幕)的位置
  • 您的眼球"能够聚焦多远(远裁剪平面")
  • 您的角视场(即,将手臂伸出,只是在外围视觉的边缘)
  • 正在查看的镜头"的宽高比(通常是屏幕分辨率)

一旦获得了这些数字,便会创建一个称为边界平截头体"的东西,它看起来像一个金字塔,顶部朝下倾斜:

Once you've got these numbers, you create something called a "bounding frustum", which looks a bit like a pyramid with the top lopped off:

\-----------------/
 \               /
  \             /
   \           /
    \         /
     \-------/

或者从前面:

 ___________________
|   _____________   |
|  |             |  |
|  |             |  |
|  |             |  |
|  |             |  |
|  |             |  |
|  |_____________|  |
|___________________|

我不会在这里进行矩阵计算,因为在其他地方都定义了矩阵-实际上,大多数库都有帮助方法,这些方法会为您生成相应的矩阵-但大致是这样的:

I won't do the matrix calculations here, since that's all well defined elsewhere - in fact, most libraries have helper methods that'll generate the corresponding matrices for you - but here's roughly how it works:

比方说,您的快乐一点在于这个平截头体:

Let's say your happy little point lies in this frustum:

\-----------------/
 \               /
  \ o<-pt       /
   \           /
    \         /
     \-------/

 ___________________
|   _____________   |
|  |             |  |
|  |             |  |
|o |             |  |
|^---- pt        |  |
|  |             |  |
|  |_____________|  |
|___________________|

请注意,它已经偏向侧面了,以至于它不在近裁剪平面"矩形中-如果您注视"金字塔的较小端会是什么样?

Notice it's way off to the side, so far that it's out of the "near clip plane" rectangle - What would it look like if you "looked into" the smaller end of the pyramid?

就像注视棱镜(或镜头)一样,该点将弯"入视野:

Much like looking into a Prism (or a lens), the point would be "bent" into view:

 ___________________
|   _____________   |
|  |             |  |
|  |             |  |
|>>>o <- pt is   |  |
|  |    shifted  |  |
|  |             |  |
|  |_____________|  |
|___________________|

以另一种方式,如果您在截锥后面后面有明亮的光线,那么来自您的点的阴影将被投射"到附近的剪切场上吗?(较小的矩形),所有的投影就是-将一个点映射到另一点,在这种情况下,删除Z分量并相应地以对我们的眼睛有意义"的方式更改X和Y.

Put another way, if you had a bright light behind the frustum, where would the shadows from your points be "cast" upon the near clipping field? (the smaller rectangle) That's all projection is - a mapping of one point to another, in this case, removing the Z component and altering the X and Y accordingly in a way that "makes sense" to our eyes.

这篇关于3d到2d点转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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