OpenGL超级宝典学习笔记——Using OpenGL(二)
OpenGL超级宝典学习笔记——Using OpenGL(二)
OpenGL数据类型
Technorati 标记: opengl
函数命名约定
<函数库前缀><命令名称><可选的参数个数><可选的参数类型>
例子:
glColor3f 代表是gl函数库里的 color命令,此命令有3个参数,参数类型是f
平台独立性
openGL没有创建窗口,处理按键输入,鼠标点击事件等函数。创建和管理窗口是操作系统本身应支持的功能。OpenGL是平台独立的图形硬件的抽象接口库。
使用OpenGL画图
1: #include
2:
3: #include
4:
5: static void RenderScenceCB()
6: {
7: glClear( GL_COLOR_BUFFER_BIT );
8:
9: glColor3f( 0.0f, 1.0f, 0.0f );
10:
11: glRectf( -25.0f, 25.0f, 25.0f, -25.0f );
12:
13: glFlush();
14: }
15:
16:
17: static void CreateWindow()
18: {
19: glutInitWindowPosition( 200, 200 );
20:
21: glutInitWindowSize( 200, 200 );
22:
23: glutCreateWindow( "opengl step 1" );
24: }
25:
26:
27: static void ChangeSize( GLsizei w, GLsizei h )
28:
29: {
30: /* 设置视口 */
31:
32: glViewport( 0, 0, w, h );
33:
34: glMatrixMode( GL_PROJECTION );
35:
36: glLoadIdentity();
37:
38: if ( h == 0 )
39:
40: {
41: h = 1;
42: }
43:
44: GLfloat aspectRatio = (GLfloat) w / (GLfloat) h;
45:
46: if ( w <= h )
47:
48: {
49: glOrtho( -100.0f, 100.0f, -100.0f / aspectRatio, 100.0f / aspectRatio, 1.0, -1.0 );
50: }else {
51: glOrtho( -100.0f * aspectRatio, 100.0f * aspectRatio, -100.0f, 100.0f, 1.0, -1.0 );
52: }
53:
54: glMatrixMode( GL_MODELVIEW );
55:
56: glLoadIdentity();
57: }
58:
59:
60: int main( int args, char *argc[] )
61: {
62: glutInit( &args, argc );
63:
64: glutInitDisplayMode( GLUT_SINGLE | GLUT_RGBA );
65:
66: CreateWindow();
67:
68: glutDisplayFunc( RenderScenceCB );
69:
70: glutReshapeFunc( ChangeSize );
71:
72: glClearColor( 1.0f, 0.0f, 0.0f, 0.0f );
73:
74: glutMainLoop();
75:
76: return(0);
77: }
78:
79:
80:
<br /></b></p>
画一个矩形
设置当前使用的颜色
glColor3f(0.0f, 1.0f, 0.0f);
画一个矩形,并用当前颜色填充
glRectf(-25.0f, 25.0f, 25.0f, -25.0f);
这两个函数的使用方式参考,官方函数文档
http://www.opengl.org/sdk/docs/man2/xhtml/glColor.xml
http://www.opengl.org/sdk/docs/man2/xhtml/glRect.xml
openGL是如何把这些坐标映射到真是的窗体的位置的。这个就是使用了ChangeSize这个回调函数。
通过glutReshapeFunc设置这个回调函数,当窗体的大小发生改变时,就会调用你设置的回调函数。
每次窗体大小的改变,我们都应该重新定义视口和裁剪区域以适应新的窗口尺寸。我们希望绘制的图像能够适应窗体的大小。
设置视口(ViewPort)和裁剪区域(Clipping volume)
视口和裁剪区域是如何影响坐标系范围,以及2D和3D的图像在屏幕上显示的。如下图:
定义视口
void glViewPort(GLint x, GLint y, GLint width, GLint height);
参数x,y制定了左下角的坐标。width和height参数制定了尺寸以像素为单位。
定义裁剪区域
void glOrtho(
GLdouble
left,
GLdouble
right,
GLdouble
bottom,
GLdouble
top,
GLdouble
nearVal,
GLdouble
farVal)
;
Parameters
left
,right
-
Specify the coordinates for the left and right vertical clipping planes.
bottom
,top
-
Specify the coordinates for the bottom and top horizontal clipping planes.
nearVal
,farVal
-
Specify the distances to the nearer and farther depth clipping planes. These values are negative if the plane is to be behind the viewer.
这个函数重新定义了裁剪区域,使得纵横比不变。纵横比就是在垂直方向上的一个单位长度内的像素个数与水平方向上一个单位长度内的像素个数的比值。上面的例子使用的是正投影。
在调用glOrtho之前,要注意调用下面两个函数
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
投影矩阵是定义可视区域的地方。glOrtho并不建立新的裁剪区域,而是对之前的裁剪区域进行修改。它把当前裁剪区域的矩阵与参数所提供的矩阵相乘,得到新的裁剪区域。glLoadIdentity();就是对当前的坐标系系统进行重置。
下面的代码可以使得正方形保持正方形
if
(w <= h)
{
glOrtho(-100.0f, 100.0f, -100.0f/aspectRatio, 100.0f/aspectRatio, 1.0, -1.0);
}
else
{
glOrtho(-100.0f*aspectRatio, 100.0f*aspectRatio, -100.0f, 100.0f, 1.0, -1.0);
}
这个函数调用使得裁剪区域 左边总是位于-100,右边则扩展到100除非窗口的宽度大于高度,此时水平范围则会根据纵横比进行缩放。同理当高度大于宽度时,上下要进行缩放。这样就可以保持一个200X200的正方形(中心为(0,0))与窗口的形状无关。如下图:
OpenGL超级宝典 第4版 中文版PDF+英文版+源代码 见
OpenGL编程指南(原书第7版)中文扫描版PDF 下载
OpenGL 渲染篇
Ubuntu 13.04 安装 OpenGL
OpenGL三维球体数据生成与绘制【附源码】
Ubuntu下OpenGL编程基础解析
如何在Ubuntu使用eclipse for c++配置OpenGL
更多《OpenGL超级宝典学习笔记》相关知识 见
评论暂时关闭