examples/eqPly/window.cpp

00001 
00002 /* Copyright (c) 2007-2009, Stefan Eilemann <eile@equalizergraphics.com> 
00003    Copyright (c) 2007, Tobias Wolf <twolf@access.unizh.ch>
00004  *
00005  * This library is free software; you can redistribute it and/or modify it under
00006  * the terms of the GNU Lesser General Public License version 2.1 as published
00007  * by the Free Software Foundation.
00008  *  
00009  * This library is distributed in the hope that it will be useful, but WITHOUT
00010  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00011  * FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
00012  * details.
00013  * 
00014  * You should have received a copy of the GNU Lesser General Public License
00015  * along with this library; if not, write to the Free Software Foundation, Inc.,
00016  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
00017  */
00018 
00019 #include "window.h"
00020 #include "pipe.h"
00021 #include "config.h"
00022 
00023 #include "fragmentShader_glsl.h"
00024 #include "vertexShader_glsl.h"
00025     
00026 #include <fstream>
00027 #include <sstream>
00028 
00029 using namespace std;
00030 
00031 namespace eqPly
00032 {
00033 
00034 bool Window::configInitGL( const uint32_t initID )
00035 {
00036     if( !eq::Window::configInitGL( initID ))
00037         return false;
00038 
00039     glLightModeli( GL_LIGHT_MODEL_LOCAL_VIEWER, 1 );
00040     glEnable( GL_CULL_FACE ); // OPT - produces sparser images in DB mode
00041     glCullFace( GL_BACK );
00042 
00043     EQASSERT( !_state );
00044     _state = new VertexBufferState( getObjectManager( ));
00045 
00046     const Config*   config   = static_cast< const Config* >( getConfig( ));
00047     const InitData& initData = config->getInitData();
00048 
00049     if( initData.showLogo( ))
00050         _loadLogo();
00051 
00052     if( initData.useGLSL() )
00053         _loadShaders();
00054 
00055     return true;
00056 }
00057 
00058 bool Window::configExitGL()
00059 {
00060     if( _state )
00061         _state->deleteAll();
00062 
00063     delete _state;
00064     _state = 0;
00065 
00066     return eq::Window::configExitGL();
00067 }
00068 
00069 static const char* _logoTextureName = "eqPly_logo";
00070 
00071 void Window::_loadLogo()
00072 {
00073     if( _state->getTexture( _logoTextureName ) != VertexBufferState::INVALID )
00074     {
00075         // Already loaded by first window
00076         const eq::Pipe* pipe        = getPipe();
00077         const Window*   firstWindow = static_cast< Window* >
00078                                           ( pipe->getWindows()[0] );
00079         
00080         _logoTexture = firstWindow->_logoTexture;
00081         _logoSize    = firstWindow->_logoSize;
00082         return;
00083     }
00084 
00085     eq::Image image;
00086     if( !image.readImage( "logo.rgb", eq::Frame::BUFFER_COLOR ) &&
00087         !image.readImage( "./examples/eqPly/logo.rgb", 
00088                           eq::Frame::BUFFER_COLOR ) )
00089     {
00090         EQWARN << "Can't load overlay logo 'logo.rgb'" << endl;
00091         return;
00092     }
00093 
00094     _logoTexture = _state->newTexture( _logoTextureName );
00095     EQASSERT( _logoTexture != VertexBufferState::INVALID );
00096 
00097     const eq::PixelViewport& pvp = image.getPixelViewport();
00098     _logoSize.x() = pvp.w;
00099     _logoSize.y() = pvp.h;
00100 
00101     glBindTexture( GL_TEXTURE_RECTANGLE_ARB, _logoTexture );
00102     glTexImage2D( GL_TEXTURE_RECTANGLE_ARB, 0, 
00103                   image.getFormat( eq::Frame::BUFFER_COLOR ),
00104                   _logoSize.x(), _logoSize.y(), 0,
00105                   image.getFormat( eq::Frame::BUFFER_COLOR ), 
00106                   image.getType( eq::Frame::BUFFER_COLOR ),
00107                   image.getPixelPointer( eq::Frame::BUFFER_COLOR ));
00108 
00109     EQVERB << "Created logo texture of size " << _logoSize << endl;
00110 }
00111 
00112 void Window::_loadShaders()
00113 {
00114     if( _state->getShader( vertexShader_glsl.c_str( )) !=
00115         VertexBufferState::INVALID )
00116 
00117         // already loaded
00118         return;
00119 
00120     // Check if functions are available
00121     if( !GLEW_VERSION_2_0 )
00122     {
00123         EQWARN << "Shader function pointers missing, using fixed function "
00124                << "pipeline" << endl;
00125         return;
00126     }
00127 
00128     const GLuint vShader = _state->newShader( vertexShader_glsl.c_str(), 
00129                                               GL_VERTEX_SHADER );
00130     EQASSERT( vShader != VertexBufferState::INVALID );
00131     const GLchar* vShaderPtr = vertexShader_glsl.c_str();
00132     glShaderSource( vShader, 1, &vShaderPtr, 0 );
00133     glCompileShader( vShader );
00134 
00135     GLint status;
00136     glGetShaderiv( vShader, GL_COMPILE_STATUS, &status );
00137     if( !status )
00138     {
00139         EQWARN << "Failed to compile vertex shader" << endl;
00140         return;
00141     }
00142     
00143     const GLuint fShader = 
00144         _state->newShader( fragmentShader_glsl.c_str(), GL_FRAGMENT_SHADER );
00145     EQASSERT( fShader != VertexBufferState::INVALID );
00146     const GLchar* fShaderPtr = fragmentShader_glsl.c_str();
00147     glShaderSource( fShader, 1, &fShaderPtr, 0 );
00148     glCompileShader( fShader );
00149     glGetShaderiv( fShader, GL_COMPILE_STATUS, &status );
00150     if( !status )
00151     {
00152         EQWARN << "Failed to compile fragment shader" << endl;
00153         return;
00154     }
00155     
00156     const GLuint program = _state->newProgram( getPipe() );
00157     EQASSERT( program != VertexBufferState::INVALID );
00158     glAttachShader( program, vShader );
00159     glAttachShader( program, fShader );
00160     glLinkProgram( program );
00161     glGetProgramiv( program, GL_LINK_STATUS, &status );
00162     if( !status )
00163     {
00164         EQWARN << "Failed to link shader program" << endl;
00165         return;
00166     }
00167     
00168     // turn off OpenGL lighting if we are using our own shaders
00169     glDisable( GL_LIGHTING );
00170 
00171     EQINFO << "Shaders loaded successfully" << endl;
00172 }
00173 
00174 void Window::frameStart( const uint32_t frameID, const uint32_t frameNumber )
00175 {
00176     const Pipe*      pipe      = static_cast<Pipe*>( getPipe( ));
00177     const FrameData& frameData = pipe->getFrameData();
00178 
00179     _state->setRenderMode( frameData.getRenderMode( ));
00180     eq::Window::frameStart( frameID, frameNumber );
00181 }
00182 
00183 void Window::swapBuffers()
00184 {
00185     const Pipe*              pipe      = static_cast<Pipe*>( getPipe( ));
00186     const FrameData&         frameData = pipe->getFrameData();
00187     const eq::ChannelVector& channels  = getChannels();
00188 
00189     if( frameData.useStatistics() && !channels.empty( ))
00190         EQ_GL_CALL( channels.back()->drawStatistics( ));
00191 
00192     eq::Window::swapBuffers();
00193 }
00194 }
Generated on Mon Aug 10 18:58:41 2009 for Equalizer 0.9 by  doxygen 1.5.8