OpenGl 游戏编程笔记 第四章: 变换和矩阵(一)
OpenGl 游戏编程笔记 第七章:纹理映射 (texture mapping) (一)

OpenGl 游戏编程笔记 第四章: 变换和矩阵(二)

皮贝贝 posted @ 2010年4月08日 06:24 in opengl with tags OpenGL game 游戏 , 4690 阅读

OpenGl 游戏编程笔记 第四章: 变换和矩阵(二)

1 投影

设置一个投影变换, 实际上创建了一个视景体, 主要有两个目的. 首先, 定义了裁剪面, 即决定了 那些可见, 在观察范围以外的不被变换和渲染. 其次, 决定了物体怎样被渲染绘出, 这主要取决于观察角度 的范围, 依立体投影和正交投影而不同. 指定了一种投影变换, 你需要保证当前选择的矩阵栈为投影矩阵. 主要是通过 glMatrixMode() 来设置:

 

glMatrixMode(GL_PROJECTION);

通常其后跟着一句 glLoadIdentity() 的调用来清除在投影矩阵中的残留信息.

1.1 正交投影 (Orthographic)

如前所述, 正交投影中, 物体无论离相机多远, 投影后尺寸大小是相同的. 尽管不如立体投影真实感, 但是依然用处颇广, 如一些 CAD 这类的应用程序, 或2D 的游戏.

OpenGL 中利用 glOrtho() 函数来设置正交投影:


glOrtho(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble near, GLdouble far);

left 和 right 指定了在 x 轴的裁剪量, bottom 和 top 指定了在 y 轴的裁剪量, near 和 far 指定了在 z 轴的裁剪量. 合起来, 这些参数制定了一个立体的观看量.

因为正交投影通常用在 2D 游戏中, 所以OpenGL 的 Utility 库提供了另外的一个变体, 来为你不用z轴的场景来设置正交投影:


gluOrth2D(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top);

效果同 glOrtho() 的参数 near为-1.0, far 为1.0一样. 使用 gluOrgh2D() 的另一个好处是可以使用 glVertex() 的两参数版本(因为 z 轴坐标不需要了).

1.2 立体投影 (Perspective)

立体投影如人们平时所见的那样, 物体离你越远就看起来越小.


void glFrustum(GLdouble left, GLdouble right, GLdouble bottom, DLdouble top, GLdouble near, GLdouble far)
它创建一个透视视景体。其操作是创建一个透视投影矩阵,并且用这个矩阵乘以当前矩阵。这个函数的参数只定义近裁剪平面的左下角点和右上角点的三维空间坐标,即(left,bottom,-near)和(right,top,-near);最后一个参数far是远裁剪平面的Z负值,其左下角点和右上角点空间坐标由函数根据透视投影原理自动生成。near和far表示离视点的远近,它们总为正值。

  另一个函数是:


void gluPerspective(GLdouble fovy,GLdouble aspect,GLdouble zNear, GLdouble zFar);

  它也创建一个对称透视视景体,但它的参数定义于前面的不同,如图8-11所示。其操作是创建一个对称的透视投影矩阵,并且用这个矩阵乘以当前矩阵。参数fovy定义视野在X-Z平面的角度,范围是[0.0, 180.0];参数aspect是投影平面宽度与高度的比率;参数zNear和Far分别是远近裁剪面沿Z负轴到视点的距离,它们总为正值。

1.3 设置视口

视口变换在投影变换之后发生.

视口变换就是将视景体内的投影的物体显示到 2D 的窗口中去. 主要通过 :


void glViewport (GLint x, GLint y, GLsizei width, GLsizei height);

其中 x 和 y 指定了视口的左下角坐标, width 和 height 指定了窗口的像素宽度和高度.

当一个 渲染上下文rc 第一次创建并关联到你的窗口上时, 视口会自动的调整到与你的窗口匹配的尺寸. 这在某些情况下已经足够了. 但是我们希望在调整窗口大小的同时更新视口. 虽然视口会匹配你的窗口大小, 但不一定和窗口的大小相同. 如我们想限制渲染到窗口中的一小段区域内, 通常的做法是设置一个小一点的视口.


 

Date: 2010-04-07 22:15:07

HTML generated by org-mode 6.30c in emacs 23

 

Avatar_small
JDC Result Sylhet 说:
2022年8月28日 16:36

Bangladesh Sylhet Division Secondary and Higher Secondary Education Board also successfully completed the Junior School Certificate and Junior Dakhil Certificate of Grade 8th standard annual terminal examination tests on November 2022 at all selected examination centers of the division, according to the reports lacks students have appeared from all districts under the Division and they all are waiting to check JSC Result 2022 Sylhet Board. Department of Secondary Education, JDC Result Sylhet Sylhet Division also conducted an evaluation process to the valuation of subject wise marks through answer sheet correction and the process of evaluation will be complete in before 2nd week of December 2022 and the education board will update answer scripts with student wise mark sheets in subject wise with total GPA grade points to Secondary and Higher Secondary Education Board Bangladesh.

Avatar_small
pavzi.com 说:
2024年1月23日 14:39

Pavzi.com is a startup by passionate webmasters and bloggers who have a passion for providing engaging content that is accurate, interesting, and worthy to read. pavzi.com We are more like a web community where you can find different information, resources, and topics on day-to-day incidents or news. We provide you with the finest web content on every topic possible with the help of the editorial and content team.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter