SplatCloudNode.cc 15.1 KB
Newer Older
Jan Möbius's avatar
Jan Möbius committed
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
/*===========================================================================*\
*                                                                            *
*                              OpenFlipper                                   *
*      Copyright (C) 2001-2011 by Computer Graphics Group, RWTH Aachen       *
*                           www.openflipper.org                              *
*                                                                            *
*--------------------------------------------------------------------------- *
*  This file is part of OpenFlipper.                                         *
*                                                                            *
*  OpenFlipper is free software: you can redistribute it and/or modify       *
*  it under the terms of the GNU Lesser General Public License as            *
*  published by the Free Software Foundation, either version 3 of            *
*  the License, or (at your option) any later version with the               *
*  following exceptions:                                                     *
*                                                                            *
*  If other files instantiate templates or use macros                        *
*  or inline functions from this file, or you compile this file and          *
*  link it with other files to produce an executable, this file does         *
*  not by itself cause the resulting executable to be covered by the         *
*  GNU Lesser General Public License. This exception does not however        *
*  invalidate any other reasons why the executable file might be             *
*  covered by the GNU Lesser General Public License.                         *
*                                                                            *
*  OpenFlipper 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 Lesser General Public License for more details.                       *
*                                                                            *
*  You should have received a copy of the GNU LesserGeneral Public           *
*  License along with OpenFlipper. If not,                                   *
*  see <http://www.gnu.org/licenses/>.                                       *
*                                                                            *
\*===========================================================================*/

/*===========================================================================*\
*                                                                            *
*   $Revision$                                                       *
*   $LastChangedBy$                                                *
*   $Date$                     *
*                                                                            *
\*===========================================================================*/

//================================================================
//
//  CLASS SplatCloudNode - IMPLEMENTATION
//
//================================================================


//== INCLUDES ====================================================


#include "SplatCloudNode.hh"


//== NAMESPACES ==================================================


