File: radianceScalingRenderer.cpp

package info (click to toggle)
meshlab 1.3.2+dfsg1-4
  • links: PTS, VCS
  • area: main
  • in suites: buster, sid
  • size: 21,096 kB
  • ctags: 33,630
  • sloc: cpp: 224,813; ansic: 8,170; xml: 119; makefile: 80
file content (283 lines) | stat: -rw-r--r-- 9,015 bytes parent folder | download | duplicates (3)
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
/****************************************************************************
* Render Radiance Scaling                                                   *
* Meshlab's plugin                                                          *
*                                                                           *
* Copyright(C) 2010                                                         *
* Vergne Romain, Dumas Olivier                                              *
* INRIA - Institut Nationnal de Recherche en Informatique et Automatique    *
*                                                                           *
* All rights reserved.                                                      *
*                                                                           *
* This program is free software; you can redistribute it and/or modify      *
* it under the terms of the GNU General Public License as published by      *
* the Free Software Foundation; either version 2 of the License, or         *
* (at your option) any later version.                                       *
*                                                                           *
* This program is distributed in the hope that it will be useful,           *
* but WITHOUT ANY WARRANTY; without even the implied warranty of            *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             *
* GNU General Public License (http://www.gnu.org/licenses/gpl.txt)          *
* for more details.                                                         *
*                                                                           *
****************************************************************************/
#include "radianceScalingRenderer.h"
#include <QtGui>
#include <math.h>
#include <stdlib.h>
#include <iostream>
#include <QGLWidget>
#include <QTextStream>
#include <QResource>

using namespace std;

#define GL_TEST_ERR							\
  {									\
   GLenum eCode;							\
   if((eCode=glGetError())!=GL_NO_ERROR)				\
     std::cerr << "OpenGL error : " <<  gluErrorString(eCode) << " in " <<  __FILE__ << " : " << __LINE__ << std::endl;	\
 }

RadianceScalingRendererPlugin::RadianceScalingRendererPlugin()
  : _supported(false),
    _sDialog(0),
    _fbo(NULL),
    _buffPass(NULL),
    _rsPass(NULL),
    _depthTex(NULL), 
    _gradTex(NULL), 
    _normTex(NULL),
    _colorTex(NULL),
    _convexLS(NULL),
    _concavLS(NULL) {

}

void RadianceScalingRendererPlugin::Init(QAction *, MeshDocument &, RenderMode &, QGLWidget *gla) {
  if(_sDialog) {
    _sDialog->close();
    delete _sDialog;
    _sDialog=0;
  }

  gla->makeCurrent();
  glewInit();

  GL_TEST_ERR

  if(!GLEW_ARB_vertex_program   ||
     !GLEW_ARB_fragment_program ||
     !GLEW_ARB_texture_float    ||
     !GLEW_ARB_draw_buffers     ||
     !GLEW_EXT_framebuffer_object) {

    _supported = false;
    return;
  }
  
  _supported = true;
  _sDialog = new ShaderDialog(this,gla);
  _sDialog->move(10,100);
  _sDialog->show();

  _sDialog->changeIcon(":/RadianceScalingRenderer/litSpheres/ls02.png",ShaderDialog::convex_icon);
  _sDialog->changeIcon(":/RadianceScalingRenderer/litSpheres/ls01.png",ShaderDialog::concav_icon);

  createLit(":/RadianceScalingRenderer/litSpheres/ls02.png",0);
  createLit(":/RadianceScalingRenderer/litSpheres/ls01.png",1);

  initFBOs();
  GL_TEST_ERR

  initShaders();
  GL_TEST_ERR
}

void RadianceScalingRendererPlugin::Render(QAction *, MeshDocument &md, RenderMode &rm, QGLWidget *) {
  checkViewport();

  // first pass: buffers 
  _fbo->bind();
  glDrawBuffers(3,FramebufferObject::buffers(0));
  glClearColor(0.0,0.0,0.0,1.0);
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);  
  _buffPass->enable();
  foreach(MeshModel *mp,md.meshList) {
    if(mp->visible) mp->render(rm.drawMode,rm.colorMode,rm.textureMode);
  }
  _buffPass->disable();
  
  // second pass: descriptor, radiance scaling, lighting 
  FramebufferObject::unbind();
  swapToScreenMode();
  _rsPass->enable();
  drawQuad();
  _rsPass->disable();
  swapToWorldMode();
}

void RadianceScalingRendererPlugin::Finalize(QAction *, MeshDocument *, GLArea *) {
  cleanShaders();
  cleanFBOs();

  if(_sDialog) {
    _sDialog->close();
    delete _sDialog;
    _sDialog=0;
  }

  if(_convexLS!=NULL) {
    delete _convexLS;
    _convexLS = NULL;
  }

  if(_concavLS!=NULL) {
    delete _concavLS;
    _concavLS = NULL;
  }
}
 
void RadianceScalingRendererPlugin::initActionList() {
  _actionList << new QAction("Radiance Scaling",this);
}

void RadianceScalingRendererPlugin::createLit(const QString &filename,int type) {
  QImage t;
  QImage b;
  
  if(!b.load(filename)) {
    return;
  } 

  t = QGLWidget::convertToGLFormat(b);

  if(type==0) {
    if(_convexLS!=NULL) {
      delete _convexLS;
      _convexLS = NULL;
    }
    
    _convexLS = new UbyteTexture2D(TextureFormat(GL_TEXTURE_2D,t.width(),t.height(),3,GL_RGBA,GL_UNSIGNED_BYTE),TextureParams(GL_LINEAR,GL_LINEAR),t.bits());
  } else {

    if(_concavLS!=NULL) {
      delete _concavLS;
      _concavLS = NULL;
    }
    
    _concavLS = new UbyteTexture2D(TextureFormat(GL_TEXTURE_2D,t.width(),t.height(),3,GL_RGBA,GL_UNSIGNED_BYTE),TextureParams(GL_LINEAR,GL_LINEAR),t.bits());
  }
}

