forked from iGame-Lab/iGameView
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathViewOpenGL.cpp
More file actions
388 lines (323 loc) · 10.9 KB
/
ViewOpenGL.cpp
File metadata and controls
388 lines (323 loc) · 10.9 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
#include <iostream>
#include <sstream>
#include "ViewOpenGL.h"
#include <QtWidgets/QApplication>
#if __linux__
#include <QMouseEvent>
#else
#include <QtGUI/QMouseEvent>
#endif
#include <QtWidgets/QDesktopWidget>
const double ViewOpenGL::trackballradius = 0.6;
ViewOpenGL::ViewOpenGL (QWidget *_parent)
: QOpenGLWidget (_parent),
drawMode (FLATFILL),
colorMode(LIGHT),
projectionMode (PERSPECTIVE),
windowleft (-1),
windowright (1),
windowtop (1),
windowbottom (-1),
mouseMode (Qt::NoButton),
center (0, 0, 0),
radius (0),
lastpoint2 (0,0),
lastpoint3 (0, 0, 0),
lastpointok (false)
{
Init ();
//QSurfaceFormat surfaceFormat;
//surfaceFormat.setSamples(16);//多重采样
//setFormat(surfaceFormat); //setFormat是QOpenGLWidget的函数
}
ViewOpenGL::~ViewOpenGL (void){
}
void ViewOpenGL::Init (void){
// qt stuff
setAttribute (Qt::WA_NoSystemBackground,true);
setFocusPolicy (Qt::StrongFocus);//选中窗口变色
//setAcceptDrops(true); //拖拽功能
//SetProjectionMode (projectionMode);
}
QSize ViewOpenGL::minimumSizeHint (void) const{
return QSize (10,10);
}
QSize ViewOpenGL::sizeHint (void) const{
QRect rect = QApplication::desktop()->screenGeometry ();
return QSize (int (rect.width () * 0.8),int (rect.height () * 1.0));
}
const double &ViewOpenGL::Radius (void) const{
return radius;
}
const Vector3d &ViewOpenGL::Center (void) const{
return center;
}
const QMatrix4x4 *ViewOpenGL::GetModelviewMatrix1(void) const {
return &viewMatrix;
}
const QMatrix4x4* ViewOpenGL::GetProjectionMatrix1(void) const {
return &projectionMatrix;
}
const ViewOpenGL::ProjectionMode& ViewOpenGL::GetProjectionMode(void) const {
return projectionMode;
}
void ViewOpenGL::ResetModelviewMatrix (void){
viewMatrix.setToIdentity();
}
void ViewOpenGL::SetProjectionMode(const ProjectionMode& pm) {
projectionMode = pm;
UpdateProjectionMatrix();
ViewAll();
}
void ViewOpenGL::CopyModelViewMatrix (void){
}
void ViewOpenGL::LoadCopyModelViewMatrix (void){
}
void ViewOpenGL::SetDrawMode (const DrawMode &dm){
drawMode = dm;
update();
}
void ViewOpenGL::SetColorMode(const ColorMode& rm) {
colorMode = rm;
update();
}
const ViewOpenGL::DrawMode &ViewOpenGL::GetDrawMode (void)const {
return drawMode;
}
void ViewOpenGL::initializeGL (void){
// OpenGL state
//initializeOpenGLFunctions ();
core = QOpenGLContext::currentContext()->versionFunctions<QOpenGLFunctions_3_3_Core>();
// 加载shader
QOpenGLShader vertexShader(QOpenGLShader::Vertex);
vertexShader.compileSourceFile(":/shader/color.vert");
QOpenGLShader fragmentShader(QOpenGLShader::Fragment);
fragmentShader.compileSourceFile(":/shader/color.frag");
shaderProgram.addShader(&vertexShader);
shaderProgram.addShader(&fragmentShader);
shaderProgram.link();
shaderProgram.bind();
core->glClearColor (0.3, 0.3, 0.3, 1.0);
core->glClearDepth (1.0);
core->glEnable(GL_DITHER);
core->glEnable (GL_DEPTH_TEST);//开启深度测试
//开启抗锯齿
core->glEnable (GL_MULTISAMPLE);
core->glEnable(GL_POINT_SMOOTH);
core->glEnable(GL_BLEND);
core->glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
core->glHint(GL_POINT_SMOOTH_HINT,GL_DONT_CARE);//GL_NICEST图形显示质量优先 GL_FASTEST 速度优先 GL_DONT_CARE系统默认
core->glEnable(GL_MULTISAMPLE);
core->glEnable (GL_LINE_SMOOTH);
core->glEnable(GL_BLEND);
core->glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
core->glHint (GL_LINE_SMOOTH_HINT,GL_DONT_CARE);
core->glEnable(GL_MULTISAMPLE);
core->glEnable (GL_POLYGON_SMOOTH);
core->glEnable(GL_BLEND);
core->glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
core->glHint(GL_POLYGON_SMOOTH_HINT,GL_DONT_CARE);
viewMatrix.setToIdentity();
SetScenePosition (Vector3d (0.0,0.0,0.0),1.0);
}
void ViewOpenGL::resizeGL (int _w,int _h){
core->glViewport (0,0,_w,_h);
UpdateProjectionMatrix ();
update ();
}
void ViewOpenGL::paintGL(void){
core->glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
DrawScene ();
}
void ViewOpenGL::DrawScene(){
}
void ViewOpenGL::mousePressEvent (QMouseEvent *_event){
lastpoint2 = _event->pos ();
lastpointok = MapToSphere (lastpoint2,lastpoint3);
mouseMode = _event->button ();
}
void ViewOpenGL::mouseMoveEvent (QMouseEvent *_event){
QPoint newPoint2D = _event->pos ();
if(lastpointok){
switch(mouseMode){
case Qt::LeftButton:
Rotation (newPoint2D);
break;
case Qt::RightButton:
Translation (newPoint2D);
break;
default:
break;
}
} // end of if
// remember this point
lastpoint2 = newPoint2D;
lastpointok = MapToSphere (lastpoint2,lastpoint3);
// trigger redraw
update ();
}
void ViewOpenGL::mouseReleaseEvent (QMouseEvent *_event){
mouseMode = Qt::NoButton;
lastpointok = false;
}
void ViewOpenGL::wheelEvent (QWheelEvent *_event){
// 0.1代表缩放尺度
double d = -_event->delta () / 120.0 * 0.1*radius;
Translate (Vector3d (0.0,0.0,d));
update ();
}
void ViewOpenGL::keyPressEvent (QKeyEvent *_event){
switch(_event->key ()){
case Qt::Key_Escape:
qApp->quit ();
break;
}
_event->ignore ();
}
void ViewOpenGL::keyReleaseEvent (QKeyEvent *_event){
_event->ignore ();
}
void ViewOpenGL::PickItemTranslation(const QPoint& p) {
pickItemMoveMatrix.setToIdentity();
double z = -(viewMatrix(2, 3) / viewMatrix(2, 2)); // 右键拖动的距离与模型离视角的远近进行绑定(使拖动更流畅)
double w = width(); double h = height();
double aspect = w / h;
double near_plane = 0.01 * radius;
double top = tan(45.0 / 2.0f * M_PI / 180.0f) * near_plane;
double right = aspect * top;
double dx = p.x() - lastpoint2.x();
double dy = p.y() - lastpoint2.y();
QVector4D trans(2.0 * dx / w * right / near_plane * z,
-2.0 * dy / h * top / near_plane * z,
0.0f, 1);
// trans = currentModel->rotateMatrix.inverted() * trans;
std::cout << trans[0] << " " << trans[1] << " " << trans[2] << " " << std::endl;
pickItemMoveMatrix.translate(trans[0], trans[1], trans[2]);
}
void ViewOpenGL::Translation (const QPoint &p){
double z = -(viewMatrix(2, 3) / viewMatrix(2, 2)); // 右键拖动的距离与模型离视角的远近进行绑定(使拖动更流畅)
double w = width (); double h = height ();
double aspect = w / h;
double near_plane = 0.01 * radius;
double top = tan (45.0 / 2.0f * M_PI / 180.0f) * near_plane;
double right = aspect * top;
double dx = p.x () - lastpoint2.x ();
double dy = p.y () - lastpoint2.y ();
Translate(Vector3d(2.0 * dx / w * right / near_plane * z,
-2.0 * dy / h * top / near_plane * z,
0.0f));
}
void ViewOpenGL::Translate (const Vector3d &_trans){
float x = _trans[0];
float y = _trans[1];
float z = _trans[2];
QVector4D trans = QVector4D{ x, y, z, 1.0f};
// trans = currentModel->rotateMatrix.inverted() * viewRotateMatrix.inverted() * trans;
// //modelmatrix.translate(_trans[0] / modelScale, _trans[1] / modelScale, _trans[2] / modelScale);
// float scale = currentModel->modelScale;
// currentModel->modelMatrix.translate(trans[0] / scale, trans[1] / scale, trans[2] / scale);
}
void ViewOpenGL::ModelScale(const Vector3d& _scale) {
// currentModel->modelMatrix.scale(_scale[0], _scale[1], _scale[2]);
}
void ViewOpenGL::Rotation (const QPoint &p){
Vector3d newPoint3D;
bool newPoint_hitSphere = MapToSphere (p,newPoint3D);
if(newPoint_hitSphere){
Vector3d axis = lastpoint3.Cross(newPoint3D);// corss product
if(axis.Length() < 1e-7){
axis = Vector3d (1,0,0);
}
else{
axis.Normalize();
}
// find the amount of rotation
Vector3d d = lastpoint3 - newPoint3D;
double t = 0.5 * d.Length() / trackballradius;
if(t < -1.0) t = -1.0;
else if(t > 1.0) t = 1.0;
double phi = 2.0 * asin (t);
double angle = phi * 180.0 / M_PI;
Rotate (axis,angle);
}
}
void ViewOpenGL::Rotate (const Vector3d &_axis,const double &_angle){
QMatrix4x4 tempmatrix;
//tempmatrix.translate(modelmatrix(0,3), modelmatrix(1, 3), modelmatrix(2, 3)); // 由于许多模型的初始center并不在坐标原点,需要把center先移到坐标原点再旋转
tempmatrix.rotate(_angle, _axis[0], _axis[1], _axis[2]);
//tempmatrix.translate(-modelmatrix(0, 3), -modelmatrix(1, 3), -modelmatrix(2, 3));
// currentModel->modelMatrix = tempmatrix * currentModel->modelMatrix; // 左乘的方式,使动态欧拉角改成静态欧拉角,解决方向旋转的问题
// currentModel->rotateMatrix = tempmatrix * currentModel->rotateMatrix; // rotatematrix是用来储存旋转状态的(没有translate),模型法向量的旋转需要用到此矩阵
}
bool ViewOpenGL::MapToSphere (const QPoint &_v2D,Vector3d &_v3D){
// This is actually doing the Sphere/Hyperbolic sheet hybrid thing,
// based on Ken Shoemake's ArcBall in Graphics Gems IV, 1993.
double x = (2.0 * _v2D.x () - width ()) / width ();
double y = -(2.0 * _v2D.y () - height ()) / height ();
double xval = x;
double yval = y;
double x2y2 = xval * xval + yval * yval;
const double rsqr = trackballradius * trackballradius;
_v3D[0] = xval;
_v3D[1] = yval;
if(x2y2 < 0.5 * rsqr){
_v3D[2] = sqrt (rsqr - x2y2);
}
else{
_v3D[2] = 0.5 * rsqr / sqrt (x2y2);
}
return true;
}
void ViewOpenGL::SetScenePosition(const Vector3d& _center, const double& _radius) {
center = _center;
radius = _radius;
viewMatrix.setToIdentity();
UpdateProjectionMatrix();
ViewAll();
}
void ViewOpenGL::UpdateProjectionMatrix (void){
projectionMatrix.setToIdentity();
if(PERSPECTIVE == projectionMode){
projectionMatrix.frustum(-0.01 * radius * (sqrt(2.0) - 1) * width() / height(),
0.01 * radius * (sqrt(2.0) - 1) * width() / height(),
-0.01 * radius * (sqrt(2.0) - 1),
0.01 * radius * (sqrt(2.0) - 1),
0.01 * radius,
100.0 * radius);
}
else if(ORTHOGRAPHIC == projectionMode) //not work for
{
projectionMatrix.ortho(windowleft, windowright, windowbottom, windowtop, -1, 1);
}
}
void ViewOpenGL::ViewAll (void){
//makeCurrent();
viewMatrix.translate(-center[0], -center[1], -(center[2] + 2.0 * radius));
}
void ViewOpenGL::ViewRotate(const QPoint& p) {
Vector3d newPoint3D;
bool newPoint_hitSphere = MapToSphere(p, newPoint3D);
if (newPoint_hitSphere) {
Vector3d axis = lastpoint3.Cross(newPoint3D);// corss product
if (axis.Length() < 1e-7) {
axis = Vector3d(1, 0, 0);
}
else {
axis.Normalize();
}
// find the amount of rotation
Vector3d d = lastpoint3 - newPoint3D;
double t = 0.5 * d.Length() / trackballradius;
if (t < -1.0) t = -1.0;
else if (t > 1.0) t = 1.0;
double phi = 2.0 * asin(t);
double angle = phi * 180.0 / M_PI;
QMatrix4x4 tempmatrix;
//tempmatrix.translate(0, 0, -2.0 * radius); // 由于许多模型的初始center并不在坐标原点,需要把center先移到坐标原点再旋转
tempmatrix.rotate(angle, axis[0], axis[1], axis[2]);
//viewRotateMatrix.rotate(angle, axis[0], axis[1], axis[2]);
//tempmatrix.translate(0, 0, 2.0 * radius);
//viewMatrix = tempmatrix * viewMatrix; // 左乘的方式,使动态欧拉角改成静态欧拉角,解决方向旋转的问题
viewRotateMatrix = tempmatrix * viewRotateMatrix;
}
}