Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

OSGDVRIsoShaderARBFragmentProgram.cpp

Go to the documentation of this file.
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 // setup state for shading with fragment program
00024 void DVRIsoShader::activate_FragmentProgramShading(DVRVolume      *volume, 
00025                                                    DrawActionBase *action)
00026 {
00027     // Initialize the fragment program 
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         // Load the appropriate program
00038         switch(m_shadingMode) 
00039         {
00040             case SM_FRAGMENT_PROGRAM_3D:
00041                 // SLOG << "Loading ... isoFragProg3D.asm" << std::endl;
00042 
00043                 beginEditCP(m_pFragProg);
00044                 {
00045                     m_pFragProg->setProgram(_fragProg3D);
00046                     // m_pFragProg->read( "isoFragProg3D.asm" );
00047                 }
00048                 endEditCP  (m_pFragProg);
00049 
00050                 break;
00051 
00052             case SM_FRAGMENT_PROGRAM_2D:
00053 
00054                 // SLOG << "Loading ... isoFragProg2D.asm" << std::endl;
00055                 
00056                 beginEditCP(m_pFragProg);
00057                 {
00058                     m_pFragProg->setProgram(_fragProg2D);
00059                     // m_pFragProg->read( "isoFragProg2D.asm" );
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                 // break;
00070         }
00071     }
00072     
00073     // get parameters from isosurface attachment if available
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; // Hard-coded in the fragment program
00094     unsigned int maxSpecularLights = 6;
00095     
00096     // Set light parameters
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)); // ambient color
00107 
00108         // Diffuse lights
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             // diffuse direction
00122             m_pFragProg->setParameter(1 + 2 * i,     Vec4f(tmp));
00123 
00124             // diffuse color
00125             m_pFragProg->setParameter(1 + 2 * i + 1, cToV(l->color)); 
00126 
00127             ++l;
00128         }
00129 
00130         for(; i < maxDiffuseLights; i++) 
00131         {
00132             // diffuse direction
00133             m_pFragProg->setParameter(1 + 2 * i,     Vec4f(0, 0, 0, 0));
00134 
00135             // diffuse color
00136             m_pFragProg->setParameter(1 + 2 * i + 1, Vec4f(0, 0, 0, 0)); 
00137         }
00138         
00139         // Specular lights
00140         unsigned int firstSpecId = 1 + 2 * maxDiffuseLights;
00141 
00142         i = 0;
00143 
00144         if(doSpecular) 
00145         {
00146             // get and classify the slicing direction
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                 // calc halfway
00166                 tmp += viewDir;
00167                 tmp.normalize();
00168 
00169                 // halfway vector
00170                 m_pFragProg->setParameter(firstSpecId + 2 * i,     
00171                                           Vec4f(tmp));
00172                 // specular color
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             // specular direction
00183             m_pFragProg->setParameter(firstSpecId + 2 * i,     
00184                                       Vec4f(0, 0, 0, 0)); 
00185 
00186             // specular color
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     // activate fragment program chunk
00197     m_pFragProg->activate(action);
00198 
00199     // no blending and lighting
00200     glDisable(GL_BLEND   );
00201     glDisable(GL_LIGHTING);
00202     
00203     // Enable Alpha Test for isosurface
00204     glEnable   (GL_ALPHA_TEST      ); 
00205     glAlphaFunc(alphaMode, isoValue);
00206 }
00207 
00208 // cleanup state for shading with fragment program
00209 void DVRIsoShader::deactivate_FragmentProgramShading(
00210     DVRVolume      *volume, 
00211     DrawActionBase *action)
00212 {
00213     glPopAttrib();
00214     
00215     // de-activate fragment program chunk
00216     m_pFragProg->deactivate(action);
00217 }
00218 
00219 // render a slice for 2D multitexture fragment program shading
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 // render a clipped slice for 2D multitexture fragment program shading
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 //         "DP3_SAT intensity, program.local[7], normal;\n"
00261 //         "MAD color, intensity, program.local[8], color;\n" 
00262 //         "DP3_SAT intensity, program.local[9], normal;\n"
00263 //         "MAD color, intensity, program.local[10], color;\n"
00264 //         "DP3_SAT intensity, program.local[11], normal;\n"
00265 //         "MAD color, intensity, program.local[12], color;\n"
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 //          "DP3_SAT intensity, program.local[15], normal;\n"
00273 //          "MUL intensity, intensity, intensity;\n"
00274 //          "MUL intensity, intensity, intensity;\n"
00275 //          "MAD color, intensity, program.local[16], color;\n"
00276 //          "DP3_SAT intensity, program.local[17], normal;\n"
00277 //          "MUL intensity, intensity, intensity;\n"
00278 //          "MUL intensity, intensity, intensity;\n"
00279 //          "MAD color, intensity, program.local[18], color;\n"
00280 //          "DP3_SAT intensity, program.local[19], normal;\n"
00281 //          "MUL intensity, intensity, intensity;\n"
00282 //          "MUL intensity, intensity, intensity;\n"
00283 //          "MAD color, intensity, program.local[20], color;\n"
00284 //          "DP3_SAT intensity, program.local[21], normal;\n"
00285 //          "MUL intensity, intensity, intensity;\n"
00286 //          "MUL intensity, intensity, intensity;\n"
00287 //          "MAD color, intensity, program.local[22], color;\n" 
00288 //          "DP3_SAT intensity, program.local[23], normal;\n"
00289 //          "MUL intensity, intensity, intensity;\n"
00290 //          "MUL intensity, intensity, intensity;\n"
00291 //          "MAD color, intensity, program.local[24], color;\n"
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 //         "DP3_SAT intensity, program.local[7], normal;\n"
00316 //         "MAD color, intensity, program.local[8], color;\n" 
00317 //         "DP3_SAT intensity, program.local[9], normal;\n"
00318 //         "MAD color, intensity, program.local[10], color;\n"
00319 //         "DP3_SAT intensity, program.local[11], normal;\n"
00320 //         "MAD color, intensity, program.local[12], color;\n"
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 //          "DP3_SAT intensity, program.local[15], normal;\n"
00328 //          "MUL intensity, intensity, intensity;\n"
00329 //          "MUL intensity, intensity, intensity;\n"
00330 //          "MAD color, intensity, program.local[16], color;\n"
00331 //          "DP3_SAT intensity, program.local[17], normal;\n"
00332 //          "MUL intensity, intensity, intensity;\n"
00333 //          "MUL intensity, intensity, intensity;\n"
00334 //          "MAD color, intensity, program.local[18], color;\n"
00335 //          "DP3_SAT intensity, program.local[19], normal;\n"
00336 //          "MUL intensity, intensity, intensity;\n"
00337 //          "MUL intensity, intensity, intensity;\n"
00338 //          "MAD color, intensity, program.local[20], color;\n"
00339 //          "DP3_SAT intensity, program.local[21], normal;\n"
00340 //          "MUL intensity, intensity, intensity;\n"
00341 //          "MUL intensity, intensity, intensity;\n"
00342 //          "MAD color, intensity, program.local[22], color;\n" 
00343 //          "DP3_SAT intensity, program.local[23], normal;\n"
00344 //          "MUL intensity, intensity, intensity;\n"
00345 //          "MUL intensity, intensity, intensity;\n"
00346 //          "MAD color, intensity, program.local[24], color;\n"
00347          "\n"
00348          "MOV result.color, volume;\n"
00349          "MOV result.color.xyz, color;\n"
00350          "END\n";

Generated on Thu Aug 25 04:03:36 2005 for OpenSG by  doxygen 1.4.3