Equalizer  1.2.1
eqPly/window.cpp
00001 
00002 /* Copyright (c) 2007-2011, Stefan Eilemann <eile@equalizergraphics.com> 
00003  *                    2007, Tobias Wolf <twolf@access.unizh.ch>
00004  *                    2010, Cedric Stalder <cedric.stalder@gmail.com>
00005  *
00006  * Redistribution and use in source and binary forms, with or without
00007  * modification, are permitted provided that the following conditions are met:
00008  *
00009  * - Redistributions of source code must retain the above copyright notice, this
00010  *   list of conditions and the following disclaimer.
00011  * - Redistributions in binary form must reproduce the above copyright notice,
00012  *   this list of conditions and the following disclaimer in the documentation
00013  *   and/or other materials provided with the distribution.
00014  * - Neither the name of Eyescale Software GmbH nor the names of its
00015  *   contributors may be used to endorse or promote products derived from this
00016  *   software without specific prior written permission.
00017  *
00018  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00019  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00020  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00021  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
00022  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00023  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00024  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00025  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00026  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00027  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00028  * POSSIBILITY OF SUCH DAMAGE.
00029  */
00030 
00031 #include "window.h"
00032 
00033 #include "config.h"
00034 #include "pipe.h"
00035 #include "vertexBufferState.h"
00036 
00037 #include "fragmentShader.glsl.h"
00038 #include "vertexShader.glsl.h"
00039 
00040 #include <fstream>
00041 #include <sstream>
00042 
00043 namespace eqPly
00044 {
00045 
00046 bool Window::configInitSystemWindow( const eq::uint128_t& initID )
00047 {
00048 #ifndef Darwin
00049     if( !eq::Window::configInitSystemWindow( initID ))
00050         return false;
00051 
00052     // OpenGL version is less than 2.0.
00053     if( !GLEW_EXT_framebuffer_object )
00054     {
00055         if( getDrawableConfig().accumBits )
00056             return true;
00057 
00058         configExitSystemWindow();
00059 #endif
00060         // try with 64 bit accum buffer
00061         setIAttribute( IATTR_PLANES_ACCUM, 16 );
00062         if( eq::Window::configInitSystemWindow( initID ))
00063             return true;
00064 
00065         // no anti-aliasing possible
00066         setIAttribute( IATTR_PLANES_ACCUM, eq::AUTO );
00067 
00068         return eq::Window::configInitSystemWindow( initID );
00069 
00070 #ifndef Darwin
00071     }
00072 
00073     return true;
00074 #endif
00075 }
00076 
00077 bool Window::configInitGL( const eq::uint128_t& initID )
00078 {
00079     if( !eq::Window::configInitGL( initID ))
00080         return false;
00081 
00082     glLightModeli( GL_LIGHT_MODEL_LOCAL_VIEWER, 1 );
00083     glEnable( GL_CULL_FACE ); // OPT - produces sparser images in DB mode
00084     glCullFace( GL_BACK );
00085 
00086     EQASSERT( !_state );
00087     _state = new VertexBufferState( getObjectManager( ));
00088 
00089     const Config*   config   = static_cast< const Config* >( getConfig( ));
00090     const InitData& initData = config->getInitData();
00091 
00092     if( initData.showLogo( ))
00093         _loadLogo();
00094 
00095     if( initData.useGLSL() )
00096         _loadShaders();
00097 
00098     return true;
00099 }
00100 
00101 bool Window::configExitGL()
00102 {
00103     if( _state && !_state->isShared( ))
00104         _state->deleteAll();
00105 
00106     delete _state;
00107     _state = 0;
00108 
00109     return eq::Window::configExitGL();
00110 }
00111 
00112 namespace
00113 {
00114 #ifdef EQ_RELEASE
00115 #  ifdef _WIN32 // final INSTALL_DIR is not known at compile time
00116 static const std::string _logoTextureName =
00117                               std::string( "../share/Equalizer/data/logo.rgb" );
00118 #  else
00119 static const std::string _logoTextureName = std::string( EQ_INSTALL_DIR ) +
00120                                  std::string( "share/Equalizer/data/logo.rgb" );
00121 #  endif
00122 #else
00123 static const std::string _logoTextureName = std::string( EQ_SOURCE_DIR ) +
00124                                        std::string( "examples/eqPly/logo.rgb" );
00125 #endif
00126 }
00127 
00128 void Window::_loadLogo()
00129 {
00130     if( !GLEW_ARB_texture_rectangle )
00131     {
00132         EQWARN << "Can't load overlay logo, GL_ARB_texture_rectangle not "
00133                << "available" << std::endl;
00134         return;
00135     }
00136 
00137     eq::Window::ObjectManager* om = getObjectManager();
00138     _logoTexture = om->getEqTexture( _logoTextureName.c_str( ));
00139     if( _logoTexture )
00140         return;
00141 
00142     eq::Image image;
00143     if( !image.readImage( _logoTextureName, eq::Frame::BUFFER_COLOR ))
00144     {
00145         EQWARN << "Can't load overlay logo " << _logoTextureName << std::endl;
00146         return;
00147     }
00148 
00149     _logoTexture = om->newEqTexture( _logoTextureName.c_str(),
00150                                      GL_TEXTURE_RECTANGLE_ARB );
00151     EQASSERT( _logoTexture );
00152     
00153     image.upload(eq::Frame::BUFFER_COLOR, _logoTexture, eq::Vector2i::ZERO, om);
00154     EQVERB << "Created logo texture of size " << _logoTexture->getWidth() << "x"
00155            << _logoTexture->getHeight() << std::endl;
00156 }
00157 
00158 void Window::_loadShaders()
00159 {
00160     if( _state->getShader( vertexShader_glsl ) !=
00161         VertexBufferState::INVALID )
00162     {
00163         // already loaded
00164         return;
00165     }
00166 
00167     // Check if functions are available
00168     if( !GLEW_VERSION_2_0 )
00169     {
00170         EQWARN << "Shader function pointers missing, using fixed function "
00171                << "pipeline" << std::endl;
00172         return;
00173     }
00174 
00175     const GLuint vShader = _state->newShader( vertexShader_glsl,
00176                                               GL_VERTEX_SHADER );
00177     EQASSERT( vShader != VertexBufferState::INVALID );
00178     const GLchar* vShaderPtr = vertexShader_glsl;
00179     glShaderSource( vShader, 1, &vShaderPtr, 0 );
00180     glCompileShader( vShader );
00181 
00182     GLint status;
00183     glGetShaderiv( vShader, GL_COMPILE_STATUS, &status );
00184     if( !status )
00185     {
00186         EQWARN << "Failed to compile vertex shader" << std::endl;
00187         return;
00188     }
00189     
00190     const GLuint fShader = 
00191         _state->newShader( fragmentShader_glsl, GL_FRAGMENT_SHADER );
00192     EQASSERT( fShader != VertexBufferState::INVALID );
00193     const GLchar* fShaderPtr = fragmentShader_glsl;
00194     glShaderSource( fShader, 1, &fShaderPtr, 0 );
00195     glCompileShader( fShader );
00196     glGetShaderiv( fShader, GL_COMPILE_STATUS, &status );
00197     if( !status )
00198     {
00199         EQWARN << "Failed to compile fragment shader" << std::endl;
00200         return;
00201     }
00202     
00203     const GLuint program = _state->newProgram( getPipe() );
00204     EQASSERT( program != VertexBufferState::INVALID );
00205     glAttachShader( program, vShader );
00206     glAttachShader( program, fShader );
00207     glLinkProgram( program );
00208     glGetProgramiv( program, GL_LINK_STATUS, &status );
00209     if( !status )
00210     {
00211         EQWARN << "Failed to link shader program" << std::endl;
00212         return;
00213     }
00214     
00215     // turn off OpenGL lighting if we are using our own shaders
00216     glDisable( GL_LIGHTING );
00217 
00218     EQINFO << "Shaders loaded successfully" << std::endl;
00219 }
00220 
00221 void Window::frameStart( const eq::uint128_t& frameID, const uint32_t frameNumber )
00222 {
00223     const Pipe*      pipe      = static_cast<Pipe*>( getPipe( ));
00224     const FrameData& frameData = pipe->getFrameData();
00225 
00226     _state->setRenderMode( frameData.getRenderMode( ));
00227     eq::Window::frameStart( frameID, frameNumber );
00228 }
00229 
00230 }
Generated on Fri Jun 8 2012 15:44:33 for Equalizer 1.2.1 by  doxygen 1.8.0