您的位置:首页 > 运维架构

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

主要使用方法


概要

最近在学习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_H
1
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 0x0009
1
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 0x1702
1
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); 

这两个函数是父类的虚函数, 我们主要是通过重写这两个函数,来进行对应图片的绘制, 在绘制的过程中调用对应的接口,然后通过相应的坐标计算来实现最后的图片绘制
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: