00001 /*========================================================================= 00002 00003 Program: Medical Imaging & Interaction Toolkit 00004 Language: C++ 00005 Date: $Date: 2009-07-14 19:11:20 +0200 (Tue, 14 Jul 2009) $ 00006 Version: $Revision: 18127 $ 00007 00008 Copyright (c) German Cancer Research Center, Division of Medical and 00009 Biological Informatics. All rights reserved. 00010 See MITKCopyright.txt or https://www.mitk.org/copyright.html for details. 00011 00012 This software is distributed WITHOUT ANY WARRANTY; without even 00013 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 00014 PURPOSE. See the above copyright notices for more information. 00015 00016 =========================================================================*/ 00017 00018 // .NAME vtkMitkVolumeTextureMapper3D - volume render with 3D texture mapping 00019 00020 // .SECTION Description 00021 // vtkMitkVolumeTextureMapper3D renders a volume using 3D texture mapping. 00022 // This class is actually an abstract superclass - with all the actual 00023 // work done by vtkMitkOpenGLVolumeTextureMapper3D. 00024 // 00025 // This mappers currently supports: 00026 // 00027 // - any data type as input 00028 // - one component, or two or four non-independent components 00029 // - composite blending 00030 // - intermixed opaque geometry 00031 // - multiple volumes can be rendered if they can 00032 // be sorted into back-to-front order (use the vtkFrustumCoverageCuller) 00033 // 00034 // This mapper does not support: 00035 // - more than one independent component 00036 // - maximum intensity projection 00037 // 00038 // Internally, this mapper will potentially change the resolution of the 00039 // input data. The data will be resampled to be a power of two in each 00040 // direction, and also no greater than 128*256*256 voxels (any aspect) 00041 // for one or two component data, or 128*128*256 voxels (any aspect) 00042 // for four component data. The limits are currently hardcoded after 00043 // a check using the GL_PROXY_TEXTURE3D because some graphics drivers 00044 // were always responding "yes" to the proxy call despite not being 00045 // able to allocate that much texture memory. 00046 // 00047 // Currently, calculations are computed using 8 bits per RGBA channel. 00048 // In the future this should be expanded to handle newer boards that 00049 // can support 15 bit float compositing. 00050 // 00051 // This mapper supports two main families of graphics hardware: 00052 // nvidia and ATI. There are two different implementations of 00053 // 3D texture mapping used - one based on nvidia's GL_NV_texture_shader2 00054 // and GL_NV_register_combiners2 extension, and one based on 00055 // ATI's GL_ATI_fragment_shader (supported also by some nvidia boards) 00056 // To use this class in an application that will run on various 00057 // hardware configurations, you should have a back-up volume rendering 00058 // method. You should create a vtkMitkVolumeTextureMapper3D, assign its 00059 // input, make sure you have a current OpenGL context (you've rendered 00060 // at least once), then call IsRenderSupported with a vtkVolumeProperty 00061 // as an argument. This method will return 0 if the input has more than 00062 // one independent component, or if the graphics hardware does not 00063 // support the set of required extensions for using at least one of 00064 // the two implemented methods (nvidia or ati) 00065 // 00066 // .SECTION Thanks 00067 // Thanks to Alexandre Gouaillard at the Megason Lab, Department of Systems 00068 // Biology, Harvard Medical School 00069 // https://wiki.med.harvard.edu/SysBio/Megason/ 00070 // for the idea and initial patch to speed-up rendering with compressed 00071 // textures. 00072 // 00073 // .SECTION see also 00074 // vtkVolumeMapper 00075 00076 #ifndef __vtkMitkVolumeTextureMapper3D_h 00077 #define __vtkMitkVolumeTextureMapper3D_h 00078 00079 #include "vtkVolumeMapper.h" 00080 #include "MitkExtExports.h" 00081 00082 class vtkImageData; 00083 class vtkColorTransferFunction; 00084 class vtkPiecewiseFunction; 00085 class vtkVolumeProperty; 00086 00087 #include "mitkCommon.h" 00088 00089 class MitkExt_EXPORT vtkMitkVolumeTextureMapper3D : public vtkVolumeMapper 00090 { 00091 public: 00092 vtkTypeRevisionMacro(vtkMitkVolumeTextureMapper3D,vtkVolumeMapper); 00093 void PrintSelf(ostream& os, vtkIndent indent); 00094 00095 static vtkMitkVolumeTextureMapper3D *New(); 00096 00097 // Description: 00098 // The distance at which to space sampling planes. This 00099 // may not be honored for interactive renders. An interactive 00100 // render is defined as one that has less than 1 second of 00101 // allocated render time. 00102 vtkSetMacro( SampleDistance, float ); 00103 vtkGetMacro( SampleDistance, float ); 00104 00105 // Description: 00106 // These are the dimensions of the 3D texture 00107 vtkGetVectorMacro( VolumeDimensions, int, 3 ); 00108 00109 // Description: 00110 // This is the spacing of the 3D texture 00111 vtkGetVectorMacro( VolumeSpacing, float, 3 ); 00112 00113 // Description: 00114 // Based on hardware and properties, we may or may not be able to 00115 // render using 3D texture mapping. This indicates if 3D texture 00116 // mapping is supported by the hardware, and if the other extensions 00117 // necessary to support the specific properties are available. 00118 virtual int IsRenderSupported( vtkRenderer *, vtkVolumeProperty *) =0; 00119 00120 // Description: 00121 // Allow access to the number of polygons used for the 00122 // rendering. 00123 vtkGetMacro( NumberOfPolygons, int ); 00124 00125 // Description: 00126 // Allow access to the actual sample distance used to render 00127 // the image. 00128 vtkGetMacro( ActualSampleDistance, float ); 00129 00130 //BTX 00131 00132 // Description: 00133 // WARNING: INTERNAL METHOD - NOT INTENDED FOR GENERAL USE 00134 // DO NOT USE THIS METHOD OUTSIDE OF THE RENDERING PROCESS 00135 // Render the volume 00136 virtual void Render(vtkRenderer *, vtkVolume *) {}; 00137 00138 00139 // Description: 00140 // Set/Get if the mapper use compressed textures (if supported by the 00141 // hardware). Initial value is false. 00142 // There are two reasons to use compressed textures: 1. rendering can be 4 00143 // times faster. 2. It saves some VRAM. 00144 // There is one reason to not use compressed textures: quality may be lower 00145 // than with uncompressed textures. 00146 vtkSetMacro(UseCompressedTexture,bool); 00147 vtkGetMacro(UseCompressedTexture,bool); 00148 00149 void UpdateMTime(); 00150 00151 protected: 00152 vtkMitkVolumeTextureMapper3D(); 00153 ~vtkMitkVolumeTextureMapper3D(); 00154 00155 float *PolygonBuffer; 00156 float *IntersectionBuffer; 00157 int NumberOfPolygons; 00158 int BufferSize; 00159 00160 /* 00161 unsigned char *Volume1; 00162 unsigned char *Volume2; 00163 unsigned char *Volume3; 00164 */ 00165 /* 00166 int VolumeSize; 00167 int VolumeComponents; 00168 */ 00169 00170 int VolumeDimensions[3]; 00171 float VolumeSpacing[3]; 00172 00173 float SampleDistance; 00174 float ActualSampleDistance; 00175 00176 vtkImageData *SavedTextureInput; 00177 vtkImageData *SavedParametersInput; 00178 00179 vtkColorTransferFunction *SavedRGBFunction; 00180 vtkPiecewiseFunction *SavedGrayFunction; 00181 vtkPiecewiseFunction *SavedScalarOpacityFunction; 00182 vtkPiecewiseFunction *SavedGradientOpacityFunction; 00183 int SavedColorChannels; 00184 float SavedSampleDistance; 00185 float SavedScalarOpacityDistance; 00186 00187 unsigned char ColorLookup[65536*4]; 00188 unsigned char AlphaLookup[65536]; 00189 float TempArray1[3*4096]; 00190 float TempArray2[4096]; 00191 int ColorTableSize; 00192 float ColorTableScale; 00193 float ColorTableOffset; 00194 00195 unsigned char DiffuseLookup[65536*4]; 00196 unsigned char SpecularLookup[65536*4]; 00197 00198 vtkTimeStamp SavedTextureMTime; 00199 vtkTimeStamp SavedParametersMTime; 00200 00201 bool UseCompressedTexture; 00202 00203 bool SupportsNonPowerOfTwoTextures; 00204 00205 // Description: 00206 // For the given viewing direction, compute the set of polygons. 00207 void ComputePolygons( vtkRenderer *ren, vtkVolume *vol, double bounds[6] ); 00208 00209 // Description: 00210 // Update the internal RGBA representation of the volume. Return 1 if 00211 // anything change, 0 if nothing changed. 00212 int UpdateColorLookup( vtkVolume * ); 00213 00214 // Description: 00215 // Impemented in subclass - check is texture size is OK. 00216 //BTX 00217 virtual int IsTextureSizeSupported(int vtkNotUsed(size)[3], 00218 int vtkNotUsed(components)) 00219 { 00220 return 0; 00221 } 00222 //ETX 00223 00224 private: 00225 vtkMitkVolumeTextureMapper3D(const vtkMitkVolumeTextureMapper3D&); // Not implemented. 00226 void operator=(const vtkMitkVolumeTextureMapper3D&); // Not implemented. 00227 }; 00228 00229 00230 #endif 00231 00232 00233 00234 00235 00236