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 =========================================================================*/
00033 #ifndef __vtkOpenGLGPUVolumeRayCastMapper_h
00034 #define __vtkOpenGLGPUVolumeRayCastMapper_h
00035 
00036 #include "vtkGPUVolumeRayCastMapper.h"
00037 
00038 class vtkVolume;
00039 class vtkRenderer;
00040 class vtkOpenGLExtensionManager;
00041 class vtkMatrix4x4;
00042 class vtkUnsupportedRequiredExtensionsStringStream; // Pimpl
00043 class vtkMapDataArrayTextureId; // Pimpl
00044 class vtkMapMaskTextureId; // Pimpl
00045 class vtkPolyData;
00046 class vtkClipConvexPolyData;
00047 class vtkClipPolyData;
00048 class vtkTessellatedBoxSource;
00049 
00050 class vtkOpacityTable; // internal class.
00051 class vtkRGBTable; // internal class.
00052 class vtkKWScalarField; // internal class.
00053 class vtkKWMask; // internal class.
00054 
00055 class vtkOpacityTables; // Pimpl
00056 class vtkDensifyPolyData;
00057 class vtkStdString;
00058 
00059 class vtkShaderProgram2;
00060 class vtkShader2;
00061 
00062 class VTK_VOLUMERENDERING_EXPORT vtkOpenGLGPUVolumeRayCastMapper : public vtkGPUVolumeRayCastMapper
00063 {
00064 public:
00065   static vtkOpenGLGPUVolumeRayCastMapper *New();
00066   vtkTypeMacro(vtkOpenGLGPUVolumeRayCastMapper,vtkGPUVolumeRayCastMapper);
00067   virtual void PrintSelf(ostream& os, vtkIndent indent);
00068 
00070 
00074   virtual int IsRenderSupported(vtkRenderWindow *window,
00075                                 vtkVolumeProperty *property);
00077 
00079   virtual void ReleaseGraphicsResources(vtkWindow *window);
00080 
00083   static const char *OpenGLErrorMessage(unsigned int errorCode);
00084 
00087   static void PrintError(const char *headerMessage);
00088 
00089 protected:
00090   vtkOpenGLGPUVolumeRayCastMapper();
00091   ~vtkOpenGLGPUVolumeRayCastMapper();
00092 
00093   // The render method called by the superclass
00094   virtual void GPURender(vtkRenderer *ren,
00095                          vtkVolume *vol);
00096 
00097   // Methods called by the AMR Volume Mapper.
00098   virtual void PreRender(vtkRenderer *ren,
00099                          vtkVolume *vol,
00100                          double datasetBounds[6],
00101                          double scalarRange[2],
00102                          int numberOfScalarComponents,
00103                          unsigned int numberOfLevels);
00104 
00105   // \pre input is up-to-date
00106   virtual void RenderBlock(vtkRenderer *ren,
00107                            vtkVolume *vol,
00108                            unsigned int level);
00109 
00110   virtual void PostRender(vtkRenderer *ren,
00111                           int numberOfScalarComponents);
00112 
00114 
00118   int TestRequiredExtension(vtkOpenGLExtensionManager *extensions,
00119                             const char *extensionName);
00121 
00127   void LoadExtensions(vtkRenderWindow *window);
00128 
00134   void CreateOpenGLObjects(vtkRenderer *ren);
00135 
00141   int AllocateFrameBuffers(vtkRenderer *ren);
00142 
00144 
00161   int LoadScalarField(vtkImageData *input,
00162                       vtkImageData *maskInput,
00163                       int textureExtent[6],
00164                       vtkVolume *volume);
00166 
00168 
00172   int UpdateColorTransferFunction(vtkVolume *vol,
00173                                   int numberOfScalarComponents);
00175 
00176 
00180   int UpdateOpacityTransferFunction(vtkVolume *vol,
00181                                     int numberOfScalarComponents,
00182                                     unsigned int level);
00184 
00187   void SetupRender(vtkRenderer *ren, vtkVolume *vol);
00188 
00190 
00192   void ClipBoundingBox(vtkRenderer *ren,
00193                        double worldBounds[6],
00194                        vtkVolume *vol);
00196 
00198 
00201   int RenderClippedBoundingBox(int tcoordFlag,
00202                                size_t currentBlock,
00203                                size_t numberOfBlocks,
00204                                vtkRenderWindow *renWin);
00206 
00209   void CopyFBOToTexture();
00210 
00212   void CleanupRender();
00213 
00215   void RenderTextureToScreen(vtkRenderer *ren);
00216 
00219   int PowerOfTwoGreaterOrEqual(int x);
00220 
00222   void CheckFrameBufferStatus();
00223 
00226   vtkStdString BufferToString(int buffer);
00227 
00229   void DisplayReadAndDrawBuffers();
00230 
00232   void DisplayFrameBufferAttachments();
00233 
00235   void DisplayFrameBufferAttachment(unsigned int uattachment);
00236 
00238 
00244   void BuildProgram(vtkRenderWindow *w,
00245                     int parallelProjection,
00246                     int raycastMethod,
00247                     int shadeMethod,
00248                     int componentMethod);
00250 
00252   void GetLightingStatus();
00253 
00263   void ComputeReductionFactor(double allocatedTime);
00264 
00266 
00267   void RenderWholeVolume(vtkRenderer *ren,
00268                          vtkVolume *vol);
00270 
00272 
00273   void RenderRegions(vtkRenderer *ren,
00274                      vtkVolume *vol);
00276 
00277   // Return abort status (true==abort)
00278   int RenderSubVolume(vtkRenderer *ren,
00279                       double bounds[6],
00280                       vtkVolume *vol);
00281 
00282   void LoadProjectionParameters(vtkRenderer *ren,
00283                                 vtkVolume *vol);
00284 
00286   void ComputeNumberOfCroppingRegions();
00287 
00288   void GetTextureFormat(vtkImageData *input,
00289                         unsigned int *internalFormat,
00290                         unsigned int *format,
00291                         unsigned int *type,
00292                         int *componentSize);
00293 
00294   bool TestLoadingScalar(unsigned int internalFormat,
00295                          unsigned int format,
00296                          unsigned int type,
00297                          int textureSize[3],
00298                          int componentSize);
00299 
00300   void SlabsFromDatasetToIndex(double slabsDataSet[6],
00301                                double slabsPoints[6]);
00302 
00303   void SlabsFromIndexToDataset(double slabsPoints[6],
00304                                double slabsDataSet[6]);
00305 
00306   const char *GetEnabledString(unsigned char value);
00307   void GetOpenGLState();
00308 
00309   void DebugDisplayBox(vtkPolyData *box);
00310 
00311   void UpdateNoiseTexture();
00312 
00314 
00318   double ComputeMinimalSampleDistancePerPixel(vtkRenderer *renderer,
00319                                               vtkVolume *volume);
00321 
00330   virtual void GetReductionRatio(double ratio[3]);
00331 
00332   int NumberOfCroppingRegions;
00333 
00334   // World coordinates of each corner of the dataset.
00335   double BoundingBox[8][3];
00336 
00337   // Used during the clipping process.
00338   vtkPolyData *PolyDataBoundingBox;
00339   vtkPlaneCollection *Planes;
00340   vtkPlane *NearPlane;
00341 
00342   vtkClipConvexPolyData *Clip;
00343   vtkMatrix4x4 *InvVolumeMatrix;
00344 
00345   vtkDensifyPolyData *Densify;
00346 
00347   int OpenGLObjectsCreated;
00348   int NumberOfFrameBuffers;
00349 
00350   unsigned int FrameBufferObject;
00351   unsigned int DepthRenderBufferObject;
00352 
00353   // 3D scalar texture +1D color+1D opacity+2D grabbed depth buffer
00354   // +1 2D colorbuffer.
00355   unsigned int TextureObjects[5];
00356   // used in MIP Mode (2 needed for ping-pong technique)
00357   unsigned int MaxValueFrameBuffer;
00358   unsigned int MaxValueFrameBuffer2;
00359   int ReducedSize[2];
00360 
00361   vtkPolyData *ClippedBoundingBox;
00362 
00363   int LastSize[2];
00364 
00365   double ReductionFactor;
00366 
00367   // Supported extensions
00368   // List of unsupported required extensions. Pimpl.
00369   vtkUnsupportedRequiredExtensionsStringStream *UnsupportedRequiredExtensions;
00370   int LoadExtensionsSucceeded;
00371 
00372   int Supports_GL_ARB_texture_float;
00373   int SupportsPixelBufferObjects;
00374 
00375   vtkTimeStamp DataBufferTime;
00376 
00377   // Matrices used in internal computation. As a member variable,
00378   // only one memory allocation is performed.
00379   vtkMatrix4x4 *TempMatrix[3];
00380 
00381   double TableRange[2];
00382 
00383   // Final string to send to the GPU as the fragment program source code.
00384 //  char *FragmentCode;
00385 //  int FragmentCodeCapacity;
00386 
00387   int ErrorLine;
00388   int ErrorColumn;
00389   char *ErrorString;
00390 
00391   // Store the last projection an raycast method in order to not rebuild
00392   // the fragment code at every call.
00393   int LastParallelProjection;
00394   int LastRayCastMethod;
00395   int LastCroppingMode;
00396   int LastComponent;
00397   int LastShade;
00398 
00399   vtkImageData *SmallInput;
00400   vtkTimeStamp SmallInputBuildTime;
00401 
00404   void BuildScaleBiasProgram(vtkRenderWindow *w);
00405 
00406 #if 0
00407   vtkIdType LoadedExtent[6];
00408   double LoadedBounds[6];
00409   vtkTimeStamp LoadedScalarTime;
00410   int LoadedCellFlag; // point data or cell data (or field data, not handled) ?
00411 #endif
00412 
00413   unsigned int SavedFrameBuffer; // some offscreen mode use a framebuffer too.
00414 
00415   vtkTessellatedBoxSource *BoxSource;
00416 
00417   float *NoiseTexture;
00418   int NoiseTextureSize; // size of one dimension.
00419   unsigned int NoiseTextureId; // GLuint
00420 
00421   bool IgnoreSampleDistancePerPixel;
00422 
00423   vtkMapDataArrayTextureId *ScalarsTextures; // need a list for AMR mode.
00424   vtkMapMaskTextureId *MaskTextures; // need a list for AMR mode.
00425 
00426   vtkRGBTable *RGBTable;
00427   vtkRGBTable *Mask1RGBTable;
00428   vtkRGBTable *Mask2RGBTable;
00429 
00430   vtkOpacityTables *OpacityTables;
00431 
00432   vtkKWScalarField *CurrentScalar;
00433   vtkKWMask *CurrentMask;
00434 
00435   float ActualSampleDistance;
00436 
00437   double LastProgressEventTime; // initial value is 0.0. Expressed in seconds.
00438 
00439   bool PreserveOrientation;
00440   
00441   vtkShaderProgram2 *Program;
00442   vtkShader2 *Main;
00443   vtkShader2 *Projection;
00444   vtkShader2 *Trace;
00445   vtkShader2 *CroppingShader;
00446   vtkShader2 *Component;
00447   vtkShader2 *Shade;
00448 
00449   // Internal Variable used to keep track of whether or render window's size
00450   // changed and therefore we need re-allocation.
00451   bool        SizeChanged;
00452 
00453   vtkShaderProgram2 *ScaleBiasProgram;
00454 
00455 private:
00456   vtkOpenGLGPUVolumeRayCastMapper(const vtkOpenGLGPUVolumeRayCastMapper&);  // Not implemented.
00457   void operator=(const vtkOpenGLGPUVolumeRayCastMapper&);  // Not implemented.
00458 };
00459 
00460 #endif