-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmachinewindow.cpp
300 lines (240 loc) · 7.39 KB
/
machinewindow.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
#include "machinewindow.h"
#include <QResizeEvent>
#include <QMouseEvent>
#include <math.h>
const uint NumVertices = 6;
MachineWindow::MachineWindow(QWindow *parent) :
QWindow(parent),
_logger(NULL),
_shaderProg(this),
_hasInitialized(false),
_positions(QOpenGLBuffer::VertexBuffer)
{
setSurfaceType(QWindow::OpenGLSurface);
// Create the format for our OpenGL Surface
QSurfaceFormat format;
format.setDepthBufferSize(24);
format.setMajorVersion(3);
format.setMinorVersion(2);
format.setSamples(4);
format.setProfile(QSurfaceFormat::CoreProfile);
format.setOption(QSurfaceFormat::DebugContext);
setFormat( format );
create();
// Create a context
_context = new QOpenGLContext;
_context->setFormat(format);
_context->create();
// Associate these two things
_context->makeCurrent( this );
// Get our open gl functions. Instead of just inheriting this lets
// us have full access. Which I guess is cool?
// From https://developer.apple.com/graphicsimaging/opengl/capabilities/
// a good version of things is OpenGL 3.2, GLSL 150
_gl = (QOpenGLFunctions_3_2_Core*)_context->versionFunctions();
if (!_gl)
{
qWarning("Couldn't get gl functions. Crash eminent");
}
_gl->initializeOpenGLFunctions();
// We will later want the vao so make it now??
_vao = NULL;
}
MachineWindow::~MachineWindow()
{
// Yep...
}
void
MachineWindow::exposeEvent(QExposeEvent *evt)
{
Q_UNUSED(evt);
if (isExposed() && !_hasInitialized)
{
initialize();
}
}
void
MachineWindow::resizeEvent(QResizeEvent* evt)
{
//Q_UNUSED(evt);
const QSize& s = evt->size();
_matProject.setToIdentity();
//_matProject.frustum(-1, 1, -1, 1, 0, 1);
//_matProject.frustum(0, s.width()/10, 0, s.height()/10, -100, 100);
//_matProject.frustum(-100, 100, -100.0, 100.0, -0.1, 100.0);
GLfloat aspect = (float)s.height() / (float)s.width();
_matProject.frustum(-1, 1, -1 * aspect, 1 * aspect, 1.0, 1000.0);
//_matProject.translate(0.2, 0.2, 0);
//_matProject = _matProject.inverted();
//_matModel.setToIdentity();
//_matModel.translate(1,0,-1.4);
//_matModel.scale(0.08);
//_matModel.translate(1, 1, 10);
//_matModel.frustum(-20, 20, -20, 20, 20, 100);
if (_hasInitialized)
{
render();
}
}
void
MachineWindow::mousePressEvent(QMouseEvent *evt)
{
Qt::MouseButtons buttons = evt->buttons();
if (buttons & (Qt::LeftButton | Qt::RightButton) )
{
// track the mouse
_dragStart = evt->globalPos();
}
QWindow::mousePressEvent(evt);
}
void
MachineWindow::mouseMoveEvent(QMouseEvent *evt)
{
Qt::MouseButtons buttons = evt->buttons();
if (buttons & Qt::LeftButton)
{
// track the mouse
QPoint offset = _dragStart - evt->globalPos();
_dragStart = evt->globalPos();
// Add this many degrees of rotation to X and Y of the view matrix
_matModel.rotate((float)offset.x() / 2.0f, 0, 1, 0);
_matModel.rotate((float)offset.y() / 2.0f, 1, 0, 0);
//qDebug() << offset << _matModel;
render();
}
else if (buttons & Qt::RightButton)
{
QPoint offset = _dragStart - evt->globalPos();
_dragStart = evt->globalPos();
// Add this many degrees of rotation to X and Y of the view matrix
_matModel.translate((float)-offset.x(), (float)offset.y(), 0);
//qDebug() << offset << _matModel;
render();
}
QWindow::mouseMoveEvent(evt);
}
void
MachineWindow::initialize()
{
///////////////////////////
// Setup logging
_logger = new QOpenGLDebugLogger(this);
connect(_logger, SIGNAL( messageLogged(QOpenGLDebugMessage)), this, SLOT( onDebugMsg(QOpenGLDebugMessage) ), Qt::DirectConnection );
if (_logger->initialize())
{
_logger->startLogging(QOpenGLDebugLogger::SynchronousLogging);
_logger->enableMessages();
}
else
{
qWarning() << "Unable to setup GL debug logging";
if (!_context->hasExtension(QByteArrayLiteral("GL_KHR_debug")))
{
qWarning() << "The GL context doesn't have the right extension GL_KHR_debug";
}
else
{
qWarning() << "The extension is present. Maybe the context isn't setup for debug format?";
}
}
// Setup our shaders
_shaderProg.addShaderFromSourceFile(QOpenGLShader::Vertex, QString(":/gl/default.vert"));
_shaderProg.addShaderFromSourceFile(QOpenGLShader::Fragment, QString(":/gl/default.frag"));
if (!_shaderProg.link())
{
qDebug() << "Failed to link GL Shaders";
qDebug() << _shaderProg.log();
}
else
{
qDebug() << "Shaders loaded ok";
_hasInitialized = true;
emit readyForData();
render();
}
}
void
MachineWindow::render()
{
if (!_hasInitialized || !isExposed()) return;
if (!_context->makeCurrent(this))
{
qDebug() << "Failed to make context current";
}
_gl->glClearColor(1.0, 0.0, 0.0, 1.0);
_gl->glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
_shaderProg.bind();
QMatrix4x4 mvt = ((_matProject * _matView) * _matModel);
_shaderProg.setUniformValue("ModelViewProject", mvt);
if (_vao)
{
_vao->bind();
_gl->glDrawArrays(GL_TRIANGLES, 0, NumVertices);
}
_context->swapBuffers(this);
}
void
MachineWindow::render(QPainter* painter)
{
// Nothing of interest
Q_UNUSED(painter);
}
void
MachineWindow::setProgram(QList<Word*>& prog)
{
// Store this program. Shallow copy so be careful I guess...
_program.clear();
_program.append(prog);
// Reset the visual data
// GLfloat posData[6][3] = {
// { -0.90, -0.90, -0.5 }, // Triangle 1
// { 0.85, -0.90, -0.5 },
// { -0.90, 0.90, -0.5 },
// { 0.90, -0.85, 0.5 }, // Triangle 2
// { 0.90, 0.90, 0.5 },
// { -0.90, 0.90, 0.5 }
//// { -0.85, 0.90, 0.5 }
// };
GLfloat posData[6][3] = {
{ -90, -90, -10.0 }, // Triangle 1
{ 85, -90, -10.0 },
{ -90, 85, -10.0 },
{ 90, -85, -5 }, // Triangle 2
{ 90, 90, -5 },
{ -85, 90, -5 }
};
if (_vao)
{
qWarning("_vao already exists and I don't know what to do in this case yet");
_vao->bind();
}
else
{
_context->makeCurrent( this );
_vao = new QOpenGLVertexArrayObject( this );
_vao->create();
_vao->bind();
_positions.create();
_positions.setUsagePattern(QOpenGLBuffer::StaticDraw);
_positions.bind();
_positions.allocate(posData, NumVertices * 3 * sizeof(GLfloat));
_shaderProg.enableAttributeArray("Vertex");
_shaderProg.setAttributeBuffer("Vertex", GL_FLOAT, 0, 3);
// color buffer....
// Initial view position
_matView.setToIdentity();
_matView.translate(0, 0, 500);
//_matView.rotate(10, 0, 0, 1);
// Inverting it here at the end lets us express the move in regular coords without minuses and shit
_matView = _matView.inverted();
// Initial model position
_matModel.setToIdentity();
// Ok, make with rendering. Hopefully on a good thread and shit...
render();
}
}
void
MachineWindow::onDebugMsg(QOpenGLDebugMessage msg)
{
qDebug() << msg;
}