Equalizer  1.2.1
seqPly/vertexBufferState.h
00001 
00002 /* Copyright (c) 2009-2011, Stefan Eilemann <eile@equalizergraphics.com>
00003  *                    2007, Tobias Wolf <twolf@access.unizh.ch>
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions are met:
00007  *
00008  * - Redistributions of source code must retain the above copyright notice, this
00009  *   list of conditions and the following disclaimer.
00010  * - Redistributions in binary form must reproduce the above copyright notice,
00011  *   this list of conditions and the following disclaimer in the documentation
00012  *   and/or other materials provided with the distribution.
00013  * - Neither the name of Eyescale Software GmbH nor the names of its
00014  *   contributors may be used to endorse or promote products derived from this
00015  *   software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00018  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
00021  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00022  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00023  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00024  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00025  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00026  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00027  * POSSIBILITY OF SUCH DAMAGE.
00028  */
00029 
00030 
00031 #ifndef MESH_VERTEXBUFFERSTATE_H
00032 #define MESH_VERTEXBUFFERSTATE_H
00033 
00034 #include "typedefs.h"
00035 #include <map>
00036 
00037 #ifdef EQUALIZER
00038 #  include <eq/eq.h>
00039 #  include "channel.h"
00040 #endif // EQUALIZER
00041 
00042 namespace mesh 
00043 {
00044     /*  The abstract base class for kd-tree rendering state.  */
00045     class VertexBufferState
00046     {
00047     public:
00048         enum
00049         {
00050             INVALID = 0 //<! return value for failed operations.
00051         };
00052 
00053         virtual bool useColors() const { return _useColors; }
00054         virtual void setColors( const bool colors ) { _useColors = colors; }
00055         virtual bool stopRendering() const { return false; }
00056         virtual RenderMode getRenderMode() const { return _renderMode; }
00057         virtual void setRenderMode( const RenderMode mode ) 
00058         { 
00059             if( _renderMode == mode )
00060                 return;
00061 
00062             _renderMode = mode;
00063 
00064             // Check if VBO funcs available, else fall back to display lists
00065             if( _renderMode == RENDER_MODE_BUFFER_OBJECT && !GLEW_VERSION_1_5 )
00066             {
00067                 MESHINFO << "VBO not available, using display lists"
00068                          << std::endl;
00069                 _renderMode = RENDER_MODE_DISPLAY_LIST;
00070             }
00071         }
00072 
00073         void setProjectionModelViewMatrix( const Matrix4f& pmv )
00074             { _pmvMatrix = pmv; }
00075         const Matrix4f& getProjectionModelViewMatrix() const
00076             { return _pmvMatrix; }
00077 
00078         void setRange( const Range& range ) { _range = range; }
00079         const Range& getRange() const { return _range; }
00080 
00081         virtual GLuint getDisplayList( const void* key ) = 0;
00082         virtual GLuint newDisplayList( const void* key ) = 0;
00083         virtual GLuint getBufferObject( const void* key ) = 0;
00084         virtual GLuint newBufferObject( const void* key ) = 0;
00085         virtual void deleteAll() = 0;
00086 
00087         const GLEWContext* glewGetContext() const { return _glewContext; }
00088         
00089     protected:
00090         VertexBufferState( const GLEWContext* glewContext ) 
00091                 : _pmvMatrix( Matrix4f::IDENTITY )
00092                 , _glewContext( glewContext )
00093                 , _renderMode( RENDER_MODE_DISPLAY_LIST )
00094                 , _useColors( false )
00095         {
00096             _range[0] = 0.f;
00097             _range[1] = 1.f;
00098             MESHASSERT( glewContext );
00099         } 
00100         
00101         virtual ~VertexBufferState() {}
00102         
00103         Matrix4f      _pmvMatrix; 
00104         Range         _range; 
00105         const GLEWContext* const _glewContext;
00106         RenderMode    _renderMode;
00107         bool          _useColors;
00108         
00109     private:
00110     };
00111     
00112     
00113     /*  Simple state for stand-alone single-pipe usage.  */
00114     class VertexBufferStateSimple : public VertexBufferState 
00115     {
00116     private:
00117         typedef std::map< const void*, GLuint > GLMap;
00118         typedef GLMap::const_iterator GLMapCIter;
00119 
00120     public:
00121         VertexBufferStateSimple( const GLEWContext* glewContext )
00122             : VertexBufferState( glewContext ) {}
00123         
00124         virtual GLuint getDisplayList( const void* key )
00125         {
00126             if( _displayLists.find( key ) == _displayLists.end() )
00127                 return INVALID;
00128             return _displayLists[key];
00129         }
00130         
00131         virtual GLuint newDisplayList( const void* key )
00132         {
00133             _displayLists[key] = glGenLists( 1 );
00134             return _displayLists[key];
00135         }
00136         
00137         virtual GLuint getBufferObject( const void* key )
00138         {
00139             if( _bufferObjects.find( key ) == _bufferObjects.end() )
00140                 return INVALID;
00141             return _bufferObjects[key];
00142         }
00143         
00144         virtual GLuint newBufferObject( const void* key )
00145         {
00146             if( !GLEW_VERSION_1_5 )
00147                 return INVALID;
00148             glGenBuffers( 1, &_bufferObjects[key] );
00149             return _bufferObjects[key];
00150         }
00151         
00152         virtual void deleteAll()
00153             {
00154                 for( GLMapCIter i = _displayLists.begin();
00155                      i != _displayLists.end(); ++i )
00156                 {
00157                     glDeleteLists( i->second, 1 );
00158                 }
00159                 for( GLMapCIter i = _bufferObjects.begin();
00160                      i != _bufferObjects.end(); ++i )
00161                 {
00162                     glDeleteBuffers( 1, &(i->second) );
00163                 }
00164                 _displayLists.clear();
00165                 _bufferObjects.clear();
00166             }
00167 
00168     private:
00169         GLMap  _displayLists;
00170         GLMap  _bufferObjects;
00171     };
00172 } // namespace mesh
00173 
00174 #ifdef EQUALIZER
00175 namespace eqPly
00176 {
00177     /*  State for Equalizer usage, uses Eq's Object Manager.  */
00178     class VertexBufferState : public mesh::VertexBufferState 
00179     {
00180     public:
00181         VertexBufferState( eq::Window::ObjectManager* objectManager ) 
00182                 : mesh::VertexBufferState( objectManager->glewGetContext( ))
00183                 , _objectManager( objectManager )
00184             {} 
00185         
00186         virtual GLuint getDisplayList( const void* key )
00187             { return _objectManager->getList( key ); }
00188         
00189         virtual GLuint newDisplayList( const void* key )
00190             { return _objectManager->newList( key ); }
00191         
00192         virtual GLuint getTexture( const void* key )
00193             { return _objectManager->getTexture( key ); }
00194         
00195         virtual GLuint newTexture( const void* key )
00196             { return _objectManager->newTexture( key ); }
00197         
00198         virtual GLuint getBufferObject( const void* key )
00199             { return _objectManager->getBuffer( key ); }
00200         
00201         virtual GLuint newBufferObject( const void* key )
00202             { return _objectManager->newBuffer( key ); }
00203         
00204         virtual GLuint getProgram( const void* key )
00205             { return _objectManager->getProgram( key ); }
00206         
00207         virtual GLuint newProgram( const void* key )
00208             { return _objectManager->newProgram( key ); }
00209         
00210         virtual GLuint getShader( const void* key )
00211             { return _objectManager->getShader( key ); }
00212         
00213         virtual GLuint newShader( const void* key, GLenum type )
00214             { return _objectManager->newShader( key, type ); }
00215 
00216         virtual void deleteAll() { _objectManager->deleteAll(); }
00217         bool isShared() const { return _objectManager->isShared(); }
00218         
00219         void setChannel( const Channel* channel )
00220              { _channel = channel; }
00221 
00222         virtual bool stopRendering( ) const
00223             { return _channel ? _channel->stopRendering() : false; }
00224 
00225     private:
00226         eq::Window::ObjectManager* _objectManager;
00227         const Channel* _channel;
00228     };
00229 } // namespace eqPly
00230 #endif // EQUALIZER
00231     
00232 
00233 #endif // MESH_VERTEXBUFFERSTATE_H
Generated on Fri Jun 8 2012 15:44:33 for Equalizer 1.2.1 by  doxygen 1.8.0