Equalizer  1.6.1
eqPly/window.cpp
1 
2 /* Copyright (c) 2007-2012, Stefan Eilemann <eile@equalizergraphics.com>
3  * 2007, Tobias Wolf <twolf@access.unizh.ch>
4  * 2010, Cedric Stalder <cedric.stalder@gmail.com>
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * - Redistributions of source code must retain the above copyright notice, this
10  * list of conditions and the following disclaimer.
11  * - Redistributions in binary form must reproduce the above copyright notice,
12  * this list of conditions and the following disclaimer in the documentation
13  * and/or other materials provided with the distribution.
14  * - Neither the name of Eyescale Software GmbH nor the names of its
15  * contributors may be used to endorse or promote products derived from this
16  * software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #include "window.h"
32 
33 #include "config.h"
34 #include "pipe.h"
35 #include "vertexBufferState.h"
36 
37 #include "fragmentShader.glsl.h"
38 #include "vertexShader.glsl.h"
39 
40 #include <fstream>
41 #include <sstream>
42 
43 namespace eqPly
44 {
45 
46 bool Window::configInitSystemWindow( const eq::uint128_t& initID )
47 {
48 #ifndef Darwin
50  return false;
51 
52  // OpenGL version is less than 2.0.
53  if( !GLEW_EXT_framebuffer_object )
54  {
55  if( getDrawableConfig().accumBits )
56  return true;
57 
59 #endif
60  // try with 64 bit accum buffer
63  return true;
64 
65  // no anti-aliasing possible
66  setIAttribute( IATTR_PLANES_ACCUM, eq::AUTO );
67 
68  return eq::Window::configInitSystemWindow( initID );
69 
70 #ifndef Darwin
71  }
72 
73  return true;
74 #endif
75 }
76 
77 bool Window::configInitGL( const eq::uint128_t& initID )
78 {
79  if( !eq::Window::configInitGL( initID ))
80  return false;
81 
82  glLightModeli( GL_LIGHT_MODEL_LOCAL_VIEWER, 1 );
83  glEnable( GL_CULL_FACE ); // OPT - produces sparser images in DB mode
84  glCullFace( GL_BACK );
85 
86  LBASSERT( !_state );
87  _state = new VertexBufferState( getObjectManager( ));
88 
89  const Config* config = static_cast< const Config* >( getConfig( ));
90  const InitData& initData = config->getInitData();
91 
92  if( initData.showLogo( ))
93  _loadLogo();
94 
95  if( initData.useGLSL() )
96  _loadShaders();
97 
98  return true;
99 }
100 
102 {
103  if( _state && !_state->isShared( ))
104  _state->deleteAll();
105 
106  delete _state;
107  _state = 0;
108 
109  return eq::Window::configExitGL();
110 }
111 
112 namespace
113 {
114 #ifdef EQ_RELEASE
115 # ifdef _WIN32 // final INSTALL_DIR is not known at compile time
116 static const std::string _logoTextureName =
117  std::string( "../share/Equalizer/data/logo.rgb" );
118 # else
119 static const std::string _logoTextureName = std::string( EQ_INSTALL_DIR ) +
120  std::string( "share/Equalizer/data/logo.rgb" );
121 # endif
122 #else
123 static const std::string _logoTextureName = std::string( EQ_SOURCE_DIR ) +
124  std::string( "examples/eqPly/logo.rgb" );
125 #endif
126 }
127 
128 void Window::_loadLogo()
129 {
130  if( !GLEW_ARB_texture_rectangle )
131  {
132  LBWARN << "Can't load overlay logo, GL_ARB_texture_rectangle not "
133  << "available" << std::endl;
134  return;
135  }
136 
138  _logoTexture = om->getEqTexture( _logoTextureName.c_str( ));
139  if( _logoTexture )
140  return;
141 
142  eq::Image image;
143  if( !image.readImage( _logoTextureName, eq::Frame::BUFFER_COLOR ))
144  {
145  LBWARN << "Can't load overlay logo " << _logoTextureName << std::endl;
146  return;
147  }
148 
149  _logoTexture = om->newEqTexture( _logoTextureName.c_str(),
150  GL_TEXTURE_RECTANGLE_ARB );
151  LBASSERT( _logoTexture );
152 
153  image.upload(eq::Frame::BUFFER_COLOR, _logoTexture, eq::Vector2i::ZERO, om);
154  image.deleteGLObjects( om );
155  LBVERB << "Created logo texture of size " << _logoTexture->getWidth() << "x"
156  << _logoTexture->getHeight() << std::endl;
157 }
158 
159 void Window::_loadShaders()
160 {
161  if( _state->getShader( vertexShader_glsl ) != VertexBufferState::INVALID )
162  // already loaded
163  return;
164 
165  // Check if functions are available
166  if( !GLEW_VERSION_2_0 )
167  {
168  LBWARN << "Shader function pointers missing, using fixed function "
169  << "pipeline" << std::endl;
170  return;
171  }
172 
173  const GLuint vShader = _state->newShader( vertexShader_glsl,
174  GL_VERTEX_SHADER );
175  LBASSERT( vShader != VertexBufferState::INVALID );
176  const GLchar* vShaderPtr = vertexShader_glsl;
177  glShaderSource( vShader, 1, &vShaderPtr, 0 );
178  glCompileShader( vShader );
179 
180  GLint status;
181  glGetShaderiv( vShader, GL_COMPILE_STATUS, &status );
182  if( !status )
183  {
184  LBWARN << "Failed to compile vertex shader" << std::endl;
185  return;
186  }
187 
188  const GLuint fShader =
189  _state->newShader( fragmentShader_glsl, GL_FRAGMENT_SHADER );
190  LBASSERT( fShader != VertexBufferState::INVALID );
191  const GLchar* fShaderPtr = fragmentShader_glsl;
192  glShaderSource( fShader, 1, &fShaderPtr, 0 );
193  glCompileShader( fShader );
194  glGetShaderiv( fShader, GL_COMPILE_STATUS, &status );
195  if( !status )
196  {
197  LBWARN << "Failed to compile fragment shader" << std::endl;
198  return;
199  }
200 
201  const GLuint program = _state->newProgram( getPipe() );
202  LBASSERT( program != VertexBufferState::INVALID );
203  glAttachShader( program, vShader );
204  glAttachShader( program, fShader );
205  glLinkProgram( program );
206  glGetProgramiv( program, GL_LINK_STATUS, &status );
207  if( !status )
208  {
209  LBWARN << "Failed to link shader program" << std::endl;
210  return;
211  }
212 
213  // turn off OpenGL lighting if we are using our own shaders
214  glDisable( GL_LIGHTING );
215 
216  LBINFO << "Shaders loaded successfully" << std::endl;
217 }
218 
219 void Window::frameStart( const eq::uint128_t& frameID, const uint32_t frameNumber )
220 {
221  const Pipe* pipe = static_cast<Pipe*>( getPipe( ));
222  const FrameData& frameData = pipe->getFrameData();
223 
224  _state->setRenderMode( frameData.getRenderMode( ));
225  eq::Window::frameStart( frameID, frameNumber );
226 }
227 
228 }
bool upload(const Frame::Buffer buffer, util::Texture *texture, const Vector2i &position, ObjectManager *glObjects) const
Upload this image to the frame buffer or a texture.
virtual bool configExitGL()
De-initialize the OpenGL state for this window.
A holder for pixel data.
Definition: image.h:35
virtual void frameStart(const eq::uint128_t &frameID, const uint32_t frameNumber)
Start rendering a frame.
int32_t getHeight() const
A facility class to manage OpenGL objects across shared contexts.
bool readImage(const std::string &filename, const Frame::Buffer buffer)
Read pixel data from an uncompressed rgb image file.
virtual bool configInitSystemWindow(const uint128_t &initID)
Initialize the OS-specific window.
The representation of one GPU.
void deleteGLObjects(ObjectManager *om)
Delete all OpenGL objects allocated from the given object manager.
ObjectManager * getObjectManager()
virtual bool configExitGL()
De-initialize the OpenGL state for this window.
const Config * getConfig() const
void setIAttribute(const IAttribute attr, const int32_t value)
Set a window attribute.
virtual bool configInitGL(const uint128_t &initID)
Initialize the OpenGL state for this window.
virtual bool configInitGL(const eq::uint128_t &initID)
Initialize the OpenGL state for this window.
virtual bool configExitSystemWindow()
De-initialize the OS-specific window.
int32_t getWidth() const
virtual void frameStart(const uint128_t &frameID, const uint32_t frameNumber)
Start rendering a frame.
virtual bool configInitSystemWindow(const eq::uint128_t &initID)
Initialize the OS-specific window.
The configuration, run be the EqPly application.