00001 #include <OSGConfig.h>
00002 #include <OSGDVRShader.h>
00003 #include <OSGDrawActionBase.h>
00004 #include <OSGDVRVolume.h>
00005 #include <OSGDVRVolumeTexture.h>
00006 #include <OSGDVRIsoSurface.h>
00007 #include <OSGSlicer.h>
00008 #include <OSGWindow.h>
00009 #include <OSGCamera.h>
00010 #include <OSGColor.h>
00011 #include <OSGGLU.h>
00012 #include <OSGGLEXT.h>
00013 #include "OSGDVRIsoShader.h"
00014
00015 OSG_USING_NAMESPACE
00016
00017 Vec4f cToV (Color4f & col)
00018 {
00019 return Vec4f(col.red(), col.green(), col.blue(), col.alpha());
00020 }
00021
00022
00023
00024 void DVRIsoShader::activate_FragmentProgramShading(DVRVolume *volume,
00025 DrawActionBase *action)
00026 {
00027
00028 if(m_pFragProg == NullFC)
00029 {
00030 FDEBUG(("Initializing fragment program "));
00031 m_pFragProg = FragmentProgramChunk::create();
00032
00033 addRefCP( m_pFragProg );
00034
00035 FDEBUG((" - DONE\n"));
00036
00037
00038 switch(m_shadingMode)
00039 {
00040 case SM_FRAGMENT_PROGRAM_3D:
00041
00042
00043 beginEditCP(m_pFragProg);
00044 {
00045 m_pFragProg->setProgram(_fragProg3D);
00046
00047 }
00048 endEditCP (m_pFragProg);
00049
00050 break;
00051
00052 case SM_FRAGMENT_PROGRAM_2D:
00053
00054
00055
00056 beginEditCP(m_pFragProg);
00057 {
00058 m_pFragProg->setProgram(_fragProg2D);
00059
00060 }
00061 endEditCP(m_pFragProg);
00062
00063 break;
00064
00065 default:
00066 FFATAL(("Texture mode for fragment programs not supported "
00067 "by DVRSimpleLUTShader"));
00068 return;
00069
00070 }
00071 }
00072
00073
00074 Real32 isoValue;
00075 UInt32 alphaMode;
00076 bool doSpecular;
00077
00078 DVRIsoSurfacePtr isoParam = DVRVOLUME_PARAMETER(volume, DVRIsoSurface);
00079
00080 if(isoParam != NullFC)
00081 {
00082 isoValue = isoParam->getIsoValue ();
00083 alphaMode = isoParam->getAlphaMode ();
00084 doSpecular = isoParam->getSpecularLighting();
00085 }
00086 else
00087 {
00088 isoValue = 1.0;
00089 alphaMode = GL_GREATER;
00090 doSpecular = false;
00091 }
00092
00093 unsigned int maxDiffuseLights = 6;
00094 unsigned int maxSpecularLights = 6;
00095
00096
00097 DirLightList diffuseLights;
00098 DirLightList specularLights;
00099 Color4f ambientLight;
00100
00101 getLightSources(diffuseLights, specularLights, ambientLight);
00102
00103 beginEditCP(m_pFragProg, ProgramChunk::ParamValuesFieldMask);
00104 {
00105 m_pFragProg->setParameter((short int) 0,
00106 cToV(ambientLight));
00107
00108
00109 unsigned int i;
00110
00111 DirLightList::iterator l = diffuseLights.begin();
00112
00113 for(i = 0; i < maxDiffuseLights && l != diffuseLights.end(); i++)
00114 {
00115 Vec3f tmp;
00116
00117 Slicer::rotateToLocal(action,l->dir,tmp);
00118
00119 tmp.normalize();
00120
00121
00122 m_pFragProg->setParameter(1 + 2 * i, Vec4f(tmp));
00123
00124
00125 m_pFragProg->setParameter(1 + 2 * i + 1, cToV(l->color));
00126
00127 ++l;
00128 }
00129
00130 for(; i < maxDiffuseLights; i++)
00131 {
00132
00133 m_pFragProg->setParameter(1 + 2 * i, Vec4f(0, 0, 0, 0));
00134
00135
00136 m_pFragProg->setParameter(1 + 2 * i + 1, Vec4f(0, 0, 0, 0));
00137 }
00138
00139
00140 unsigned int firstSpecId = 1 + 2 * maxDiffuseLights;
00141
00142 i = 0;
00143
00144 if(doSpecular)
00145 {
00146
00147 Vec3f viewDir;
00148
00149 Slicer::getSlicingDirection(action, &viewDir);
00150
00151 viewDir.normalize();
00152
00153 DirLightList::iterator ls = specularLights.begin();
00154
00155 for(i = 0;
00156 i < maxSpecularLights && ls != specularLights.end();
00157 i++)
00158 {
00159 Vec3f tmp;
00160
00161 Slicer::rotateToLocal(action,ls->dir,tmp);
00162
00163 tmp.normalize();
00164
00165
00166 tmp += viewDir;
00167 tmp.normalize();
00168
00169
00170 m_pFragProg->setParameter(firstSpecId + 2 * i,
00171 Vec4f(tmp));
00172
00173 m_pFragProg->setParameter(firstSpecId + 2 * i + 1,
00174 cToV(ls->color));
00175
00176 ++ls;
00177 }
00178 }
00179
00180 for(; i < maxSpecularLights; i++)
00181 {
00182
00183 m_pFragProg->setParameter(firstSpecId + 2 * i,
00184 Vec4f(0, 0, 0, 0));
00185
00186
00187 m_pFragProg->setParameter(firstSpecId + 2 * i + 1,
00188 Vec4f(0, 0, 0, 0));
00189 }
00190 }
00191 endEditCP(m_pFragProg, ProgramChunk::ParamValuesFieldMask);
00192
00193
00194 glPushAttrib(GL_ENABLE_BIT);
00195
00196
00197 m_pFragProg->activate(action);
00198
00199
00200 glDisable(GL_BLEND );
00201 glDisable(GL_LIGHTING);
00202
00203
00204 glEnable (GL_ALPHA_TEST );
00205 glAlphaFunc(alphaMode, isoValue);
00206 }
00207
00208
00209 void DVRIsoShader::deactivate_FragmentProgramShading(
00210 DVRVolume *volume,
00211 DrawActionBase *action)
00212 {
00213 glPopAttrib();
00214
00215
00216 m_pFragProg->deactivate(action);
00217 }
00218
00219
00220 void DVRIsoShader::renderSlice_FragmentProgramShading(
00221 DVRVolume *volume,
00222 DrawActionBase *action,
00223 Real32 *data,
00224 UInt32 vertices,
00225 UInt32 values )
00226 {
00227 SLOG << "DVRIsoShader::renderSlice for fragment program shading NI"
00228 << std::endl;
00229 }
00230
00231
00232 void DVRIsoShader::renderSlice_FragmentProgramShading(
00233 DVRVolume *volume,
00234 DrawActionBase *action,
00235 DVRRenderSlice *clippedSlice)
00236 {
00237 SLOG << "DVRIsoShader::renderSlice for fragment program shading NI"
00238 << std::endl;
00239 }
00240
00241
00242 char DVRIsoShader::_fragProg2D[] =
00243 "!!ARBfp1.0 \n"
00244 "PARAM scaleBias = {2, -1, 0, 0};\n"
00245 "TEMP volume, normal, intensity, color;\n"
00246 "\n"
00247 "TEX volume, fragment.texcoord[0], texture[0], 2D;\n"
00248 "MAD normal.xyz, volume, scaleBias.x, scaleBias.y;\n"
00249 "\n"
00250 "# Ambient color color\n"
00251 "MOV color, program.local[0]; # ambient color\n"
00252 "\n"
00253 "# Diffuse lighting\n"
00254 "DP3_SAT intensity, program.local[1], normal;\n"
00255 "MAD color, intensity, program.local[2], color;\n"
00256 "DP3_SAT intensity, program.local[3], normal;\n"
00257 "MAD color, intensity, program.local[4], color;\n"
00258 "DP3_SAT intensity, program.local[5], normal;\n"
00259 "MAD color, intensity, program.local[6], color;\n"
00260
00261
00262
00263
00264
00265
00266 "\n"
00267 "# Specular lighting - coefficient 4\n"
00268 "DP3_SAT intensity, program.local[13], normal;\n"
00269 "MUL intensity, intensity, intensity;\n"
00270 "MUL intensity, intensity, intensity;\n"
00271 "MAD color, intensity, program.local[14], color;\n"
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292 "\n"
00293 "MOV result.color, volume;\n"
00294 "MOV result.color.xyz, color;\n"
00295 "END\n";
00296
00297 char DVRIsoShader::_fragProg3D[] =
00298 "!!ARBfp1.0 \n"
00299 "PARAM scaleBias = {2, -1, 0, 0};\n"
00300 "TEMP volume, normal, intensity, color;\n"
00301 "\n"
00302 "TEX volume, fragment.texcoord[0], texture[0], 3D;\n"
00303 "MAD normal.xyz, volume, scaleBias.x, scaleBias.y;\n"
00304 "\n"
00305 "# Ambient color color\n"
00306 "MOV color, program.local[0]; # ambient color\n"
00307 "\n"
00308 "# Diffuse lighting\n"
00309 "DP3_SAT intensity, program.local[1], normal;\n"
00310 "MAD color, intensity, program.local[2], color;\n"
00311 "DP3_SAT intensity, program.local[3], normal;\n"
00312 "MAD color, intensity, program.local[4], color;\n"
00313 "DP3_SAT intensity, program.local[5], normal;\n"
00314 "MAD color, intensity, program.local[6], color;\n"
00315
00316
00317
00318
00319
00320
00321 "\n"
00322 "# Specular lighting - coefficient 4\n"
00323 "DP3_SAT intensity, program.local[13], normal;\n"
00324 "MUL intensity, intensity, intensity;\n"
00325 "MUL intensity, intensity, intensity;\n"
00326 "MAD color, intensity, program.local[14], color;\n"
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347 "\n"
00348 "MOV result.color, volume;\n"
00349 "MOV result.color.xyz, color;\n"
00350 "END\n";