3d数学基础图形 教程_学习3D图形引擎中使用的基本数学

3d数学基础图形 教程

Image 1

(You need Visual Studio to compile the project.)

(您需要Visual Studio来编译项目。)

介绍 (Introduction)

A voxel is just a point within a space, like an atom that composes the material, it's my own definition, for other engines, voxels are defined as volume of pixels, like in Minecraft/Roblox but I don't use this definition.

体素只是空间中的一个点,就像组成材料的原子一样,这是我自己的定义,对于其他引擎,体素定义为像素体积,就像在Minecraft / Roblox中一样,但我不使用此定义。

背景 (Background)

This article was possible with my wish (2010) to see a game with texture and object built only with voxel.

出于我的愿望(2010年),这篇文章是有可能看到的,这种游戏的纹理和对象仅由体素构建。

使用代码 (Using the Code)

First of all, we need to ask few questions before:

首先,我们需要先问几个问题:

  • What is a space ?

    什么是空间?
  • What is an axis ?

    什么是轴?
  • What is a dimension ?

    什么是尺寸 ?

  • What is a plane ?

    什么是飞机 ?

  • What are trigonometric functions ?

    什么是三角函数?
  • What is a vector ?

    什么是向量?
  • What is a rotation matrix ?

    什么是旋转矩阵?
  • What is a rotation and translation ?

    什么是旋转和平移?

Now let's give answers to these questions:

现在让我们为这些问题提供答案:

A space is defined by points, mathematically localized by a number of axes in unique directions that form the dimensions of the space (a plane is formed by 2 axes = 2D space).

一个space是由点定义的,这些点在数学上被数个axes沿唯一的方向定位,这些方向形成该空间的dimensions (一个plane由2个轴= 2D空间形成)。

Trigonometric functions are functions such as sin(θ) and cos(θ) where θ represent the angle around an axis and draw a circle in a 2D plan.

Trigonometric functions是诸如sin(θ)和cos(θ)之类的函数,其中θ表示围绕轴的角度并在2D平面中绘制一个圆。

A rotation matrix is a set of equations made with trigonometric functions.

rotation matrix 是由 trigonometric functions 一组方程 。

A vector is a direction in a space (like for GPS), that is the result of the transformation (math operators) of a point by a rotation matrix. The point is driven into the space by the rotation matrix, in order to reach the direction given by the angles inside trigo functions.

vector是空间中的方向(类似于GPS),这是rotation matrix对点进行变换(算术运算符)的结果。 该点由旋转矩阵驱动到空间中,以便到达trigo函数内部的角度所指定的方向。

A 1D space is represented by a single line in one direction/axis: ----------------------------

一维space在一个方向/一个axis由一条线表示:----------------------------

To move an object on this axis, the equations are:

要在此轴上移动对象,公式为:

x' = Dot.x + x To move right

x'=点x + x 向右移动

x' = Dot.x - x To move left

x'=点x-x 向左移动

  -3  -2  -1   0  +1  +2  +3
X--|---|---|---|---|---|---|-- 

A translation is done within a line, 1D space, so we have 1 translation possible on X-axis.

在直线,一维空间内完成translation ,因此我们在X-axis.可能有1个translation X-axis.

A 2D space is just a 1D space with another line in a direction at 90° of the X-axis where Y represents it:

2D space就是一维space ,在X-axis 90 °方向上有另一条线,其中Y表示它:

Image 2

To add an object in this 2D space, we need to set 2 coordinates for each point of the object and to move the object, we apply an addition like we did before to translate the object on x-axis.

要在此2D space添加对象,我们需要为该对象的每个点设置2个坐标并移动该对象,就像在x -axis translate该对象一样,我们像以前一样进行了加法运算。

But we can also rotate the object around the center of the 2 axis, for doing that, an addition is not enough, we need to call math operators that is designed for rotation calculations.

但是我们也可以绕2 axis的中心旋转对象,为此,加法是不够的,我们需要调用专门用于rotation计算的数学运算符。

A rotation is done within a plane, 2D space, so we have 1 rotation possible around the center of the two axis.

rotation是在一个平面2D space内完成的,因此我们可以绕两个axis的中心进行1 rotation

