数学计算一个简单的图形流水线流水线、图形、数学、简单

2023-09-07 17:07:20 作者:干啥啥不行

我试图做/了解所有从像VRML三维场景的描述呈现一个简单的二维图像所需的图形管线的基本数学计算。是否有必要,像步骤一个很好的例子模式转型(目标坐标为世界坐标),查看转化(从世界坐标,查看坐标),计算顶点法线作为照明,剪切 计算的屏幕坐标的视锥内的对象和创建2D投影以计算各个像素颜色

I am trying to do / understand all the basic mathematical computations needed in the graphics pipeline to render a simple 2D image from a 3D scene description like VRML. Is there a good example of the steps needed, like model transformation (object coordinates to world coordinates), view transformation (from world coordinate to view coordinate), calculation of vertex normals for lighting, clipping, calculating the screen coordinates of objects inside the view frustum and creating the 2D projection to calculate the individual pixels with colors.

推荐答案

我习惯了OpenGL的风格呈现数学,所以我坚持下去(所有渲染使用几乎相同的数学)

I am used to OpenGL style render math so I stick to it (all the renders use almost the same math)

首先,一些温线来解释:

First some therms to explain:

1。变换矩阵

再$ P $在三维空间psents坐标系

Represents a coordinate system in 3D space

    double m[16]; // it is 4x4 matrix stored as 1 dimensional array for speed
    m[0]=xx; m[4]=yx; m[ 8]=zx; m[12]=x0;
    m[1]=xy; m[5]=yy; m[ 9]=zy; m[13]=y0;
    m[2]=xz; m[6]=yz; m[10]=zz; m[14]=z0;
    m[3]= 0; m[7]= 0; m[11]= 0; m[15]= 1;

其中:

X(XX,XY,XZ) X 轴GCS的单位矢量(全局坐标系) Y(YX,YY,YZ)轴GCS 单位矢量 Z(ZX,ZY,ZZ)以Z 轴的GCS单位矢量 P(X0,Y0,Z0)是重$ P $起源psented坐标系中GCS X(xx,xy,xz) is unit vector of X axis in GCS (global coordinate system) Y(yx,yy,yz) is unit vector of Y axis in GCS Z(zx,zy,zz) is unit vector of Z axis in GCS P(x0,y0,z0) is origin of represented coordinate system in GCS

变换矩阵被用于转化GCS和LCS之间坐标(局部坐标 系统)

Transformation matrix is used to transform coordinates between GCS and LCS (local coordinate system)

GCS - > LCS AL =银*米; GCS&LT; - LCS 银= A *(平方公尺-1); 铝(X,Y,Z,W = 1)是3D点LCS ......在齐次坐标 银(X,Y,Z,W = 1)是3D点在GCS ......在齐次坐标 齐次坐标 W = 1 添加,所以我们可以繁殖三维矢量由4x4矩阵 M 变换矩阵 平方公尺-1 逆变换矩阵 GCS -> LCS Al = Ag * m; GCS <- LCS Ag = Al * (m^-1); Al (x,y,z,w=1) is 3D point in LCS ... in homogenous coordinates Ag (x,y,z,w=1) is 3D point in GCS ... in homogenous coordinates homogenous coordinate w=1 is added so we can multiply 3D vector by 4x4 matrix m transformation matrix m^-1 inverse transformation matrix

在大多数情况下是 M 正交,这意味着:

In most cases is m orthonormal which means:

X,Y,Z 矢量互相垂直,并与单位大小 在此可用于恢复后的矩阵准确性旋转,平移等.. X,Y,Z vectors are perpendicular to each other and with unit size this can be used for restoration of matrix accuracy after rotations,translations,etc ...

2。渲染矩阵:

有通常使用这些矩阵:

There are usually used these matrices:

模式 - 重新presents实际呈现的物体坐标系

model - represents actual rendered object coordinate system

3。渲染数学:

要渲染的3D场景,你需要的2D渲染程序像绘制2D纹理三角... 渲染三维场景数据转换成2D并呈现它。 有更多的技术在那里,但最常用的就是 使用的边界模型重新presentation +界面渲染(表面只) 的3D - > 2D转换是通过投影完成(正交或立体)和Z缓冲器或Z分级 Z缓冲区是容易的,原产于现在天GFX HW Z-排序是由CPU完成的,而不是因此它的速度较慢,需要更多的内存,但它是必要的正确的透明表面渲染。

这样的管道是为这样的:

so the pipeline is as this:

1.obtain从模型中实际呈现的数据。

1.obtain actual rendered data from model

顶点 v 正常 N 纹理坐标 T 颜色,雾坐标,等等... Vertex v Normal n Texture coord t Color,Fog coord, etc...

2.convert它适当的空间

2.convert it to appropriate space

V =投影*鉴于*模型* V ...摄像机空间+投影 N =正常* N ...全球空间 T =质地* T ...纹理空间 v=projection*view*model*v ... camera space + projection n=normal*n ... global space t=texture*t ... texture space

3.clip数据到屏幕

3.clip data to screen

在这一步是没有必要的,但prevent呈现的画面东西速度 也面临剔除通常在这里完成 如果的渲染'三角'法向量相反则多边形缠绕规则设置,则忽略三角

4.render的3D / 2D数据

4.render the 3D/2D data

使用仅 VX,VY 坐标画面渲染和VZ用于z缓冲测试/值 在这里也变的角度划分为透视投影( VX / = VZ,VY / = VZ ) Z缓冲区的工作原理是这样的: Z缓冲(捷思)是二维数组大小相同的屏幕(SCR) 像素 SCR [Y] [X] 只渲染如果(捷思[Y] [X]&GT; = Z) 在这种情况下, SCR [Y] [X] =颜色; ZED [Y] [X] = Z; if条件可以是不同的(这是可变的) use only v.x,v.y coordinates for screen rendering and v.z for z-buffer test/value also here goes the perspective division for perspective projections (v.x/=v.z,vy/=v.z) z-buffer works like this: Z-buffer (zed) is 2D array with the same size as screen (scr) pixel scr[y][x] is rendered only if (zed[y][x]>=z) in that case scr[y][x]=color; zed[y][x]=z; the if condition can be different (it is changeable)

为了更清楚这里是它的样子:

For more clarity here is how it looks like:

[注意事项]

变换矩阵的乘法,所以如果你需要用M矩阵变换 N 点,你可以创建一个矩阵= M1 * M2 * ...毫米并转换 N 点这个导致基质仅供速度。 有时被用来 3×3 变换 4×4 矩阵的矩阵+移位向量来代替。它在某些情况下,速度更快,但你不能繁殖更多的变换,在一起这么容易 的变换矩阵操作看起来像旋转的基本操作或翻译 也有矩阵内的濒海战斗舰轮换这是更适合人体的控制输入但这些都不是原生渲染,比如OpenGL或DirectX。 (因为他们使用逆矩阵) Transformation matrices are multiplicative so if you need transform N points by M matrices you can create single matrix = m1*m2*...mM and convert N points by this resulting matrix only for speed. sometimes are used 3x3 transform matrix + shift vector instead of 4x4 matrix. it is faster in some cases but you cannot multiply more transformations together so easy for transformation matrix manipulation look for basic operations like Rotate or Translate there are also matrices for rotations inside LCS which are more suitable for human control input but these are not native to renders like OpenGL or DirectX. (because they use inverse matrix)