MapPainter.cpp
Go to the documentation of this file.
00001 /*******************************************************************************
00002  *  MapPainter.cpp
00003  *
00004  *  (C) 2007 AG Aktives Sehen <agas@uni-koblenz.de>
00005  *           Universitaet Koblenz-Landau
00006  *
00007  *  Additional information:
00008  *  $Id: $
00009  *******************************************************************************/
00010 
00011 #include "MapPainter.h"
00012 
00013 #include "Messages/MapDataM.h"
00014 
00015 #include "GUI/RobbieWidget/RobbieGLWidget.h"
00016 
00017 #include <QtOpenGL>
00018 #include <GL/glut.h>
00019 
00020 #include "Architecture/Config/Config.h"
00021 
00022 #define THIS MapPainter
00023 
00024 THIS::THIS() : PainterPlugin( )
00025 {
00026   setName ( "SLAM Occupancy Map" );
00027 
00028   m_MapMmSize = Config::getInt( "Map.iSize" );
00029   m_MapPixelSize = m_MapMmSize / Config::getInt( "Map.iCellSize" ) + 1;
00030 
00031   m_TextureSize = RobbieGLWidget::calcTextureSize ( m_MapPixelSize, m_MapPixelSize );
00032   m_TextureId = -1;
00033   m_TextureData = new unsigned char[m_TextureSize*m_TextureSize];
00034   memset ( m_TextureData, 127, m_TextureSize*m_TextureSize );
00035   m_TextureLoaded = false;
00036 
00037 }
00038 
00039 
00040 THIS::~THIS()
00041 {
00042   if ( m_TextureData )
00043   {
00044     TRACE_SYSTEMINFO( "Deleting map texture" )
00045     delete m_TextureData;
00046   }
00047 
00048   if ( m_TextureId > 0 )
00049   {
00050     TRACE_SYSTEMINFO( "Deleting OpenGL textures" )
00051     glDeleteTextures ( 1, &m_TextureId );
00052   }
00053 }
00054 
00055 
00056 void THIS::processMessage ( Message* newMessage )
00057 {
00058   PainterPlugin::processMessage ( newMessage );
00059   switch ( newMessage->getType() )
00060   {
00061     case MessageTypes::MAP_DATA_M:
00062     {
00063       MapDataM* message = Message::castTo<MapDataM> ( newMessage );
00064       if ( message )
00065       {
00066         updateMap ( message->getMapPointer() );
00067         requestRedraw();
00068       }
00069       break;
00070     }
00071 
00072     default:
00073       break;
00074 
00075   }
00076 }
00077 
00078 void THIS::paint ( float next2DLayer )
00079 {
00080   glEnable ( GL_BLEND );
00081   glBlendFunc ( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
00082   glPolygonMode ( GL_FRONT_AND_BACK, GL_FILL );
00083 
00084   glColor4f ( 0.5, 0.7, 1.0, 0.6 );
00085 //  glColor4f ( 1,1,1,1 );
00086 
00087   glEnable ( GL_TEXTURE_2D );
00088 
00089   if ( !m_TextureLoaded )
00090   {
00091     loadGlTexture();
00092   }
00093   else
00094   {
00095     glBindTexture ( GL_TEXTURE_2D, m_TextureId );
00096   }
00097 
00098   glTexParameterf ( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
00099   glTexParameterf ( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
00100   glTexEnvf ( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
00101 //  glTexEnvf ( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE );
00102 
00103   //clamp texture so it doesn't repeat
00104   glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP );
00105   glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP );
00106 
00107 
00108   float halfSize = float ( m_TextureSize ) / float ( m_MapPixelSize ) * float ( m_MapMmSize ) / 2.0;
00109   float s=sqrt(2);
00110 
00111   glBegin ( GL_POLYGON );
00112   /*
00113   glTexCoord2f ( 0.0,  0.0 );
00114   glVertex3f ( halfSize,  halfSize, 0.0 );
00115   glTexCoord2f ( 0.0,  1.0 );
00116   glVertex3f ( -halfSize,  halfSize, 0.0 );
00117   glTexCoord2f ( 1.0,  1.0 );
00118   glVertex3f ( -halfSize, -halfSize, 0.0 );
00119   glTexCoord2f ( 1.0,  0.0 );
00120   glVertex3f ( halfSize, -halfSize, 0.0 );
00121   */
00122 
00123   //paint circle with map as texture
00124   for ( unsigned i=0; i<100; i++ )
00125   {
00126     float angle = float(i) / 100.0 * 2.0 * M_PI;
00127     float x = cos(angle)*s;
00128     float y = sin(angle)*s;
00129     glTexCoord2f ( 0.5-0.5*y, 0.5-0.5*x );
00130     glVertex3f ( halfSize*x,  halfSize*y, 0.0 );
00131   }
00132 
00133 
00134   glEnd();
00135 
00136   glDisable ( GL_TEXTURE_2D );
00137 }
00138 
00139 
00140 void THIS::updateMap ( unsigned char* mapData )
00141 {
00142   if ( m_TextureData )
00143   {
00144     int textureCenter = m_TextureSize / 2;
00145     int startX = textureCenter - ( m_MapPixelSize / 2 );
00146     int startY = textureCenter - ( m_MapPixelSize / 2 );
00147 
00148     //pointer to top-left corner of the draw window inside the texture
00149     unsigned char* texturePos = m_TextureData + startX + ( startY * m_TextureSize );
00150     unsigned char* mapPos = mapData;
00151 
00152     //draw new image in the center
00153     for ( int y = 0; y < m_MapPixelSize ; y++ )
00154     {
00155       memcpy ( texturePos, mapPos, m_MapPixelSize );
00156       texturePos += m_TextureSize;
00157       mapPos += m_MapPixelSize;
00158     }
00159 
00160     m_TextureLoaded = false;
00161 
00162   }
00163 }
00164 
00165 
00166 void THIS::loadGlTexture()
00167 {
00168   if ( m_TextureId < 1 )
00169   {
00170     glGenTextures ( 1, &m_TextureId );
00171     ostringstream stream;
00172     stream << "Using OpenGL texture # " << m_TextureId;
00173     TRACE_INFO( stream.str() );
00174   }
00175 
00176   if ( m_TextureData == 0 || m_TextureSize == 0 )
00177   {
00178     return;
00179   }
00180 
00181 
00182   glBindTexture ( GL_TEXTURE_2D, m_TextureId );
00183   glTexImage2D ( GL_TEXTURE_2D, 0, GL_LUMINANCE, m_TextureSize, m_TextureSize, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, m_TextureData );
00184   m_TextureLoaded = true;
00185 }
00186 
00187 
00188 
00189 #undef THIS


obj_rec_gui
Author(s): AGAS/agas@uni-koblenz.de
autogenerated on Mon Oct 6 2014 02:53:43