These math operators are called, trigonometric functions: sin() and cos() that take the angle between the two lines: [center, object's point] and x-axis for cos() and y-axis for sin().

这些数学运算符称为三角函数: sin()cos() ,它们占据两条线之间的角度:[中心,对象的点], x-axis表示cos(), y-axis表示sin()

Visually, we have a point at 0° (x = 1, y = 0) and we apply a rotation of +90° (+ is anticlockwise and - is clockwise) around the center, the result is x = 0, y = 1.

视觉上,我们有一个0 ° ( x = 1y = 0 )的点,并且绕中心rotation +90 ° (+为逆时针,-为顺时针),结果为x = 0y = 1

And the calculus to find this result is:

找到这个结果的演算是:

x' = x.cos(90) - y.sin(90)
y' = x.sin(90) + y.cos(90)

It's quite easy to retrieve this equation from scratch, it's what I did and surprisingly I have seen that this equation was the linear form of the Z rotation matrix.

从头开始检索该方程式非常容易,这就是我所做的,而且令人惊讶的是,我已经看到该方程式是Z旋转矩阵的线性形式。

Here's how I did it:

这是我的做法:

  • We have a 2D XY circle with a diameter of 8.

    我们有一个直径为8的2D XY圆。

    Image 3
  • We set 1 point, rotate it and try to retrieve the equation of its new locations, we will find the final equation once we have operate on a couple of different XY combinations.

    我们设置1点,旋转它并尝试检索其新位置的方程式,一旦我们对几个不同的XY组合进行运算,我们将找到最终方程式。
  • Let's begin with P(4, 0), a red point:

    让我们从一个红点P(4,0)开始:

    Image 4
  • Now we rotate this point of +90° (θ) and try to find the equation of its new location, visually the result to find is P(0, 4):

    现在我们将这个点旋转+ 90°(θ)并尝试找到其新位置的方程,在视觉上找到的结果为P(0,4):

    Image 5
    X = sin(θ) = 1 ❌
    X = cos(θ) = 0 ✔
    
    Y = cos(θ) = 0 ❌
    Y = sin(θ) = 1 ❌✔
    Y = 4 × sin(θ) = 4 ✔
    	
  • So for P(4, 0) and θ = 90, we have:

    因此,对于P(4,0)和θ= 90,我们有:

    X = cos(θ)
    Y = 4 × sin(θ)
    	
  • Try with any other angles for P(4, 0) and different X value with Y at zero (use mathsisfun for that), you will find the same equations, with 4× for X, but it's not alterate the final equation:

    尝试将P( 4,0 )的任何其他角度和Y都为零的不同X值(使用mathsisfun)进行尝试 ,您会发现相同的方程式,其中X的角度为4×,但不会改变最终方程式:

    X' = 4 × cos(θ) = X × cos(θ)
    Y' = 4 × sin(θ) = X × sin(θ)
    	
  • Now we set the red point at P(0, 4):

    现在我们将红点设置为P(0,4):

    Image 6
  • We rotate this point of +90°, visually the result is P(-4, 0):

    我们将此点旋转+ 90°,在视觉上结果为P(-4,0):

    Image 7
    X = cos(θ) = 0 ❌
    X = sin(θ) = 1 ❌✔
    X = 4 × -sin(θ) = -4 ✔
    
    Y = sin(θ) = 1 ❌
    Y = cos(θ) = 0 ✔
    	
  • So for P(0, 4) and θ = 90, we have:

    因此,对于P(0,4)和θ= 90,我们有:

    X = 4 × -sin(θ)
    Y = cos(θ)
    	
  • Try with any other angles for P(0, 4) and different Y values with X at zero , you will find the same equations, with 4 × for Y, but it's not alterate the final equation:

    尝试使用P(0,4)的任何其他角度以及X值 为零的 不同Y值 ,您会发现相同的方程式,其中 Y的角度为 4×,但不会改变最终方程式:

    X = 4 × -sin(θ) = Y × -sin(θ)
    Y = 4 ×  cos(θ) = Y ×  cos(θ)
    	
  • Now let's summarize:

    现在让我们总结一下:

     _____ ________________ ________________
    |     |                |                |
    |     |     P(X, 0)    |     P(0, Y)    |
    |_____|________________|________________|
    |     |                |                |
    |  θ  | X = X × cos(θ) | X = Y × -sin(θ)|
    |     | Y = X × sin(θ) | Y = Y ×  cos(θ)|
    |_____|________________|________________|
    
    	

    As we can see, different XY combinations generate different equations, so let's work on X and Y set above 0:

    如我们所见,不同的XY组合生成不同的方程式,因此让我们对设置为0以上的X和Y进行操作:

  • Hence we set the red point at P(2, 4×√3/2):

    因此,我们将红点设置为P(2,4×√3/ 2):

    Image 8
  • Now we rotate this point of +30°, visually the result to find is P(0, 4):

    现在我们将这个点旋转+ 30°,在视觉上找到的结果是P(0,4):

    Image 9
    X = sin(θ) = 0.5  ❌
    X = cos(θ) = √3/2 ❌
    
    Y = sin(θ) = 0.5  ❌
    Y = cos(θ) = √3/2 ❌
    
    	

    We have a problem, none of the trigonometric functions work, so we need to find the solution away while still working with sin() and cos().

    我们有一个问题,三角函数都不起作用,因此我们需要在仍然使用sin()cos()找到解决方案。

    Let's review the previous equations found:

    让我们回顾一下之前找到的等式:

     _____ ________________ ________________
    |     |                |                |
    |     |     P(X, 0)    |     P(0, Y)    |
    |_____|________________|________________|
    |     |                |                |
    |  θ  | X = X × cos(θ) | X = Y × -sin(θ)|
    |     | Y = X × sin(θ) | Y = Y ×  cos(θ)|
    |_____|________________|________________|
    
    	

    These are strange equations that we have, what if we have P(X, Y), it should be a mix of the both equations:

    这些是我们有的奇怪方程,如果我们有P(X,Y),应该将这两个方程混合在一起:

    X' = X × cos(θ) X = Y × -sin(θ)
    Y' = X × sin(θ) Y = Y ×  cos(θ)
    
    X' = X × cos(θ)     Y × -sin(θ)
    Y' = X × sin(θ)     Y ×  cos(θ)
    
    	

    Let's see if an addition is working:

    让我们看看添加是否有效:

    X' = X × cos(θ) + Y × -sin(θ)
         X × cos(θ) - Y ×  sin(θ)
    Y' = X × sin(θ) + Y ×  cos(θ)
    
    X = 2 × cos(θ) - 4×√3/2 × sin(θ) = 0 ✔
    Y = 2 × sin(θ) + 4×√3/2 × cos(θ) = 4 ✔
    
    	

    Perfect, this equation is working, so at the end, the final equations on a XY plane is:

    完美,此方程式有效,因此最后,XY平面上的最终方程式为:

    X' = X × cos(θ) - Y × sin(θ)
    Y' = X × sin(θ) + Y × cos(θ)
    
    	

    But let's remove the doubt about P(X, 0) and P(0, Y):

    但是,让我们消除对P(X,0)和P(0,Y)的疑问:

  • For P(4, 0), we apply a rotation of +90°, visually the result to find is P(0, 4):

    对于P(4,0),我们应用+ 90°旋转,在视觉上找到的结果为P(0,4):

    X' = 4 × cos(θ) - 0 ×  sin(θ) = 0 ✔
    Y' = 4 × sin(θ) + 0 ×  cos(θ) = 4 ✔
    	
  • For P(0, 4), we apply a rotation of +90°, visually the result to find is P(-4, 0):

    对于P(0,4),我们应用+ 90°旋转,在视觉上找到的结果是P(-4,0):

    X' = 0 × cos(θ) - 4 ×  sin(θ) = -4 ✔
    Y' = 0 × sin(θ) + 4 ×  cos(θ) =  0 ✔
    
    	

    Good, all is working, you can test this equation with your own XY and angle values to verifiy if it's working.

    很好,一切正常,您可以使用自己的XY和角度值测试此方程式,以验证其是否正常运行。

    This equation is also surprisingly (as said earlier) just a linear form of the Z rotation (XZ plane rotation matrix) matrix, multiplied by the point called vector, where Z is the center of the both axis)

    该方程式还令人惊讶地(如前所述)只是Z旋转的线性形式(XZ平面旋转矩阵) 矩阵乘以称为vector的点,其中Z是两个axis的中心)

    Rotation matrix on z:                                 Vector:
     _______________ _______________ _______________       _______________
    |               |               |               |     |               |
    |    cos(θz)    |   -sin(θz)    |       0       |     |       x       |
    |_______________|_______________|_______________|     |_______________|
    |               |               |               |     |               |
    |    sin(θz)    |    cos(θz)    |       0       |  ×  |       y       |
    |_______________|_______________|_______________|     |_______________|
    |               |               |               |     |               |
    |       0       |       0       |       1       |     |       z       |
    |_______________|_______________|_______________|     |_______________|
    
    	

    And a 3D space is 2D space with another axis called Z and at 90° of the XY plan.

    3D space是2D space其中另一个轴称为Z且与XY平面成90°

    The moves possible are translation (addition equation) and rotation (sin and cos).

    可能的移动是translation (加法方程)和rotation ( sincos )。

    But now rotations are in number of 3, around Z, around X and around Y.

    但是现在rotations数为3,围绕Z ,围绕X和围绕Y

    Because as we said before, a rotation is made on a 2D plane, but now we have a mix of 3x 2D space/plane:

    因为如前所述,旋转是在2D平面上进行的,但是现在我们混合使用3x 2D space/plane

  • X/Y, Y/Z and Z/X.

    X/YY/ZZ/X

    So we need to gather all the rotation matrices and use them.

    因此,我们需要收集所有rotation矩阵并使用它们。