namespace ACG {
namespace SceneGraph {


//== IMPLEMENTATION ==============================================


void SplatCloudNode::boundingBox( ACG::Vec3d &_bbMin, ACG::Vec3d &_bbMax )
{
68
69
    ACG::Vec3f bbMin( FLT_MAX, FLT_MAX, FLT_MAX );
    ACG::Vec3f bbMax(-FLT_MAX,-FLT_MAX,-FLT_MAX );
Jan Möbius's avatar
Jan Möbius committed
70

71
72
73
74
    PointVector::const_iterator pointIter;
    for ( pointIter = points_.begin(); pointIter != points_.end(); ++pointIter )
    {
        const Point &p = *pointIter;
Jan Möbius's avatar
Jan Möbius committed
75

76
        ACG::Vec3f acgp( p[0], p[1], p[2] );
Jan Möbius's avatar
Jan Möbius committed
77

78
79
80
        bbMin.minimize( acgp );
        bbMax.maximize( acgp );
    }
Jan Möbius's avatar
Jan Möbius committed
81

82
83
    ACG::Vec3d bbMind = ACG::Vec3d( bbMin );
    ACG::Vec3d bbMaxd = ACG::Vec3d( bbMax );
Jan Möbius's avatar
Jan Möbius committed
84

85
86
    _bbMin.minimize( bbMind );
    _bbMax.maximize( bbMaxd );
Jan Möbius's avatar
Jan Möbius committed
87
88
89
90
91
}


//----------------------------------------------------------------

92

Jan Möbius's avatar
Jan Möbius committed
93
94
void SplatCloudNode::draw( GLState &_state, const DrawModes::DrawMode &_drawMode )
{
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
    static const int RENDERMODE_POINTS = 0;
    static const int RENDERMODE_DOTS   = 1;
    static const int RENDERMODE_SPLATS = 2;

    // check if drawmode is valid
    int rendermode;
    if ( _drawMode.containsAtomicDrawMode( splatsDrawMode_ ) )
        rendermode = RENDERMODE_SPLATS;
    else if ( _drawMode.containsAtomicDrawMode( dotsDrawMode_ ) )
        rendermode = RENDERMODE_DOTS;
    else if ( _drawMode.containsAtomicDrawMode( pointsDrawMode_ ) )
        rendermode = RENDERMODE_POINTS;
    else
        return;

    // set desired depth function
    ACG::GLState::depthFunc( _state.depthFunc() );

    // is vertex-buffer-object is invalid then rebuild
    if ( !vboValid_ )
        rebuildVBO();

    // if vertex-buffer-object is valid now...
    // (if not it will be rebuilt the next time, but this should not happen)
    if ( vboValid_ )
    {
        // activate vertex-buffer-object
        ACG::GLState::bindBufferARB( GL_ARRAY_BUFFER_ARB, vboGlId_ );

        // tell GL where our data is (NULL = beginning of vertex-buffer-object)
        glInterleavedArrays( GL_C4F_N3F_V3F, 0, 0 );

        // arrays are automatically enabled by glInterleavedArrays()
        //glEnableClientState( GL_VERTEX_ARRAY );
        //glEnableClientState( GL_NORMAL_ARRAY );
        //glEnableClientState( GL_COLOR_ARRAY  );

        // Normals are always needed for backface culling, even in drawmode 'Points'!
        // Colors are always needed for pointsizes, even in color picking mode!

        // enable "pointsize by program" depending on current rendermode
        if ( rendermode != RENDERMODE_POINTS )
        {
            ACG::GLState::enable( GL_VERTEX_PROGRAM_POINT_SIZE );
        }

        // draw as GLpoints
        glDrawArrays( GL_POINTS, 0, numPoints() );

        // disable "pointsize by program" if it was enabled
        if ( rendermode != RENDERMODE_POINTS )
        {
            ACG::GLState::disable( GL_VERTEX_PROGRAM_POINT_SIZE );
        }

        // disable arrays
        ACG::GLState::disableClientState( GL_VERTEX_ARRAY );
        ACG::GLState::disableClientState( GL_NORMAL_ARRAY );
        ACG::GLState::disableClientState( GL_COLOR_ARRAY  );

        // deactivate vertex-buffer-object
        ACG::GLState::bindBufferARB( GL_ARRAY_BUFFER_ARB, 0 );
    }
Jan Möbius's avatar
Jan Möbius committed
158
159
160
161
162
163
164
165
166
}


//----------------------------------------------------------------


// TODO: hack, because pick() doesn't get a drawmode
static DrawModes::DrawMode g_pickDrawMode;
void SplatCloudNode::enterPick( GLState &_state, PickTarget _target, const DrawModes::DrawMode &_drawMode )
167
168
169
{
    g_pickDrawMode = _drawMode;
}
Jan Möbius's avatar
Jan Möbius committed
170
171
172
173
174

// ----

void SplatCloudNode::pick( GLState &_state, PickTarget _target )
{
175
176
//     if ( !_state.pick_set_maximum( points_.size() ) )
//      return;
Jan Möbius's avatar
Jan Möbius committed
177

178
179
180
181
    if ( _target == PICK_ANYTHING || _target == PICK_VERTEX )
    {
        // change GL's primary color according to pick name
//         _state.pick_set_name( 0 );
Jan Möbius's avatar
Jan Möbius committed
182

183
184
185
186
187
        // Copy GL's primary color to texture coordinate, which will be outputted by the picking shader as color.
        // This enables us to use RGBA instead of just RGB as it is the case when using GL's secondary color.
//         GLfloat color[4];
//         glGetFloatv( GL_CURRENT_COLOR, color );
//         glTexCoord4fv( color );
Jan Möbius's avatar
Jan Möbius committed
188

189
190
        // TODO: see above ( enterPick() )
//         draw( _state, g_pickDrawMode );
Jan Möbius's avatar
Jan Möbius committed
191

192
193
        draw_pick( _state );
    }
Jan Möbius's avatar
Jan Möbius committed
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
void
SplatCloudNode::
draw_pick( GLState &_state )
{
    _state.pick_set_maximum(points_.size());

    for ( unsigned int i = 0; i < points_.size(); ++i )
    {
        _state.pick_set_name(i);
        glBegin(GL_POINTS);
        glVertex3f( float(points_[i][0] ),float(points_[i][1] ),float(points_[i][2] ) );
        glEnd();
    }

/*
    if (!_state.pick_set_maximum (points_.size()))
    {
        std::cerr << "Splat Node::draw_pick: color range too small, " << "picking failed\n";
        return;
    }

    if ( updateVertexPicking_ || _state.pick_current_index () != vertexPickingBaseIndex_) {
        pick_colors_.clear();

        pick_colors_.reserve( points_.size() );
        for ( unsigned int i = 0; i < points_.size(); ++i )
            pick_colors_[i] = _state.pick_get_name_color( i );

        vertexPickingBaseIndex_ = _state.pick_current_index ();
        updateVertexPicking_    = false;
    }

    // For this version we load the colors directly not from vbo
    ACG::GLState::bindBuffer(GL_ARRAY_BUFFER_ARB, 0);
    ACG::GLState::colorPointer( &(pick_colors_[0]) );
    ACG::GLState::enableClientState(GL_COLOR_ARRAY);

    // vertex positions
    ACG::GLState::vertexPointer( &(points_[0]) );
    ACG::GLState::enableClientState(GL_VERTEX_ARRAY);

    // Draw color picking
    glDrawArrays(GL_POINTS, 0, points_.size());

    // Disable color array
    ACG::GLState::disableClientState(GL_COLOR_ARRAY);

    // disable vertex array
    ACG::GLState::disableClientState(GL_VERTEX_ARRAY);*/
}

//----------------------------------------------------------------
Jan Möbius's avatar
Jan Möbius committed
251
252
253

void SplatCloudNode::createVBO()
{
254
255
256
257
258
    // create a new vertex-buffer-object if not already existing
    if ( vboGlId_ == 0 )
    {
        glGenBuffersARB( 1, &vboGlId_ );
    }
Jan Möbius's avatar
Jan Möbius committed
259
260
261
262
263
264
265
266
}


//----------------------------------------------------------------


void SplatCloudNode::destroyVBO()
{
267
268
269
270
271
272
273
274
275
    // delete existing vertex-buffer-object
    if ( vboGlId_ != 0 )
    {
        glDeleteBuffersARB( 1, &vboGlId_ );
        vboGlId_ = 0;
    }

    // make vertex-buffer-object invalid so it will not be used
    vboValid_ = false;
Jan Möbius's avatar
Jan Möbius committed
276
277
278
279
280
281
282
283
}


//----------------------------------------------------------------


static void addFloatToBuffer( float _value, unsigned char *&_buffer )
{
284
285
286
287
288
289
290
291
    // get pointer
    unsigned char *v = (unsigned char *) &_value;

    // copy over 4 bytes
    *_buffer++ = *v++;
    *_buffer++ = *v++;
    *_buffer++ = *v++;
    *_buffer++ = *v;
Jan Möbius's avatar
Jan Möbius committed
292
293
294
295
296
297
298
299
}


//----------------------------------------------------------------


void SplatCloudNode::rebuildVBO()
{
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
    // check if vertex-buffer-object has already been created (and not destroyed so far)
    if ( vboGlId_ == 0 )
        return;

    // check if there could be a valid vertex-buffer-object
    if ( numPoints() == 0 )
    {
        vboValid_ = false;
        return;
    }

    // we use GL_C4F_N3F_V3F as interleaved array type, so we have 4+3+3 = 10 floats per splat
    unsigned int size = numPoints() * 10 * sizeof(float);

    // activate vertex-buffer-object
    ACG::GLState::bindBufferARB( GL_ARRAY_BUFFER_ARB, vboGlId_ );

    // tell GL that we are seldom updating the vertex-buffer-object but are often drawing it
    glBufferDataARB( GL_ARRAY_BUFFER_ARB, size, 0, GL_STATIC_DRAW_ARB );

    // get pointer to vertex-buffer-object's memory
    unsigned char *buffer = (unsigned char *) glMapBufferARB( GL_ARRAY_BUFFER_ARB, GL_WRITE_ONLY_ARB );

    // if pointer is valid...
    if ( buffer )
    {
        bool hasNrm = hasNormals();
        bool hasPS  = hasPointsizes();
        bool hasCol = hasColors();

        // for all points...
        int i, num = numPoints();
        for ( i=0; i<num; ++i )
        {
            static const float RCP255 = 1.0f / 255.0f;

            // add color
            const Color &c = hasCol ? colors_[i] : defaultColor_;
            addFloatToBuffer( RCP255 * c[0], buffer );
            addFloatToBuffer( RCP255 * c[1], buffer );
            addFloatToBuffer( RCP255 * c[2], buffer );

            // add pointsize (as alpha-component of color)
            const Pointsize &ps = hasPS ? pointsizes_[i] : defaultPointsize_;
            addFloatToBuffer( ps, buffer );

            // add normal
            const Normal &n = hasNrm ? normals_[i] : defaultNormal_;
            addFloatToBuffer( n[0], buffer );
            addFloatToBuffer( n[1], buffer );
            addFloatToBuffer( n[2], buffer );

            // add point
            const Point &p = points_[i];
            addFloatToBuffer( p[0], buffer );
            addFloatToBuffer( p[1], buffer );
            addFloatToBuffer( p[2], buffer );

            // add pick color

        }
    }

    // release pointer to vertex-buffer-object's memory
    bool success = glUnmapBufferARB( GL_ARRAY_BUFFER_ARB );

    // deactivate vertex-buffer-object
    ACG::GLState::bindBufferARB( GL_ARRAY_BUFFER_ARB, 0 );

    // mark vertex-buffer-object as valid only if it was successfully updated and unmaped
    vboValid_ = (buffer != 0) && success;
}


//----------------------------------------------------------------


void SplatCloudNode::normalizeSize()
{
        // check if there is nothing to do
	if( points_.size() == 0 )
Jan Möbius's avatar
Jan Möbius committed
381
382
		return;

383
384
385
386
387
388
	// calculate center-of-gravety
	float cogX = 0.0f;
	float cogY = 0.0f;
	float cogZ = 0.0f;
	SplatCloudNode::PointVector::iterator pointIter;
	for( pointIter = points_.begin(); pointIter != points_.end(); ++pointIter )
Jan Möbius's avatar
Jan Möbius committed
389
	{
390
391
392
393
394
		const SplatCloudNode::Point &p = *pointIter;

		cogX += p[0];
		cogY += p[1];
		cogZ += p[2];
Jan Möbius's avatar
Jan Möbius committed
395
396
	}

397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
	float rcp_count = 1.0f / (float) points_.size();
	cogX *= rcp_count;
	cogY *= rcp_count;
	cogZ *= rcp_count;
	
	cur_translation_ = -Point ( cogX, cogY, cogZ );
	translate( cur_translation_ );
	std::cout << "SplatCloudNode::normalizeSize(): translate points by: " << cur_translation_ << std::endl;
	
	// calculate squared length
	float sqLength = 0.0f;
	for( pointIter = points_.begin(); pointIter != points_.end(); ++pointIter )
	{
		SplatCloudNode::Point &p = *pointIter;
		sqLength += p[0]*p[0] + p[1]*p[1] + p[2]*p[2];
	}
Jan Möbius's avatar
Jan Möbius committed
413

414
	float s = (float) sqrt( sqLength * rcp_count );
Jan Möbius's avatar
Jan Möbius committed
415

416
417
418
419
	if( s == 0.0f )
	  return;
	
	cur_scale_factor_ = 1.0f / s;
Jan Möbius's avatar
Jan Möbius committed
420

421
422
423
	scale( cur_scale_factor_ );
	std::cout << "SplatCloudNode::normalizeSize(): scaling points with factor: " << cur_scale_factor_ << std::endl;
}
Jan Möbius's avatar
Jan Möbius committed
424

425
426
427
428
429
430
431
//----------------------------------------------------------------


void SplatCloudNode::translate( const Point& _t )
{
        SplatCloudNode::PointVector::iterator pointIter;
	for( pointIter = points_.begin(); pointIter != points_.end(); ++pointIter )
Jan Möbius's avatar
Jan Möbius committed
432
	{
433
434
435
436
437
438
439
440
441
		SplatCloudNode::Point &p = *pointIter;
		
		p[0] += _t[0];
		p[1] += _t[1];
		p[2] += _t[2];
	}
}

//----------------------------------------------------------------
Jan Möbius's avatar
Jan Möbius committed
442

443
444
445
446
447
448
449
450
451

void SplatCloudNode::scale( const float _scale )
{
        // scale points (and pointsizes as well) by calculated scale
        SplatCloudNode::PointVector::iterator pointIter;
	if( pointsizes_.size() == points_.size() )
	{
		SplatCloudNode::PointsizeVector::iterator pointsizeIter = pointsizes_.begin();
		for( pointIter = points_.begin(); pointIter != points_.end(); ++pointIter, ++pointsizeIter )
Jan Möbius's avatar
Jan Möbius committed
452
		{
453
454
455
456
457
458
459
			SplatCloudNode::Point     &p  = *pointIter;
			SplatCloudNode::Pointsize &ps = *pointsizeIter;

			p[0] *= _scale;
			p[1] *= _scale;
			p[2] *= _scale;
			ps   *= _scale; // scale pointsize as well
Jan Möbius's avatar
Jan Möbius committed
460
461
		}
	}
462
463
464
465
466
	else
	{
		for( pointIter = points_.begin(); pointIter != points_.end(); ++pointIter )
		{
			SplatCloudNode::Point &p = *pointIter;
Jan Möbius's avatar
Jan Möbius committed
467

468
469
470
471
472
473
			p[0] *= _scale;
			p[1] *= _scale;
			p[2] *= _scale;
		}
	}
	
Jan Möbius's avatar
Jan Möbius committed
474
475
476
477
478
479
480
}

//================================================================


} // namespace SceneGraph
} // namespace ACG