TAPs 0.7.7.3
TAPsOpenGLCtrl.cpp
Go to the documentation of this file.
00001 /******************************************************************************
00002 TAPsOpenGLCtrl.hpp
00003 
00004 Abstact Class for Control Object via OpenGL
00005 
00006 SUKITTI PUNAK   (05/02/2007)
00007 UPDATE          (05/02/2007)
00008 ******************************************************************************/
00009 #include "TAPsOpenGLCtrl.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 int OpenGLCtrl::g_ID = 0;
00017 //
00018 float OpenGLCtrl::g_BorderColor[4] = { 0.5, 0.5, 0.5, 0.7 };
00019 //=============================================================================
00020 // Constructor(s) and Destructor
00021 //-----------------------------------------------------------------------------
00022 OpenGLCtrl::OpenGLCtrl ()
00023 {
00024     ++g_ID;
00025     Default();
00026 }
00027 //-----------------------------------------------------------------------------
00028 OpenGLCtrl::~OpenGLCtrl ()
00029 {}
00030 //-----------------------------------------------------------------------------
00031 //=============================================================================
00032 
00033 //=============================================================================
00034 // Get/Set Function(s)
00035 //-----------------------------------------------------------------------------
00036 void OpenGLCtrl::Default ()
00037 {
00038     SetStartPos( 0, 0 );
00039     SetSize( 20, 20 );
00040     SetText( "Ctrl" );
00041     //
00042     m_BorderSize = 2;
00043     //
00044     m_IsOn = false;
00045     //
00046     m_ForegroundColor[0] = 1.0;
00047     m_ForegroundColor[1] = 1.0;
00048     m_ForegroundColor[2] = 1.0;
00049     m_ForegroundColor[3] = 0.70;
00050     //
00051     m_BackgroundColor[0] = 0.25;
00052     m_BackgroundColor[1] = 0.25;
00053     m_BackgroundColor[2] = 0.25;
00054     m_BackgroundColor[3] = 0.70;
00055     //
00056     m_ForegroundActColor[0] = 0.2;
00057     m_ForegroundActColor[1] = 0.1;
00058     m_ForegroundActColor[2] = 0.2;
00059     m_ForegroundActColor[3] = 0.70;
00060     //
00061     m_BackgroundActColor[0] = 0.2;
00062     m_BackgroundActColor[1] = 1.0;
00063     m_BackgroundActColor[2] = 0.2;
00064     m_BackgroundActColor[3] = 0.70;
00065 }
00066 //-----------------------------------------------------------------------------
00067 void OpenGLCtrl::GetStartPos ( int & x, int & y ) const
00068 {
00069     x = m_xyLowerLeftPt[0];
00070     y = m_xyLowerLeftPt[1];
00071 }
00072 //-----------------------------------------------------------------------------
00073 void OpenGLCtrl::SetStartPos ( int x, int y )
00074 {
00075     m_xyLowerLeftPt[0] = x;
00076     m_xyLowerLeftPt[1] = y;
00077 }
00078 //-----------------------------------------------------------------------------
00079 void OpenGLCtrl::GetSize ( int & width, int & height ) const
00080 {
00081     width  = m_DrawSizeXY[0];
00082     height = m_DrawSizeXY[1];
00083 }
00084 //-----------------------------------------------------------------------------
00085 void OpenGLCtrl::SetSize ( int width, int height )
00086 {
00087     m_DrawSizeXY[0] = width;
00088     m_DrawSizeXY[1] = height;
00089 }
00090 //-----------------------------------------------------------------------------
00091 void OpenGLCtrl::GetForegroundColor ( 
00092     float & r, float & g, float & b, float & a ) const
00093 {
00094     r = m_ForegroundColor[0];
00095     g = m_ForegroundColor[1];
00096     b = m_ForegroundColor[2];
00097     a = m_ForegroundColor[3];
00098 }
00099 //-----------------------------------------------------------------------------
00100 void OpenGLCtrl::SetForegroundColor ( 
00101     float r, float g, float b, float a )
00102 {
00103     m_ForegroundColor[0] = r;
00104     m_ForegroundColor[1] = g;
00105     m_ForegroundColor[2] = b;
00106     m_ForegroundColor[3] = a;
00107 }
00108 //-----------------------------------------------------------------------------
00109 void OpenGLCtrl::GetBackgroundColor ( 
00110     float & r, float & g, float & b, float & a ) const
00111 {
00112     r = m_BackgroundColor[0];
00113     g = m_BackgroundColor[1];
00114     b = m_BackgroundColor[2];
00115     a = m_BackgroundColor[3];
00116 }
00117 //-----------------------------------------------------------------------------
00118 void OpenGLCtrl::SetBackgroundColor ( 
00119     float r, float g, float b, float a )
00120 {
00121     m_BackgroundColor[0] = r;
00122     m_BackgroundColor[1] = g;
00123     m_BackgroundColor[2] = b;
00124     m_BackgroundColor[3] = a;
00125 }
00126 //-----------------------------------------------------------------------------
00127 void OpenGLCtrl::GetForegroundActColor ( 
00128     float & r, float & g, float & b, float & a ) const
00129 {
00130     r = m_ForegroundActColor[0];
00131     g = m_ForegroundActColor[1];
00132     b = m_ForegroundActColor[2];
00133     a = m_ForegroundActColor[3];
00134 }
00135 //-----------------------------------------------------------------------------
00136 void OpenGLCtrl::SetForegroundActColor ( 
00137     float r, float g, float b, float a )
00138 {
00139     m_ForegroundActColor[0] = r;
00140     m_ForegroundActColor[1] = g;
00141     m_ForegroundActColor[2] = b;
00142     m_ForegroundActColor[3] = a;
00143 }
00144 //-----------------------------------------------------------------------------
00145 void OpenGLCtrl::GetBackgroundActColor ( 
00146     float & r, float & g, float & b, float & a ) const
00147 {
00148     r = m_BackgroundActColor[0];
00149     g = m_BackgroundActColor[1];
00150     b = m_BackgroundActColor[2];
00151     a = m_BackgroundActColor[3];
00152 }
00153 //-----------------------------------------------------------------------------
00154 void OpenGLCtrl::SetBackgroundActColor ( 
00155     float r, float g, float b, float a )
00156 {
00157     m_BackgroundActColor[0] = r;
00158     m_BackgroundActColor[1] = g;
00159     m_BackgroundActColor[2] = b;
00160     m_BackgroundActColor[3] = a;
00161 }
00162 //-----------------------------------------------------------------------------
00163 void OpenGLCtrl::GetBorderColor ( 
00164     float & r, float & g, float & b, float & a )
00165 {
00166     r = g_BorderColor[0];
00167     g = g_BorderColor[1];
00168     b = g_BorderColor[2];
00169     a = g_BorderColor[3];
00170 }
00171 //-----------------------------------------------------------------------------
00172 void OpenGLCtrl::SetBorderColor ( 
00173     float r, float g, float b, float a )
00174 {
00175     g_BorderColor[0] = r;
00176     g_BorderColor[1] = g;
00177     g_BorderColor[2] = b;
00178     g_BorderColor[3] = a;
00179 }
00180 //-----------------------------------------------------------------------------
00181 void OpenGLCtrl::SetOpacity ( float alpha )
00182 {
00183     m_ForegroundColor[3] = 
00184     m_BackgroundColor[3] =
00185     m_ForegroundActColor[3] =
00186     m_BackgroundActColor[3] = alpha; 
00187 }
00188 //-----------------------------------------------------------------------------
00189 //=============================================================================
00190 
00191 //=============================================================================
00192 // Draw Function(s)
00193 //-----------------------------------------------------------------------------
00194 //-----------------------------------------------------------------------------
00195 void OpenGLCtrl::BeginDrawing ( float fUnitsPerPixel, bool useBlending )
00196 {
00197     glPushAttrib( GL_ALL_ATTRIB_BITS );
00198     //---------------------------------------------------------------
00199     // Setup Ortho and Viewport
00200     glMatrixMode( GL_PROJECTION );
00201     glPushMatrix();
00202     glLoadIdentity();
00203     GLfloat pViewPort[4];
00204     glGetFloatv( GL_VIEWPORT, pViewPort );
00205     //glOrtho( 0, pViewPort[2]-pViewPort[0], 0, pViewPort[3]-pViewPort[1], -1, 1 );
00206     glOrtho( pViewPort[0]*fUnitsPerPixel, pViewPort[2]*fUnitsPerPixel, 
00207              pViewPort[1]*fUnitsPerPixel, pViewPort[3]*fUnitsPerPixel, 
00208              -1, 1 );
00209     glMatrixMode( GL_MODELVIEW );
00210     glPushMatrix();
00211     glLoadIdentity();
00212     //---------------------------------------------------------------
00213     if ( useBlending ) {
00214         glEnable( GL_BLEND );
00215         glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
00216     }
00217     else {
00218         glDisable( GL_BLEND );
00219     }
00220     glDisable( GL_LIGHTING );
00221     glDisable( GL_DEPTH_TEST );
00222     //glDisable( GL_CULL_FACE );
00223     glFrontFace( GL_CW );
00224     glPointSize( 1 );
00225     glLineWidth( 1 );
00226 }
00227 //-----------------------------------------------------------------------------
00228 void OpenGLCtrl::EndDrawing ()
00229 {
00230     //---------------------------------------------------------------
00231     // Return the previous Ortho and Viewport
00232     glPopMatrix();
00233     glMatrixMode( GL_PROJECTION );
00234     glPopMatrix();
00235     glMatrixMode( GL_MODELVIEW );
00236     //---------------------------------------------------------------
00237     glPopAttrib();
00238 }
00239 //-----------------------------------------------------------------------------
00240 void OpenGLCtrl::DrawBackgroundAndBorder ()
00241 {
00242     //---------------------------------------------------------------
00243     // Draw Background with Border
00244     if ( m_IsOn ) {
00245         DrawBackgroundAndBorder_On();
00246     }
00247     else {
00248         DrawBackgroundAndBorder_Off();
00249     }
00250 }
00251 //-----------------------------------------------------------------------------
00252 void OpenGLCtrl::DrawBackgroundAndBorder_Off ()
00253 {
00254     //---------------------------------------------------------------
00255     // Draw Background with Border
00256     int X = m_xyLowerLeftPt[0] + m_DrawSizeXY[0];
00257     int Y = m_xyLowerLeftPt[1] + m_DrawSizeXY[1];
00258     int mx = m_xyLowerLeftPt[0];
00259     int my = m_xyLowerLeftPt[1];
00260     glBegin( GL_QUADS );
00261         // Draw Background
00262         glColor4fv( m_BackgroundColor );
00263         glVertex2f( mx, my );
00264         glVertex2f(  X, my );
00265         glVertex2f(  X,  Y );
00266         glVertex2f( mx,  Y );
00267         //
00268         glColor4fv( g_BorderColor );
00269         // Draw Left Border
00270         int LX = m_xyLowerLeftPt[0] - m_BorderSize;
00271         int BY = m_xyLowerLeftPt[1] - m_BorderSize;
00272         glVertex2f( LX,  m_xyLowerLeftPt[1] );
00273         glVertex2f( m_xyLowerLeftPt[0], m_xyLowerLeftPt[1] );
00274         glVertex2f( m_xyLowerLeftPt[0], Y );
00275         glVertex2f( LX,  Y );
00276         // Draw Right Border
00277         int RX = X + m_BorderSize;
00278         int TY = Y + m_BorderSize;
00279         glVertex2f(  X, m_xyLowerLeftPt[1] );
00280         glVertex2f( RX, m_xyLowerLeftPt[1] );
00281         glVertex2f( RX,  Y );
00282         glVertex2f(  X,  Y );
00283         // Draw Top Border
00284         glVertex2f( LX,  Y );
00285         glVertex2f( RX,  Y );
00286         glVertex2f( RX, TY );
00287         glVertex2f( LX, TY );
00288         // Draw Bottom Border
00289         glVertex2f( LX, BY );
00290         glVertex2f( RX, BY );
00291         glVertex2f( RX, m_xyLowerLeftPt[1] );
00292         glVertex2f( LX, m_xyLowerLeftPt[1] );
00293     glEnd();
00294 }
00295 //-----------------------------------------------------------------------------
00296 void OpenGLCtrl::DrawBackgroundAndBorder_On ()
00297 {
00298     //---------------------------------------------------------------
00299     // Draw Background with Border
00300     int X = m_xyLowerLeftPt[0] + m_DrawSizeXY[0];
00301     int Y = m_xyLowerLeftPt[1] + m_DrawSizeXY[1];
00302     int mx = m_xyLowerLeftPt[0];
00303     int my = m_xyLowerLeftPt[1];
00304     glBegin( GL_QUADS );
00305         // Draw Background
00306         glColor4fv( m_BackgroundActColor );
00307         glVertex2f( mx, my );
00308         glVertex2f(  X, my );
00309         glVertex2f(  X,  Y );
00310         glVertex2f( mx,  Y );
00311         //
00312         //X  += m_BorderSize;
00313         //Y  += m_BorderSize;
00314         //mx -= m_BorderSize;
00315         //my -= m_BorderSize;
00316         glColor4fv( g_BorderColor );
00317         // Draw Left Border
00318         int LX = m_xyLowerLeftPt[0] - m_BorderSize;
00319         int BY = m_xyLowerLeftPt[1] - m_BorderSize;
00320         glVertex2f( LX, m_xyLowerLeftPt[1] );
00321         glVertex2f( m_xyLowerLeftPt[0], m_xyLowerLeftPt[1] );
00322         glVertex2f( m_xyLowerLeftPt[0], Y );
00323         glVertex2f( LX,  Y );
00324         // Draw Right Border
00325         int RX = X + m_BorderSize;
00326         int TY = Y + m_BorderSize;
00327         glVertex2f(  X, m_xyLowerLeftPt[1] );
00328         glVertex2f( RX, m_xyLowerLeftPt[1] );
00329         glVertex2f( RX,  Y );
00330         glVertex2f(  X,  Y );
00331         // Draw Top Border
00332         glVertex2f( LX,  Y );
00333         glVertex2f( RX,  Y );
00334         glVertex2f( RX, TY );
00335         glVertex2f( LX, TY );
00336         // Draw Bottom Border
00337         glVertex2f( LX, BY );
00338         glVertex2f( RX, BY );
00339         glVertex2f( RX, m_xyLowerLeftPt[1] );
00340         glVertex2f( LX, m_xyLowerLeftPt[1] );
00341     glEnd();
00342 }
00343 //-----------------------------------------------------------------------------
00344 void OpenGLCtrl::DrawText ()
00345 {
00346     if ( m_IsOn )   glColor4fv( m_ForegroundActColor );
00347     else            glColor4fv( m_ForegroundColor );
00348     OpenGL::Fn::DrawGLText( m_Text, 
00349         m_xyLowerLeftPt[0]+2, m_xyLowerLeftPt[1] + m_DrawSizeXY[1]/3 );
00350 }
00351 //-----------------------------------------------------------------------------
00352 //=============================================================================
00353 
00354 //=============================================================================
00355 END_NAMESPACE_TAPs__OpenGL
00356 //-----------------------------------------------------------------------------
00357 //34567890123456789012345678901234567890123456789012345678901234567890123456789
00358 //--+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines