Equalizer  1.6.1
rawVolModelRenderer.cpp
1 
2 /* Copyright (c) 2007-2011, Maxim Makhinya <maxmah@gmail.com>
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * - Redistributions of source code must retain the above copyright notice, this
8  * list of conditions and the following disclaimer.
9  * - Redistributions in binary form must reproduce the above copyright notice,
10  * this list of conditions and the following disclaimer in the documentation
11  * and/or other materials provided with the distribution.
12  * - Neither the name of Eyescale Software GmbH nor the names of its
13  * contributors may be used to endorse or promote products derived from this
14  * software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include "rawVolModelRenderer.h"
30 
31 #include "fragmentShader.glsl.h"
32 #include "vertexShader.glsl.h"
33 
34 
35 namespace eVolve
36 {
37 
38 
39 RawVolumeModelRenderer::RawVolumeModelRenderer( const std::string& filename,
40  const uint32_t precision )
41  : _rawModel( filename )
42  , _precision( precision )
43  , _glewContext( 0 )
44  , _ortho( false )
45 {
46 }
47 
48 
49 static void renderSlices( const SliceClipper& sliceClipper )
50 {
51  int numberOfSlices = static_cast<int>( 3.6 / sliceClipper.sliceDistance );
52 
53  for( int s = 0; s < numberOfSlices; ++s )
54  {
55  glBegin( GL_POLYGON );
56  for( int i = 0; i < 6; ++i )
57  {
58  eq::Vector3f pos =
59  sliceClipper.getPosition( i, numberOfSlices-1-s );
60 
61  glVertex4f( pos.x(), pos.y(), pos.z(), 1.0 );
62  }
63  glEnd();
64  }
65 }
66 
67 
68 void RawVolumeModelRenderer::_putVolumeDataToShader(
69  const VolumeInfo& volumeInfo,
70  const float sliceDistance,
71  const eq::Matrix4f& invRotationM,
72  const eq::Vector4f& taintColor,
73  const int normalsQuality )
74 {
75  LBASSERT( _glewContext );
76 
77  GLhandleARB shader = _shaders.getProgram();
78  LBASSERT( shader );
79 
80  const DataInTextureDimensions& TD = volumeInfo.TD;
81 
82  GLint tParamNameGL;
83 
84  // Put texture coordinates modifyers to the shader
85  tParamNameGL = glGetUniformLocationARB( shader, "W" );
86  glUniform1fARB( tParamNameGL, TD.W );
87 
88  tParamNameGL = glGetUniformLocationARB( shader, "H" );
89  glUniform1fARB( tParamNameGL, TD.H );
90 
91  tParamNameGL = glGetUniformLocationARB( shader, "D" );
92  glUniform1fARB( tParamNameGL, TD.D );
93 
94  tParamNameGL = glGetUniformLocationARB( shader, "Do" );
95  glUniform1fARB( tParamNameGL, TD.Do );
96 
97  tParamNameGL = glGetUniformLocationARB( shader, "Db" );
98  glUniform1fARB( tParamNameGL, TD.Db );
99 
100  // Put Volume data to the shader
101  glActiveTextureARB( GL_TEXTURE1 );
102  glBindTexture( GL_TEXTURE_2D, volumeInfo.preint ); //preintegrated values
103  tParamNameGL = glGetUniformLocationARB( shader, "preInt" );
104  glUniform1iARB( tParamNameGL, 1 ); //f-shader
105 
106  // Activate last because it has to be the active texture
107  glActiveTextureARB( GL_TEXTURE0 );
108  glBindTexture( GL_TEXTURE_3D, volumeInfo.volume ); //gx, gy, gz, val
109  glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER,GL_LINEAR );
110 
111  tParamNameGL = glGetUniformLocationARB( shader, "volume" );
112  glUniform1iARB( tParamNameGL , 0 ); //f-shader
113 
114  tParamNameGL = glGetUniformLocationARB( shader, "sliceDistance" );
115  glUniform1fARB( tParamNameGL, sliceDistance ); //v-shader
116 
117  tParamNameGL = glGetUniformLocationARB( shader, "perspProj" );
118  glUniform1fARB( tParamNameGL, _ortho ? 0.0f : 1.0f ); //v-shader
119 
120  tParamNameGL = glGetUniformLocationARB( shader, "shininess" );
121  glUniform1fARB( tParamNameGL, 8.0f ); //f-shader
122 
123  tParamNameGL = glGetUniformLocationARB( shader, "taint" );
124  glUniform4fARB( tParamNameGL, taintColor.r(),
125  taintColor.g(),
126  taintColor.b(),
127  taintColor.a() ); //f-shader
128 
129  tParamNameGL = glGetUniformLocationARB( shader, "sizeVec" );
130  glUniform3fARB( tParamNameGL, volumeInfo.voxelSize.W,
131  volumeInfo.voxelSize.H,
132  volumeInfo.voxelSize.D ); //f-shader
133 
134  tParamNameGL = glGetUniformLocationARB( shader, "normalsQuality");
135  glUniform1iARB( tParamNameGL, normalsQuality ); //f-shader
136 
137  // rotate viewPosition in the opposite direction of model rotation
138  // to keep light position constant but not recalculate normals
139  // in the fragment shader
140  // viewPosition = invRotationM * eq::Vector4f( 0, 0, 1, 0 );
141  tParamNameGL = glGetUniformLocationARB( shader, "viewVec" );
142  glUniform3fARB( tParamNameGL, invRotationM.array[8],
143  invRotationM.array[9],
144  invRotationM.array[10] ); //f-shader
145 }
146 
147 
148 bool RawVolumeModelRenderer::render
149 (
150  const eq::Range& range,
151  const eq::Matrix4d& modelviewM,
152  const eq::Matrix3d& modelviewITM,
153  const eq::Matrix4f& invRotationM,
154  const eq::Vector4f& taintColor,
155  const int normalsQuality
156 )
157 {
158  VolumeInfo volumeInfo;
159 
160  if( !_rawModel.getVolumeInfo( volumeInfo, range ))
161  {
162  LBERROR << "Can't get volume data" << std::endl;
163  return false;
164  }
165 
166  glScalef( volumeInfo.volScaling.W,
167  volumeInfo.volScaling.H,
168  volumeInfo.volScaling.D );
169 
170  // Enable shaders
171  glUseProgramObjectARB( _shaders.getProgram( ));
172 
173  // Calculate and put necessary data to shaders
174 
175  const uint32_t resolution = _rawModel.getResolution();
176  const double sliceDistance = 3.6 / ( resolution * _precision );
177 
178  _putVolumeDataToShader( volumeInfo, float( sliceDistance ),
179  invRotationM, taintColor, normalsQuality );
180 
181  _sliceClipper.updatePerFrameInfo( modelviewM, modelviewITM,
182  sliceDistance, range );
183 
184  //Render slices
185  glEnable( GL_BLEND );
186  glBlendFuncSeparateEXT( GL_ONE, GL_SRC_ALPHA, GL_ZERO, GL_SRC_ALPHA );
187 
188  renderSlices( _sliceClipper );
189 
190  glDisable( GL_BLEND );
191 
192  // Disable shader
193  glUseProgramObjectARB( 0 );
194 
195  return true;
196 }
197 
198 
199 bool RawVolumeModelRenderer::loadShaders()
200 {
201  if( !_shaders.loadShaders( vertexShader_glsl, fragmentShader_glsl,
202  _glewContext ))
203  {
204  LBERROR << "Can't load glsl shaders" << std::endl;
205  return false;
206  }
207 
208  LBLOG( eq::LOG_CUSTOM ) << "glsl shaders loaded" << std::endl;
209  return true;
210 }
211 }
212 
213 
User-defined log topics (65536)
Definition: client/log.h:35