basic/geometry/IGS/IGS_Image.cpp

Go to the documentation of this file.
00001 #include <assert.h>
00002 #include "synchronization/guid.h"
00003 #include "IGS_Image.h"
00004 #include <stdio.h>
00005 
00006 #include "opengl/glos.h"
00007 #include <gl/gl.h>
00008 
00009 
00010 
00011 //=============================================================================
00012 // Constructor
00013 //
00014 // Description: constructs an empty image with no pixels
00015 //=============================================================================
00016 IGS_Image::IGS_Image():
00017         guid( GuidGenerator::GenerateNewGuid() ),
00018         semaphore( guid ),
00019         format( GL_LUMINANCE ),
00020         posX( 0 ),
00021         poxY( 0 ),
00022         sizeX( 0 ),
00023         sizeY( 0 ),
00024         type( GL_BYTE )
00025 {
00026 }
00027 
00028 //=============================================================================
00029 // Copy Constructor
00030 //
00031 // Description: copies the image object
00032 //=============================================================================
00033 IGS_Image::IGS_Image( const IGS_Image& right ):
00034         guid( GuidGenerator::GenerateNewGuid() ),               //these must not be copied
00035         semaphore( guid )
00036 {
00037 }
00038 
00039 //=============================================================================
00040 // Destructor
00041 //
00042 // Description: cleans up the object
00043 //=============================================================================
00044 IGS_Image::~IGS_Image()
00045 {
00046 }
00047 
00048 //=============================================================================
00049 // ComputeIndex
00050 //
00051 // Description: computes the pixel index for a given XY position
00052 //=============================================================================
00053 int IGS_Image::ComputeIndex( const int x, const int y ) const
00054 {
00055         assert( x < this->sizeX );
00056         assert( y < this->sizeY );
00057         assert( x >= 0 );
00058         assert( y >= 0 );
00059         return y * this->sizeX + x;
00060 }
00061 
00062 //=============================================================================
00063 // ComputeIndex
00064 //
00065 // Description: computes the pixel index for a given XY position
00066 //=============================================================================
00067 int IGS_Image::ComputeMaxIndex() const
00068 {
00069         return this->sizeX * this->sizeY;
00070 }
00071 
00072 //=============================================================================
00073 // Draw
00074 //
00075 // Description: draws the image to the screen
00076 //=============================================================================
00077 void IGS_Image::Draw() const
00078 {
00079 /*      int i;
00080         int width = this->sizeX;
00081         int height = this->sizeY;
00082         for( i = 0; i < width * height; i++ )
00083         {
00084                 char* pixel= ( char* )pixels + i;
00085                 if( *pixel != 0x00 )
00086                 {
00087                         int i = 0;
00088                 }
00089         }
00090 
00091         GLenum errorcode;
00092         errorcode = glGetError();
00093 
00094         ::glBindTexture( GL_TEXTURE_2D, 1 );
00095         errorcode = glGetError();
00096         ::glPixelStorei (GL_UNPACK_ALIGNMENT, 1); 
00097         errorcode = glGetError();
00098         glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
00099         errorcode = glGetError();
00100         glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
00101         errorcode = glGetError();
00102         glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
00103         errorcode = glGetError();
00104         glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
00105         errorcode = glGetError();
00106         glTexEnvf (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL); 
00107         errorcode = glGetError();
00108         glTexImage2D 
00109         (
00110                 GL_TEXTURE_2D, 
00111                 0, 
00112                 GL_RGB, 
00113 //              this->sizeX, 
00114 //              this->sizeY, 
00115                 256,                            //width
00116                 256,                            //height
00117                 0, 
00118                 GL_GREEN, 
00119                 GL_BYTE, 
00120                 this->pixels
00121         );
00122         errorcode = glGetError();
00123 
00124         glColor3f( 1.0, 1.0, 1.0 );
00125         glBegin( GL_POLYGON );
00126                 glTexCoord2f (0.0, 0.0);
00127                 glVertex2f( 0, 0 );
00128                 glTexCoord2f (1.0, 0.0);
00129                 glVertex2f( 2, 0 );
00130                 glTexCoord2f (1.0, 1.0);
00131                 glVertex2f( 2, -2 );
00132                 glTexCoord2f (0.0, 1.0);
00133                 glVertex2f( 0, -2 );
00134         glEnd();
00135 */
00136 
00137         if( ( this->sizeX == 0 ) || ( this->sizeY == 0 ) )
00138         {
00139                 return;
00140         }
00141 
00142         GLenum errorcode;
00143         errorcode = glGetError();
00144 
00145         int depth = 0;
00146 
00147         ::glDepthFunc( GL_ALWAYS );
00148 
00149         ::glMatrixMode( GL_PROJECTION );
00150         errorcode = ::glGetError();
00151         assert( errorcode == GL_NO_ERROR );
00152 
00153         ::glPushMatrix();
00154         errorcode = ::glGetError();
00155         assert( errorcode == GL_NO_ERROR );
00156 
00157         ::glLoadIdentity();
00158         errorcode = ::glGetError();
00159         assert( errorcode == GL_NO_ERROR );
00160 
00161         ::glOrtho( 0, this->sizeX, 0, this->sizeY, 100, -100 );
00162         errorcode = ::glGetError();
00163         assert( errorcode == GL_NO_ERROR );
00164 
00165         ::glMatrixMode( GL_MODELVIEW );
00166         errorcode = ::glGetError();
00167         assert( errorcode == GL_NO_ERROR );
00168 
00169         ::glPushMatrix();
00170         errorcode = ::glGetError();
00171         assert( errorcode == GL_NO_ERROR );
00172 
00173         ::glLoadIdentity();
00174         errorcode = ::glGetError();
00175         assert( errorcode == GL_NO_ERROR );
00176 
00177         ::glRasterPos2d( static_cast< double >( this->posX ), static_cast< double >( this->poxY ) );
00178         errorcode = ::glGetError();
00179         assert( errorcode == GL_NO_ERROR );
00180 
00181         //::glDrawBuffer( GL_BACK_LEFT );
00182         //::glDrawBuffer( GL_BACK_RIGHT );
00183         //::glDrawBuffer( GL_FRONT );                   //ok
00184         errorcode = ::glGetError();
00185         assert( errorcode == GL_NO_ERROR );
00186         //GL_INVALID_ENUM                   0x0500
00187         //GL_INVALID_OPERATION              0x0502
00188 
00189         GLenum formatToUse = this->format;
00190         if( format == GL_DEPTH )
00191         {
00192                 formatToUse = GL_LUMINANCE;
00193         }
00194 
00195         this->semaphore.Lock();
00196         const void* const buffer = this->GetBuffer();
00197         if( buffer != NULL )
00198         {
00199                 ::glDrawPixels
00200                 ( 
00201                         this->sizeX, 
00202                         this->sizeY,
00203                         formatToUse,
00204                         this->type,
00205                         buffer
00206                 );
00207         }
00208         this->semaphore.Unlock();
00209         errorcode = glGetError();
00210         assert( errorcode == GL_NO_ERROR );
00211 
00212 
00213 
00214         ::glPopMatrix();
00215         errorcode = ::glGetError();
00216         assert( errorcode == GL_NO_ERROR );
00217 
00218         ::glMatrixMode( GL_PROJECTION );
00219         errorcode = ::glGetError();
00220         assert( errorcode == GL_NO_ERROR );
00221 
00222         ::glPopMatrix();
00223         errorcode = ::glGetError();
00224         assert( errorcode == GL_NO_ERROR );
00225 }
00226 
00227 //=============================================================================
00228 // GetSizeX
00229 //
00230 // Description: allows access to the image size
00231 //=============================================================================
00232 int IGS_Image::GetSizeX() const
00233 {
00234         return this->sizeX;
00235 }
00236 
00237 //=============================================================================
00238 // GetSizeY
00239 //
00240 // Description: allows access to the image size
00241 //=============================================================================
00242 int IGS_Image::GetSizeY() const
00243 {
00244         return this->sizeY;
00245 }
00246 
00247 //=============================================================================
00248 // GetSizeY
00249 //
00250 // Description: allows access to the image size
00251 //=============================================================================
00252 void IGS_Image::Invalidate()
00253 {
00254         void*& buffer = this->GetBuffer();
00255         if( buffer != NULL )
00256         {
00257                 delete[] buffer;
00258         }
00259 }
00260 
00261 //=============================================================================
00262 // ScreenCapture
00263 //
00264 // Description: does a screen capture of this type of image
00265 //=============================================================================
00266 void IGS_Image::ScreenCapture()
00267 {
00268         void* pixels = this->GetBuffer();
00269         if( pixels == NULL )
00270         {
00271                 this->Initialize();
00272                 pixels = this->GetBuffer();
00273                 assert( pixels != NULL );
00274         }
00275         GLenum errorcode;
00276         errorcode = glGetError();
00277 
00278         ::glReadBuffer( GL_BACK );
00279         errorcode = glGetError();
00280         assert( errorcode == GL_NO_ERROR );
00281 
00282         GLenum formatToUse = this->format;
00283         if( format == GL_DEPTH )
00284         {
00285                 formatToUse = GL_DEPTH_COMPONENT;
00286         }
00287 
00288         ::glReadPixels
00289         ( 
00290                 this->posX, this->poxY,                 //x,y
00291                 this->sizeX, this->sizeY,
00292                 formatToUse,
00293                 this->type,
00294                 pixels
00295         );
00296         errorcode = glGetError();
00297         if( errorcode != GL_NO_ERROR )
00298         {
00299                 assert( errorcode == GL_NO_ERROR );
00300                 //GL_BYTE                           0x1400
00301                 //GL_DEPTH                          0x1801
00302                 //GL_DEPTH_COMPONENT                0x1902
00303                 //GL_INVALID_ENUM                   0x0500
00304         }
00305 }
00306 
00307 //=============================================================================
00308 // SetFormat
00309 //
00310 // Description: sets the format of the image
00311 //=============================================================================
00312 void IGS_Image::SetFormat( const GLenum format )
00313 {
00314         this->format = format;
00315 }
00316 
00317 //=============================================================================
00318 // SetPosition
00319 //
00320 // Description: sets the position that this image will be drawn at
00321 //=============================================================================
00322 void IGS_Image::SetPosition( const int x, const int y )
00323 {
00324         this->posX = x;
00325         this->poxY = y;
00326 }
00327 
00328 //=============================================================================
00329 // SetType
00330 //
00331 // Description: sets the type of the image
00332 //=============================================================================
00333 void IGS_Image::SetResolution( const int xres, const int yres )
00334 {
00335         this->Invalidate();
00336         this->sizeX = xres;
00337         this->sizeY = yres;
00338 }
00339 
00340 //=============================================================================
00341 // SetType
00342 //
00343 // Description: sets the type of the image
00344 //=============================================================================
00345 void IGS_Image::SetType( const GLenum type )
00346 {
00347         this->type = type;
00348         this->Invalidate();
00349 }
00350 
00351 //=============================================================================
00352 // SizeOfType
00353 //
00354 // Description: determines the size of a particular type for memory allocation
00355 //=============================================================================
00356 int IGS_Image::SizeOfType( const GLenum type )
00357 {
00358         switch( type )
00359         {
00360         case GL_UNSIGNED_BYTE:
00361                 {
00362                         return sizeof( GLubyte );
00363                 }
00364         case GL_BYTE:
00365                 {
00366                         return sizeof( GLbyte );
00367                 }
00368         case GL_BITMAP:
00369                 {
00370                         assert( false );                //I don't know what to do with this
00371                         return 0;
00372                 }
00373         case GL_UNSIGNED_SHORT:
00374                 {
00375                         return sizeof( GLushort );
00376                 }
00377         case GL_SHORT:
00378                 {
00379                         return sizeof( GLshort );
00380                 }
00381         case GL_UNSIGNED_INT:
00382                 {
00383                         return sizeof( GLuint );
00384                 }
00385         case GL_INT:
00386                 {
00387                         return sizeof( GLint );
00388                 }
00389         case GL_FLOAT:
00390                 {
00391                         return sizeof( GLfloat );
00392                 }
00393         default:
00394                 {
00395                         printf( "IGS_Image::SizeOfType - invalid type\n" );
00396                         assert( false );
00397                         return 0;
00398                 }
00399         }
00400 }
00401 //=============================================================================
00402 // Valid
00403 //
00404 // Description: determines if the image is valid
00405 //=============================================================================
00406 bool IGS_Image::Valid() const
00407 {
00408         const void* buffer = this->GetBuffer();
00409         return ( buffer != NULL );
00410 }

Generated on Sat Apr 1 21:30:32 2006 for Motion Planning Kernel by  doxygen 1.4.6-NO