X旋转矩阵(XY平面旋转矩阵) (X Rotation Matrix (XY Plane Rotation Matrix))

 _______________ _______________ _______________
|               |               |               |
|       1       |       0       |       0       |
|_______________|_______________|_______________|
|               |               |               |
|       0       |    cos(θx)    |   -sin(θx)    |
|_______________|_______________|_______________|
|               |               |               |
|       0       |    sin(θx)    |    cos(θx)    |
|_______________|_______________|_______________|

Y旋转矩阵(YZ平面旋转矩阵) (Y Rotation Matrix (YZ Plane Rotation Matrix))

 _______________ _______________ _______________
|               |               |               |
|    cos(θy)    |       0       |   -sin(θy)    |
|_______________|_______________|_______________|
|               |               |               |
|       0       |       1       |       0       |
|_______________|_______________|_______________|
|               |               |               |
|    sin(θy)    |       0       |    cos(θy)    |
|_______________|_______________|_______________|

Z旋转矩阵(XZ平面旋转矩阵) (Z Rotation Matrix (XZ Plane Rotation Matrix))

 _______________ _______________ _______________
|               |               |               |
|    cos(θz)    |   -sin(θz)    |       0       |
|_______________|_______________|_______________|
|               |               |               |
|    sin(θz)    |    cos(θz)    |       0       |
|_______________|_______________|_______________|
|               |               |               |
|       0       |       0       |       1       |
|_______________|_______________|_______________|

