Equalizer
1.4.1
|
00001 00002 /* Copyright (c) 2011-2012, Stefan Eilemann <eile@eyescale.ch> 00003 * 00004 * Redistribution and use in source and binary forms, with or without 00005 * modification, are permitted provided that the following conditions are met: 00006 * 00007 * - Redistributions of source code must retain the above copyright notice, this 00008 * list of conditions and the following disclaimer. 00009 * - Redistributions in binary form must reproduce the above copyright notice, 00010 * this list of conditions and the following disclaimer in the documentation 00011 * and/or other materials provided with the distribution. 00012 * - Neither the name of Eyescale Software GmbH nor the names of its 00013 * contributors may be used to endorse or promote products derived from this 00014 * software without specific prior written permission. 00015 * 00016 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00017 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00018 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00019 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 00020 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 00021 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 00022 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00023 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 00024 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 00025 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00026 * POSSIBILITY OF SUCH DAMAGE. 00027 */ 00028 00029 00030 #include "vertexBufferState.h" 00031 00032 namespace mesh 00033 { 00034 VertexBufferState::VertexBufferState( const GLEWContext* glewContext ) 00035 : _pmvMatrix( Matrix4f::IDENTITY ) 00036 , _glewContext( glewContext ) 00037 , _renderMode( RENDER_MODE_DISPLAY_LIST ) 00038 , _useColors( false ) 00039 , _useFrustumCulling( true ) 00040 { 00041 _range[0] = 0.f; 00042 _range[1] = 1.f; 00043 resetRegion(); 00044 MESHASSERT( glewContext ); 00045 } 00046 00047 void VertexBufferState::setRenderMode( const RenderMode mode ) 00048 { 00049 if( _renderMode == mode ) 00050 return; 00051 00052 _renderMode = mode; 00053 00054 // Check if VBO funcs available, else fall back to display lists 00055 if( _renderMode == RENDER_MODE_BUFFER_OBJECT && !GLEW_VERSION_1_5 ) 00056 { 00057 MESHINFO << "VBO not available, using display lists" << std::endl; 00058 _renderMode = RENDER_MODE_DISPLAY_LIST; 00059 } 00060 } 00061 00062 void VertexBufferState::resetRegion() 00063 { 00064 _region[0] = std::numeric_limits< float >::max(); 00065 _region[1] = std::numeric_limits< float >::max(); 00066 _region[2] = -std::numeric_limits< float >::max(); 00067 _region[3] = -std::numeric_limits< float >::max(); 00068 } 00069 00070 void VertexBufferState::updateRegion( const BoundingBox& box ) 00071 { 00072 const Vertex corners[8] = { Vertex( box[0][0], box[0][1], box[0][2] ), 00073 Vertex( box[1][0], box[0][1], box[0][2] ), 00074 Vertex( box[0][0], box[1][1], box[0][2] ), 00075 Vertex( box[1][0], box[1][1], box[0][2] ), 00076 Vertex( box[0][0], box[0][1], box[1][2] ), 00077 Vertex( box[1][0], box[0][1], box[1][2] ), 00078 Vertex( box[0][0], box[1][1], box[1][2] ), 00079 Vertex( box[1][0], box[1][1], box[1][2] ) }; 00080 00081 Vector4f region( std::numeric_limits< float >::max(), 00082 std::numeric_limits< float >::max(), 00083 -std::numeric_limits< float >::max(), 00084 -std::numeric_limits< float >::max( )); 00085 00086 for( size_t i = 0; i < 8; ++i ) 00087 { 00088 const Vertex corner = _pmvMatrix * corners[i]; 00089 region[0] = std::min( corner[0], region[0] ); 00090 region[1] = std::min( corner[1], region[1] ); 00091 region[2] = std::max( corner[0], region[2] ); 00092 region[3] = std::max( corner[1], region[3] ); 00093 } 00094 00095 // transform region of interest from [ -1 -1 1 1 ] to normalized viewport 00096 const Vector4f normalized( region[0] * .5f + .5f, 00097 region[1] * .5f + .5f, 00098 ( region[2] - region[0] ) * .5f, 00099 ( region[3] - region[1] ) * .5f ); 00100 00101 declareRegion( normalized ); 00102 _region[0] = std::min( _region[0], normalized[0] ); 00103 _region[1] = std::min( _region[1], normalized[1] ); 00104 _region[2] = std::max( _region[2], normalized[2] ); 00105 _region[3] = std::max( _region[3], normalized[3] ); 00106 } 00107 00108 Vector4f VertexBufferState::getRegion() const 00109 { 00110 if( _region[0] > _region[2] || _region[1] > _region[3] ) 00111 return Vector4f::ZERO; 00112 00113 return _region; 00114 } 00115 00116 GLuint VertexBufferStateSimple::getDisplayList( const void* key ) 00117 { 00118 if( _displayLists.find( key ) == _displayLists.end() ) 00119 return INVALID; 00120 return _displayLists[key]; 00121 } 00122 00123 GLuint VertexBufferStateSimple::newDisplayList( const void* key ) 00124 { 00125 _displayLists[key] = glGenLists( 1 ); 00126 return _displayLists[key]; 00127 } 00128 00129 GLuint VertexBufferStateSimple::getBufferObject( const void* key ) 00130 { 00131 if( _bufferObjects.find( key ) == _bufferObjects.end() ) 00132 return INVALID; 00133 return _bufferObjects[key]; 00134 } 00135 00136 GLuint VertexBufferStateSimple::newBufferObject( const void* key ) 00137 { 00138 if( !GLEW_VERSION_1_5 ) 00139 return INVALID; 00140 glGenBuffers( 1, &_bufferObjects[key] ); 00141 return _bufferObjects[key]; 00142 } 00143 00144 void VertexBufferStateSimple::deleteAll() 00145 { 00146 for( GLMapCIter i = _displayLists.begin(); i != _displayLists.end(); ++i ) 00147 glDeleteLists( i->second, 1 ); 00148 00149 for( GLMapCIter i = _bufferObjects.begin(); i != _bufferObjects.end(); ++i ) 00150 glDeleteBuffers( 1, &(i->second) ); 00151 00152 _displayLists.clear(); 00153 _bufferObjects.clear(); 00154 } 00155 00156 }