-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmygl.cpp
More file actions
312 lines (248 loc) · 12 KB
/
mygl.cpp
File metadata and controls
312 lines (248 loc) · 12 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
#include "mygl.h"
#include <la.h>
#include <QResizeEvent>
#include <iostream>
MyGL::MyGL(QWidget *parent)
: OpenGLContext(parent),
m_geomQuad(this), m_camera(640, 480, glm::vec3(0, 0, 12), glm::vec3(0, 0, 0), glm::vec3(0, 1, 0)),
m_surfaceShaders(), m_postprocessShaders(),
mp_progSurfaceCurrent(nullptr),
mp_progPostprocessCurrent(nullptr),
m_matcapTextures(), mp_matcapTexCurrent(nullptr),
m_frameBuffer(-1),
m_renderedTexture(-1),
m_depthRenderBuffer(-1),
m_time(0.f), m_mousePosPrev()
{
setFocusPolicy(Qt::StrongFocus);
}
MyGL::~MyGL()
{
makeCurrent();
glDeleteVertexArrays(1, &m_vao);
m_geomQuad.destroy();
}
void MyGL::initializeGL()
{
// Create an OpenGL context using Qt's QOpenGLFunctions_3_2_Core class
// If you were programming in a non-Qt context you might use GLEW (GL Extension Wrangler)instead
initializeOpenGLFunctions();
// Print out some information about the current OpenGL context
debugContextVersion();
// Set a few settings/modes in OpenGL rendering
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
glEnable(GL_LINE_SMOOTH);
glEnable(GL_POLYGON_SMOOTH);
glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
glHint(GL_POLYGON_SMOOTH_HINT, GL_NICEST);
// Set the size with which points should be rendered
glPointSize(5);
// Set the color with which the screen is filled at the start of each render call.
glClearColor(0.5, 0.5, 0.5, 1);
printGLErrorLog();
// Create a Vertex Attribute Object
glGenVertexArrays(1, &m_vao);
createRenderBuffers();
m_geomQuad.create();
createShaders();
createMeshes();
createMatcapTextures();
// We have to have a VAO bound in OpenGL 3.2 Core. But if we're not
// using multiple VAOs, we can just bind one once.
glBindVertexArray(m_vao);
}
void MyGL::resizeGL(int w, int h)
{
mp_progSurfaceCurrent->setViewProjMatrix(m_camera.getView(), m_camera.getProj());
for(std::shared_ptr<PostProcessShader> p : m_postprocessShaders)
{
p->setDimensions(glm::ivec2(w * this->devicePixelRatio(), h * this->devicePixelRatio()));
}
printGLErrorLog();
}
//This function is called by Qt any time your GL window is supposed to update
//For example, when the function update() is called, paintGL is called implicitly.
void MyGL::paintGL()
{
render3DScene();
performPostprocessRenderPass();
mp_progSurfaceCurrent->setTime(m_time);
mp_progPostprocessCurrent->setTime(m_time);
m_time++;
}
void MyGL::render3DScene()
{
// Render the 3D scene to our frame buffer
// Render to our framebuffer rather than the viewport
glBindFramebuffer(GL_FRAMEBUFFER, m_frameBuffer);
// Render on the whole framebuffer, complete from the lower left corner to the upper right
glViewport(0,0,this->width() * this->devicePixelRatio(), this->height() * this->devicePixelRatio());
// Clear the screen so that we only see newly drawn images
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Draw the background texture first
mp_modelCurrent->bindBGTexture();
mp_progPostprocessNoOp->draw(m_geomQuad, 2);
// Set the surface shader's transformation matrices
mp_progSurfaceCurrent->setViewProjMatrix(m_camera.getView(), m_camera.getProj());
mp_progSurfaceCurrent->setModelMatrix(glm::mat4());
bindAppropriateTexture();
// Draw the model
mp_progSurfaceCurrent->draw(*mp_modelCurrent, 0);
}
void MyGL::performPostprocessRenderPass()
{
// Render the frame buffer as a texture on a screen-size quad
// Tell OpenGL to render to the viewport's frame buffer
glBindFramebuffer(GL_FRAMEBUFFER, this->defaultFramebufferObject());
// Render on the whole framebuffer, complete from the lower left corner to the upper right
glViewport(0,0,this->width() * this->devicePixelRatio(), this->height() * this->devicePixelRatio());
// Clear the screen
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Bind our texture in Texture Unit 0
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, m_renderedTexture);
mp_progPostprocessCurrent->draw(m_geomQuad, 0);
}
void MyGL::createRenderBuffers()
{
// Initialize the frame buffers and render textures
glGenFramebuffers(1, &m_frameBuffer);
glGenTextures(1, &m_renderedTexture);
glGenRenderbuffers(1, &m_depthRenderBuffer);
glBindFramebuffer(GL_FRAMEBUFFER, m_frameBuffer);
// Bind our texture so that all functions that deal with textures will interact with this one
glBindTexture(GL_TEXTURE_2D, m_renderedTexture);
// Give an empty image to OpenGL ( the last "0" )
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, this->width() * this->devicePixelRatio(), this->height() * this->devicePixelRatio(), 0, GL_RGB, GL_UNSIGNED_BYTE, (void*)0);
// Set the render settings for the texture we've just created.
// Essentially zero filtering on the "texture" so it appears exactly as rendered
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
// Clamp the colors at the edge of our texture
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
// Initialize our depth buffer
glBindRenderbuffer(GL_RENDERBUFFER, m_depthRenderBuffer);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, this->width() * this->devicePixelRatio(), this->height() * this->devicePixelRatio());
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, m_depthRenderBuffer);
// Set m_renderedTexture as the color output of our frame buffer
glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, m_renderedTexture, 0);
// Sets the color output of the fragment shader to be stored in GL_COLOR_ATTACHMENT0, which we previously set to m_renderedTextures[i]
GLenum drawBuffers[1] = {GL_COLOR_ATTACHMENT0};
glDrawBuffers(1, drawBuffers); // "1" is the size of drawBuffers
if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
{
std::cout << "Frame buffer did not initialize correctly..." << std::endl;
printGLErrorLog();
}
}
void MyGL::createShaders()
{
// Surface shaders
std::shared_ptr<SurfaceShader> lambert = std::make_shared<SurfaceShader>(this);
lambert->create(":/glsl/surface/lambert.vert.glsl", ":/glsl/surface/lambert.frag.glsl");
m_surfaceShaders.push_back(lambert);
std::shared_ptr<SurfaceShader> blinnPhong = std::make_shared<SurfaceShader>(this);
blinnPhong->create(":/glsl/surface/blinnPhong.vert.glsl", ":/glsl/surface/blinnPhong.frag.glsl");
m_surfaceShaders.push_back(blinnPhong);
std::shared_ptr<SurfaceShader> matcap = std::make_shared<SurfaceShader>(this);
matcap->create(":/glsl/surface/matcap.vert.glsl", ":/glsl/surface/matcap.frag.glsl");
m_surfaceShaders.push_back(matcap);
std::shared_ptr<SurfaceShader> gradient = std::make_shared<SurfaceShader>(this);
gradient->create(":/glsl/surface/gradient.vert.glsl", ":/glsl/surface/gradient.frag.glsl");
m_surfaceShaders.push_back(gradient);
std::shared_ptr<SurfaceShader> deform = std::make_shared<SurfaceShader>(this);
deform->create(":/glsl/surface/deform.vert.glsl", ":/glsl/surface/deform.frag.glsl");
m_surfaceShaders.push_back(deform);
slot_setCurrentSurfaceShaderProgram(0);
// Post-process shaders
std::shared_ptr<PostProcessShader> noOp = std::make_shared<PostProcessShader>(this);
noOp->create(":/glsl/post/passthrough.vert.glsl", ":/glsl/post/noOp.frag.glsl");
m_postprocessShaders.push_back(noOp);
std::shared_ptr<PostProcessShader> greyscale = std::make_shared<PostProcessShader>(this);
greyscale->create(":/glsl/post/passthrough.vert.glsl", ":/glsl/post/greyscale.frag.glsl");
m_postprocessShaders.push_back(greyscale);
std::shared_ptr<PostProcessShader> gaussian = std::make_shared<PostProcessShader>(this);
gaussian->create(":/glsl/post/passthrough.vert.glsl", ":/glsl/post/gaussian.frag.glsl");
m_postprocessShaders.push_back(gaussian);
std::shared_ptr<PostProcessShader> sobel = std::make_shared<PostProcessShader>(this);
sobel->create(":/glsl/post/passthrough.vert.glsl", ":/glsl/post/sobel.frag.glsl");
m_postprocessShaders.push_back(sobel);
std::shared_ptr<PostProcessShader> bloom = std::make_shared<PostProcessShader>(this);
bloom->create(":/glsl/post/passthrough.vert.glsl", ":/glsl/post/bloom.frag.glsl");
m_postprocessShaders.push_back(bloom);
std::shared_ptr<PostProcessShader> worley = std::make_shared<PostProcessShader>(this);
worley->create(":/glsl/post/passthrough.vert.glsl", ":/glsl/post/worleywarp.frag.glsl");
m_postprocessShaders.push_back(worley);
slot_setCurrentPostprocessShaderProgram(0);
mp_progPostprocessNoOp = m_postprocessShaders[0].get();
}
void MyGL::createMeshes()
{
std::shared_ptr<Mesh> wahoo = std::make_shared<Mesh>(this);
wahoo->createFromOBJ(":/objs/wahoo.obj", ":/textures/wahoo.bmp", ":/textures/smb.jpg");
wahoo->loadTexture();
wahoo->loadBGTexture();
m_models.push_back(wahoo);
std::shared_ptr<Mesh> cow = std::make_shared<Mesh>(this);
cow->createFromOBJ(":/objs/cow.obj", ":/textures/uvTest.jpg", ":/textures/winxp.jpg");
cow->loadTexture();
cow->loadBGTexture();
m_models.push_back(cow);
std::shared_ptr<Mesh> cube = std::make_shared<Mesh>(this);
cube->createCube(":/textures/fractal.jpg", ":/textures/mengersponge.jpg");
cube->loadTexture();
cube->loadBGTexture();
m_models.push_back(cube);
slot_setCurrentModel(0);
}
void MyGL::createMatcapTextures()
{
std::shared_ptr<Texture> zbrush = std::make_shared<Texture>(this);
zbrush->create(":/textures/matcaps/00ZBrush_RedWax.png");
m_matcapTextures.push_back(zbrush);
std::shared_ptr<Texture> redPlastic = std::make_shared<Texture>(this);
redPlastic->create(":/textures/matcaps/JG_Red.png");
m_matcapTextures.push_back(redPlastic);
std::shared_ptr<Texture> chrome = std::make_shared<Texture>(this);
chrome->create(":/textures/matcaps/silver.jpg");
m_matcapTextures.push_back(chrome);
std::shared_ptr<Texture> pearl = std::make_shared<Texture>(this);
pearl->create(":/textures/matcaps/pearl.jpg");
m_matcapTextures.push_back(pearl);
std::shared_ptr<Texture> orangeGreen = std::make_shared<Texture>(this);
orangeGreen->create(":/textures/matcaps/JGSpecial_01.png");
m_matcapTextures.push_back(orangeGreen);
std::shared_ptr<Texture> blurple = std::make_shared<Texture>(this);
blurple->create(":/textures/matcaps/Shiny_Fire_1c.png");
m_matcapTextures.push_back(blurple);
std::shared_ptr<Texture> outline = std::make_shared<Texture>(this);
outline->create(":/textures/matcaps/Outline.png");
m_matcapTextures.push_back(outline);
std::shared_ptr<Texture> normals = std::make_shared<Texture>(this);
normals->create(":/textures/matcaps/normals.jpg");
m_matcapTextures.push_back(normals);
// If this vector of textures were to be altered
// while the program was running, storing a pointer
// to an element within it would be bad news, since
// the elements inside would not be guaranteed to be
// in the same memory as before.
// However, this vector's contents are only ever modified
// in this function, which is called exactly once in the
// program's lifetime.
slot_setCurrentMatcapTexture(0);
}
void MyGL::bindAppropriateTexture()
{
// Matcap
if(mp_progSurfaceCurrent == m_surfaceShaders[2].get())
{
mp_matcapTexCurrent->bind(0);
}
// Default case, use the texture provided by the model to be rendered
else
{
mp_modelCurrent->bindTexture();
}
}