-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopenglwidget.cpp
More file actions
147 lines (115 loc) · 3.81 KB
/
openglwidget.cpp
File metadata and controls
147 lines (115 loc) · 3.81 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include "openglwidget.h"
OpenGLWidget:: OpenGLWidget(QWidget *parent){
xSlider=new QSlider(Qt::Horizontal,this);
xSlider->setRange(-180,180);
connect(xSlider, SIGNAL(valueChanged(int)), this, SLOT(setXRotate()));
resize(800,800);
//setGeometry(400,200,640,480);
}
void OpenGLWidget:: keyPressEvent(QKeyEvent *event){
if(event->key()==Qt::Key_Shift)
qDebug()<<"aaa"<<endl;
}
void OpenGLWidget:: wheelEvent(QWheelEvent*event){
if(event->delta()>0)
ratio+=0.2;
else if(event->delta()<0 && ratio>0.2)
ratio-=0.2;
update();
}
void OpenGLWidget::initializeGL()
{
initializeOpenGLFunctions();
//设置背景色——用于填充背景
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
//设置多边形填充模式为smooth 方式
glShadeModel(GL_SMOOTH);
//打开深度测试开关——用于检测物体间的z 深度差异
glClearDepth(1.0);
glEnable(GL_DEPTH_TEST);
//线的抗锯齿开关
glEnable(GL_LINE_SMOOTH);
//启用抗锯齿效果
glHint(GL_LINE_SMOOTH,GL_NICEST);
//指定混合函数
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
//启用色彩混合状态
glEnable(GL_BLEND);
QFile file(filename);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
return;
QTextStream in(&file);
in.readLine();
QStringList list=in.readLine().split(' ');
nVertices=list[0].toInt();
nFaces=list[1].toInt();
for(int i=0;i<nVertices;i++){
list=in.readLine().split(' ');
vertice tmp;
tmp.x=list[0].toDouble();
tmp.y=list[1].toDouble();
tmp.z=list[2].toDouble();
Vertices.push_back(tmp);
}
for(int i=0;i<nFaces;i++){
list=in.readLine().split(' ');
int num=list[0].toInt();
QVector<int>tmp;
for(int j=1;j<=num;j++)
tmp.push_back(list[j].toInt());
Faces.push_back(tmp);
}
}
void OpenGLWidget::paintGL()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
//将当前点移置屏幕中心,相当于复位的操作
glLoadIdentity();
//平移函数,参数指的是分别从X轴,Y轴,Z轴平移
glTranslatef(0.0,0.0,-6.0);
glScaled(ratio,ratio,ratio);
glRotatef(xRotate,1,0,0);
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
for(int i=0;i<nFaces;i++){
glBegin(GL_POLYGON);
glColor3f(1.0, 1.0,1.0);
for(int j=0;j<Faces[i].size();j++){
glVertex3f( Vertices[Faces[i][j]].x , Vertices[Faces[i][j]].y, Vertices[Faces[i][j]].z);
}
glEnd();
}
glLineWidth(2);
for(int i=0;i<nFaces;i++){
glBegin(GL_LINE_LOOP);
glColor3f(0, 0, 0);
for(int j=0;j<Faces[i].size();j++){
glVertex3f( Vertices[Faces[i][j]].x , Vertices[Faces[i][j]].y, Vertices[Faces[i][j]].z);
}
glEnd();
}
}
void OpenGLWidget::resizeGL(int width, int height)
{
if(0==height)
height=1;
//告诉绘制到窗体的哪个位置
glViewport(0,0,width,height);
// 设置矩阵模式,参数是设置为投影矩阵
glMatrixMode(GL_PROJECTION);
//复位操作
glLoadIdentity();
GLdouble aspectRatio=(GLfloat)width/(GLfloat)height;
GLdouble rFov=45.0*3.14159265/180.0;
GLdouble zNear=0.1;
GLdouble zFar=100.0;
//调用glFrustum,生成矩阵与当前矩阵相乘,生成透视效果
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();
}