-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgldisplay.cpp
More file actions
160 lines (132 loc) · 4.09 KB
/
gldisplay.cpp
File metadata and controls
160 lines (132 loc) · 4.09 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
#include "gldisplay.h"
#include <iostream>
#include <cmath>
#include <QMouseEvent>
GLDisplay::GLDisplay(QWidget *parent):
QOpenGLWidget(parent),
m_geometry(0),
m_particles(0),
m_mesh(0)
{
matrix.translate(0.0, 0.0, -5.0);
QSurfaceFormat format = QSurfaceFormat::defaultFormat();
format.setSamples(16);
format.setSwapInterval(0);
QSurfaceFormat::setDefaultFormat(format);
this->setFormat(format);
}
GLDisplay::~GLDisplay()
{
makeCurrent();
delete m_geometry;
delete m_particles;
delete m_mesh;
doneCurrent();
}
void GLDisplay::mousePressEvent(QMouseEvent *e)
{
mousePressPosition = QVector2D(e->localPos());
}
void GLDisplay::mouseMoveEvent(QMouseEvent *e)
{
if(e->MouseButtonPress)
{
QVector2D displacement(e->localPos().x()-mousePressPosition.x(),
e->localPos().y()-mousePressPosition.y());
QVector2D absisca(1,0); QVector2D ordinates(0,1);
float dota = QVector2D::dotProduct(displacement, absisca);
float dotb = QVector2D::dotProduct(displacement, ordinates);
if(e->buttons() == Qt::LeftButton)
{
if(fabs(dotb) > fabs(dota))
{
if(e->localPos().y() > mousePressPosition.y())
matrix.rotate(0.5, QVector3D(1,0,0));
if(e->localPos().y() < mousePressPosition.y())
matrix.rotate(-0.5, QVector3D(1,0,0));
}
if(fabs(dota) > fabs(dotb))
{
if(e->localPos().x() > mousePressPosition.x())
matrix.rotate(0.5, QVector3D(0,1,0));
if(e->localPos().x() < mousePressPosition.y())
matrix.rotate(-0.5, QVector3D(0,1,0));
}
}
if(e->buttons() == Qt::RightButton)
{
if(fabs(dotb) > fabs(dota))
{
if(e->localPos().y() > mousePressPosition.y())
matrix.translate(0,-0.02,0);
if(e->localPos().y() < mousePressPosition.y())
matrix.translate(0,0.02,0);
}
if(fabs(dota) > fabs(dotb))
{
if(e->localPos().x() > mousePressPosition.x())
matrix.translate(0.02,0,0);
if(e->localPos().x() < mousePressPosition.y())
matrix.translate(-0.02,0,0);
}
}
if(e->buttons() == Qt::MidButton)
{
if(e->localPos().y() > mousePressPosition.y())
matrix.translate(0,0,0.02);
if(e->localPos().y() < mousePressPosition.y())
matrix.translate(0,0,-0.02);
}
}
normal = matrix.normalMatrix();
update();
}
void GLDisplay::mouseReleaseEvent(QMouseEvent *e)
{
e = e;
}
void GLDisplay::timerEvent(QTimerEvent *e)
{
e = e;
update();
}
void GLDisplay::initializeGL()
{
initializeOpenGLFunctions();
glClearColor(0, 0, 0, 1);
initShaders();
glEnable(GL_DEPTH_TEST);
// m_geometry = new Triangle();
// m_particles = new Particles();
m_mesh = new Mesh("");
timer.start(12, this);
}
void GLDisplay::resizeGL(int w, int h)
{
qreal aspect = qreal(w) / qreal(h ? h : 1);
const qreal zNear = 3.0, zFar = 30.0, fov = 45.0;
projection.setToIdentity();
projection.perspective(fov, aspect, zNear, zFar);
}
void GLDisplay::paintGL()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
program.setUniformValue("normal_matrix", normal);
program.setUniformValue("mvp_matrix", projection * matrix);
m_lightPosLoc = program.uniformLocation("lightPos");
program.setUniformValue(m_lightPosLoc, QVector3D(0, 0, 70));
// m_geometry->drawTriangle(&program);
// m_particles->drawParticles(&program);
m_mesh->drawMesh(&program);
}
void GLDisplay::initShaders()
{
if (!program.addShaderFromSourceFile(QOpenGLShader::Vertex, ":/simple.vert"))
close();
if (!program.addShaderFromSourceFile(QOpenGLShader::Fragment, ":/simple.frag"))
close();
if (!program.link())
close();
if (!program.bind())
close();
}