Main Page | Class Hierarchy | Class List | Directories | File List | Class Members

CTex.cpp

00001 #include <iostream>
00002 #include <stdio.h>
00003 #include <stdlib.h>
00004 #include <glut.h>
00005 #include <windows.h>
00006 #include <gl/glu.h>
00007 #include "lifeVariables.h"
00008 
00009 //
00010 // CTexture
00011 //
00012 void CTex::Init(unsigned short dimX, unsigned short dimY)
00013 {
00014         _dimX = dimX; 
00015         _dimY = dimY;
00016         _size = _dimX * _dimY * _cColourChannels; 
00017                 
00018         if ( _pTex && dimX )
00019         {
00020             delete[] _pTex;
00021             _pTex = 0;
00022         }
00023 
00024         _pTex = new GLfloat[_size];
00025         if ( !_pTex )
00026             throw;
00027 
00028         for ( int i = 0; i < _size; i++ )
00029         {
00030             _pTex[i] = 0;
00031         }
00032 }
00033 
00034 void CTex::GenTexture()
00035 {
00036         unsigned short  * pD     = GDensities.GetDensitiesPtr();
00037     unsigned short  uMaxValue= 0;
00038 
00039         GLfloat         * texPtr = 0; 
00040     int i,j;
00041 
00042     texPtr = _pTex;
00043 
00044    /*
00045     * For each of the densitiy values, create a corresponding RGBA value
00046         *
00047         */
00048 
00049 
00050     // uMaxValue = GDensities.GetMaxValue();
00051 
00052     uMaxValue = GVolVoxels.GetMaxValue();
00053 
00054     if (!uMaxValue )
00055         uMaxValue = 1;
00056 
00057    for( i=0;i< _dimY; i++)
00058    {
00059      for( j=0;j< _dimX; j++, texPtr += _cColourChannels)
00060      {
00061                 histogram->getColorByDensity(*pD, (*texPtr), 
00062                                                   (*(texPtr+1)),
00063                                                   (*(texPtr+2)),
00064                                                   (*(texPtr+3)));
00065                  pD++;
00066 
00067      }
00068    }
00069 }
00070 
00071 void CTex::DisplaySliceAlong_Z_Axis(unsigned iSliceNum)
00072 {
00073     unsigned uWidth, uHeight;
00074 
00075     uWidth  = GVolVoxels.GetDimX();
00076     uHeight = GVolVoxels.GetDimY();
00077 
00078     GDensities.Init(uWidth,uHeight);
00079 
00080         GDensities.InitDensities_XY_plain_by_Slice(iSliceNum);
00081     
00082     Init(uWidth, uHeight);
00083 
00084     GenTexture();
00085 }
00086 
00087 void CTex::DisplaySliceAlong_X_Axis(unsigned iSliceNum)
00088 { 
00089     unsigned uWidth, uHeight;
00090 
00091     uWidth  = GVolVoxels.GetDimY();
00092     uHeight = GVolVoxels.GetDimZ();
00093 
00094     GDensities.Init(uWidth,uHeight);
00095 
00096         GDensities.InitDensities_YZ_plain_by_Slice(iSliceNum);
00097     
00098     Init(uWidth, uHeight);
00099 
00100     GenTexture();
00101 
00102   }
00103 
00104 void CTex::DisplaySliceAlong_Y_Axis(unsigned iSliceNum)
00105 {
00106     unsigned uWidth, uHeight;
00107 
00108     uWidth  = GVolVoxels.GetDimX();
00109     uHeight = GVolVoxels.GetDimZ();
00110 
00111     GDensities.Init(uWidth,uHeight);
00112 
00113         GDensities.InitDensities_XZ_plain_by_Slice(iSliceNum);
00114     
00115     Init(uWidth, uHeight);
00116 
00117     GenTexture();
00118 
00119 }
00120 
00121 void CTex::GenerateBackgroundGeometry()
00122 {
00123     int x = 1;  // scale factor
00124 
00125     glBegin(GL_QUADS);
00126 
00127     glTexCoord2f(1.0, 0.0); glVertex3f(-1.0*x, -1.0*x, 0.0);
00128     glTexCoord2f(0.0, 0.0); glVertex3f( 1.0*x, -1.0*x, 0.0);
00129     glTexCoord2f(0.0, 1.0); glVertex3f( 1.0*x,  1.0*x, 0.0);
00130     glTexCoord2f(1.0, 1.0); glVertex3f(-1.0*x,  1.0*x, 0.0);
00131 
00132     glEnd();
00133 }
00134 
00135 void CTex::LetThereBeBackground(unsigned uWidth, unsigned uHeight)
00136 {
00137     GenerateBackgroundGeometry();
00138 
00139     /*
00140      * scale up to size that's power of two
00141      */
00142     if ( uWidth <= 64 )
00143         _uBGTexWidth = 64;
00144     else if ( uWidth <= 128 )
00145         _uBGTexWidth = 128;
00146     else if ( uWidth <= 256)
00147         _uBGTexWidth = 256;
00148     else if ( uWidth <= 512 )
00149         _uBGTexWidth = 512;
00150     else 
00151     {
00152 //        cout "Error:  uTexSize Not Supported  " << uTexWidth << endl;
00153     }
00154 
00155     if ( uHeight <= 64 )
00156         _uBGTexHeight = 64;
00157     else if ( uHeight <= 128 )
00158         _uBGTexHeight = 128;
00159     else if ( uHeight <= 256)
00160         _uBGTexHeight = 256;
00161     else if ( uHeight <= 512 )
00162         _uBGTexHeight = 512;
00163     else 
00164     {
00165   //      cout "Error:  uTexSize Not Supported  " << uTexHeight << endl;
00166     }
00167     
00168     unsigned uBGSize = _uBGTexWidth * _uBGTexHeight * _cColourChannels;
00169 
00170     if ( _pBGTex && _uBGTexWidth )
00171     {
00172         delete[] _pBGTex;
00173         _pBGTex = 0;
00174     }
00175 
00176     _pBGTex = new GLfloat[uBGSize];
00177     if ( !_pBGTex )
00178          throw;
00179 
00180     for ( int i = 0; i < uBGSize; i++ )
00181     {
00182             _pBGTex[i] = 10;
00183     }
00184  
00185     if ( _texName )
00186     {
00187        glDeleteTextures(1,&_texName);
00188        glGenTextures(1, &_texName);
00189        glBindTexture(GL_TEXTURE_2D, _texName);
00190     }
00191 
00192     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
00193     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
00194     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
00195     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
00196 
00197     glTexImage2D(GL_TEXTURE_2D,0,3, _uBGTexWidth, _uBGTexHeight,0,GL_RGBA,GL_FLOAT, _pBGTex);
00198 }
00199 
00200 void CTex::RenderSubTexture()
00201 {
00202     int iAlongAxis = optionPanel->selectedPlane;
00203     int iSliceNum = optionPanel->selectedSlice;
00204     int interpolationMode_2D = optionPanel->interpolationMode;
00205 
00206     unsigned TexWidth = 0;
00207     unsigned TexHeight= 0;
00208 
00209     if ( true ) 
00210     {
00211         switch (iAlongAxis)
00212         {
00213             case 0: if ( iSliceNum < GVolVoxels.GetDimZ() )
00214                     {
00215                         TexWidth = GVolVoxels.GetDimX();
00216                         TexHeight= GVolVoxels.GetDimY();
00217 
00218                         DisplaySliceAlong_Z_Axis(iSliceNum);
00219                     }
00220                     break;
00221 
00222             case 1: if ( iSliceNum < GVolVoxels.GetDimX() )
00223                     {
00224                         TexWidth = GVolVoxels.GetDimY();
00225                         TexHeight= GVolVoxels.GetDimZ();
00226 
00227                         DisplaySliceAlong_X_Axis(iSliceNum);
00228                     }
00229                     break;
00230 
00231             case 2: if ( iSliceNum < GVolVoxels.GetDimY() )
00232                     {
00233                         TexWidth = GVolVoxels.GetDimX();
00234                         TexHeight= GVolVoxels.GetDimZ();
00235 
00236                         DisplaySliceAlong_Y_Axis(iSliceNum);
00237                     }
00238                     break;
00239 
00240             default:
00241                     cout << "Slice Error: Unexpected Axis Selection" << iSliceNum << endl;
00242                     break;
00243         }
00244 
00245         //cout << "Info: Displaying slice num: " << iSliceNum << endl;
00246 
00247         LetThereBeBackground(TexWidth,TexHeight);
00248 
00249         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
00250         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
00251 
00252         //cout << "interpolatoin method  " << interpolationMode_2D << endl ;
00253         switch( interpolationMode_2D )
00254         {
00255         case 0:  
00256 
00257             glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
00258             glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
00259 
00260             break;
00261 
00262         case 1:
00263             glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
00264             glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
00265 
00266             break;
00267 
00268         default:
00269 
00270             break;
00271         }
00272 
00273     }
00274 
00275     glBindTexture(GL_TEXTURE_2D, _texName);
00276 
00277     glTexSubImage2D(GL_TEXTURE_2D,0, (_uBGTexWidth-TexWidth)/2 , (_uBGTexHeight-TexHeight)/2,  TexWidth, TexHeight, GL_RGBA, GL_FLOAT, _pTex);
00278 }
00279 
00280 void CTex::RenderVolumeSubTexture()
00281 {
00282     int iAlongAxis = optionPanel->selectedPlane;
00283     int iSliceNum = optionPanel->selectedSlice;
00284 
00285     unsigned TexWidth = GScanner.GetTexWidth();
00286     unsigned TexHeight= GScanner.GetTexHeight();
00287 
00288     LetThereBeBackground(TexWidth,TexHeight);
00289 
00290     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
00291     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
00292 
00293     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
00294     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
00295 
00296     glBindTexture(GL_TEXTURE_2D, _texName);
00297 
00298     glTexSubImage2D(GL_TEXTURE_2D,0, (_uBGTexWidth-TexWidth)/2 , (_uBGTexHeight-TexHeight)/2,  TexWidth, TexHeight, GL_RGBA, GL_FLOAT,  GScanner.GetView() );
00299 }

Generated on Mon Dec 12 15:20:26 2005 for CCube by  doxygen 1.4.1