Ch2-6-OpenGL图形软件包

Download Report

Transcript Ch2-6-OpenGL图形软件包

YTU
2.6 OpenGL图形软件包

What Is OpenGL?
– Open Graphics Library
– SGI 公司开发
– Graphics Rendering API

[程序2-1] OpenGL 绘制矩形的简单例子
YTU
OpenGL

Portable
– window system independent
– operating system independent



Interactive
2D and 3D
Not Object-Oriented
YTU
2.6.1 OpenGL 的主要功能

模型绘制

位图和图像处理

模型观察

纹理映射

颜色模式

实时动画

光照应用

交互技术

图像效果增强
YTU
2.6.2 OpenGL 的绘制流程
应用软件
OpenGL
窗口系统
操作系统
图形硬件
图2.37 OpenGL图形处
理系统在计算机系统
中的层次结构
YTU
流水线
b
+
*
c
a+(b*c)
1+(1*1)
2+(2*2)
3+(3*3)
流水线(Pipeline) 举例
YTU
OpenGL 的两条流水线
变换
Transform
投影
Project
裁减
Clip
顶点信息
帧缓存
Frame Buffer
OpenGL
应用程序
像素信息
像素操作
Pixel Ops
YTU
调用OpenGL
API函数
OpenGL命
令缓冲区
变换、光照
光栅化
图2.35 一条简化的OpenGL绘制流水线
帧缓存
YTU
OpenGL Geometric Architecture
顶点 Vertices
变换(Transformer)
裁减(Clipper)
投影(projector)
光栅化(Rasterizer)
像素Pixels
YTU
2.6.3 OpenGL 的基本语法
1.
2.
3.
相关库
命名规则
数据类型
YTU
1. 相关库 Related APIs
GLX - X Windows system
AGL - Apple Macintosh
WGL - Microsoft Windows
YTU

GLU (OpenGL Utility Library)
– part of OpenGL
– NURBS, quadric shapes, etc.

GLUT (OpenGL Utility Toolkit)
– portable windowing API
– not officially part of OpenGL

AGL, GLX, WGL
– glue between OpenGL and windowing
systems
YTU
2.6.4 一个完整的OpenGl程序
1.
2.
3.
头文件包含
使用 GLUT 库进行窗口管理
利用OpenGL进行绘图
YTU
1. 头文件包含

#include <GL/gl.h>
#include <GL/glu.h>

#include <GL/glut.h>
glut.h includes gl.h and glu.h
YTU
2. 使用 GLUT 库进行窗口管理

初始化(glutInit )

创建窗口(glutCreateWindow)

设定窗口的显示模式(glutInitDisplayMode)

指定窗口的位置和大小(glutInitWindowPosition
和 glutInitWindowSize )

指定窗口的显示内容函数(glutDisplayFunc)

运行框架(glutMainLoop)
void main( int argc, char** argv )
GLUT 基础
{ //设置参数
int mode = GLUT_RGB | GLUT_DOUBLE;
glutInitDisplayMode( mode );
//打开窗口
glutCreateWindow( argv[0] );
// 初始化 OpenGL 状态
init( );
//注册回调函数 (callback functions)
glutDisplayFunc( display );
glutReshapeFunc( resize );
glutKeyboardFunc( key );
//进入事件处理循环
glutMainLoop( );
Sample
}
Program
YTU
3. 利用OpenGL进行绘图

glColor3f(1.0, 0.0, 0.0)
– 三个分量代表RGB
– 每个颜色分量的取值范围0-1
– 设置绘图色为红色

glClearColor(1.0, 1.0, 1.0, 1.0)
– 四个分量代表RGBA, A为透明度
– 设置背景色为白色
YTU
OpenGL 按照状态机的方式工作 State machine

OpenGL 可以做两件事
– draw something
– change the state of how OpenGL draws

所有的绘制属性被封装在 OpenGL 状态中
– rendering styles
– shading
– lighting
– texture mapping
YTU
相关函数

设置状态
– glPointSize( size );
– glLineStipple( repeat, pattern );
– glShadeModel( GL_ SMOOTH );

打开或关闭某种属性
– glEnable( GL_ LIGHTING );
– glDisable( GL_TEXTURE_2D );
YTU
绘制图形所需其他函数

