~ubuntu-branches/ubuntu/vivid/meshlab/vivid

« back to all changes in this revision

Viewing changes to meshlab/src/meshlabplugins/render_splatpyramid/splat_pyramid.cc

  • Committer: Bazaar Package Importer
  • Author(s): Teemu Ikonen
  • Date: 2009-10-08 16:40:41 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20091008164041-0c2ealqv8b8uc20c
Tags: 1.2.2-1
* New upstream version
* Do not build filter_isoparametrization because liblevmar dependency
  is not (yet) in Debian
* Fix compilation with gcc-4.4, thanks to Jonathan Liu for the patch
  (closes: #539544)
* rules: Add compiler variables to the qmake call (for testing with new
  GCC versions)
* io_3ds.pro: Make LIBS and INCLUDEPATH point to Debian version of lib3ds
* io_epoch.pro: Make LIBS point to Debian version of libbz2
* control:
  - Move Homepage URL to the source package section
  - Update to standards-version 3.8.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 * Interface independent application class
 
3
 * 
 
4
 * Author : Ricardo Marroquim
 
5
 *
 
6
 * Date created : 20-12-2007
 
7
 *
 
8
 **/
 
9
 
 
10
#include <QtGui>
 
11
 
 
12
#include <math.h>
 
13
#include <stdlib.h>
 
14
#include <iostream>
 
15
#include "splat_pyramid.h"
 
16
 
 
17
#include <QGLWidget>
 
18
#include <QTextStream>
 
19
#include <wrap/gl/trimesh.h>
 
20
 
 
21
//#include <QGLFramebufferObject>
 
22
 
 
23
//#include "GL/glut.h"
 
24
 
 
25
using namespace std;
 
26
using namespace vcg;
 
27
 
 
28
void PyramidPointRendererPlugin::Init(QAction *, MeshDocument &md, RenderMode &, QGLWidget *gla)
 
29
{
 
30
        gla->makeCurrent();
 
31
        glewInit();
 
32
 
 
33
        canvas_width = gla->width();
 
34
        canvas_height = gla->height();
 
35
 
 
36
        //canvas_width = canvas_height = 1024.0;
 
37
 
 
38
        objects.clear();
 
39
 
 
40
        bool color_per_vertex = false;
 
41
 
 
42
        foreach(MeshModel * mp, md.meshList) {
 
43
        // Create a new primitive from given file
 
44
        objects.push_back( Object( objects.size() ) );
 
45
 
 
46
        vector<Surfeld> *surfels = (objects.back()).getSurfels();
 
47
 
 
48
        if (mp->hasDataMask( MeshModel::MM_VERTCOLOR ) )
 
49
                color_per_vertex = true;
 
50
          
 
51
        Color4b c (180, 180, 180, 255);
 
52
        float quality = 0.0001;
 
53
        double radius = 1.0;
 
54
 
 
55
        int pos = 0;
 
56
        CMeshO::VertexIterator vi;
 
57
 
 
58
        for(vi=mp->cm.vert.begin(); vi!=mp->cm.vert.end(); ++vi)
 
59
                if(!(*vi).IsD())
 
60
                {
 
61
                        Point3f p = (*vi).P();
 
62
                        Point3f n = (*vi).N();
 
63
                        radius = (*vi).R();
 
64
                        if (color_per_vertex)
 
65
                        c = (*vi).C();
 
66
 
 
67
                        surfels->push_back ( Surfeld (p, n, c, quality, radius, pos) );
 
68
                        ++pos;
 
69
                }
 
70
        }
 
71
  
 
72
        if (color_per_vertex)
 
73
                render_mode =  PYRAMID_POINTS_COLOR;
 
74
        else
 
75
                render_mode =  PYRAMID_POINTS;
 
76
                        
 
77
        // Sets the default rendering algorithm and loads vertex arrays
 
78
        for (unsigned int i = 0; i < objects.size(); ++i)
 
79
                objects[i].setRendererType( render_mode );
 
80
 
 
81
  
 
82
        QDir shadersDir = QDir(qApp->applicationDirPath());
 
83
#if defined(Q_OS_WIN)
 
84
        if (shadersDir.dirName() == "debug" || shadersDir.dirName() == "release" || shadersDir.dirName() == "plugins"  )
 
85
                shadersDir.cdUp();
 
86
#elif defined(Q_OS_MAC)
 
87
        if (shadersDir.dirName() == "MacOS") {
 
88
                for(int i=0;i<6;++i){
 
89
                        if(shadersDir.exists("shaders")) break;
 
90
                        shadersDir.cdUp();
 
91
                }
 
92
        }
 
93
#endif
 
94
        
 
95
        QDir::setCurrent(shadersDir.absolutePath());
 
96
 
 
97
 
 
98
        createPointRenderer( );  
 
99
 
 
100
        if (sDialog) {
 
101
                sDialog->close();
 
102
        delete sDialog;
 
103
                sDialog=0;
 
104
        }
 
105
 
 
106
        sDialog = new Dialog(point_based_render, gla);
 
107
        sDialog->move(10,100);
 
108
 
 
109
        sDialog->show();
 
110
}
 
111
 
 
112
/**
 
113
 * Initialize opengl and application state variables.
 
114
 * @param default_mode Defines the initial rendering mode.
 
115
 **/
 
116
PyramidPointRendererPlugin::PyramidPointRendererPlugin() {
 
117
 
 
118
  render_mode = PYRAMID_POINTS;
 
119
  point_based_render = NULL;
 
120
 
 
121
  fps_loop = 0;
 
122
 
 
123
  rotating = 0;
 
124
  show_points = false;
 
125
  selected = 0;
 
126
  scale_factor = 1.0;
 
127
 
 
128
  sDialog = 0;
 
129
 
 
130
  //check_for_ogl_error("Init");
 
131
}
 
132
 
 
133
void PyramidPointRendererPlugin::initActionList()
 
134
{
 
135
  actionList << new QAction("SplatPyramid", this);
 
136
}
 
137
 
 
138
PyramidPointRendererPlugin::~PyramidPointRendererPlugin( ) {
 
139
  objects.clear();
 
140
  delete point_based_render;
 
141
}
 
142
 
 
143
/// Render all points with OpenGL
 
144
void PyramidPointRendererPlugin::drawPoints(void) {
 
145
  glPointSize(1.0);
 
146
  glColor4f(0.0f, 0.0f, 0.0f, 1.0f);
 
147
 
 
148
  glBegin(GL_POINTS);
 
149
  
 
150
  for (surfelVectorIter it = objects[0].getSurfels()->begin(); it != objects[0].getSurfels()->end(); ++it) {
 
151
        //      Color4b c = it->Color();
 
152
        //      glColor4f(c[0], c[1], c[2], 1.0f);  
 
153
        Point3f p = it->Center();
 
154
        //glVertex3f(p[0], p[1], p[2]);
 
155
  }
 
156
  glEnd();
 
157
}
 
158
 
 
159
/** 
 
160
 * Display method to render the models.
 
161
 **/
 
162
//void PyramidPointRendererPlugin::draw( void ) {
 
163
void PyramidPointRendererPlugin::Render(QAction *, MeshDocument &, RenderMode &, QGLWidget* gla )
 
164
{
 
165
        if (objects.size() == 0)
 
166
                return;
 
167
 
 
168
        point_based_render->clearBuffers();
 
169
 
 
170
        glPushMatrix();
 
171
 
 
172
        Matrix44f mv;
 
173
        glGetv(GL_MODELVIEW_MATRIX,mv);
 
174
 
 
175
        scale_factor = (mv.GetColumn3(0)).Norm();
 
176
 
 
177
        float fov_scale = ((GLArea*)gla)->getFov();
 
178
        if (fov_scale == 5)
 
179
                fov_scale = 1.0;
 
180
        else
 
181
                fov_scale = 1.0 / (tanf(vcg::math::ToRad(fov_scale*.5f)) * 1.0);
 
182
 
 
183
        // Set factor for scaling projected radii of samples in projection phase
 
184
        scale_factor = fov_scale;
 
185
        point_based_render->setScaleFactor( scale_factor );
 
186
 
 
187
        Invert(mv);
 
188
        Point3f eye = mv * Point3f(0, 0, 0);
 
189
 
 
190
        // Set eye for back face culling in vertex shader of projection phase
 
191
        point_based_render->setEye( Point3f(eye[0], eye[1], eye[2]) );
 
192
 
 
193
        for (unsigned int i = 0; i < objects.size(); ++i)
 
194
                point_based_render->projectSamples( &objects[i] );
 
195
 
 
196
        point_based_render->interpolate();
 
197
 
 
198
        point_based_render->draw();
 
199
 
 
200
        //   if (show_points)
 
201
        //     drawPoints();
 
202
 
 
203
        glPopMatrix();
 
204
 
 
205
        //glFinish();
 
206
}
 
207
 
 
208
/// Reshape func
 
209
/// @param w New window width
 
210
/// @param h New window height
 
211
void PyramidPointRendererPlugin::reshape(int, int) {
 
212
  //  camera->reshape(w, h);
 
213
}
 
214
 
 
215
/**
 
216
 * Changes the rendering algorithm.
 
217
 * @param type Rendering mode.
 
218
 **/
 
219
void PyramidPointRendererPlugin::changeRendererType( int type ) {
 
220
  for (unsigned int i = 0; i < objects.size(); ++i)
 
221
        objects[i].setRendererType((point_render_type_enum) type);
 
222
  render_mode = type;
 
223
  createPointRenderer( );
 
224
}
 
225
 
 
226
/**
 
227
 * Defines the rendering algorithm.
 
228
 * point_based_render is of generic type, depending on
 
229
 * the choice one of the inherited classes is instanced.
 
230
 **/
 
231
void PyramidPointRendererPlugin::createPointRenderer( void ) {  
 
232
 
 
233
        if (point_based_render)
 
234
                delete point_based_render;
 
235
 
 
236
        if (render_mode == PYRAMID_POINTS)
 
237
                point_based_render = new PyramidPointRenderer(canvas_width, canvas_height);
 
238
        else if (render_mode == PYRAMID_POINTS_COLOR)
 
239
                point_based_render = new PyramidPointRendererColor(canvas_width, canvas_height);
 
240
 
 
241
        assert (point_based_render);
 
242
 
 
243
        ((PyramidPointRendererBase*)point_based_render)->setShadersDir(QDir::currentPath());
 
244
        ((PyramidPointRendererBase*)point_based_render)->createShaders();
 
245
}
 
246
 
 
247
 
 
248
/**
 
249
 * Returns the model's number of points.
 
250
 * @return Number of points.
 
251
 **/
 
252
int PyramidPointRendererPlugin::getNumberPoints ( void ) {
 
253
  
 
254
  int num_pts = 0;
 
255
  for (unsigned int i = 0; i < objects.size(); ++i)
 
256
        num_pts += objects[i].numberPoints();
 
257
 
 
258
  return num_pts;
 
259
}
 
260
 
 
261
/**
 
262
 * Sets the reconstruction filter size.
 
263
 * Multiplicator of the radius size.
 
264
 * @param s Reconstruction filter size.
 
265
 **/
 
266
void PyramidPointRendererPlugin::setReconstructionFilter ( double s ) { 
 
267
  if (point_based_render)
 
268
    point_based_render->setReconstructionFilterSize(s);
 
269
}
 
270
 
 
271
/**
 
272
 * Sets the quality threshold for interpolating samples.
 
273
 * @param q Quality threshold.
 
274
 **/
 
275
void PyramidPointRendererPlugin::setQualityThreshold ( double q ) { 
 
276
  if (point_based_render)
 
277
    point_based_render->setQualityThreshold(q);
 
278
}
 
279
 
 
280
/**
 
281
 * Sets the prefilter size.
 
282
 * @param s Prefilter size.
 
283
 **/
 
284
void PyramidPointRendererPlugin::setPrefilter ( double s ) { 
 
285
  if (point_based_render)
 
286
    point_based_render->setPrefilterSize(s);
 
287
}
 
288
 
 
289
/**
 
290
 * Sets the kernel size for the template rendering algorithm only.
 
291
 * @param m Kernel size mxm.
 
292
 **/
 
293
void PyramidPointRendererPlugin::setGpuMask ( int m ) {
 
294
  point_based_render->setGpuMaskSize( m );
 
295
}
 
296
 
 
297
/**
 
298
 * Sets per-vertex color on/off if the model has
 
299
 * color information per point.
 
300
 * @param c Per-vertex color state.
 
301
 **/
 
302
void PyramidPointRendererPlugin::setPerVertexColor ( bool ) {
 
303
  //point_based_render->setVertexPerColor( c );
 
304
  for (unsigned int i = 0; i < objects.size(); ++i) {
 
305
        // Reset renderer type to load per vertex color or default color in vertex array
 
306
        objects[i].setRendererType( objects[i].getRendererType() );
 
307
  }
 
308
}
 
309
 
 
310
/**
 
311
 * Turns auto-rotate on/off.
 
312
 * Auto-rotate turns the model continuously.
 
313
 * @param r Auto-rotate state.
 
314
 **/
 
315
void PyramidPointRendererPlugin::setAutoRotate ( bool r ) {
 
316
  rotating = r;
 
317
}
 
318
 
 
319
/**
 
320
 * Turns depth test on/off.
 
321
 * @param d Depth test state.
 
322
 **/
 
323
void PyramidPointRendererPlugin::setDepthTest ( bool d ) {
 
324
  if (point_based_render)
 
325
    point_based_render->setDepthTest(d);
 
326
}
 
327
 
 
328
/**
 
329
 * Change model material properties.
 
330
 * @param mat Id of material (see materials.h for list)
 
331
 **/
 
332
void PyramidPointRendererPlugin::changeMaterial( int mat ) {  
 
333
  point_based_render->setMaterial( mat );
 
334
}
 
335
 
 
336
/**
 
337
 * Turns backface culling on/off.
 
338
 * @param b Backface culling state.
 
339
 **/
 
340
void PyramidPointRendererPlugin::setBackFaceCulling ( bool c ) {
 
341
  if (point_based_render)
 
342
        point_based_render->setBackFaceCulling(c);
 
343
}
 
344
 
 
345
/**
 
346
 * Turns elliptical weights on/off.
 
347
 * @param b Elliptical weight state.
 
348
 **/
 
349
void PyramidPointRendererPlugin::setEllipticalWeight ( bool b ) {
 
350
  point_based_render->setEllipticalWeight(b);
 
351
}
 
352
 
 
353
/**
 
354
 * Turns quality per vertex on/off.
 
355
 * @param c Quality per vertex state.
 
356
 **/
 
357
void PyramidPointRendererPlugin::setQualityPerVertex ( bool c ) {
 
358
  point_based_render->setQualityPerVertex(c);
 
359
}
 
360
 
 
361
/// Cycles through objects list for displaying individual parts of the model.
 
362
/// When selected = 0 displays all files
 
363
void PyramidPointRendererPlugin::increaseSelected ( void ) {
 
364
  selected++;
 
365
  if (selected > (int)objects.size())
 
366
        selected = 0;
 
367
  cout << "selected : " << selected << endl;
 
368
 
 
369
}
 
370
 
 
371
/// Cycles through objects list for displaying individual parts of the model.
 
372
/// When selected = 0 displays all files
 
373
void PyramidPointRendererPlugin::decreaseSelected ( void ) {
 
374
  selected--;
 
375
  if (selected < 0)
 
376
        selected = objects.size();
 
377
  cout << "selected : " << selected << endl;
 
378
}
 
379
 
 
380
Q_EXPORT_PLUGIN(PyramidPointRendererPlugin)