00001 #ifndef NOGL 00002 #include "opengl/glos.h" 00003 #include <gl/gl.h> 00004 #endif 00005 #include "IGS_Object.h" 00006 #include "math/vector2.h" 00007 #include "math/vector4.h" 00008 #include "debug/debug.h" 00009 00010 char IGS_Object::g_IdCode[] = "OBJE"; 00011 00012 //============================================================================= 00013 // Destructor 00014 // 00015 // Description: Destructor 00016 //============================================================================= 00017 IGS_Object::IGS_Object(): 00018 m_DisplayListNumber( -1 ), 00019 mPreparationComplete( false ), 00020 mUid( IGS_Object::GenerateUid() ) 00021 { 00022 } 00023 00024 //============================================================================= 00025 // Destructor 00026 // 00027 // Description: Destructor 00028 //============================================================================= 00029 IGS_Object::~IGS_Object() 00030 { 00031 } 00032 00033 //============================================================================= 00034 // BinaryFileSize 00035 // 00036 // Description: determines how much space is needed in a binary file for this 00037 // object 00038 //============================================================================= 00039 int IGS_Object::BinaryFileSize() const 00040 { 00041 return 8; //8 bytes is the size of the header 00042 } 00043 00044 //============================================================================= 00045 // CreateDisplayList 00046 // 00047 // Description: creates a new display list for this object 00048 //============================================================================= 00049 void IGS_Object::CreateDisplayList( const IGS_Controller& controller ) const 00050 { 00051 #ifndef NOGL 00052 if( m_DisplayListNumber != -1 ) 00053 { 00054 glDeleteLists( m_DisplayListNumber, 1 ); 00055 } 00056 00057 m_DisplayListNumber = glGenLists( 1 ); 00058 IJG_Assert( m_DisplayListNumber != 0 ); 00059 ::CheckGlErrorCode(); 00060 ::glNewList( m_DisplayListNumber, GL_COMPILE ); 00061 ::CheckGlErrorCode(); 00062 Render( controller ); 00063 glEndList(); 00064 #endif 00065 } 00066 00067 //============================================================================= 00068 // GenerateUid 00069 // 00070 // Description: Generates unique IDs for each object 00071 //============================================================================= 00072 int IGS_Object::GenerateUid() 00073 { 00074 static int idNumber = 0; 00075 idNumber++; 00076 return idNumber; //1,2,3,4... 00077 } 00078 00079 //============================================================================= 00080 // GetIdCode 00081 // 00082 // Description: Returns the ID code for this object 00083 //============================================================================= 00084 const char* IGS_Object::GetIdCode() const 00085 { 00086 return g_IdCode; 00087 } 00088 00089 //============================================================================= 00090 // GlDraw 00091 // 00092 // Description: Draws the object, but uses a display list 00093 //============================================================================= 00094 void IGS_Object::GlDraw( const IGS_Controller& controller ) const 00095 { 00096 #ifndef NOGL 00097 if( m_DisplayListNumber == -1 ) 00098 { 00099 CreateDisplayList( controller ); 00100 } 00101 glCallList( m_DisplayListNumber ); 00102 #endif 00103 } 00104 00105 //============================================================================= 00106 // IGS_Object::GlDraw 00107 // 00108 // Description: good for drawing points 00109 //============================================================================= 00110 void IGS_Object::GlDraw( const Vector2& point ) const 00111 { 00112 ::glVertex2d( point[ 0 ], point[ 1 ] ); 00113 } 00114 00115 //============================================================================= 00116 // IGS_Object::GlDraw 00117 // 00118 // Description: good for drawing points 00119 //============================================================================= 00120 void IGS_Object::GlDraw( const Vector4& point ) const 00121 { 00122 ::glVertex3d( point[ 0 ], point[ 1 ], point[ 2 ] ); 00123 } 00124 00125 //============================================================================= 00126 // Destructor 00127 // 00128 // Description: Destructor 00129 //============================================================================= 00130 bool IGS_Object::PreparationComplete() const 00131 { 00132 return this->mPreparationComplete; 00133 } 00134 00135 //============================================================================= 00136 // Destructor 00137 // 00138 // Description: Destructor 00139 //============================================================================= 00140 void IGS_Object::Prepare( const IGS_Controller& controller ) 00141 { 00142 this->mPreparationComplete = true; 00143 } 00144 00145 //============================================================================= 00146 // ReadBinary 00147 // 00148 // Description: reads a file in from a binary format 00149 //============================================================================= 00150 bool IGS_Object::ReadBinary( char* buffer ) 00151 { 00152 IJG_Assert( false ); 00153 return false; 00154 } 00155 00156 //============================================================================= 00157 // WriteBinary 00158 // 00159 // Description: writes the object out in binary format to a specific buffer 00160 //============================================================================= 00161 void IGS_Object::WriteBinary( char* buffer ) const 00162 { 00163 //write the size; 00164 *( reinterpret_cast< int* >( buffer ) )= 2; 00165 00166 const char* code = GetIdCode(); 00167 00168 //write the ID code 00169 buffer[ 4 ] = code[ 0 ]; 00170 buffer[ 5 ] = code[ 1 ]; 00171 buffer[ 6 ] = code[ 2 ]; 00172 buffer[ 7 ] = code[ 3 ]; 00173 00174 //advance in the buffer till after this object 00175 buffer += 8; 00176 } 00177 00178 //============================================================================= 00179 // CheckGlErrorCode 00180 // 00181 // Description: checks the error code from opengl 00182 //============================================================================= 00183 #ifdef _DEBUG 00184 void CheckGlErrorCode() 00185 { 00186 GLenum errorcode; 00187 errorcode = ::glGetError(); 00188 IJG_Assert( errorcode == GL_NO_ERROR ); 00189 } 00190 #endif