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

OSGDVRIsoShaderColorMatrix.cpp

Go to the documentation of this file.
00001 #include <OSGConfig.h>
00002 
00003 #include "OSGDVRIsoShader.h"
00004 #include <OSGDVRShader.h>
00005 #include <OSGDrawActionBase.h>
00006 #include <OSGDVRVolume.h>
00007 #include <OSGDVRVolumeTexture.h>
00008 #include <OSGDVRIsoSurface.h>
00009 #include <OSGWindow.h>
00010 #include <OSGViewport.h>
00011 #include <OSGSlicer.h>
00012 #include <OSGCamera.h>
00013 
00014 #include <OSGGLU.h>
00015 
00016 OSG_USING_NAMESPACE
00017 
00018 void DVRIsoShader::activate_ColorMatrixShading(DVRVolume      *volume, 
00019                                                DrawActionBase *)
00020 {
00021     DVRVolumeTexturePtr vol      = DVRVOLUME_PARAMETER(volume,
00022                                                        DVRVolumeTexture);
00023 
00024     DVRIsoSurfacePtr    isoParam = DVRVOLUME_PARAMETER(volume, 
00025                                                        DVRIsoSurface);
00026 
00027     if((volume == NULL) || (vol == NullFC)) 
00028     {
00029         SWARNING << "NO Volume ..." << std::endl;
00030         return;
00031     }
00032 
00033     Real32 isoValue;
00034     UInt32 alphaMode;
00035 
00036     // get parameters from isosurface attachment if available
00037     if(isoParam!=NullFC)
00038     {
00039         isoValue  = isoParam->getIsoValue ();
00040         alphaMode = isoParam->getAlphaMode();
00041     }
00042     else
00043     {
00044         isoValue  = 1.0;
00045         alphaMode = GL_GREATER;
00046     }
00047 
00048     glPushAttrib(GL_COLOR_BUFFER_BIT   |
00049                  GL_ENABLE_BIT         | 
00050                  GL_DEPTH_BUFFER_BIT   | 
00051                  GL_STENCIL_BUFFER_BIT | 
00052                  GL_POLYGON_BIT        |
00053                  GL_PIXEL_MODE_BIT);
00054 
00055     if(volume->getDoTextures()) 
00056     { 
00057         
00058         glGetIntegerv(GL_COLOR_WRITEMASK,m_colorWriteMask);
00059         
00060         // draw color 1 for MODULATE
00061         glColor4f(1.0, 1.0, 1.0, 1.0);
00062 
00063         // Enable Alpha Test for isosurface
00064         glEnable(GL_ALPHA_TEST); 
00065         glAlphaFunc(alphaMode, isoValue);
00066 
00067         // no blending and lighting
00068         glDisable(GL_BLEND);
00069         glDisable(GL_LIGHTING);
00070     
00071         // setup stencil buffer
00072         // to mask the "volume region"
00073         glColorMask(GL_TRUE,GL_TRUE,GL_TRUE,GL_FALSE);
00074         glEnable(GL_STENCIL_TEST);
00075         glClearStencil(0xFF);
00076         glStencilMask(0xFF);
00077         glClear(GL_STENCIL_BUFFER_BIT);
00078         glStencilFunc(GL_ALWAYS, 0x0, 0x1); // write always
00079         glStencilOp(GL_KEEP, GL_KEEP, GL_ZERO);
00080     }
00081 
00082 }
00083 
00084 
00085 void DVRIsoShader::deactivate_ColorMatrixShading(DVRVolume      *volume, 
00086                                                  DrawActionBase *action)
00087 {
00088     if(volume->getDoTextures()) 
00089     {
00090         
00091         glDisable(GL_DEPTH_TEST);
00092         glDisable(GL_ALPHA_TEST);
00093         glDepthMask(GL_FALSE);
00094         glDisable(GL_LIGHTING);
00095 
00096         glStencilFunc(GL_EQUAL, 0x0, 0xFF);
00097         glStencilOp(GL_KEEP,GL_KEEP,GL_KEEP);          
00098 
00099         Color4f diffuseLightColor;
00100         Vec3f   diffuseDir;
00101 
00102         DirLightList diffuseLights;
00103         DirLightList specularLights;
00104         Color4f ambientLight(0.0f,0.0f,0.0f,0.0f);
00105 
00106         getLightSources(diffuseLights,specularLights,ambientLight);
00107 
00108         // use the first available diffuse lightsource for shading
00109         if(diffuseLights.empty())
00110         {
00111             diffuseLightColor[0] = 0.0f;
00112             diffuseLightColor[1] = 0.0f;
00113             diffuseLightColor[2] = 0.0f;
00114             diffuseLightColor[3] = 0.0f;
00115         }
00116         else
00117         {
00118             diffuseLightColor = diffuseLights.begin()->color;
00119 
00120             Slicer::rotateToLocal(action, 
00121                                   diffuseLights.begin()->dir, 
00122                                   diffuseDir);
00123         }
00124 
00125         // store the matrix mode
00126         GLint oldMatrixMode;
00127         glGetIntegerv(GL_MATRIX_MODE, &oldMatrixMode);
00128     
00129         // store alpha scale and bias
00130         GLfloat oldAlphaBias;
00131         GLfloat oldAlphaScale;
00132 
00133         glGetFloatv(GL_ALPHA_BIAS,  &oldAlphaBias);
00134         glGetFloatv(GL_ALPHA_SCALE, &oldAlphaScale);
00135 
00136         // ensure alpha is 1 
00137         glPixelTransferf(GL_ALPHA_SCALE, 0.00001f);
00138         glPixelTransferf(GL_ALPHA_BIAS,  0.999999f);
00139 
00140         // compute shading
00141 
00142         // switch to color matrix mode to compute diffuse shading
00143         glMatrixMode(GL_COLOR);
00144     
00145         // push actual color matrix
00146         glPushMatrix();
00147     
00148         GLfloat colorMatV[16];
00149 
00150         // set light color
00151         colorMatV[0] = m_colorWriteMask[0] ? diffuseLightColor[0] : 0.0f;
00152         colorMatV[1] = 0.0f;
00153         colorMatV[2] = 0.0f;
00154         colorMatV[3] = 0.0f;
00155 
00156         colorMatV[4] = 0.0f;
00157         colorMatV[5] = m_colorWriteMask[1] ? diffuseLightColor[1] : 0.0f;
00158         colorMatV[6] = 0.0f;
00159         colorMatV[7] = 0.0f;
00160     
00161         colorMatV[8] = 0.0f;
00162         colorMatV[9] = 0.0f;
00163         colorMatV[10] = m_colorWriteMask[2] ? diffuseLightColor[2] : 0.0f;
00164         colorMatV[11] = 0.0f;
00165      
00166         colorMatV[12] = m_colorWriteMask[0] ? ambientLight[0] : 0.0f;
00167         colorMatV[13] = m_colorWriteMask[1] ? ambientLight[1] : 0.0f;
00168         colorMatV[14] = m_colorWriteMask[2] ? ambientLight[2] : 0.0f;
00169         colorMatV[15] = 1.0f;
00170     
00171         glLoadMatrixf(colorMatV);   
00172 
00173         // set light direction for computation of cosine-term
00174         colorMatV[0] = diffuseDir[0];
00175         colorMatV[1] = diffuseDir[0];
00176         colorMatV[2] = diffuseDir[0];
00177         //colorMatV[3] = 0.0f;
00178     
00179         colorMatV[4] = diffuseDir[1];
00180         colorMatV[5] = diffuseDir[1];
00181         colorMatV[6] = diffuseDir[1];
00182         //colorMatV[7] = 0.0f;
00183     
00184         colorMatV[8] = diffuseDir[2];
00185         colorMatV[9] = diffuseDir[2];
00186         colorMatV[10] = diffuseDir[2];
00187         //colorMatV[11] = 0.0f;
00188     
00189         colorMatV[12] = 0.0f;
00190         colorMatV[13] = 0.0f;
00191         colorMatV[14] = 0.0f;
00192         //colorMatV[15] = 1.0f;
00193     
00194         glMultMatrixf(colorMatV);       
00195 
00196         // rescale/bias gradients
00197         colorMatV[0] = 2.0f;
00198         colorMatV[1] = 0.0f;
00199         colorMatV[2] = 0.0f;
00200     
00201         colorMatV[3] = 0.0f;
00202     
00203         colorMatV[4] = 0.0f;
00204         colorMatV[5] = 2.0f;
00205         colorMatV[6] = 0.0f;
00206     
00207         colorMatV[7] = 0.0f;
00208      
00209         colorMatV[8] = 0.0f;
00210         colorMatV[9] = 0.0f;
00211         colorMatV[10] = 2.0f;
00212     
00213         colorMatV[11] = 0.0f;
00214       
00215         colorMatV[12] = -1.0f;
00216         colorMatV[13] = -1.0f;
00217         colorMatV[14] = -1.0f;
00218     
00219         colorMatV[15] = 1.0f;
00220 
00221         // set the color matrix on top of the stack
00222         glMultMatrixf(colorMatV);
00223     
00224         // compute shading
00225         GLint drawBuf;
00226         glGetIntegerv(GL_DRAW_BUFFER, &drawBuf);
00227         glReadBuffer(drawBuf);
00228 
00229         // get covered screen rect in device coordinates
00230         GLfloat screenS[4];
00231         getCoveredScreenRect(volume,action,screenS);
00232 
00233         GLint vpOrigin[2];
00234         GLint vpSize[2];
00235 
00236         vpOrigin[0] = action->getViewport()->getPixelLeft();
00237         vpOrigin[1] = action->getViewport()->getPixelBottom();
00238 
00239         vpSize[0]   = 
00240             action->getViewport()->getPixelRight() -
00241             action->getViewport()->getPixelLeft ();
00242 
00243         vpSize[1]   = 
00244             action->getViewport()->getPixelTop   () -
00245             action->getViewport()->getPixelBottom();
00246     
00247         // compute covered pixels
00248         GLint sr[4];
00249 
00250         sr[0] = (GLint) osgfloor(vpOrigin[0]+(screenS[0]+1.0f)*(vpSize[0]/2));
00251         sr[1] = (GLint) osgfloor(vpOrigin[1]+(screenS[1]+1.0f)*(vpSize[1]/2));
00252         sr[2] = (GLint) osgceil (vpOrigin[0]+(screenS[2]+1.0f)*(vpSize[0]/2));
00253         sr[3] = (GLint) osgceil (vpOrigin[1]+(screenS[3]+1.0f)*(vpSize[1]/2));
00254 
00255         // copy covered pixels 
00256         glMatrixMode(GL_MODELVIEW);
00257         glPushMatrix();
00258         glLoadIdentity();
00259         glMatrixMode(GL_PROJECTION);
00260         glPushMatrix();
00261         glLoadIdentity();
00262         glRasterPos2f(screenS[0],screenS[1]);
00263         glCopyPixels(sr[0],sr[1],sr[2]-sr[0]+1,sr[3]-sr[1]+1, GL_COLOR);
00264         glPopMatrix();
00265         glMatrixMode(GL_MODELVIEW);
00266         glPopMatrix();
00267     
00268         // restore alpha bias and scale
00269         glPixelTransferf(GL_ALPHA_BIAS, oldAlphaBias);
00270         glPixelTransferf(GL_ALPHA_SCALE, oldAlphaScale);
00271 
00272         // restore old color matrix
00273         glMatrixMode(GL_COLOR);
00274         glPopMatrix();
00275       
00276         // restore old matrix mode
00277         glMatrixMode(oldMatrixMode);
00278 
00279     }
00280 
00281     glPopAttrib();
00282 }
00283 
00284 void DVRIsoShader::getCoveredScreenRect(DVRVolume      *volume, 
00285                                         DrawActionBase *action, 
00286                                         GLfloat         screenRect[4])
00287 {
00288     int vpSize[2];
00289 
00290     vpSize[0] = action->getViewport()->getPixelWidth();
00291     vpSize[1] = action->getViewport()->getPixelHeight();
00292     
00293     DVRVolumeTexturePtr tex = DVRVOLUME_PARAMETER(volume, DVRVolumeTexture);
00294 
00295     if(tex != NullFC) 
00296     {
00297         
00298         Matrix4f modelMatrix = action->getActNode()->getToWorld();
00299         Matrix4f viewMatrix  = action->getCamera()->getBeacon()->getToWorld();
00300 
00301         viewMatrix.invert();
00302 
00303         Matrix4f prjTranslationMatrix;
00304 
00305         action->getCamera()->getProjectionTranslation(prjTranslationMatrix,
00306                                                       vpSize[0],
00307                                                       vpSize[1]);
00308         Matrix4f prjMatrix;
00309 
00310         action->getCamera()->getProjection(prjMatrix,
00311                                            vpSize[0],
00312                                            vpSize[1]);       
00313 
00314         Matrix4f toScreenMatrix = modelMatrix;
00315 
00316         toScreenMatrix.multLeft(viewMatrix);
00317         toScreenMatrix.multLeft(prjTranslationMatrix);
00318         toScreenMatrix.multLeft(prjMatrix);        
00319         
00320  
00321         Vec3f & res   = tex->getResolution();
00322         Vec3f & slice = tex->getSliceThickness();
00323         
00324         Pnt3f minBB(-0.5f * res[0] * slice[0], 
00325                     -0.5f * res[1] * slice[1], 
00326                     -0.5f * res[2] * slice[2]);
00327 
00328         Pnt3f maxBB(-minBB);
00329     
00330         Pnt3f BB[8] = 
00331         {
00332             Pnt3f(minBB[0],minBB[1],minBB[2]),
00333             Pnt3f(maxBB[0],minBB[1],minBB[2]),
00334             Pnt3f(maxBB[0],maxBB[1],minBB[2]),
00335             Pnt3f(minBB[0],maxBB[1],minBB[2]),
00336             Pnt3f(minBB[0],minBB[1],maxBB[2]),
00337             Pnt3f(maxBB[0],minBB[1],maxBB[2]),
00338             Pnt3f(maxBB[0],maxBB[1],maxBB[2]),
00339             Pnt3f(minBB[0],maxBB[1],maxBB[2])
00340         };
00341  
00342 
00343         Pnt3f tBB;
00344         toScreenMatrix.multFullMatrixPnt(BB[0],tBB);
00345 
00346         screenRect[0] = tBB[0]; 
00347         screenRect[1] = tBB[1]; 
00348         screenRect[2] = tBB[0]; 
00349         screenRect[3] = tBB[1];
00350 
00351         // get min,max coordinates
00352         for(unsigned int i = 1; i < 8; i++)
00353         {
00354             toScreenMatrix.multFullMatrixPnt(BB[i],tBB);
00355             
00356             if(tBB[0] < screenRect[0]) 
00357             {
00358                 screenRect[0] = tBB[0];
00359             }
00360             else if(tBB[0] > screenRect[2]) 
00361             {
00362                 screenRect[2] = tBB[0]; 
00363             }
00364           
00365             if(tBB[1] < screenRect[1]) 
00366             {
00367                 screenRect[1] = tBB[1]; 
00368             }
00369             else if(tBB[1] > screenRect[3])
00370             {
00371                 screenRect[3] = tBB[1]; 
00372             }
00373         }
00374         
00375         // clamp covered rectangle to viewport
00376         for(unsigned int i = 0; i < 4; i++)
00377         {
00378             if(screenRect[i] < -1.0) 
00379                 screenRect[i] = -1.0;
00380             else if(screenRect[i] > 1.0) 
00381                 screenRect[i] = 1.0;
00382         }
00383     }    
00384     else 
00385     {
00386         // something wrong with initialization - do not copy anything
00387         screenRect[0] = screenRect[1] = screenRect[2] = screenRect[3] = 0;
00388     }
00389 }

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