VTK
dox/VolumeRendering/vtkOpenGLGPUVolumeRayCastMapper.h
Go to the documentation of this file.
00001 /*=========================================================================
00002 
00003   Program:   Visualization Toolkit
00004   Module:    vtkOpenGLGPUVolumeRayCastMapper.h
00005 
00006   Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
00007   All rights reserved.
00008   See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
00009 
00010      This software is distributed WITHOUT ANY WARRANTY; without even
00011      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
00012      PURPOSE.  See the above copyright notice for more information.
00013 
00014 =========================================================================*/
00029 #ifndef __vtkOpenGLGPUVolumeRayCastMapper_h
00030 #define __vtkOpenGLGPUVolumeRayCastMapper_h
00031 
00032 #include "vtkGPUVolumeRayCastMapper.h"
00033 
00034 class vtkVolume;
00035 class vtkRenderer;
00036 class vtkOpenGLExtensionManager;
00037 class vtkMatrix4x4;
00038 class vtkUnsupportedRequiredExtensionsStringStream; // Pimpl
00039 class vtkMapDataArrayTextureId; // Pimpl
00040 class vtkMapMaskTextureId; // Pimpl
00041 class vtkPolyData;
00042 class vtkClipConvexPolyData;
00043 class vtkClipPolyData;
00044 class vtkTessellatedBoxSource;
00045 
00046 class vtkOpacityTable; // internal class.
00047 class vtkRGBTable; // internal class.
00048 class vtkKWScalarField; // internal class.
00049 class vtkKWMask; // internal class.
00050 
00051 class vtkOpacityTables; // Pimpl
00052 class vtkDensifyPolyData;
00053 class vtkStdString;
00054 
00055 class vtkShaderProgram2;
00056 class vtkShader2;
00057 
00058 class VTK_VOLUMERENDERING_EXPORT vtkOpenGLGPUVolumeRayCastMapper : public vtkGPUVolumeRayCastMapper
00059 {
00060 public:
00061   static vtkOpenGLGPUVolumeRayCastMapper *New();
00062   vtkTypeMacro(vtkOpenGLGPUVolumeRayCastMapper,vtkGPUVolumeRayCastMapper);
00063   virtual void PrintSelf(ostream& os, vtkIndent indent);
00064 
00066 
00070   virtual int IsRenderSupported(vtkRenderWindow *window,
00071                                 vtkVolumeProperty *property);
00073 
00075   virtual void ReleaseGraphicsResources(vtkWindow *window);
00076 
00079   static const char *OpenGLErrorMessage(unsigned int errorCode);
00080 
00083   static void PrintError(const char *headerMessage);
00084 
00085 protected:
00086   vtkOpenGLGPUVolumeRayCastMapper();
00087   ~vtkOpenGLGPUVolumeRayCastMapper();
00088 
00089   // The render method called by the superclass
00090   virtual void GPURender(vtkRenderer *ren,
00091                          vtkVolume *vol);
00092 
00093   // Methods called by the AMR Volume Mapper.
00094   virtual void PreRender(vtkRenderer *ren,
00095                          vtkVolume *vol,
00096                          double datasetBounds[6],
00097                          double scalarRange[2],
00098                          int numberOfScalarComponents,
00099                          unsigned int numberOfLevels);
00100 
00101   // \pre input is up-to-date
00102   virtual void RenderBlock(vtkRenderer *ren,
00103                            vtkVolume *vol,
00104                            unsigned int level);
00105 
00106   virtual void PostRender(vtkRenderer *ren,
00107                           int numberOfScalarComponents);
00108 
00110 
00114   int TestRequiredExtension(vtkOpenGLExtensionManager *extensions,
00115                             const char *extensionName);
00117 
00123   void LoadExtensions(vtkRenderWindow *window);
00124 
00130   void CreateOpenGLObjects(vtkRenderer *ren);
00131 
00137   int AllocateFrameBuffers(vtkRenderer *ren);
00138 
00140 
00157   int LoadScalarField(vtkImageData *input,
00158                       vtkImageData *maskInput,
00159                       int textureExtent[6],
00160                       vtkVolume *volume);
00162 
00164 
00168   int UpdateColorTransferFunction(vtkVolume *vol,
00169                                   int numberOfScalarComponents);
00170   // Description:
00171   // Allocate memory and load opacity table on the GPU or
00172   // reload it if the transfer functions changed.
00173   // \pre vol_exists: vol!=0
00174   // \pre valid_numberOfScalarComponents: numberOfScalarComponents==1 || numberOfScalarComponents==4
00175   int UpdateOpacityTransferFunction(vtkVolume *vol,
00176                                     int numberOfScalarComponents,
00177                                     unsigned int level);
00179 
00182   void SetupRender(vtkRenderer *ren, vtkVolume *vol);
00183 
00185 
00187   void ClipBoundingBox(vtkRenderer *ren,
00188                        double worldBounds[6],
00189                        vtkVolume *vol);
00191 
00193 
00196   int RenderClippedBoundingBox(int tcoordFlag,
00197                                size_t currentBlock,
00198                                size_t numberOfBlocks,
00199                                vtkRenderWindow *renWin);
00201 
00204   void CopyFBOToTexture();
00205 
00207   void CleanupRender();
00208 
00210   void RenderTextureToScreen(vtkRenderer *ren);
00211 
00214   int PowerOfTwoGreaterOrEqual(int x);
00215 
00217   void CheckFrameBufferStatus();
00218 
00221   vtkStdString BufferToString(int buffer);
00222 
00224   void DisplayReadAndDrawBuffers();
00225 
00227   void DisplayFrameBufferAttachments();
00228 
00230   void DisplayFrameBufferAttachment(unsigned int uattachment);
00231 
00233 
00239   void BuildProgram(vtkRenderWindow *w,
00240                     int parallelProjection,
00241                     int raycastMethod,
00242                     int shadeMethod,
00243                     int componentMethod);
00245 
00247   void GetLightingStatus();
00248 
00258   void ComputeReductionFactor(double allocatedTime);
00259 
00261 
00262   void RenderWholeVolume(vtkRenderer *ren,
00263                          vtkVolume *vol);
00265 
00267 
00268   void RenderRegions(vtkRenderer *ren,
00269                      vtkVolume *vol);
00271 
00272   // Return abort status (true==abort)
00273   int RenderSubVolume(vtkRenderer *ren,
00274                       double bounds[6],
00275                       vtkVolume *vol);
00276 
00277   void LoadProjectionParameters(vtkRenderer *ren,
00278                                 vtkVolume *vol);
00279 
00281   void ComputeNumberOfCroppingRegions();
00282 
00283   void GetTextureFormat(vtkImageData *input,
00284                         unsigned int *internalFormat,
00285                         unsigned int *format,
00286                         unsigned int *type,
00287                         int *componentSize);
00288 
00289   bool TestLoadingScalar(unsigned int internalFormat,
00290                          unsigned int format,
00291                          unsigned int type,
00292                          int textureSize[3],
00293                          int componentSize);
00294 
00295   void SlabsFromDatasetToIndex(double slabsDataSet[6],
00296                                double slabsPoints[6]);
00297 
00298   void SlabsFromIndexToDataset(double slabsPoints[6],
00299                                double slabsDataSet[6]);
00300 
00301   const char *GetEnabledString(unsigned char value);
00302   void GetOpenGLState();
00303 
00304   void DebugDisplayBox(vtkPolyData *box);
00305 
00306   void UpdateNoiseTexture();
00307 
00309 
00313   double ComputeMinimalSampleDistancePerPixel(vtkRenderer *renderer,
00314                                               vtkVolume *volume);
00316 
00325   virtual void GetReductionRatio(double ratio[3]);
00326 
00327   int NumberOfCroppingRegions;
00328 
00329   // World coordinates of each corner of the dataset.
00330   double BoundingBox[8][3];
00331 
00332   // Used during the clipping process.
00333   vtkPolyData *PolyDataBoundingBox;
00334   vtkPlaneCollection *Planes;
00335   vtkPlane *NearPlane;
00336 
00337   vtkClipConvexPolyData *Clip;
00338   vtkMatrix4x4 *InvVolumeMatrix;
00339 
00340   vtkDensifyPolyData *Densify;
00341 
00342   int OpenGLObjectsCreated;
00343   int NumberOfFrameBuffers;
00344 
00345   unsigned int FrameBufferObject;
00346   unsigned int DepthRenderBufferObject;
00347 
00348   // 3D scalar texture +1D color+1D opacity+2D grabbed depth buffer
00349   // +1 2D colorbuffer.
00350   unsigned int TextureObjects[5];
00351   // used in MIP Mode (2 needed for ping-pong technique)
00352   unsigned int MaxValueFrameBuffer;
00353   unsigned int MaxValueFrameBuffer2;
00354   int ReducedSize[2];
00355 
00356   vtkPolyData *ClippedBoundingBox;
00357 
00358   int LastSize[2];
00359 
00360   double ReductionFactor;
00361 
00362   // Supported extensions
00363   // List of unsupported required extensions. Pimpl.
00364   vtkUnsupportedRequiredExtensionsStringStream *UnsupportedRequiredExtensions;
00365   int LoadExtensionsSucceeded;
00366 
00367   int Supports_GL_ARB_texture_float;
00368   int SupportsPixelBufferObjects;
00369 
00370   vtkTimeStamp DataBufferTime;
00371 
00372   // Matrices used in internal computation. As a member variable,
00373   // only one memory allocation is performed.
00374   vtkMatrix4x4 *TempMatrix[3];
00375 
00376   double TableRange[2];
00377 
00378   // Final string to send to the GPU as the fragment program source code.
00379 //  char *FragmentCode;
00380 //  int FragmentCodeCapacity;
00381 
00382   int ErrorLine;
00383   int ErrorColumn;
00384   char *ErrorString;
00385 
00386   // Store the last projection an raycast method in order to not rebuild
00387   // the fragment code at every call.
00388   int LastParallelProjection;
00389   int LastRayCastMethod;
00390   int LastCroppingMode;
00391   int LastComponent;
00392   int LastShade;
00393 
00394   vtkImageData *SmallInput;
00395   vtkTimeStamp SmallInputBuildTime;
00396 
00399   void BuildScaleBiasProgram(vtkRenderWindow *w);
00400 
00401 #if 0
00402   vtkIdType LoadedExtent[6];
00403   double LoadedBounds[6];
00404   vtkTimeStamp LoadedScalarTime;
00405   int LoadedCellFlag; // point data or cell data (or field data, not handled) ?
00406 #endif
00407 
00408   unsigned int SavedFrameBuffer; // some offscreen mode use a framebuffer too.
00409 
00410   vtkTessellatedBoxSource *BoxSource;
00411 
00412   float *NoiseTexture;
00413   int NoiseTextureSize; // size of one dimension.
00414   unsigned int NoiseTextureId; // GLuint
00415 
00416   bool IgnoreSampleDistancePerPixel;
00417 
00418   vtkMapDataArrayTextureId *ScalarsTextures; // need a list for AMR mode.
00419   vtkMapMaskTextureId *MaskTextures; // need a list for AMR mode.
00420 
00421   vtkRGBTable *RGBTable;
00422   vtkRGBTable *Mask1RGBTable;
00423   vtkRGBTable *Mask2RGBTable;
00424 
00425   vtkOpacityTables *OpacityTables;
00426 
00427   vtkKWScalarField *CurrentScalar;
00428   vtkKWMask *CurrentMask;
00429 
00430   float ActualSampleDistance;
00431 
00432   double LastProgressEventTime; // initial value is 0.0. Expressed in seconds.
00433 
00434   bool PreserveOrientation;
00435   
00436   vtkShaderProgram2 *Program;
00437   vtkShader2 *Main;
00438   vtkShader2 *Projection;
00439   vtkShader2 *Trace;
00440   vtkShader2 *CroppingShader;
00441   vtkShader2 *Component;
00442   vtkShader2 *Shade;
00443 
00444   // Internal Variable used to keep track of whether or render window's size
00445   // changed and therefore we need re-allocation.
00446   bool        SizeChanged;
00447 
00448   vtkShaderProgram2 *ScaleBiasProgram;
00449 
00450 private:
00451   vtkOpenGLGPUVolumeRayCastMapper(const vtkOpenGLGPUVolumeRayCastMapper&);  // Not implemented.
00452   void operator=(const vtkOpenGLGPUVolumeRayCastMapper&);  // Not implemented.
00453 };
00454 
00455 #endif