刷新窗口的缓冲区( glClear)
glClear(GL_COLOR_BUFFER_BIT)

设定投影参数
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0.0,200.0,0.0,150.0);

绘制矩形
glRectf(50.0f, 100.0f, 150.0f, 50.0f);
YTU
观察 Viewing
YTU
二维观察 Two Dimensional Viewing
观察窗 Viewing Rectangle
裁减窗 Clipping Rectangle
YTU
三维观察空间

Three-Dimensional Viewing Volume
right, top, far
OpenGL’s default:
a 2*2*2 cube
y
x
Z
Left, bottom, near
YTU
Camera Analogy
YTU
OpenGL’s default: 2X2X2
YTU
正投影 Orthographic Projection
Void glOrtho
( GLdouble
GLdouble
GLdouble
GLdouble
GLdouble
GLdouble
)
left,
right,
bottom,
top,
near,
far
YTU
二维图形的正投影
Void gluOrtho2D
( GLdouble left,
GLdouble right,
GLdouble bottom,
GLdouble top
)
 Example :
gluOrtho2D (0.0, 500.0, 0.0, 500.0)
YTU
视口 Viewport
显示器
YTU
显示器
YTU
显示器
YTU
Viewport
Void glViewport
(
GLint x,
GLint y,
GLint w,
GLint h
)
YTU
Matrix Modes
glMatrixMode(GL_PROJECTION)
glLoadIdentity();
gluOrtho2D(0.0, 500.0, 0.0, 500.0);
glMatrixMode(GL_MODELVIEW)
YTU
OpenGL 提供的绘图原语
YTU
OpenGL 提供两类原语

几何原语 Geometric Primitives
定义点 、线、多边形、曲线、曲面
 在几何流水线上进行处理


光栅原语 Raster Primitives
– 定义像素信息
– 在光栅流水线上进行处理
YTU
几何原语的定义
glBegin(……);
……
……
glEnd( );
YTU
直线 GL_LINES
glBegin(GL_LINES);
glVertex2f(x1, y1);
glVertex2f(x2, y2);
glEnd( );
YTU
点 GL_POINTS
glBegin(GL_POINTS);
glVertex2f(x1, y1);
glVertex2f(x2, y2);
glEnd( );
YTU
顶点坐标 glVertex*



二维坐标
glVertex2i (GLint xi, GLint yi)
三维坐标
glVertex3f (GLfloat x, GLfloat y, GLfloat z )
用数组(向量)指定顶点坐标
GLfloat vertex[3]
glVertex3fv(vertex)
YTU
OpenGL Command Formats
YTU
OpenGL 几何原语 Geometric Primitives




GL_POINTS
GL_LINES
GL_LINE_STRIP
GL_LINE_LOOP
YTU
GL_POINTS
P2
P1
P3
P4
P0
P5
YTU
GL_LINES
P2
P1
P3
P4
P0
P5
YTU
GL_LINE_STRIP
P2
P1
P3
P4
P0
P5
YTU
GL_LINE_LOOP
P2
P1
P3
P4
P0
P5
YTU
Polygon Types in OpenGL






GL_POLYGON
GL_TRIANGLES
GL_QUADS
GL_TRIANGLE_STRIP
GL_QUAD_STRIP
GL_TRIANGLE_FAN
YTU
GL_POLYGON
P2
P1
P3
P4
P0
P5
YTU
GL_TRIANGLES
P2
P1
P3
P4
P0
P5
YTU
GL_QUADS
P2
P1
P3
P4
P0
P5
YTU
Geometric Primitives
YTU
Example 1
void drawRhombus( GLfloat color[])
{
glBegin( GL_QUADS );
glVertex2f( 0.0, 0.0 );
glVertex2f( 1.0, 0.0 );
glVertex2f( 1.5, 1.118 );
glVertex2f( 0.5, 1.118 );
glEnd();
}
YTU
Example 2
GLfloat red, greed, blue;
Glfloat coords[3];
glBegin( GL_QUADS );
for ( i = 0; i < nVerts; ++i )
{
glColor3f( red, green, blue );
glVertex3fv( coords );
}
glEnd();
YTU
程序演示 shapes