gluOrtho2D glFrustum

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测试)。

推导为:
z' = az + b
0 = azn + b
1= azf + b
其中这里zn,zf是距离也是左手坐标系的坐标,且不需要除以z,直接求取即可。

D3DXMatrix mOrtho
D3DXMatrixOrthoLH(&mOrtho, WINDOW_WIDTH, WINDOW_HEIGHT, 0.1f, 1000.0f);

g_pd3dDevice->SetTransform(D3DTS_PROJECTION,&mOrtho); // 设置变换矩阵和状态,没有真正开始变换,提交后交给图形硬件实现变换

1.镜头平移:改变cameraPos用来计算观察位置

2.镜头拉近放大物体对象:

1).Z深度值被丢弃了,可以通过放大屏幕投影来变大,那么需要放大正交投影的值,因为正交投影后2/w2/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

leftright

Specify the coordinates for the left and right vertical clipping planes.

bottomtop

Specify the coordinates for the bottom and top horizontal clipping planes.

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

leftright

Specify the coordinates for the left and right vertical clipping planes.

bottomtop

Specify the coordinates for the bottom and top horizontal clipping planes.

nearfar

Specify the distances to the near and far depth clipping planes. Both distances must be positive.

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:

2 ⁢ near right - left 0 A 0 0 2 ⁢ near top - bottom B 0 0 0 C D 0 0 -1 0
A = right + left right - left
B = top + bottom top - bottom
C = - far + near far - near
D = - 2 ⁢ far ⁢ near far - near

Typically, the matrix mode is GL_PROJECTION, and (leftbottom, -near) and (righttop, -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

r = farnear

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.

你可能感兴趣的:(gluOrtho2D glFrustum)