TAPs 0.7.7.3
TAPsOpenGLSupport.cpp
Go to the documentation of this file.
00001 /******************************************************************************
00002 TAPsOpenGLSupport.cpp
00003 
00004 Design as a base class for OpenGL Drawing and Properties of inherited class.
00005 
00006 SUKITTI PUNAK   (09/16/2004)
00007 UPDATE          (10/27/2004)
00008 ******************************************************************************/
00009 #include "TAPsOpenGLSupport.hpp"
00010 // Using Inclusion Model (i.e. definitions are included in declarations)
00011 //                       (this name.cpp is included in name.hpp)
00012 // Each friend is defined directly inside its declaration.
00013 
00014 BEGIN_NAMESPACE_TAPs__OpenGL
00015 //=============================================================================
00016 // Constructor and Destructor
00017 //-----------------------------------------------------------------------------
00018 // Constructor
00019 OpenGLSupport::OpenGLSupport ( BitmapImage *pcImg, TargaImage *pcImg2 )
00020     : m_pcImg( pcImg ), m_pcImg2( pcImg2 ), isFacetShading( false ),
00021       m_acImageFileName( NULL )
00022 {
00023     EnableTexture();
00024     //GenerateTexture();
00025 
00026     // Render by GLSL -----------------------------------------------
00027     #ifdef  TAPs_RENDER_BY_GLSL_DS_FACE_VERTEX
00028         GlobalGLSLShaderPool::Init();
00029         ShaderName = GlobalGLSLShaderPool::DEFAULT;
00030         //ShaderName = GlobalGLSLShaderPool::BRICK;
00031         //ShaderName = GlobalGLSLShaderPool::DEFAULT_MESH_2DTEXTURE;
00032         // Set of parameters for shader program
00033         // ...
00034     #endif//TAPs_RENDER_BY_GLSL_DS_FACE_VERTEX
00035     //---------------------------------------------------------------
00036 }
00037 //-----------------------------------------------------------------------------
00038 // Constructor
00039 OpenGLSupport::OpenGLSupport ( TargaImage *pcImg )
00040     : m_pcImg( NULL ), m_pcImg2( pcImg ), isFacetShading( false ),
00041       m_acImageFileName( NULL )
00042 {
00043     EnableTexture();
00044     //GenerateTexture();
00045 
00046     // Render by GLSL -----------------------------------------------
00047     #ifdef  TAPs_RENDER_BY_GLSL_DS_FACE_VERTEX
00048         GlobalGLSLShaderPool::Init();
00049         ShaderName = GlobalGLSLShaderPool::DEFAULT;
00050         //ShaderName = GlobalGLSLShaderPool::BRICK;
00051         //ShaderName = GlobalGLSLShaderPool::DEFAULT_MESH_2DTEXTURE;
00052         // Set of parameters for shader program
00053         // ...
00054     #endif//TAPs_RENDER_BY_GLSL_DS_FACE_VERTEX
00055     //---------------------------------------------------------------
00056 }
00057 //-----------------------------------------------------------------------------
00058 // Destructor
00059 OpenGLSupport::~OpenGLSupport()
00060 {
00061     if ( m_acImageFileName ) {
00062         delete [] m_acImageFileName;
00063         m_acImageFileName = NULL;
00064     }
00065     if ( m_pcImg ) {
00066         delete m_pcImg;
00067         m_pcImg  = NULL;
00068     }
00069     if ( m_pcImg2 ) {
00070         delete m_pcImg2;
00071         m_pcImg2 = NULL;
00072     }
00073 
00074 #ifdef  TAPs_SUPPORT_ASE_FORMAT
00075     std::vector< Material * >::iterator it = submaterials.begin();
00076     while ( it != submaterials.end() ) {
00077         delete *it;
00078         *it = NULL;
00079         ++it;
00080     }
00081 #endif//TAPs_SUPPORT_ASE_FORMAT
00082 }
00083 //=============================================================================
00084 //-----------------------------------------------------------------------------
00085 // To make the material affects the drawing, 
00086 // this fn has to be called before drawing.
00087 //void OpenGLSupport::ApplyMaterial( Enum::Face f )
00088 //{ material.ApplyMaterial( f );    }
00089 //-----------------------------------------------------------------------------
00090 // This fn set the material property to the material parameter,
00091 // but does not apply the material.
00092 //void OpenGLSupport::SetMaterial ( Enum::MaterialType mt )
00093 //{ material.SetMaterial( mt ); }
00094 //=============================================================================
00095 // Helper Functions
00096 //-----------------------------------------------------------------------------
00097 void OpenGLSupport::EnableTexture( GLuint & texName )
00098 {
00099     if ( !m_pcImg ) return;
00100     if ( m_pcImg->GetConstPtrToImageData() != NULL ) {
00101         glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
00102         //glGenTextures(1, texName);
00103         glBindTexture(GL_TEXTURE_2D, texName);
00104         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
00105         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
00106         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
00107         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
00108         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, m_pcImg->GetWidth(),
00109                     m_pcImg->GetHeight(), 0, GL_RGB, GL_UNSIGNED_BYTE,
00110                     m_pcImg->GetConstPtrToImageData() );
00111         //glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE );
00112         glEnable( GL_TEXTURE_2D );
00113     }
00114 }
00115 //-----------------------------------------------------------------------------
00116 /*
00117 void OpenGLSupport::GenerateTexture()
00118 {
00120     if ( !m_pcImg ) return;
00121     if ( m_pcImg->GetConstPtrToImageData() != NULL ) {
00122         //GLuint texName[1];
00123         glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
00124         glGenTextures(1, texName);
00125         glBindTexture(GL_TEXTURE_2D, texName[0]);
00126         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
00127         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
00128         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
00129         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
00130         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, m_pcImg->GetWidth(),
00131                     m_pcImg->GetHeight(), 0, GL_RGB, GL_UNSIGNED_BYTE,
00132                     m_pcImg->GetConstPtrToImageData() );
00133         //glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE );
00134         glEnable( GL_TEXTURE_2D );
00135     }
00136 }
00137 */
00138 //-----------------------------------------------------------------------------
00139 void OpenGLSupport::EnableTexture()
00140 {
00142     if ( !m_pcImg ) return;
00143     if ( m_pcImg->GetConstPtrToImageData() != NULL ) {
00144         glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
00145         glGenTextures(1, texName);
00146         glBindTexture(GL_TEXTURE_2D, texName[0]);
00147         //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
00148         //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
00149         //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
00150         //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
00151         //glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, m_pcImg->GetWidth(),
00152         //          m_pcImg->GetHeight(), 0, GL_RGB, GL_UNSIGNED_BYTE,
00153         //          m_pcImg->GetConstPtrToImageData() );
00154         //glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE );
00155         glEnable( GL_TEXTURE_2D );
00156     }
00157     //*/
00158     /*
00159     if ( m_pcImg2->GetConstPtrToImageData() != NULL ) {
00160         GLuint texName[1];
00161         glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
00162         glGenTextures(1, texName);
00163         glBindTexture(GL_TEXTURE_2D, texName[0]);
00164         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
00165         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
00166         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
00167         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
00168         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, m_pcImg2->GetWidth(),
00169                     m_pcImg2->GetHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE,
00170                     m_pcImg2->GetConstPtrToImageData() );
00171         //glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE );
00172         glEnable( GL_TEXTURE_2D );
00173     }
00174     */
00175 }
00176 //-----------------------------------------------------------------------------
00177 // This fn must be called inside the DisplayGL()
00178 void OpenGLSupport::DrawTexture()
00179 {
00180     if ( !m_pcImg ) return;
00181     if ( m_pcImg->GetConstPtrToImageData() != NULL ) {
00182         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
00183         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
00184         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
00185         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
00186         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, m_pcImg->GetWidth(),
00187                     m_pcImg->GetHeight(), 0, GL_RGB, GL_UNSIGNED_BYTE,
00188                     m_pcImg->GetConstPtrToImageData() );
00189     }
00190 }
00191 //-----------------------------------------------------------------------------
00192 // Draw this 2-D image at a raster position
00193 void OpenGLSupport::DrawImage1 ( Real x, Real y, Real z )
00194 {
00195     if ( m_pcImg->Available() ) {
00196         glRasterPos3i( static_cast<int>(x), static_cast<int>(y), static_cast<int>(z) );
00197         glDrawPixels( m_pcImg->GetWidth(), m_pcImg->GetHeight(), GL_RGB, 
00198                     GL_UNSIGNED_BYTE, m_pcImg->GetConstPtrToImageData() );
00199     }
00200 }
00201 //-----------------------------------------------------------------------------
00202 // Draw this 2-D image at a raster position
00203 void OpenGLSupport::DrawImage2 ( Real x, Real y, Real z )
00204 {
00205     if ( m_pcImg2->Available() ) {
00206         glRasterPos3i( static_cast<int>(x), static_cast<int>(y), static_cast<int>(z) );
00207         glDrawPixels( m_pcImg2->GetWidth(), m_pcImg2->GetHeight(), GL_BGRA_EXT, 
00208                     GL_UNSIGNED_BYTE, m_pcImg2->GetConstPtrToImageData() );
00209     }
00210 }
00211 //-----------------------------------------------------------------------------
00212 //=============================================================================
00213 // Get/Set Fn(s)
00214 //-----------------------------------------------------------------------------
00215 // Get Material Pointer
00216 //Material * const OpenGLSupport::GetMaterial ()
00217 //{
00218 //  return &material;
00219 //}
00220 //-----------------------------------------------------------------------------
00221 // Get Image File Name
00222 char const * const OpenGLSupport::GetImageFileName () const
00223 {
00224     return m_acImageFileName;
00225 }
00226 //-----------------------------------------------------------------------------
00227 // Set Image File Name
00228 void OpenGLSupport::SetImageFileName ( char * fileName )
00229 {
00230     if ( m_acImageFileName ) {
00231         delete [] m_acImageFileName;
00232         m_acImageFileName = NULL;
00233     }
00234     if ( fileName ) {
00235         m_acImageFileName = new char[ strlen( fileName ) + 1 ];
00236         strcpy( m_acImageFileName, fileName );
00237     }
00238 }
00239 //-----------------------------------------------------------------------------
00240 // Set Texture
00241 void OpenGLSupport::SetTexture( BitmapImage *img )
00242 {
00243     m_pcImg = img;
00244     EnableTexture();
00245 }
00246 //-----------------------------------------------------------------------------
00247 //=============================================================================
00248 // Friend Fn(s)
00249 //-----------------------------------------------------------------------------
00250 // put it through ostream
00251 std::ostream &operator<<( std::ostream &output, const OpenGLSupport &o )
00252 {
00253     output  << "OpenGLSupport Class:"
00254             << "\n Material:" << o.material << "\n";
00255     return output;
00256 }
00257 //-----------------------------------------------------------------------------
00258 //=============================================================================
00259 END_NAMESPACE_TAPs__OpenGL
00260 //345678901234567890123456789012345678901234567890123456789012345678901234567890
00261 //--+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines