Equalizer  1.6.1
seqPly/vertexBufferState.cpp
1 
2 /* Copyright (c) 2011-2012, Stefan Eilemann <eile@eyescale.ch>
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * - Redistributions of source code must retain the above copyright notice, this
8  * list of conditions and the following disclaimer.
9  * - Redistributions in binary form must reproduce the above copyright notice,
10  * this list of conditions and the following disclaimer in the documentation
11  * and/or other materials provided with the distribution.
12  * - Neither the name of Eyescale Software GmbH nor the names of its
13  * contributors may be used to endorse or promote products derived from this
14  * software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 
30 #include "vertexBufferState.h"
31 
32 namespace mesh
33 {
34 VertexBufferState::VertexBufferState( const GLEWContext* glewContext )
35  : _pmvMatrix( Matrix4f::IDENTITY )
36  , _glewContext( glewContext )
37  , _renderMode( RENDER_MODE_DISPLAY_LIST )
38  , _useColors( false )
39  , _useFrustumCulling( true )
40 {
41  _range[0] = 0.f;
42  _range[1] = 1.f;
43  resetRegion();
44  MESHASSERT( glewContext );
45 }
46 
47 void VertexBufferState::setRenderMode( const RenderMode mode )
48 {
49  if( _renderMode == mode )
50  return;
51 
52  _renderMode = mode;
53 
54  // Check if VBO funcs available, else fall back to display lists
55  if( _renderMode == RENDER_MODE_BUFFER_OBJECT && !GLEW_VERSION_1_5 )
56  {
57  MESHINFO << "VBO not available, using display lists" << std::endl;
58  _renderMode = RENDER_MODE_DISPLAY_LIST;
59  }
60 }
61 
62 void VertexBufferState::resetRegion()
63 {
64  _region[0] = std::numeric_limits< float >::max();
65  _region[1] = std::numeric_limits< float >::max();
66  _region[2] = -std::numeric_limits< float >::max();
67  _region[3] = -std::numeric_limits< float >::max();
68 }
69 
70 void VertexBufferState::updateRegion( const BoundingBox& box )
71 {
72  const Vertex corners[8] = { Vertex( box[0][0], box[0][1], box[0][2] ),
73  Vertex( box[1][0], box[0][1], box[0][2] ),
74  Vertex( box[0][0], box[1][1], box[0][2] ),
75  Vertex( box[1][0], box[1][1], box[0][2] ),
76  Vertex( box[0][0], box[0][1], box[1][2] ),
77  Vertex( box[1][0], box[0][1], box[1][2] ),
78  Vertex( box[0][0], box[1][1], box[1][2] ),
79  Vertex( box[1][0], box[1][1], box[1][2] ) };
80 
81  Vector4f region( std::numeric_limits< float >::max(),
82  std::numeric_limits< float >::max(),
83  -std::numeric_limits< float >::max(),
84  -std::numeric_limits< float >::max( ));
85 
86  for( size_t i = 0; i < 8; ++i )
87  {
88  const Vertex corner = _pmvMatrix * corners[i];
89  region[0] = std::min( corner[0], region[0] );
90  region[1] = std::min( corner[1], region[1] );
91  region[2] = std::max( corner[0], region[2] );
92  region[3] = std::max( corner[1], region[3] );
93  }
94 
95  // transform region of interest from [ -1 -1 1 1 ] to normalized viewport
96  const Vector4f normalized( region[0] * .5f + .5f,
97  region[1] * .5f + .5f,
98  ( region[2] - region[0] ) * .5f,
99  ( region[3] - region[1] ) * .5f );
100 
101  declareRegion( normalized );
102  _region[0] = std::min( _region[0], normalized[0] );
103  _region[1] = std::min( _region[1], normalized[1] );
104  _region[2] = std::max( _region[2], normalized[2] );
105  _region[3] = std::max( _region[3], normalized[3] );
106 }
107 
108 Vector4f VertexBufferState::getRegion() const
109 {
110  if( _region[0] > _region[2] || _region[1] > _region[3] )
111  return Vector4f::ZERO;
112 
113  return _region;
114 }
115 
116 GLuint VertexBufferStateSimple::getDisplayList( const void* key )
117 {
118  if( _displayLists.find( key ) == _displayLists.end() )
119  return INVALID;
120  return _displayLists[key];
121 }
122 
123 GLuint VertexBufferStateSimple::newDisplayList( const void* key )
124 {
125  _displayLists[key] = glGenLists( 1 );
126  return _displayLists[key];
127 }
128 
129 GLuint VertexBufferStateSimple::getBufferObject( const void* key )
130 {
131  if( _bufferObjects.find( key ) == _bufferObjects.end() )
132  return INVALID;
133  return _bufferObjects[key];
134 }
135 
136 GLuint VertexBufferStateSimple::newBufferObject( const void* key )
137 {
138  if( !GLEW_VERSION_1_5 )
139  return INVALID;
140  glGenBuffers( 1, &_bufferObjects[key] );
141  return _bufferObjects[key];
142 }
143 
144 void VertexBufferStateSimple::deleteAll()
145 {
146  for( GLMapCIter i = _displayLists.begin(); i != _displayLists.end(); ++i )
147  glDeleteLists( i->second, 1 );
148 
149  for( GLMapCIter i = _bufferObjects.begin(); i != _bufferObjects.end(); ++i )
150  glDeleteBuffers( 1, &(i->second) );
151 
152  _displayLists.clear();
153  _bufferObjects.clear();
154 }
155 
156 }
Matrix4f _pmvMatrix
projection * modelView matrix
Vector4f _region
normalized x1 y1 x2 y2 region from cullDraw
vmml::matrix< 4, 4, float > Matrix4f
A 4x4 float matrix.
Definition: vmmlib.h:39
vmml::vector< 4, float > Vector4f
A four-component float vector.
Definition: vmmlib.h:47