SplatCloudNode.cc 9.87 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
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
/*===========================================================================*\
*                                                                            *
*                              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 )
{
	ACG::Vec3f bbMin( FLT_MAX, FLT_MAX, FLT_MAX );
	ACG::Vec3f bbMax(-FLT_MAX,-FLT_MAX,-FLT_MAX );

	PointVector::const_iterator pointIter;
	for( pointIter = points_.begin(); pointIter != points_.end(); ++pointIter )
	{
		const Point &p = *pointIter;

		ACG::Vec3f acgp( p[0], p[1], p[2] );

		bbMin.minimize( acgp );
		bbMax.maximize( acgp );
	}

	ACG::Vec3d bbMind = ACG::Vec3d( bbMin );
	ACG::Vec3d bbMaxd = ACG::Vec3d( bbMax );

	_bbMin.minimize( bbMind );
	_bbMax.maximize( bbMaxd );
}


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

void SplatCloudNode::draw( GLState &_state, const DrawModes::DrawMode &_drawMode )
{
	// check if drawmode is valid
	if( _drawMode != splatsDrawMode_ && 
96
97
	    _drawMode != dotsDrawMode_   &&
	    _drawMode != pointsDrawMode_ )
Jan Möbius's avatar
Jan Möbius committed
98
99
100
101
102
	{
		return;
	}

	// set desired depth function
Jan Möbius's avatar
Jan Möbius committed
103
	ACG::GLState::depthFunc( _state.depthFunc() );
Jan Möbius's avatar
Jan Möbius committed
104
105
106
107
108
109
110
111
112
113

	// 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
Jan Möbius's avatar
Jan Möbius committed
114
		ACG::GLState::bindBufferARB( GL_ARRAY_BUFFER_ARB, vboGlId_ );
Jan Möbius's avatar
Jan Möbius committed
115
116
117
118
119
120
121
122
123
124
125
126
127
128

		// 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 drawmode
		if( _drawMode != pointsDrawMode_ )
Jan Möbius's avatar
Jan Möbius committed
129
		  ACG::GLState::enable( GL_VERTEX_PROGRAM_POINT_SIZE );
Jan Möbius's avatar
Jan Möbius committed
130
131
132
133
134
135

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

		// disable "pointsize by program" if it was enabled
		if( _drawMode != pointsDrawMode_ )
Jan Möbius's avatar
Jan Möbius committed
136
		  ACG::GLState::disable( GL_VERTEX_PROGRAM_POINT_SIZE );
Jan Möbius's avatar
Jan Möbius committed
137
138

		// disable arrays
Jan Möbius's avatar
Jan Möbius committed
139
140
141
		ACG::GLState::disableClientState( GL_VERTEX_ARRAY );
		ACG::GLState::disableClientState( GL_NORMAL_ARRAY );
		ACG::GLState::disableClientState( GL_COLOR_ARRAY  );
Jan Möbius's avatar
Jan Möbius committed
142
143

		// deactivate vertex-buffer-object
Jan Möbius's avatar
Jan Möbius committed
144
		ACG::GLState::bindBufferARB( GL_ARRAY_BUFFER_ARB, 0 );
Jan Möbius's avatar
Jan Möbius committed
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
	}
}


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


// 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 )
{ g_pickDrawMode = _drawMode; }

// ----

void SplatCloudNode::pick( GLState &_state, PickTarget _target )
{
	if( !_state.pick_set_maximum( 1 ) )
		return;

	if( _target == PICK_ANYTHING )
	{
		// change GL's primary color according to pick name
		_state.pick_set_name( 0 );

		// 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 );

		// TODO: see above ( enterPick() )
		draw( _state, g_pickDrawMode );

	}
}


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


void SplatCloudNode::createVBO()
{
	// create a new vertex-buffer-object if not already existing
	if( vboGlId_ == 0 )
	{
		glGenBuffersARB( 1, &vboGlId_ );
	}
}


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


void SplatCloudNode::destroyVBO()
{
	// 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;
}


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


static void addFloatToBuffer( float _value, unsigned char *&_buffer )
{
	// get pointer
	unsigned char *v = (unsigned char *) &_value;

	// copy over 4 bytes
	*_buffer++ = *v++;
	*_buffer++ = *v++;
	*_buffer++ = *v++;
	*_buffer++ = *v;
}


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


void SplatCloudNode::rebuildVBO()
{
	// 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
Jan Möbius's avatar
Jan Möbius committed
248
	ACG::GLState::bindBufferARB( GL_ARRAY_BUFFER_ARB, vboGlId_ );
Jan Möbius's avatar
Jan Möbius committed
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

	// 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 );
		}
	}

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

	// deactivate vertex-buffer-object
Jan Möbius's avatar
Jan Möbius committed
297
	ACG::GLState::bindBufferARB( GL_ARRAY_BUFFER_ARB, 0 );
Jan Möbius's avatar
Jan Möbius committed
298
299
300
301
302
303
304
305
306
307
308

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


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


} // namespace SceneGraph
} // namespace ACG