Equalizer 1.0

rawVolModelRenderer.cpp

00001 
00002 /* Copyright (c) 2007       Maxim Makhinya
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( const VolumeInfo& volumeInfo, const float sliceDistance, const eq::Matrix4f& invRotationM )
00069 {
00070     EQASSERT( _glewContext );
00071 
00072     GLhandleARB shader = _shaders.getProgram();
00073     EQASSERT( shader );
00074 
00075     const DataInTextureDimensions& TD = volumeInfo.TD; 
00076 
00077     GLint tParamNameGL;
00078 
00079     // Put texture coordinates modifyers to the shader
00080     tParamNameGL = glGetUniformLocationARB( shader, "W"  );
00081     glUniform1fARB( tParamNameGL, TD.W );
00082 
00083     tParamNameGL = glGetUniformLocationARB( shader, "H"  );
00084     glUniform1fARB( tParamNameGL, TD.H );
00085 
00086     tParamNameGL = glGetUniformLocationARB( shader, "D"  );
00087     glUniform1fARB( tParamNameGL, TD.D  );
00088 
00089     tParamNameGL = glGetUniformLocationARB( shader, "Do" );
00090     glUniform1fARB( tParamNameGL, TD.Do );
00091 
00092     tParamNameGL = glGetUniformLocationARB( shader, "Db" );
00093     glUniform1fARB( tParamNameGL, TD.Db );
00094 
00095     // Put Volume data to the shader
00096     glActiveTextureARB( GL_TEXTURE1 );
00097     glBindTexture( GL_TEXTURE_2D, volumeInfo.preint ); //preintegrated values
00098     tParamNameGL = glGetUniformLocationARB( shader, "preInt" );
00099     glUniform1iARB( tParamNameGL,  1    ); //f-shader
00100 
00101     // Activate last because it has to be the active texture
00102     glActiveTextureARB( GL_TEXTURE0 );
00103     glBindTexture( GL_TEXTURE_3D, volumeInfo.volume ); //gx, gy, gz, val
00104     glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER,GL_LINEAR    );
00105 
00106     tParamNameGL = glGetUniformLocationARB(  shader,  "volume"        );
00107     glUniform1iARB( tParamNameGL ,  0            ); //f-shader
00108 
00109     tParamNameGL = glGetUniformLocationARB(  shader,  "sliceDistance" );
00110     glUniform1fARB( tParamNameGL,  sliceDistance ); //v-shader
00111 
00112     tParamNameGL = glGetUniformLocationARB(  shader,  "perspProj" );
00113     glUniform1fARB( tParamNameGL,  _ortho ? 0.0f : 1.0f ); //v-shader
00114 
00115     tParamNameGL = glGetUniformLocationARB(  shader,  "shininess"     );
00116     glUniform1fARB( tParamNameGL,  8.0f         ); //f-shader
00117 
00118     // rotate viewPosition in the opposite direction of model rotation
00119     // to keep light position constant but not recalculate normals 
00120     // in the fragment shader
00121     // viewPosition = invRotationM * eq::Vector4f( 0, 0, 1, 0 );
00122     tParamNameGL = glGetUniformLocationARB(  shader,  "viewVec"       );
00123     glUniform3fARB( tParamNameGL, invRotationM.array[8],
00124                                   invRotationM.array[9],
00125                                   invRotationM.array[10] ); //f-shader
00126 }
00127 
00128 
00129 bool RawVolumeModelRenderer::render
00130 (
00131     const eq::Range&        range,
00132     const eq::Matrix4d&   modelviewM,
00133     const eq::Matrix3d&   modelviewITM,
00134     const eq::Matrix4f&   invRotationM
00135 )
00136 {
00137     VolumeInfo volumeInfo;
00138 
00139     if( !_rawModel.getVolumeInfo( volumeInfo, range ))
00140     {
00141         EQERROR << "Can't get volume data" << std::endl;
00142         return false;
00143     }
00144 
00145     glScalef( volumeInfo.volScaling.W,
00146               volumeInfo.volScaling.H,
00147               volumeInfo.volScaling.D );
00148 
00149     // Enable shaders
00150     glUseProgramObjectARB( _shaders.getProgram( ));
00151 
00152     // Calculate and put necessary data to shaders 
00153 
00154     const uint32_t resolution    = _rawModel.getResolution();
00155     const double   sliceDistance = 3.6 / ( resolution * _precision );
00156 
00157     _putVolumeDataToShader( volumeInfo, float( sliceDistance ), invRotationM );
00158 
00159     _sliceClipper.updatePerFrameInfo( modelviewM, modelviewITM,
00160                                       sliceDistance, range );
00161 
00162     //Render slices
00163     glEnable( GL_BLEND );
00164     glBlendFuncSeparateEXT( GL_ONE, GL_SRC_ALPHA, GL_ZERO, GL_SRC_ALPHA );
00165 
00166     renderSlices( _sliceClipper );
00167 
00168     glDisable( GL_BLEND );
00169 
00170     // Disable shader
00171     glUseProgramObjectARB( 0 );
00172 
00173     return true;
00174 }
00175 
00176 
00177 bool RawVolumeModelRenderer::loadShaders()
00178 {
00179     if( !_shaders.loadShaders( vertexShader_glsl, fragmentShader_glsl,
00180                                _glewContext ))
00181     {
00182         EQERROR << "Can't load glsl shaders" << std::endl;
00183         return false;
00184     }
00185 
00186     EQLOG( eq::LOG_CUSTOM ) << "glsl shaders loaded" << std::endl;
00187     return true;
00188 }
00189 }
00190 
00191 
Generated on Sun May 8 2011 19:11:07 for Equalizer 1.0 by  doxygen 1.7.3