opengl学习——绘制图形
2016-06-01 15:38
369 查看
概要
QT5
需要加的模块
实例运行效果图
实例代码
部分函数接口的解释
glLoadIdentity
glBegin
glVertex3f
x y z
glViewport
0 0 GLintwidth GLintheight
glMatrixMode
glLoadIdentity
gluPerspectiveGLdouble
fovyGLdouble aspectGLdouble zNearGLdouble zFar
glFrustumGLdouble
leftGLdouble rightGLdouble bottomGLdouble topGLdouble zNearGLdouble zFar
主要使用方法
opengl是一个模单独的模块,所以在使用时,应该在pro里面添加对应的模块。
QT += opengl
.h文件
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
.cpp文件代码
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
当您调用glLoadIdentity()之后,您实际上将当前点移到了屏幕中心,X坐标轴从左至右,Y坐标轴从下至上,Z坐标轴从里至外。OpenGL屏幕中心的坐标值是X和Y轴上的0.0点。中心左面的坐标值是负值,右面是正值。移向屏幕顶端是正值,移向屏幕底端是负值。移入屏幕深处是负值,移出屏幕则是正值。
这里是绘制什么图形的接口,比如说绘制三角形可以用glBegin(GL_TRIANGLES), 而多边形使用
glBegin(GL_QUADS) 其它的点线等可以参考下面的。
2
3
4
5
6
7
8
9
10
这里需要说明的是在绘制的时候,需要以glBegin(…) 以 glEnd() 结束。
x,y, z 表示在一个三维空间里面的坐标。
重置当前的视口(Viewport)
选择矩阵模式, 主要用得多的是投影矩阵 和 模型观察矩阵
2
3
重置矩阵,这里主要功能是把坐标移动到原点,好进行后续的操作。
设置投影矩阵
设置投影矩阵, 需要讲一下的是, gluPerspective 和 glFrustum功能 是一样的, 准确的说, gluPerspective 是glFrustum的封装, 它们的关系我将在另一篇博客中进行讲解。而gluPerspective在一些平台或版本上找不到,这时就应该用glFrustum进行转换。
void paintGL();
void resizeGL(int width, int height);
这两个函数是父类的虚函数, 我们主要是通过重写这两个函数,来进行对应图片的绘制, 在绘制的过程中调用对应的接口,然后通过相应的坐标计算来实现最后的图片绘制
QT5
需要加的模块
实例运行效果图
实例代码
部分函数接口的解释
glLoadIdentity
glBegin
glVertex3f
x y z
glViewport
0 0 GLintwidth GLintheight
glMatrixMode
glLoadIdentity
gluPerspectiveGLdouble
fovyGLdouble aspectGLdouble zNearGLdouble zFar
glFrustumGLdouble
leftGLdouble rightGLdouble bottomGLdouble topGLdouble zNearGLdouble zFar
主要使用方法
概要
最近在学习QT的opengl, 在网上找了相关的学习资料,发现很少,通过查阅相关资料和对网上一些零碎的资料进行了总结,并用QT5 写出了一些简单的DEMO, 最近这段时间将进行持续地更新。
QT5 需要加的模块
opengl是一个模单独的模块,所以在使用时,应该在pro里面添加对应的模块。 QT += opengl
实例运行效果图
实例代码
.h文件#ifndef OPENGLWIDGET_H #define OPENGLWIDGET_H #include <QtOpenGL> class OpenglWidget : public QGLWidget { public: OpenglWidget(QWidget* parent = 0); protected: void initializeGL(); void initWidget(); void paintGL(); void resizeGL(int width, int height); }; #endif // OPENGLWIDGET_H1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
.cpp文件代码
#include "openglwidget.h" OpenglWidget::OpenglWidget(QWidget* parent) :QGLWidget(parent) { initWidget(); initializeGL(); } void OpenglWidget::initializeGL() { glShadeModel(GL_SMOOTH); glClearColor(0.0, 0.0, 0.0, 0.0); glClearDepth(1.0); glEnable(GL_DEPTH_TEST); glDepthFunc(GL_LEQUAL); glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); } void OpenglWidget::initWidget() { setGeometry( 400, 200, 640, 480 ); setWindowTitle(tr("opengl demo")); } void OpenglWidget::paintGL() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); glTranslatef( -1.5, 0.0, -6.0 ); glBegin( GL_QUADS ); glVertex3f( -1.0, 1.0, 0.0 ); glVertex3f( 1.0, 1.0, 0.0 ); glVertex3f( 1.0, -1.0, 0.0 ); glVertex3f( -1.0, -1.0, 0.0 ); glEnd(); glTranslatef( 3.0, 0.0, 0.0 ); glBegin( GL_TRIANGLES ); qDebug() << "this is a paintGL test!"; glVertex3f( 0.0, 1.0, 0.0 ); glVertex3f( -1.0, -1.0, 0.0 ); glVertex3f( 1.0, -1.0, 0.0 ); glEnd(); } void OpenglWidget::resizeGL(int width, int height) { if(0 == height) { height = 1; } glViewport(0, 0, (GLint)width, (GLint)height); glMatrixMode(GL_PROJECTION); glLoadIdentity(); // gluPerspective(45.0, (GLfloat)width/(GLfloat)height, 0.1, 100.0); GLdouble aspectRatio = (GLfloat)width/(GLfloat)height; GLdouble zNear = 0.1; GLdouble zFar = 100.0; GLdouble rFov = 45.0 * 3.14159265 / 180.0; glFrustum( -zNear * tan( rFov / 2.0 ) * aspectRatio, zNear * tan( rFov / 2.0 ) * aspectRatio, -zNear * tan( rFov / 2.0 ), zNear * tan( rFov / 2.0 ), zNear, zFar ); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); }1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
部分函数接口的解释
1.glLoadIdentity();
当您调用glLoadIdentity()之后,您实际上将当前点移到了屏幕中心,X坐标轴从左至右,Y坐标轴从下至上,Z坐标轴从里至外。OpenGL屏幕中心的坐标值是X和Y轴上的0.0点。中心左面的坐标值是负值,右面是正值。移向屏幕顶端是正值,移向屏幕底端是负值。移入屏幕深处是负值,移出屏幕则是正值。
2.glBegin(…)
这里是绘制什么图形的接口,比如说绘制三角形可以用glBegin(GL_TRIANGLES), 而多边形使用 glBegin(GL_QUADS) 其它的点线等可以参考下面的。
#define GL_POINTS 0x0000 #define GL_LINES 0x0001 #define GL_LINE_LOOP 0x0002 #define GL_LINE_STRIP 0x0003 #define GL_TRIANGLES 0x0004 #define GL_TRIANGLE_STRIP 0x0005 #define GL_TRIANGLE_FAN 0x0006 #define GL_QUADS 0x0007 #define GL_QUAD_STRIP 0x0008 #define GL_POLYGON 0x00091
2
3
4
5
6
7
8
9
10
这里需要说明的是在绘制的时候,需要以glBegin(…) 以 glEnd() 结束。
3.glVertex3f( x, y, z );
x,y, z 表示在一个三维空间里面的坐标。
4.glViewport( 0, 0, (GLint)width, (GLint)height );
重置当前的视口(Viewport)
5.glMatrixMode( …);
选择矩阵模式, 主要用得多的是投影矩阵 和 模型观察矩阵#define GL_MODELVIEW 0x1700 #define GL_PROJECTION 0x1701 #define GL_TEXTURE 0x17021
2
3
6.glLoadIdentity();
重置矩阵,这里主要功能是把坐标移动到原点,好进行后续的操作。
7.gluPerspective(GLdouble fovy,GLdouble aspect,GLdouble zNear,GLdouble zFar);
设置投影矩阵
8.glFrustum(GLdouble left,GLdouble right,GLdouble bottom,GLdouble top,GLdouble zNear,GLdouble zFar);
设置投影矩阵, 需要讲一下的是, gluPerspective 和 glFrustum功能 是一样的, 准确的说, gluPerspective 是glFrustum的封装, 它们的关系我将在另一篇博客中进行讲解。而gluPerspective在一些平台或版本上找不到,这时就应该用glFrustum进行转换。
主要使用方法
void paintGL(); void resizeGL(int width, int height);
这两个函数是父类的虚函数, 我们主要是通过重写这两个函数,来进行对应图片的绘制, 在绘制的过程中调用对应的接口,然后通过相应的坐标计算来实现最后的图片绘制
相关文章推荐
- UCOSii和Linux的区别和联系
- 微服务实践(七):从单体式架构迁移到微服务架构
- tomcat服务启动与停止的监听
- CentOS 7下ionic build android 出错 Execution failed for task ':processDebugResources'.
- Xcode 工程文件打不开, cannot be opened because the project
- tomcat(18)部署器
- ubuntu--安装opencv nonefree
- 负载均衡高可用1-LVS(架构、转发方式、算法介绍)
- Linux find文件查找命令与grep文件内容查找命令详解
- Linux与Windows之间ftp自动传输文件
- 【转】PowerPC平台linux设备移植
- 处理使用top提示terminal is not big enough
- ubuntu 1404部署tomcat7
- c 语言中的关键字static
- Linux下RS232和RS485编程有什么区别?
- Linux课题实践四——ELF文件格式分析
- linux安装tomcat
- RHEL / Centos Linux 7:更改和设置Hostname命令
- centos进入登陆界面无登陆框问题
- shell结合expect的非交互式脚本