D3DXMATRIX* D3DXMatrixOrthoLH(
_Inout_ D3DXMATRIX *pOut, // 输出的变幻矩阵
_In_ FLOAT w, // 屏幕的宽度
_In_ FLOAT h, // 屏幕的高度
_In_ FLOAT zn, // z深度缓存最小值
_In_ FLOAT zf // z深度缓存最大值
);
得到的变换矩阵为:
2/w 0 0 0
0 2/h 0 0
0 0 1/(zf-zn) 0
0 0 zn/(zn-zf) 1
2D正交变换将摄像机坐标空间里面的点变换到设备规范化坐标空间中
2/w,2/h对摄像机空间内的x,y进行正交投影,进行了一定的缩放,当w,h越大那么物体显示越小,设置w,h越小那么物体显示越大,设备规范化坐标系内[-1,1]容纳的像素就越多,变换到屏幕坐标系中就感觉远离缩小了。z轴空间上是对z进行了线性插值,使得z值在[0,1]空间内,zn时候为0,zf时候为1(深度缓存用于遮挡剔除深度测试,和stencil测试)。
推导为:D3DXMatrix mOrtho
D3DXMatrixOrthoLH(&mOrtho, WINDOW_WIDTH, WINDOW_HEIGHT, 0.1f, 1000.0f);
g_pd3dDevice->SetTransform(D3DTS_PROJECTION,&mOrtho); // 设置变换矩阵和状态,没有真正开始变换,提交后交给图形硬件实现变换
1.镜头平移:改变cameraPos用来计算观察位置
2.镜头拉近放大物体对象:
1).Z深度值被丢弃了,可以通过放大屏幕投影来变大,那么需要放大正交投影的值,因为正交投影后2/w、2/h,所以可以通过缩小正交投影传入的w、h来实现放大;放大传入的w、h可以实现模拟的远离。
3.镜头拉高拉低:
1)cameraPos,y值变大来拉高;通过放大传入的w、h可以实现模拟的远离缩小。gluOrtho2D — define a 2D orthographic projection matrix
C Specification
void gluOrtho2D( | GLdouble left, |
|
| GLdouble right, |
|
| GLdouble bottom, |
|
| GLdouble top); |
Parameters
-
Specify the coordinates for the left and right vertical clipping planes.
-
Specify the coordinates for the bottom and top horizontal clipping planes.
left,
right
bottom,
top
Description
gluOrtho2D sets up a two-dimensional orthographic viewing region. This is equivalent to calling glOrtho with near = -1 and far = 1 .
glFrustum
Name
glFrustum — multiply the current matrix by a perspective matrix
C Specification
void glFrustumf( | GLfloat left, |
|
| GLfloat right, |
|
| GLfloat bottom, |
|
| GLfloat top, |
|
| GLfloat near, |
|
| GLfloat far); |
void glFrustumx( | GLfixed left, |
|
| GLfixed right, |
|
| GLfixed bottom, |
|
| GLfixed top, |
|
| GLfixed near, |
|
| GLfixed far); |
Parameters
-
Specify the coordinates for the left and right vertical clipping planes.
-
Specify the coordinates for the bottom and top horizontal clipping planes.
-
Specify the distances to the near and far depth clipping planes. Both distances must be positive.
left,
right
bottom,
top
near,
far
Description
glFrustum describes a perspective matrix that produces a perspective projection. The current matrix (see glMatrixMode) is multiplied by this matrix and the result replaces the current matrix, as if glMultMatrix were called with the following matrix as its argument:
Typically, the matrix mode is GL_PROJECTION, and (left, bottom, -near) and (right, top, -near) specify the points on the near clipping plane that are mapped to the lower left and upper right corners of the window, assuming that the eye is located at (0, 0, 0). -far specifies the location of the far clipping plane. Both near and far must be positive.
Use glPushMatrix and glPopMatrix to save and restore the current matrix stack.
Notes
Depth buffer precision is affected by the values specified for near and far. The greater the ratio of far to near is, the less effective the depth buffer will be at distinguishing between surfaces that are near each other. If
roughly log2(r) bits of depth buffer precision are lost. Because r approaches infinity as near approaches 0, near must never be set to 0.
Errors
GL_INVALID_VALUE is generated if near or far is not positive, or if left = right, or bottom = top, or near = far.
本文详细解析了2D正交投影变换原理及其在DirectX和OpenGL中的实现方式,包括D3DXMatrixOrthoLH与gluOrtho2D等函数的使用,并探讨了如何通过调整参数来实现镜头平移、拉近放大及拉高拉低的效果。
11万+

被折叠的 条评论
为什么被折叠?



