-
Notifications
You must be signed in to change notification settings - Fork 0
/
pyramid_point_renderer_base.cc
561 lines (433 loc) · 16.9 KB
/
pyramid_point_renderer_base.cc
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
/*
** pyramid_point_render.cc Pyramid Point Based Rendering.
**
**
** history: created 02-Jul-07
*/
#include "pyramid_point_renderer_base.h"
#include <stdexcept>
using std::runtime_error;
#define FOO(a) case a: throw std::runtime_error( where + ": " #a ); break
void
checkFramebufferStatus( const std::string& where )
{
GLenum status = glCheckFramebufferStatusEXT( GL_FRAMEBUFFER_EXT );
switch( status ) {
case GL_FRAMEBUFFER_COMPLETE_EXT:
break;
FOO( GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT );
FOO( GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT );
// FOO( GL_FRAMEBUFFER_INCOMPLETE_DUPLICATE_ATTACHMENT_EXT );
FOO( GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT );
FOO( GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT );
FOO( GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER_EXT );
FOO( GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT );
FOO( GL_FRAMEBUFFER_UNSUPPORTED_EXT );
// FOO( GL_FRAMEBUFFER_STATUS_ERROR_EXT );
default:
throw std::runtime_error( where + ": Framebuffer error.\n" );
}
}
void PyramidPointRendererBase::init ( void ) {
cout << canvas_width << " " << canvas_height << endl;
levels_count = min((int)(log(canvas_width)/log(2.0)), (int)(log(canvas_height)/log(2.0)));
cout << "LEVELS : " << (int)(log(canvas_width)/log(2.0)) << " " << (int)(log(canvas_height)/log(2.0)) << " " << levels_count << endl;
resetPointers();
createFBO();
vertices[0][0] = 0.0;
vertices[0][1] = 0.0;
vertices[1][0] = 0.0;
vertices[1][1] = canvas_height;
vertices[2][0] = canvas_width;
vertices[2][1] = canvas_height;
vertices[3][0] = canvas_width;
vertices[3][1] = 0.0;
texcoors0[0][0] = 0.0;
texcoors0[0][1] = 0.0;
texcoors0[1][0] = 0.0;
texcoors0[1][1] = 1.0;
texcoors0[2][0] = 1.0;
texcoors0[2][1] = 1.0;
texcoors0[3][0] = 1.0;
texcoors0[3][1] = 0.0;
}
/**
* Default constructor.
**/
PyramidPointRendererBase::PyramidPointRendererBase() : PointBasedRenderer(),
fbo_buffers_count(2) {
init();
}
PyramidPointRendererBase::PyramidPointRendererBase(int w, int h) : PointBasedRenderer(w, h),
fbo_buffers_count(2) {
init();
}
PyramidPointRendererBase::PyramidPointRendererBase(int w, int h, int fbos) : PointBasedRenderer(w, h),
fbo_buffers_count(fbos) {
init();
}
PyramidPointRendererBase::~PyramidPointRendererBase() {
glDeleteTextures(fbo_buffers_count, fbo_textures);
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
glDrawBuffer(GL_BACK);
glDeleteTextures(1, &fbo_depth);
fbo_lod.clear();
delete [] fbo_buffers;
delete [] fbo_textures;
delete [] shader_texture_names;
}
/**
* Renders the QUAD with textures.
* Each fragment in shader will read one pixel from texture.
**/
const void PyramidPointRendererBase::rasterizePixels(void)
{
// Rasterize texture as quads.
// Uses multiple tex coords in the case of reading from multiple levels
// at the same time. (ex. synthesis phase)
glBegin(GL_QUADS);
glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
for(int i = 0; i < 4; i++) {
glMultiTexCoord2fARB(GL_TEXTURE0, texcoors0[i][0], texcoors0[i][1]);
glVertex2f(vertices[i][0], vertices[i][1]);
}
glEnd();
}
/**
* Activates textures to be accessed from shaders.
* @param text_id Texture number
* @param target_id GL_TEXTURE number, this must correspond to shader uniform var.
**/
const void PyramidPointRendererBase::activateTexture(const int text_id, const int target_id) {
glActiveTexture(GL_TEXTURE0 + target_id);
if (text_id == -1)
glBindTexture(FBO_TYPE, (GLuint)0);
else {
glBindTexture(FBO_TYPE, fbo_textures[text_id]);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
}
}
/**
* Project point samples to screen space.
* @param obj Pointer to object for rendering.
**/
void PyramidPointRendererBase::projectSurfels ( const Object* const obj )
{
int level = 0;
// render targets (GL_COLOR_ATTACHMENTs)
GLuint buffers[fbo_buffers_count];
for (int i = 0; i < fbo_buffers_count; ++i)
buffers[i] = fbo_buffers[i];
//fbo_lod[level]->bind();
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo_lod[level]);
glDrawBuffers(fbo_buffers_count, buffers);
mShaderProjection.prog.Bind();
mShaderProjection.prog.Uniform("canvas_size", (GLfloat)canvas_width, (GLfloat)canvas_height);
mShaderProjection.prog.Uniform("eye", (GLfloat)eye[0], (GLfloat)eye[1], (GLfloat)eye[2]);
mShaderProjection.prog.Uniform("back_face_culling", (GLint)back_face_culling);
mShaderProjection.prog.Uniform("scale", (GLfloat)scale_factor);
// Render vertices from surfel list.
glPointSize(1.0);
obj->render();
mShaderProjection.prog.Unbind();
// fbo_lod[level]->release();
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
}
/**
* Pull phase.
* Create pyramid structure starting one level above base level (already created with projection).
* Each pixel is the average of the four pixels in level below.
**/
void PyramidPointRendererBase::rasterizeAnalysisPyramid( void ) {
// render targets (GL_COLOR_ATTACHMENTs)
GLuint buffers[fbo_buffers_count];
for (int i = 0; i < fbo_buffers_count; ++i)
buffers[i] = fbo_buffers[i];
// source textures (samplers) that are acessed in shaders (GL_TEXTURE0, GL_TEXTURE1 ...)
for (int i = 0; i < fbo_buffers_count; ++i)
activateTexture(i, i);
mShaderAnalysis.prog.Bind();
mShaderAnalysis.prog.Uniform("prefilter_size", (GLfloat)(prefilter_size));
mShaderAnalysis.prog.Uniform("reconstruction_filter_size", (GLfloat)(reconstruction_filter_size));
mShaderAnalysis.prog.Uniform("depth_test", depth_test);
for (int i = 0; i < fbo_buffers_count; ++i)
mShaderAnalysis.prog.Uniform(shader_texture_names[i].c_str(), i); //samplers
mShaderAnalysis.prog.Unbind();
GLfloat lw, lh, ratio_w, ratio_h;
// Reconstructs all lower resolution levels bottom-up fashion
for (int level = 1; level < levels_count; level++)
{
lw = floorf(canvas_width / pow(2.0, level));
lh = floorf(canvas_height / pow(2.0, level));
glViewport(0, 0, lw, lh);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, lw, 0.0, lh);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
vertices[1][1] = lh;
vertices[2][0] = lw;
vertices[2][1] = lh;
vertices[3][0] = lw;
//fbo_lod[level]->bind();
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo_lod[level]);
glDrawBuffers(fbo_buffers_count, buffers);
ratio_w = lw;
ratio_h = lh;
lw = floorf(canvas_width / pow(2.0, level-1));
lh = floorf(canvas_height / pow(2.0, level-1));
mShaderAnalysis.prog.Bind();
mShaderAnalysis.prog.Uniform("level", (GLint)level);
mShaderAnalysis.prog.Uniform("offset", (GLfloat)0.25/lw, (GLfloat)0.25/lh);
mShaderAnalysis.prog.Uniform("level_ratio", (GLfloat)(2.0*ratio_w/lw), (GLfloat)(2.0*ratio_h/lh));
rasterizePixels();
mShaderAnalysis.prog.Unbind();
//fbo_lod[level]->release();
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
}
}
/**
* Push phase.
* Traverses pyramid from top to bottom,
* synthesize missing pixels by searching the four closest pixels of level above.
**/
void PyramidPointRendererBase::rasterizeSynthesisPyramid( void )
{
/// render targets (GL_COLOR_ATTACHMENTs)
GLuint buffers[fbo_buffers_count];
for (int i = 0; i < fbo_buffers_count; ++i)
buffers[i] = fbo_buffers[i];
/// source textures (samplers) that are acessed in shaders (GL_TEXTURE0, GL_TEXTURE1 ...)
for (int i = 0; i < fbo_buffers_count; ++i)
activateTexture(i, i);
mShaderSynthesis.prog.Bind();
mShaderSynthesis.prog.Uniform("minimum_size", (GLfloat)(minimum_radius_size));
mShaderSynthesis.prog.Uniform("reconstruction_filter_size", (GLfloat)(reconstruction_filter_size));
mShaderSynthesis.prog.Uniform("prefilter_size", (GLfloat)(prefilter_size));
mShaderSynthesis.prog.Uniform("depth_test", depth_test);
for (int i = 0; i < fbo_buffers_count; ++i)
mShaderSynthesis.prog.Uniform(shader_texture_names[i].c_str(), i);
mShaderSynthesis.prog.Unbind();
GLfloat lw, lh, ratio_w, ratio_h;
for (int level = levels_count - 2; level >= 0; level--)
{
lw = floorf(canvas_width / pow(2.0, level));
lh = floorf(canvas_height / pow(2.0, level));
glViewport(0, 0, lw, lh);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, lw, 0.0, lh);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
/// maximum coordinates for drawing quad
vertices[1][1] = lh;
vertices[2][0] = lw;
vertices[2][1] = lh;
vertices[3][0] = lw;
//fbo_lod[level]->bind();
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo_lod[level]);
glDrawBuffers(fbo_buffers_count, buffers);
mShaderSynthesis.prog.Bind();
mShaderSynthesis.prog.Uniform("level", (GLint)level);
ratio_w = lw;
ratio_h = lh;
lw = floorf(canvas_width / pow(2.0, level+1));
lh = floorf(canvas_height / pow(2.0, level+1));
mShaderSynthesis.prog.Uniform("half_pixel_size", (GLfloat)0.5/lw, (GLfloat)0.5/lh);
check_for_ogl_error("uniforms 0");
mShaderSynthesis.prog.Uniform("level_ratio", (GLfloat)(0.5*ratio_w/lw), (GLfloat)(0.5*ratio_h/lh));
check_for_ogl_error("uniforms 1");
mShaderSynthesis.prog.Uniform("canvas_ratio", (GLfloat)lw/lh);
check_for_ogl_error("uniforms 2");
rasterizePixels();
mShaderSynthesis.prog.Unbind();
//fbo_lod[level]->release();
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
}
}
/**
* Deferred shading of synthesised base level
**/
void PyramidPointRendererBase::rasterizePhongShading(void)
{
int level = 0;
mShaderPhong.prog.Bind();
mShaderPhong.prog.Uniform("color_ambient", Mats[material_id][0], Mats[material_id][1], Mats[material_id][2], Mats[material_id][3]);
mShaderPhong.prog.Uniform("color_diffuse", Mats[material_id][4], Mats[material_id][5], Mats[material_id][6], Mats[material_id][7]);
mShaderPhong.prog.Uniform("color_specular", Mats[material_id][8], Mats[material_id][9], Mats[material_id][10], Mats[material_id][11]);
mShaderPhong.prog.Uniform("shininess", Mats[material_id][12]);
mShaderPhong.prog.Uniform("level", (GLint)level);
/// samplers, binds normal texture, then binds extra attributes if any starting from third texture (color ...)
mShaderPhong.prog.Uniform(shader_texture_names[0].c_str(), 0);
for (int i = 2; i < fbo_buffers_count; ++i)
mShaderPhong.prog.Uniform(shader_texture_names[i].c_str(), i-1);
glViewport(0, 0, canvas_width, canvas_height);
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
glDrawBuffer(GL_BACK);
activateTexture(0, 0);
/// source textures that are acessed in shaders
for (int i = 2; i < fbo_buffers_count; ++i)
activateTexture(i, i-1);
rasterizePixels();
mShaderPhong.prog.Unbind();
/// clear
for (int i = 0; i < fbo_buffers_count; ++i)
activateTexture(-1, i);
}
/**
* Clear all framebuffers and screen buffer.
**/
void PyramidPointRendererBase::clearBuffers( void ) {
mShaderProjection.prog.Unbind();
mShaderAnalysis.prog.Unbind();
mShaderSynthesis.prog.Unbind();
mShaderPhong.prog.Unbind();
glEnable(FBO_TYPE);
glDisable(GL_BLEND);
glEnable(GL_DEPTH_TEST);
glDepthMask(GL_TRUE);
glDepthFunc(GL_LESS);
GLint currentDrawBuffer;
glGetIntegerv(GL_DRAW_BUFFER, ¤tDrawBuffer);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
check_for_ogl_error("before clearing ");
/// clear all buffers of all fbos
for (int level = 0; level < levels_count; level++) {
//fbo_lod[level]->bind();
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo_lod[level]);
for (int j = 0; j < fbo_buffers_count; j++) {
glDrawBuffer(fbo_buffers[j]);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}
checkFramebufferStatus( __func__ );
check_for_ogl_error("clearing");
//fbo_lod[level]->release();
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
}
/// Clear the back buffer
glDrawBuffer(GL_BACK);
glClearColor(0.7f, 0.7f, 0.8f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
check_for_ogl_error("clear buffers");
}
/**
* Reconstructs the surface for visualization.
**/
void PyramidPointRendererBase::projectSamples(Object* const obj) {
// Project points to framebuffer with depth test on.
projectSurfels( obj );
check_for_ogl_error("project samples");
}
/**
* Interpolate projected samples using pyramid interpolation
* algorithm.
**/
void PyramidPointRendererBase::interpolate() {
glDisable(GL_DEPTH_TEST);
glDepthMask(GL_FALSE);
/// Set the Ortho once, while viewport is set for each pyramid level render pass
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, canvas_width, 0.0, canvas_height);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
/// Pull phase - Create pyramid structure
rasterizeAnalysisPyramid();
check_for_ogl_error("analysis");
/// Push phase - Interpolate scattered data
rasterizeSynthesisPyramid();
check_for_ogl_error("synthesis");
}
/**
* Renders reconstructed model on screen with
* per pixel shading.
**/
void PyramidPointRendererBase::draw( void ) {
glEnable(GL_DEPTH_TEST);
glDepthMask(GL_TRUE);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glDisable(GL_COLOR_MATERIAL);
/// These parameters are matching the Splatting Plugin for comparison
glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 64);
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, vcg::Point4f(0.3, 0.3, 0.3, 1.).V());
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, vcg::Point4f(0.6, 0.6, 0.6, 1.).V());
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, vcg::Point4f(0.5, 0.5, 0.5, 1.).V());
/// Deffered shading of the final image containing normal map
rasterizePhongShading();
glDisable(FBO_TYPE);
check_for_ogl_error("draw");
}
/**
* Initialize OpenGL state variables.
**/
void PyramidPointRendererBase::createFBO() {
assert(fbo_buffers_count <= 16);
fbo_buffers = new GLuint[fbo_buffers_count];
fbo_textures = new GLuint[fbo_buffers_count];
check_for_ogl_error("new arrays fbo");
/// First create one mipmap texture for each render target
glGenTextures(fbo_buffers_count, fbo_textures);
for (int i = 0; i < fbo_buffers_count; i++) {
fbo_buffers[i] = GL_COLOR_ATTACHMENT0_EXT + i;
glBindTexture(FBO_TYPE, fbo_textures[i]);
glTexImage2D(FBO_TYPE, 0, FBO_FORMAT, canvas_width, canvas_height, 0, GL_RGBA, GL_FLOAT, NULL);
glGenerateMipmapEXT(FBO_TYPE);
glTexParameteri(FBO_TYPE, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST);
glTexParameteri(FBO_TYPE, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(FBO_TYPE, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(FBO_TYPE, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexParameteri(FBO_TYPE, GL_TEXTURE_BASE_LEVEL, 0 );
glTexParameteri(FBO_TYPE, GL_TEXTURE_MAX_LEVEL, levels_count );
}
check_for_ogl_error("buffers creation");
/// create a depth buffer:
glGenTextures(1, &fbo_depth);
glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, fbo_depth);
glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT32, canvas_width, canvas_height);
check_for_ogl_error("depth buffer creation");
fbo_lod.resize(levels_count);
check_for_ogl_error("framebuffer creation");
/// now attach all textures to fbos, each fbo stores one mipmap level of all render targets
for (int level = 0; level < levels_count; level++) {
int dim = 1024/pow(2.0, double(level));
// fbo_lod[level] = new QGLFramebufferObject(dim, dim, FBO_TYPE);
// if (!fbo_lod[level]->isValid())
// std::cout << level << " PyramidPointRenderer: invalid FBO\n";
// fbo_lod[level]->bind();
glGenFramebuffersEXT(1, &fbo_lod[level]);
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo_lod[level]);
// for each level: attach all render targets to the fbo
for (int i = 0; i < fbo_buffers_count; i++) {
glBindTexture(FBO_TYPE, fbo_textures[i]);
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, fbo_buffers[i], FBO_TYPE, fbo_textures[i], level);
}
check_for_ogl_error("fbo attachment");
//checkFramebufferStatus( __func__ );
//fbo_lod[level]->release();
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
check_for_ogl_error("fbo attachment");
}
/// And lets also attach the depth buffer to the first fbo, that is, level 0 of the pyramid
//fbo_lod[0]->bind();
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo_lod[0]);
glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT,
GL_RENDERBUFFER_EXT, fbo_depth);
check_for_ogl_error("depth attachment");
// fbo_lod[0]->release();
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
check_for_ogl_error("fbo_mipmap");
}
// QString PyramidPointRendererBase::loadShaderSource(const QString& filename) const {
// QString res;
// QFile f(shaders_path.absolutePath() + "/shaders/splatpyramid/" + filename);
// if (!f.open(QFile::ReadOnly))
// {
// std::cerr << "failed to load shader file " << filename.toAscii().data() << "\n";
// }
// // else qDebug("Succesfully loaded shader");
// QTextStream stream(&f);
// res = stream.readAll();
// f.close();
// return res;
// }