-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
peelrenderer.cpp
357 lines (304 loc) · 10.8 KB
/
peelrenderer.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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
#include <stdexcept>
#include "constants.h"
#include "geometry.h"
#include "peelrenderer.h"
#include "glassopengl.h"
#include "globalbackgroundcolor.h"
#define glewGetContext() glewContext
PeelRenderer::PeelRenderer(GLEWContext *_glewContext) : glewContext(_glewContext), expectedGLContext(QGLContext::currentContext()), bufferSize(make_vector(0U, 0U)), buffer(0), colorTex(0), depthTex(0), prevDepthTex(0), peelProgram(0), nopeelProgram(0) {
if (!GLEW_ARB_texture_rectangle
|| !GLEW_ARB_window_pos
|| !GLEW_ARB_occlusion_query
|| !GLEW_EXT_blend_func_separate
|| !GLEW_ARB_depth_texture
|| !GLEW_ARB_framebuffer_object
|| !GLEW_ARB_depth_texture
|| !GLEW_ARB_shader_objects
|| !GLEW_ARB_shading_language_100
|| !GLEW_ARB_fragment_shader
|| !GLEW_ARB_vertex_shader) {
std::cerr << "WARNING: some of the extensions required for depth peeling are not present, so it will be disabled." << std::endl;
throw std::runtime_error("Missing extensions required for depth peeling.");
}
}
PeelRenderer::~PeelRenderer() {
assert(QGLContext::currentContext() == expectedGLContext);
//Clean up peeling state:
if (buffer) {
glDeleteFramebuffers(1, &buffer);
buffer = 0;
}
if (colorTex) {
glDeleteTextures(1, &colorTex);
colorTex = 0;
}
if (depthTex) {
glDeleteTextures(1, &depthTex);
depthTex = 0;
}
if (prevDepthTex) {
glDeleteTextures(1, &prevDepthTex);
prevDepthTex = 0;
}
if (peelProgram) {
glDeleteObjectARB(peelProgram);
peelProgram = 0;
}
if (nopeelProgram) {
glDeleteObjectARB(nopeelProgram);
nopeelProgram = 0;
}
}
namespace {
//convenience function: grab the error log of a shader:
std::string shader_log(GLhandleARB shader, GLEWContext *glewContext) {
assert(glewContext);
GLint len = 0;
glGetObjectParameterivARB(shader, GL_OBJECT_INFO_LOG_LENGTH_ARB, &len);
vector< GLchar > log;
log.resize(len + 1, GLchar('\0'));
GLint written = 0;
glGetInfoLogARB(shader, len, &written, &(log[0]));
assert(written <= len);
std::string out = "";
for (unsigned int i = 0; i < log.size() && log[i] != '\0'; ++i) {
out += log[i];
}
return out;
}
GLhandleARB load_program(const char *frag, GLEWContext *glewContext) {
assert(glewContext);
GLhandleARB shader = glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB);
GLint len = strlen(frag);
glShaderSourceARB(shader, 1, &frag, &len);
glCompileShaderARB(shader);
{ //check shader:
GLint val = 0;
glGetObjectParameterivARB(shader, GL_OBJECT_COMPILE_STATUS_ARB, &val);
std::string log = shader_log(shader, glewContext);
if (val == 0) {
std::cerr << "ERROR: failed compiling shader." << std::endl;
std::cerr << "Log:\n" << log << std::endl;
} else if (log != "") {
std::cerr << "WARNING: peel shader compiled, but produced messages:\n" << log << std::endl;
}
}
GLhandleARB program = glCreateProgramObjectARB();
glAttachObjectARB(program, shader);
glLinkProgramARB(program);
{ //check program:
GLint val = 0;
glGetObjectParameterivARB(program, GL_OBJECT_LINK_STATUS_ARB, &val);
std::string log = shader_log(program, glewContext);
if (val == 0) {
std::cerr << "ERROR: failed linking program." << std::endl;
std::cerr << "Log:\n" << log << std::endl;
} else if (log != "") {
std::cerr << "WARNING: peel program linked, but produced messages:\n" << log << std::endl;
}
}
glDeleteObjectARB(shader);
return program;
}
}
void PeelRenderer::render(Geometry const & geometry)
{
assert(QGLContext::currentContext() == expectedGLContext);
GlassOpenGL::errors("(before depth peeling)");
//viewport is {x,y,w,h} in window.
//We're querying here to get the width and height.
GLint viewport[4] = {0,0,0,0};
glGetIntegerv(GL_VIEWPORT, viewport);
if (viewport[2] != GLint(bufferSize.x) || viewport[3] != GLint(bufferSize.y))
{
bufferSize.x = viewport[2];
bufferSize.y = viewport[3];
//Since the buffer has changed size, (re-)init textures:
if (colorTex == 0) {
glGenTextures(1, &colorTex);
}
glBindTexture(GL_TEXTURE_RECTANGLE_ARB, colorTex);
glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGBA, bufferSize.x, bufferSize.y,
0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
glBindTexture(GL_TEXTURE_RECTANGLE_ARB, 0);
if (depthTex == 0) {
glGenTextures(1, &depthTex);
}
glBindTexture(GL_TEXTURE_RECTANGLE_ARB, depthTex);
glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_DEPTH_COMPONENT, bufferSize.x, bufferSize.y,
0, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, NULL);
glBindTexture(GL_TEXTURE_RECTANGLE_ARB, 0);
if (prevDepthTex == 0) {
glGenTextures(1, &prevDepthTex);
}
glBindTexture(GL_TEXTURE_RECTANGLE_ARB, prevDepthTex);
glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_DEPTH_COMPONENT, bufferSize.x, bufferSize.y,
0, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, NULL);
glBindTexture(GL_TEXTURE_RECTANGLE_ARB, 0);
GlassOpenGL::errors("(depth peeling setup)");
}
if (bufferSize.x == 0 || bufferSize.y == 0) return;
if (buffer == 0)
glGenFramebuffers(1, &buffer);
if (peelProgram == 0) {
const char *peel_frag =
"#version 120 \n"
"#extension GL_ARB_texture_rectangle : enable \n"
"uniform sampler2DRect min_depth; \n"
" \n"
"void main() { \n"
" vec4 tex = texture2DRect(min_depth, gl_FragCoord.xy);\n"
" float tex_depth = tex.z;\n"
" float frag_depth = gl_FragCoord.z;\n"
" if (frag_depth <= tex_depth) { \n"
" discard; \n"
" } \n"
" //Premultiply alpha to make compositing easier later: \n"
" gl_FragColor = vec4(gl_Color.rgb*gl_Color.a, gl_Color.a);\n"
"} \n";
peelProgram = load_program(peel_frag, glewContext);
GlassOpenGL::errors("compiling peel program.");
}
if (nopeelProgram == 0) {
const char *nopeel_frag =
"#version 120 \n"
"void main() { \n"
" //Premultiply alpha to make compositing easier later: \n"
" gl_FragColor = vec4(gl_Color.rgb*gl_Color.a, gl_Color.a);\n"
"} \n";
nopeelProgram = load_program(nopeel_frag, glewContext);
GlassOpenGL::errors("compiling nopeel program.");
}
GLuint query = 0;
glGenQueriesARB(1, &query);
GLint base_draw_framebuffer = 0;
GLint base_read_framebuffer = 0;
glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &base_draw_framebuffer);
glGetIntegerv(GL_READ_FRAMEBUFFER_BINDING, &base_read_framebuffer);
glDisable(GL_CULL_FACE);
//Render depth layers, front-to-back, up to MaxPasses layers:
const unsigned int MaxPasses = 20;
for (unsigned int pass = 0; pass < MaxPasses; ++pass)
{
//---------- setup framebuffer we put peelings into ----------
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, buffer);
glPushAttrib(GL_VIEWPORT_BIT);
glViewport(0, 0, bufferSize.x, bufferSize.y);
// Set up the proper depth-n-such attachments:
glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_RECTANGLE_ARB, colorTex, 0);
glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_RECTANGLE_ARB, depthTex, 0);
if (glCheckFramebufferStatus(GL_DRAW_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
std::cerr << "WARNING: FRAMEBUFFER not complete!" << std::endl;
GlassOpenGL::errors("(depth framebuffer setup)");
// Clear the framebuffer
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//Set up program to reject (close) stuff we've already rendered
if (pass == 0)
glUseProgramObjectARB(nopeelProgram);
else
{
glUseProgramObjectARB(peelProgram);
glBindTexture(GL_TEXTURE_RECTANGLE_ARB, prevDepthTex);
}
if (pass < MaxPasses - 1)
{
glEnable(GL_DEPTH_TEST);
glDisable(GL_BLEND);
}
else
{
//If we've peeled all we can peel, just render in some order and
//hope for the best:
glDisable(GL_DEPTH_TEST);
glEnable(GL_BLEND);
glBlendFunc(GL_ONE_MINUS_DST_ALPHA, GL_ONE);
}
bool do_query = (pass > 0);
//When actually peeling, remember how many fragments were used:
if (do_query)
glBeginQueryARB(GL_SAMPLES_PASSED_ARB, query);
//---------- draw scene --------
//Check that Vertex and Triangle have proper size:
assert(sizeof(Vertex) == sizeof(GLfloat) * (3 + 3));
assert(sizeof(Triangle) == sizeof(GLuint) * 3);
glVertexPointer(3, GL_FLOAT, sizeof(Vertex), &(geometry.vertices[0].position));
glNormalPointer(GL_FLOAT, sizeof(Vertex), &(geometry.vertices[0].normal));
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_NORMAL_ARRAY);
for (std::vector< Group >::const_iterator g = geometry.groups.begin(); g != geometry.groups.end(); ++g)
{
Color c = g->color;
glColor4f(c.r, c.g, c.b, c.a);
glDrawElements(GL_TRIANGLES, g->triangle_size * 3,
GL_UNSIGNED_INT, &(geometry.triangles[g->triangle_begin].v1));
}
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_NORMAL_ARRAY);
//--------------------------
if (do_query)
glEndQueryARB(GL_SAMPLES_PASSED_ARB);
//Done drawing scene, change drawing framebuffer to visible one
glPopAttrib();
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, base_draw_framebuffer);
GlassOpenGL::errors("(depth framebuffer render)");
glUseProgramObjectARB(0);
glBindTexture(GL_TEXTURE_RECTANGLE_ARB, 0);
//Check whether fragements were passed, break out if no more fragments
if (do_query)
{
GLuint count = 0;
glGetQueryObjectuivARB(query, GL_QUERY_RESULT_ARB, &count);
if (count == 0)
break;
}
//swap out depth textures, now that we've rendered a new one:
std::swap(prevDepthTex, depthTex);
//Copy pixels over to visible framebuffer:
glBindFramebuffer(GL_READ_FRAMEBUFFER, buffer);
glWindowPos2iARB(0,0);
if (pass == 0)
glDisable(GL_BLEND);
else
{
glEnable(GL_BLEND);
glBlendFunc(GL_ONE_MINUS_DST_ALPHA, GL_ONE);
}
glDisable(GL_DEPTH_TEST);
glCopyPixels(0, 0, bufferSize.x, bufferSize.y, GL_COLOR);
glBindFramebuffer(GL_READ_FRAMEBUFFER, base_read_framebuffer);
GlassOpenGL::errors("(copy framebuffer)");
}
glDeleteQueriesARB(1, &query);
//final pass -- render background color behind everything else.
glDisable(GL_DEPTH_TEST);
glEnable(GL_BLEND);
glBlendFunc(GL_ONE_MINUS_DST_ALPHA, GL_ONE);
glDisable(GL_LIGHTING);
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
glBegin(GL_QUADS);
glColor3f(GlobalBackgroundColor::color.r,
GlobalBackgroundColor::color.g,
GlobalBackgroundColor::color.b);
glVertex2f(-1.1f,-1.1f);
glVertex2f( 1.1f,-1.1f);
glVertex2f( 1.1f, 1.1f);
glVertex2f(-1.1f, 1.1f);
glEnd();
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
glEnable(GL_LIGHTING);
}