-
Notifications
You must be signed in to change notification settings - Fork 0
/
Renderer.cpp
246 lines (191 loc) · 8.85 KB
/
Renderer.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
#include "Renderer.hpp"
#include "OpenGLWidget.hpp"
#include "Playground.hpp"
#include <QOpenGLFunctions_3_3_Core>
#include <thread>
#include <QDebug>
//std::map<int, GlobalRenderData::Mesh*> GlobalRenderData::meshes;
//std::vector<RenderJob*> Renderer::jobs;
Renderer::Renderer(OpenGLWidget *widget,
std::condition_variable *_job_ready,
std::mutex *_mtx,
Swapper *swpr)
: surfaceWindow(widget),
job_ready(_job_ready),
mtx(_mtx),
swapper(swpr)
{
// pass
threadContext = 0;
}
Renderer::~Renderer()
{
surfaceWindow->destroy();
}
bool Renderer::createContext()
{
threadContext = new QOpenGLContext;
QSurfaceFormat format = QSurfaceFormat::defaultFormat();
format.setVersion(3, 3);
format.setSwapBehavior(QSurfaceFormat::DoubleBuffer);
format.setRenderableType(QSurfaceFormat::OpenGL);
format.setProfile(QSurfaceFormat::CoreProfile);
threadContext->setFormat(format);
return threadContext->create();
}
void Renderer::start()
{
if (!createContext()) return;
forever {
{
// wait for a job from updater
std::unique_lock<std::mutex> lock(g_mtx);
/*while (jobs_available) */
g_jobs_ready.wait(lock, [](){ return !jobs_available;});
// jobs_available = false;
}
{
std::unique_lock<std::mutex> lock(g_mtx);
swapper->swap();
jobs_done = true;
}
g_jobs_done.notify_one();
run();
// std::this_thread::sleep_for(std::chrono::milliseconds(16));
}
}
void Renderer::run()
{
// Save some cycles and only run if the window is being exposed
if (surfaceWindow->isExposed()) {
threadContext->makeCurrent(surfaceWindow);
auto gl = threadContext->versionFunctions<QOpenGLFunctions_3_3_Core>();
gl->glViewport(0, 0, surfaceWindow->width(), surfaceWindow->height());
gl->glClearColor(.1f, .1f, .1f, .4f);
gl->glClear(GL_COLOR_BUFFER_BIT);
logFunc(QString("size ") + QString::number(swapper->RenderDrawCommands->size()));
// // do some stuff
std::vector<RenderCommands::CommandDrawIndexed>::iterator iter;
for (iter = swapper->RenderDrawCommands->begin();
iter != swapper->RenderDrawCommands->end();
iter++)
{
// we are using the mesh bucket!
// do a lookup for the id.
if (vaoStore.value((*iter).command_id)) {
// draw the model
// logFunc(QString("drawing cached model vao id = %1").arg((*iter).model.mesh.vertex_array));
program->bind();
QMatrix4x4 model;
QMatrix4x4 view;
QMatrix4x4 projection;
projection.setToIdentity();
projection.perspective(45.f, (GLfloat) surfaceWindow->width() / surfaceWindow->height(), .1f, 32.f);
model.setToIdentity();
model.translate(QVector3D(0, 0, 0));
view.setToIdentity();
view.lookAt(QVector3D(0, 0, 2),
QVector3D(0, 0, 0),
QVector3D(0, 1, 0));
program->setUniformValue("uModel", model);
program->setUniformValue("uView", view);
program->setUniformValue("uProj", projection);
gl->glBindVertexArray((*iter).model.mesh.vertex_array);
gl->glDrawElements(GL_TRIANGLES, (*iter).index_count, GL_UNSIGNED_INT, 0);
gl->glBindVertexArray(0);
program->release();
}
else {
QOpenGLShader *vshader = new QOpenGLShader(QOpenGLShader::Vertex, this);
QString vertexShader =
"#version 330\n"
"uniform mat4 uProj;\n"
"uniform mat4 uView;\n"
"uniform mat4 uModel;\n"
"layout (location = 0) in vec3 position;"
"void main() {\n"
" gl_Position = uProj * uView * uModel * vec4(position, 1.0);"
"}";
vshader->compileSourceCode(vertexShader.toStdString().c_str());
QOpenGLShader *fshader = new QOpenGLShader(QOpenGLShader::Fragment, this);
QString fragmentShader =
"#version 330\n"
"out vec4 FragColor;\n"
"void main() {\n"
" FragColor = vec4(0.3, 1, 0, 1);\n"
"}";
fshader->compileSourceCode(fragmentShader.toStdString().c_str());
program = new QOpenGLShaderProgram;
program->addShader(vshader);
program->addShader(fshader);
program->link();
program->bind();
program->release();
// generate and cache it
gl->glGenVertexArrays(1, &(*iter).model.mesh.vertex_array);
gl->glBindVertexArray((*iter).model.mesh.vertex_array);
gl->glGenBuffers(1, &(*iter).model.mesh.vertex_buffer);
gl->glBindBuffer(GL_ARRAY_BUFFER, (*iter).model.mesh.vertex_buffer);
gl->glBufferData(GL_ARRAY_BUFFER,
(*iter).model.mesh.rawMesh.vertices.size() * sizeof(Vertex),
(*iter).model.mesh.rawMesh.vertices.data(),
GL_STATIC_DRAW);
gl->glGenBuffers(1, &(*iter).model.mesh.index_buffer); // Generate 1 buffer
gl->glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, (*iter).model.mesh.index_buffer);
gl->glBufferData(GL_ELEMENT_ARRAY_BUFFER,
(*iter).model.mesh.rawMesh.indices.size() * sizeof(GLuint),
(*iter).model.mesh.rawMesh.indices.data(),
GL_STATIC_DRAW);
GLint posAttrib = program->attributeLocation("position");
// gl->glVertexAttribPointer(posAttrib, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float), 0);
// gl->glEnableVertexAttribArray(posAttrib);
// gl->glBindVertexArray(0);
// iter over attribs
gl->glEnableVertexAttribArray(posAttrib);
gl->glVertexAttribPointer(posAttrib, (*iter).index_count, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)0);
gl->glBindVertexArray(0);
vaoStore.insert((*iter).command_id, (*iter).model.mesh.vertex_array);
logFunc(QString("cached a model vao id = %1").arg((*iter).model.mesh.vertex_array));
}
}
// swapper->RenderDrawCommands->clear();
threadContext->swapBuffers(surfaceWindow);
}
}
//void Renderer::render(RenderJob *job)
//{
// RenderOp *op = job->op;
// auto gl = threadContext->versionFunctions<QOpenGLFunctions_3_3_Core>();
// switch ((int) op) {
// case static_cast<int>(RenderOp::CreateMesh): {
// // create space for a new mesh
// GlobalRenderData::Mesh *mesh = new GlobalRenderData::Mesh;
// // new (mesh) GlobalRenderData::Mesh;
// GlobalRenderData::meshes[1] = mesh;
//// mesh->id =
// gl->glGenVertexArrays(1, &mesh->vertex_array);
// gl->glBindVertexArray(mesh->vertex_array);
// gl->glGenBuffers(1, &mesh->vertex_buffer);
// gl->glBindBuffer(mesh->vertex_buffer, GL_STATIC_DRAW);
// gl->glBufferData(GL_ARRAY_BUFFER,
// mesh->vertices.size() * sizeof(GLfloat),
// mesh->vertices.data(),
// GL_STATIC_DRAW);
// gl->glGenBuffers(1, &mesh->index_buffer); // Generate 1 buffer
// gl->glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mesh->index_buffer);
// gl->glBufferData(GL_ELEMENT_ARRAY_BUFFER,
// mesh->indices.size() * sizeof(GLint),
// mesh->indices.data(),
// GL_STATIC_DRAW);
// qDebug() << "created mesh";
// break;
// }
// case static_cast<unsigned>(RenderOp::DrawIndexed): {
// GlobalRenderData::Mesh *mesh = GlobalRenderData::meshes[job->id];
// gl->glBindVertexArray(mesh->vertex_array);
// gl->glDrawElements(GL_TRIANGLES, mesh->index_count, GL_UNSIGNED_INT, 0);
// gl->glBindVertexArray(0);
// break;
// }
// }
//}