-
Notifications
You must be signed in to change notification settings - Fork 0
/
myopenglwidget.cpp
executable file
·322 lines (279 loc) · 7.97 KB
/
myopenglwidget.cpp
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
#include "myopenglwidget.h"
#include <iostream>
MyOpenGLWidget::MyOpenGLWidget(QWidget *parent)
{
world = new MGL_Node();
selected = aux = world->end();
resetCamera();
}
MyOpenGLWidget::~MyOpenGLWidget()
{
delete world;
}
void MyOpenGLWidget::newNode()
{
world->spawnChild(); // add it to the top level of our tree
selected = --world->end(); // select it
}
void MyOpenGLWidget::deleteSelected()
{
auto temp = selected;
MGL_Node *childToDelete = *temp;
selectNext();
delete childToDelete;
}
void MyOpenGLWidget::makeParentOf()
{
if (selected == aux || selected == world->end() || aux == world->end())
return;
world->makeXChildOfY(aux,selected);
}
void MyOpenGLWidget::selectParent()
{
// sets selected to aux, aux is the future child
if (selected != world->end())
{
if (aux == world->end())
{
aux = selected;
}
else
{
// aux is selected, and now we've chosen a parent
makeParentOf();
aux = world->end();
}
}
}
void MyOpenGLWidget::clearObjects()
{
std::cout << "clearing objects... ";
world->deleteChildren();
selected = aux = world->end();
std::cout << "objects cleared!" << std::endl;
}
void MyOpenGLWidget::activateKeyboard(bool activate)
{
keyboardActivated = activate;
mouseMode = MModes::Neutral;
keyboardMode = KModes::Neutral;
}
void MyOpenGLWidget::setKeyboardMode(int m)
{
if (m == KModes::Object)
{
colorSetting = true;
keyboardMode = m;
}
else if (m > KModes::Object && m <= KModes::FarPlane)
{
colorSetting = false;
keyboardMode = m;
}
else
{
colorSetting = false;
keyboardMode = KModes::Neutral;
}
}
void MyOpenGLWidget::setMouseMode(int m)
{
if (m > MModes::Neutral && m < MModes::CameraTranslate)
{
colorSetting = true;
mouseMode = m;
}
else if (m <= MModes::FarPlane)
{
colorSetting = false;
mouseMode = m;
}
else
{
colorSetting = false;
mouseMode = MModes::Neutral;
}
}
void MyOpenGLWidget::setAxis(int a)
{
if (a >= 0 && a < 3)
axis = a;
}
void MyOpenGLWidget::swapAxisOrder(int order)
{
static int o = 0;
if (selected != world->end())
{
(*selected)->setRotationAxisOrder(o);
if (o == 0)
o = 1;
else
o = 0;
}
}
void MyOpenGLWidget::swapTransformOrder(int order)
{
static int o = 0;
if (selected != world->end())
{
(*selected)->setXformOrder(o);
if (o == 0)
o = 1;
else
o = 0;
}
}
void MyOpenGLWidget::selectNext()
{
if (++selected == world->end())
selected = world->begin();
}
void MyOpenGLWidget::selectPrevious()
{
if (selected == world->begin())
selected = --world->end();
else
--selected;
}
void MyOpenGLWidget::initializeGL() {
initializeOpenGLFunctions();
// must run after initializeOpenGLFunctions()
pVao = new QOpenGLVertexArrayObject();
pVao->create();
pVao->bind();
pVertexShader = new QOpenGLShader(QOpenGLShader::Vertex);
pVertexShader->compileSourceFile(":/glsl/shader.vert");
std::cout << pVertexShader->log().toLatin1().data(); // print errlog
pFragmentShader = new QOpenGLShader(QOpenGLShader::Fragment);
pFragmentShader->compileSourceFile(":/glsl/shader.frag");
std::cout << pFragmentShader->log().toLatin1().data();// print errlog
// Must occur after the vertex shader and fragment shader objects
// are created.
pShaderProgram = new QOpenGLShaderProgram(this);
pShaderProgram->create();
pShaderProgram->addShader(pVertexShader);
pShaderProgram->addShader(pFragmentShader);
pShaderProgram->link();
}
void MyOpenGLWidget::paintGL()
{
float col[] = { 0.0f, 0.5f, 0.8f, 1.0f};
glClearBufferfv(GL_COLOR,0,col);
QMatrix4x4 identity;
for (auto it = world->begin(); it != world->end(); it++)
{
if (it == selected)
{
renderTree(*it, identity, QVector4D(1.0,0.0,0.0,1.0));
}
else if (it == aux)
{
renderTree(*it, identity, QVector4D(0.0,0.0,0.25,1.0));
}
else
{
renderTree(*it, identity, QVector4D(1.0,1.0,1.0,1.0));
}
}
}
void MyOpenGLWidget::renderTree(MGL_Node *node, const QMatrix4x4 &transform, const QVector4D &color)
{
// render triangles in the current object
int vertexCount = node->getVertexCount();
GLfloat *vertices = node->getTriangleArray();
QMatrix4x4 t = transform * node->getXform();
QOpenGLBuffer *pBuffer = new QOpenGLBuffer();
pBuffer->create();
pBuffer->bind();
pBuffer->setUsagePattern(QOpenGLBuffer::DynamicDraw);
pBuffer->allocate(vertices,vertexCount*16); // numVertices * 4 GLfloats/vertex * 4 bytes/GLfloat = numVertices * 16
pShaderProgram->bind();
pShaderProgram->enableAttributeArray("vPosition");
pShaderProgram->setAttributeBuffer("vPosition",GL_FLOAT,0,4);
int m_camLocation = pShaderProgram->uniformLocation("pers_cam_xform");
pShaderProgram->setUniformValue(m_camLocation, matPerspective * matCamera * t);
int colorLocation = pShaderProgram->uniformLocation("icolor");
pShaderProgram->setUniformValue(colorLocation,color);
glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
glDrawArrays(GL_TRIANGLES,0,vertexCount);
pShaderProgram->disableAttributeArray("vPosition");
pShaderProgram->release(); // release program
pBuffer->destroy(); // destroy buffer object
delete pBuffer; // release buffer object memory
for (auto &obj: node->getChildren())
{
renderTree(obj, t, color);
}
}
void MyOpenGLWidget::resizeGL(int w, int h)
{
matPerspective.setToIdentity();// mat is a QMatrix4x4 object
int fovy = 60;
matPerspective.perspective(fovy, GLfloat(w)/GLfloat(h), nearclip,farclip);
}
void MyOpenGLWidget::mousePressEvent(QMouseEvent *ev) {
// store x and y coordinates
widgetX1 = ev->x();
widgetY1 = ev->y();
}
void MyOpenGLWidget::mouseReleaseEvent(QMouseEvent *ev) {
widgetX2 = ev->x();
widgetY2 = ev->y();
setNextVertex();
}
float MyOpenGLWidget::mouseDistanceY()
{
return float(widgetY2 - widgetY1) / height();
}
void MyOpenGLWidget::calculateCameraMatrix()
{
matCamera.setToIdentity();
matCamera.lookAt(eye,center,up);
}
void MyOpenGLWidget::resetCamera()
{
eye = QVector3D(0,0,-10);
center = QVector3D(0,0,0);
up = QVector3D(0,1,0);
calculateCameraMatrix();
}
void MyOpenGLWidget::setNextVertex()
{
if (numVerticesChosen < 3)
{
GLfloat x3d = float(2*widgetX2)/width() - 1;
GLfloat y3d = 1 - float(2*widgetY2)/height();
QVector4D vertex = QVector4D(x3d,y3d,0,1);
QMatrix4x4 xform;
if (selected != world->end()) xform = (*selected)->getXform();
vertex = (matPerspective * matCamera * xform).inverted() * vertex;
qvertices[numVerticesChosen++] = vertex;
}
if (numVerticesChosen == 3)
{
if (selected != world->end())
{
(*selected)->addVerticesByVector4D(qvertices, 3);
numVerticesChosen = 0;
}
}
}
// TODO: decide if this is useful
bool MyOpenGLWidget::canAddVertex()
{
return false;
}
void MyOpenGLWidget::genericTriangle()
{
if (!(world->size() == 0) && selected != world->end())
{
QVector4D triangle[3];
triangle[0] = QVector4D(-1.0,0.0,0.0,1.0);
triangle[1] = QVector4D(1.0,0.0,0.0,1.0);
triangle[2] = QVector4D(0.0,1.0,0.0,1.0);
(*selected)->addVerticesByVector4D(triangle, 3);
}
}
void MyOpenGLWidget::refresh() {
update();
}