But we have a problem, if we are doing that, we will not be able to rotate an object around XYZ axis together, it's one rotation on one axis only with other angles set as "zero" because they are not present in the matrix chosen.

但是我们有一个问题,如果这样做,我们将无法一起围绕XYZ axis旋转对象,因为仅在其他角度设为“零”的情况下,它只能在一个axis rotation一次,因为它们不在所选矩阵中。

To fix that, we need to form 1 single matrix by multiplying XYZ matrices together (mul's order matter).

为了解决这个问题,我们需要通过将XYZ矩阵相乘形成1个单个矩阵(mul的顺序问题)。

XYZ旋转矩阵(XY YZ XZ平面旋转矩阵) (XYZ Rotation Matrix (XY YZ XZ Planes Rotation Matrix))

 ______________________________ ______________________________ ____________________
|                              |                              |                    |
|      cos(θy) × cos(θz)       |      cos(θy) × -sin(θz)      |      -sin(θy)      |
|______________________________|______________________________|____________________|
|                              |                              |                    |
| -sin(θx) × sin(θy) × cos(θz) | sin(θx) × sin(θy) × sin(θz)  | -sin(θx) × cos(θy) |
|    + cos(θx) × sin(θz)       |     + cos(θx) × cos(θz)      |                    |
|______________________________|______________________________|____________________|
|                              |                              |                    |
| cos(θx) × sin(θy) × cos(θz)  | cos(θx) × sin(θy) × -sin(θz) | cos(θx) × cos(θy)  |
|    + sin(θx) × sin(θz)       |    + sin(θx) × cos(θz)       |                    |
|______________________________|______________________________|____________________|

Now, we just need to multiply this rotation matrix (based on object's angles) to the vector point of the object to be able to rotate the object around XYZ axes.

现在,我们只需要将此rotation matrix (基于对象的角度)乘以对象的vector点即可围绕XYZ axes rotate对象。

Obj Vec:    Rotation Matrix:
 _____     ______________________________ ______________________________ ____________________
|     |   |                              |                              |                    |
|  x  |   |      cos(θy) × cos(θz)       |      cos(θy) × -sin(θz)      |      -sin(θy)      |
|_____|   |______________________________|______________________________|____________________|
|     |   |                              |                              |                    |
|  y  | × | -sin(θx) × sin(θy) × cos(θz) | sin(θx) × sin(θy) × sin(θz)  | -sin(θx) × cos(θy) |
|     |   |    + cos(θx) × sin(θz)       |     + cos(θx) × cos(θz)      |                    |
|_____|   |______________________________|______________________________|____________________|
|     |   |                              |                              |                    |
|  z  |   | cos(θx) × sin(θy) × cos(θz)  | cos(θx) × sin(θy) × -sin(θz) | cos(θx) × cos(θy)  |
|     |   |    + sin(θx) × sin(θz)       |    + sin(θx) × cos(θz)       |                    |
|_____|   |______________________________|______________________________|____________________|

Now it's almost finished, we need to create a camera to be able to navigate into the scene.

现在快要完成了,我们需要创建一个摄像机以能够导航到场景中。

The camera is defined by 3 vectors: Forward, Right and Up:

摄像机由3个vectors定义: ForwardRightUp

                  X  Y  Z
Right/Left       (1, 0, 0)
Up/Down          (0, 1, 0)
Forward/Backward (0, 0,-1)

Then, we multiply these 3 vectors to a rotation matrix (based on camera's angles) separately because we need each of them to be able to move the camera Forward/Backward/Right/Left/Up/Down.

然后,将这三个vectors乘以一个rotation matrix (基于摄像机的角度),因为我们需要它们中的每一个都能够使摄像机Forward/Backward/Right/Left/Up/Down

And, adding the result to the object vector.

并且,将结果添加到对象向量。

Example: If press W,  Forward vector is used                  If press S, -Forward vector is used                  If press D,   Right   vector is used                  ...

示例 :如果按W,则使用Forward vector如果按S, - Forward vector ,如果按D, Right   vector Right   vector Right   vector 使用 ...

Object Vector:           Forward Vector:    
 __________________       __________________
|                  |     |                  |
|         x        |     |         x        |
|__________________|     |__________________|
|                  |     |                  |
|         y        |  +  |         y        |
|__________________|     |__________________|
|                  |     |                  |
|         z        |     |         z        |
|__________________|     |__________________|

Object Vector:           Forward Vector:         
 __________________       __________________
|                  |     |                  |
|         x        |     |         x        |
|__________________|     |__________________|
|                  |     |                  |
|         y        |  -  |         y        |
|__________________|     |__________________|
|                  |     |                  |
|         z        |     |         z        |
|__________________|     |__________________|

Object Vector:           Right Vector:
 __________________       __________________
|                  |     |                  |
|         x        |     |         x        |
|__________________|     |__________________|
|                  |     |                  |
|         y        |  +  |         y        |
|__________________|     |__________________|
|                  |     |                  |
|         z        |     |         z        |
|__________________|     |__________________|

Finally, we multiply another rotation matrix (based on camera's angles) with each vector point from the object to be able to move the object depending on the camera angle (modified with the mouse movement).

最后,我们将另一个rotation matrix (基于相机的角度)乘以对象的每个vector点,从而能够根据相机的角度(随鼠标移动而修改)移动对象。

That's all for now, I hope I will give updates often because this code is not perfect, it has some bugs and does not have all the features of 3D math.

到此为止,我希望我会经常进行更新,因为该代码并不完美,存在一些错误并且不具备3D数学的所有功能。

翻译自: https://www.codeproject.com/Articles/1247960/Learning-Basic-Math-Used-In-3D-Graphics-Engines

3d数学基础图形 教程

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值