void RadianceScalingRendererPlugin::initShaders(bool reload)
{
  if(!reload) {
    string path = ":/RadianceScalingRenderer/shaders/";
	delete _buffPass;
	delete _rsPass;
    _buffPass = new GPUProgram(path+"01_buffer.vs",path+"01_buffer.fs");
    _rsPass   = new GPUProgram(path+"02_rs.vs"  ,path+"02_rs.fs");
    GL_TEST_ERR

    _rsPass->enable();
    _rsPass->addUniform("sw");
    _rsPass->addUniform("sh");
    _rsPass->addUniform("enhancement");
    _rsPass->addUniform("transition");
    _rsPass->addUniform("enabled");
    _rsPass->addUniform("invert");
    _rsPass->addUniform("twoLS");
    _rsPass->addUniform("display");
    _rsPass->addUniform("grad");
    _rsPass->addUniform("norm");
    _rsPass->addUniform("convexLS");
    _rsPass->addUniform("concavLS");
    _rsPass->addUniform("colormap");
    _rsPass->disable();

    GL_TEST_ERR    
  } else {
    _buffPass->reload();
    _rsPass->reload();
    GL_TEST_ERR
  }

  float sw  = 1.0f/(float)_w;
  float sh  = 1.0f/(float)_h;

  _rsPass->enable();
  _rsPass->setUniform1f("sw",sw);
  _rsPass->setUniform1f("sh",sh);
  _rsPass->setUniform1f("enhancement",_sDialog->getEnhancement());
  _rsPass->setUniform1f("transition",_sDialog->getTransition());
  _rsPass->setUniform1i("enabled",_sDialog->getEnable());
  _rsPass->setUniform1i("display",_sDialog->getDisplay());
  _rsPass->setUniform1i("invert",_sDialog->getInvert());
  _rsPass->setUniform1i("twoLS",_sDialog->getTwoLS());
  _rsPass->setUniformTexture("grad",0,_gradTex->format().target(),_gradTex->id());
  _rsPass->setUniformTexture("norm",1,_normTex->format().target(),_normTex->id());
  _rsPass->setUniformTexture("colormap",2,_colorTex->format().target(),_colorTex->id());
  _rsPass->setUniformTexture("convexLS",3,_convexLS->format().target(),_convexLS->id());
  _rsPass->setUniformTexture("concavLS",4,_concavLS->format().target(),_concavLS->id());
  _rsPass->disable();
  GL_TEST_ERR
}

void RadianceScalingRendererPlugin::initFBOs() {
  int v[4];
  glGetIntegerv(GL_VIEWPORT,v);
  _w = v[2];
  _h = v[3];
  
  if(_fbo==NULL) {
    GLenum filter = GL_LINEAR;
    GLenum format = GL_RGBA16F_ARB;
    _fbo = new FramebufferObject();
    _depthTex = new FloatTexture2D(TextureFormat(GL_TEXTURE_2D,_w,_h,GL_DEPTH_COMPONENT24,GL_DEPTH_COMPONENT,GL_FLOAT),TextureParams(filter,filter));
    _gradTex  = new FloatTexture2D(TextureFormat(GL_TEXTURE_2D,_w,_h,format,GL_RGBA,GL_FLOAT),TextureParams(filter,filter));
    _normTex  = new FloatTexture2D(_gradTex->format(),_gradTex->params());
    _colorTex  = new UbyteTexture2D(TextureFormat(GL_TEXTURE_2D,_w,_h,GL_RGBA8,GL_RGBA,GL_UNSIGNED_BYTE),TextureParams(GL_LINEAR,GL_LINEAR));//new FloatTexture2D(_gradTex->format(),_gradTex->params());
  }

  _fbo->bind();
  _fbo->unattachAll();
  _depthTex->bind();
  _fbo->attachTexture(_depthTex->format().target(),_depthTex->id(),GL_DEPTH_ATTACHMENT_EXT);
  _gradTex->bind();
  _fbo->attachTexture(_gradTex->format().target(),_gradTex->id(),GL_COLOR_ATTACHMENT0_EXT);
  _normTex->bind();
  _fbo->attachTexture(_normTex->format().target(),_normTex->id(),GL_COLOR_ATTACHMENT1_EXT);
  _colorTex->bind();
  _fbo->attachTexture(_colorTex->format().target(),_colorTex->id(),GL_COLOR_ATTACHMENT2_EXT);
  _fbo->isValid();

  FramebufferObject::unbind();
}

void RadianceScalingRendererPlugin::cleanShaders() {
  if(_buffPass!=NULL) {
    delete _buffPass;
    delete _rsPass;

    _buffPass = NULL;
    _rsPass   = NULL;
  }
}

void RadianceScalingRendererPlugin::cleanFBOs() {
  if(_fbo!=NULL) {
    delete _fbo;
    delete _depthTex;
    delete _gradTex;
    delete _normTex;
    delete _colorTex;
    
    _fbo      = NULL;
    _depthTex = NULL;
    _gradTex  = NULL;
    _normTex  = NULL;
    _colorTex = NULL;
  }
}

Q_EXPORT_PLUGIN(RadianceScalingRendererPlugin)