Equalizer  1.4.1
rawVolModelRenderer.cpp
00001 
00002 /* Copyright (c) 2007-2011, Maxim Makhinya  <maxmah@gmail.com>
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 #include "rawVolModelRenderer.h"
00030 
00031 #include "fragmentShader.glsl.h"
00032 #include "vertexShader.glsl.h"
00033 
00034 
00035 namespace eVolve
00036 {
00037 
00038 
00039 RawVolumeModelRenderer::RawVolumeModelRenderer( const std::string& filename, 
00040                                                 const uint32_t     precision )
00041         : _rawModel(  filename  )
00042         , _precision( precision )
00043         , _glewContext( 0 )
00044         , _ortho( false )
00045 {
00046 }
00047 
00048 
00049 static void renderSlices( const SliceClipper& sliceClipper )
00050 {
00051     int numberOfSlices = static_cast<int>( 3.6 / sliceClipper.sliceDistance );
00052 
00053     for( int s = 0; s < numberOfSlices; ++s )
00054     {
00055         glBegin( GL_POLYGON );
00056         for( int i = 0; i < 6; ++i )
00057         {
00058             eq::Vector3f pos =
00059                     sliceClipper.getPosition( i, numberOfSlices-1-s );
00060 
00061             glVertex4f( pos.x(), pos.y(), pos.z(), 1.0 );
00062         }
00063         glEnd();
00064     }
00065 }
00066 
00067 
00068 void RawVolumeModelRenderer::_putVolumeDataToShader(
00069         const VolumeInfo&   volumeInfo,
00070         const float         sliceDistance,
00071         const eq::Matrix4f& invRotationM,
00072         const eq::Vector4f& taintColor,
00073         const int           normalsQuality )
00074 {
00075     LBASSERT( _glewContext );
00076 
00077     GLhandleARB shader = _shaders.getProgram();
00078     LBASSERT( shader );
00079 
00080     const DataInTextureDimensions& TD = volumeInfo.TD; 
00081 
00082     GLint tParamNameGL;
00083 
00084     // Put texture coordinates modifyers to the shader
00085     tParamNameGL = glGetUniformLocationARB( shader, "W"  );
00086     glUniform1fARB( tParamNameGL, TD.W );
00087 
00088     tParamNameGL = glGetUniformLocationARB( shader, "H"  );
00089     glUniform1fARB( tParamNameGL, TD.H );
00090 
00091     tParamNameGL = glGetUniformLocationARB( shader, "D"  );
00092     glUniform1fARB( tParamNameGL, TD.D  );
00093 
00094     tParamNameGL = glGetUniformLocationARB( shader, "Do" );
00095     glUniform1fARB( tParamNameGL, TD.Do );
00096 
00097     tParamNameGL = glGetUniformLocationARB( shader, "Db" );
00098     glUniform1fARB( tParamNameGL, TD.Db );
00099 
00100     // Put Volume data to the shader
00101     glActiveTextureARB( GL_TEXTURE1 );
00102     glBindTexture( GL_TEXTURE_2D, volumeInfo.preint ); //preintegrated values
00103     tParamNameGL = glGetUniformLocationARB( shader, "preInt" );
00104     glUniform1iARB( tParamNameGL,  1    ); //f-shader
00105 
00106     // Activate last because it has to be the active texture
00107     glActiveTextureARB( GL_TEXTURE0 );
00108     glBindTexture( GL_TEXTURE_3D, volumeInfo.volume ); //gx, gy, gz, val
00109     glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER,GL_LINEAR    );
00110 
00111     tParamNameGL = glGetUniformLocationARB(  shader,  "volume"        );
00112     glUniform1iARB( tParamNameGL ,  0            ); //f-shader
00113 
00114     tParamNameGL = glGetUniformLocationARB(  shader,  "sliceDistance" );
00115     glUniform1fARB( tParamNameGL,  sliceDistance ); //v-shader
00116 
00117     tParamNameGL = glGetUniformLocationARB(  shader,  "perspProj"     );
00118     glUniform1fARB( tParamNameGL,  _ortho ? 0.0f : 1.0f ); //v-shader
00119 
00120     tParamNameGL = glGetUniformLocationARB(  shader,  "shininess"     );
00121     glUniform1fARB( tParamNameGL,  8.0f         ); //f-shader
00122 
00123     tParamNameGL = glGetUniformLocationARB(  shader,  "taint"         );
00124     glUniform4fARB( tParamNameGL,   taintColor.r(),
00125                                     taintColor.g(),
00126                                     taintColor.b(),
00127                                     taintColor.a()  ); //f-shader
00128 
00129     tParamNameGL = glGetUniformLocationARB(  shader,  "sizeVec"       );
00130     glUniform3fARB( tParamNameGL,   volumeInfo.voxelSize.W,
00131                                     volumeInfo.voxelSize.H,
00132                                     volumeInfo.voxelSize.D  ); //f-shader
00133 
00134     tParamNameGL = glGetUniformLocationARB(  shader,  "normalsQuality");
00135     glUniform1iARB( tParamNameGL, normalsQuality ); //f-shader
00136 
00137     // rotate viewPosition in the opposite direction of model rotation
00138     // to keep light position constant but not recalculate normals 
00139     // in the fragment shader
00140     // viewPosition = invRotationM * eq::Vector4f( 0, 0, 1, 0 );
00141     tParamNameGL = glGetUniformLocationARB(  shader,  "viewVec"       );
00142     glUniform3fARB( tParamNameGL, invRotationM.array[8],
00143                                   invRotationM.array[9],
00144                                   invRotationM.array[10] ); //f-shader
00145 }
00146 
00147 
00148 bool RawVolumeModelRenderer::render
00149 (
00150     const eq::Range&      range,
00151     const eq::Matrix4d&   modelviewM,
00152     const eq::Matrix3d&   modelviewITM,
00153     const eq::Matrix4f&   invRotationM,
00154     const eq::Vector4f&   taintColor,
00155     const int             normalsQuality
00156 )
00157 {
00158     VolumeInfo volumeInfo;
00159 
00160     if( !_rawModel.getVolumeInfo( volumeInfo, range ))
00161     {
00162         LBERROR << "Can't get volume data" << std::endl;
00163         return false;
00164     }
00165 
00166     glScalef( volumeInfo.volScaling.W,
00167               volumeInfo.volScaling.H,
00168               volumeInfo.volScaling.D );
00169 
00170     // Enable shaders
00171     glUseProgramObjectARB( _shaders.getProgram( ));
00172 
00173     // Calculate and put necessary data to shaders 
00174 
00175     const uint32_t resolution    = _rawModel.getResolution();
00176     const double   sliceDistance = 3.6 / ( resolution * _precision );
00177 
00178     _putVolumeDataToShader( volumeInfo, float( sliceDistance ),
00179                             invRotationM, taintColor, normalsQuality );
00180 
00181     _sliceClipper.updatePerFrameInfo( modelviewM, modelviewITM,
00182                                       sliceDistance, range );
00183 
00184     //Render slices
00185     glEnable( GL_BLEND );
00186     glBlendFuncSeparateEXT( GL_ONE, GL_SRC_ALPHA, GL_ZERO, GL_SRC_ALPHA );
00187 
00188     renderSlices( _sliceClipper );
00189 
00190     glDisable( GL_BLEND );
00191 
00192     // Disable shader
00193     glUseProgramObjectARB( 0 );
00194 
00195     return true;
00196 }
00197 
00198 
00199 bool RawVolumeModelRenderer::loadShaders()
00200 {
00201     if( !_shaders.loadShaders( vertexShader_glsl, fragmentShader_glsl,
00202                                _glewContext ))
00203     {
00204         LBERROR << "Can't load glsl shaders" << std::endl;
00205         return false;
00206     }
00207 
00208     LBLOG( eq::LOG_CUSTOM ) << "glsl shaders loaded" << std::endl;
00209     return true;
00210 }
00211 }
00212 
00213 
Generated on Mon Nov 26 2012 14:41:49 for Equalizer 1.4.1 by  doxygen 1.7.6.1