Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

OSGSimpleStatisticsForeground.cpp

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002  *                                OpenSG                                     *
00003  *                                                                           *
00004  *                                                                           *
00005  *             Copyright (C) 2000-2002 by the OpenSG Forum                   *
00006  *                                                                           *
00007  *                            www.opensg.org                                 *
00008  *                                                                           *
00009  *   contact: dirk@opensg.org, gerrit.voss@vossg.org, jbehr@zgdv.de          *
00010  *                                                                           *
00011 \*---------------------------------------------------------------------------*/
00012 /*---------------------------------------------------------------------------*\
00013  *                                License                                    *
00014  *                                                                           *
00015  * This library is free software; you can redistribute it and/or modify it   *
00016  * under the terms of the GNU Library General Public License as published    *
00017  * by the Free Software Foundation, version 2.                               *
00018  *                                                                           *
00019  * This library is distributed in the hope that it will be useful, but       *
00020  * WITHOUT ANY WARRANTY; without even the implied warranty of                *
00021  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU         *
00022  * Library General Public License for more details.                          *
00023  *                                                                           *
00024  * You should have received a copy of the GNU Library General Public         *
00025  * License along with this library; if not, write to the Free Software       *
00026  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.                 *
00027  *                                                                           *
00028 \*---------------------------------------------------------------------------*/
00029 /*---------------------------------------------------------------------------*\
00030  *                                Changes                                    *
00031  *                                                                           *
00032  *                                                                           *
00033  *                                                                           *
00034  *                                                                           *
00035  *                                                                           *
00036  *                                                                           *
00037 \*---------------------------------------------------------------------------*/
00038 //---------------------------------------------------------------------------
00039 //  Includes
00040 //---------------------------------------------------------------------------
00041 #include <stdlib.h>
00042 #include <stdio.h>
00043 
00044 #include <OSGConfig.h>
00045 
00046 #ifdef OSG_HAS_SSTREAM
00047 #include <sstream>
00048 #else
00049 #include <strstream>
00050 #endif
00051 #include <OSGNodePtr.h>
00052 #include <OSGImage.h>
00053 
00054 #include <OSGTextTXFFace.h>
00055 #include <OSGTextTXFGlyph.h>
00056 #include <OSGTextLayoutParam.h>
00057 #include <OSGTextLayoutResult.h>
00058 
00059 #include <OSGViewport.h>
00060 
00061 #include "OSGSimpleStatisticsForeground.h"
00062 
00063 #include "OSGStatisticsDefaultFont.h"
00064 
00065 OSG_USING_NAMESPACE
00066 
00076 /* static vars */
00077 TextTXFFace *SimpleStatisticsForeground::       _face = 0;
00078 
00079 TextureChunkPtr SimpleStatisticsForeground::    _texchunk;
00080 
00081 /*----------------------- constructors & destructors ----------------------*/
00082 SimpleStatisticsForeground::SimpleStatisticsForeground(void) :
00083     Inherited()
00084 {
00085 }
00086 
00087 /* */
00088 SimpleStatisticsForeground::SimpleStatisticsForeground(const SimpleStatisticsForeground &source) :
00089         Inherited(source)
00090 {
00091 }
00092 
00093 /* */
00094 SimpleStatisticsForeground::~SimpleStatisticsForeground(void)
00095 {
00096 }
00097 
00098 /*----------------------------- class specific ----------------------------*/
00099 void SimpleStatisticsForeground::initMethod(void)
00100 {
00101 }
00102 
00103 /* */
00104 void SimpleStatisticsForeground::changed(BitVector whichField, UInt32 origin)
00105 {
00106     Inherited::changed(whichField, origin);
00107 }
00108 
00109 /* */
00110 void SimpleStatisticsForeground::dump(UInt32, const BitVector) const
00111 {
00112     SLOG << "Dump SimpleStatisticsForeground NI" << std::endl;
00113 }
00114 
00117 void SimpleStatisticsForeground::addElement(StatElemDescBase &desc,
00118                                             const char *format)
00119 {
00120     getElementIDs().push_back(desc.getID());
00121     getFormats().push_back(format ? format : "");
00122 }
00123 
00126 void SimpleStatisticsForeground::addElement(Int32 id, const char *format)
00127 {
00128     getElementIDs().push_back(id);
00129     getFormats().push_back(format ? format : "");
00130 }
00131 
00135 void SimpleStatisticsForeground::initText(void)
00136 {
00137     // create the text needed
00138 #ifdef OSG_HAS_SSTREAM
00139     std::istringstream stream(StatisticsDefaultFontString,
00140                               std::istringstream::in |
00141                               std::istringstream::out);
00142 #else
00143     std::istrstream stream((char *) StatisticsDefaultFontData,
00144                            StatisticsDefaultFontDataSize);
00145 #endif
00146     _face = TextTXFFace::createFromStream(stream);
00147     addRefP(_face);
00148 
00149     ImagePtr texture = _face->getTexture();
00150     _texchunk = TextureChunk::create();
00151     beginEditCP(_texchunk);
00152     {
00153         _texchunk->setImage(texture);
00154         _texchunk->setWrapS(GL_CLAMP);
00155         _texchunk->setWrapT(GL_CLAMP);
00156         _texchunk->setEnvMode(GL_MODULATE);
00157     }
00158 
00159     endEditCP(_texchunk);
00160 }
00161 
00164 void SimpleStatisticsForeground::draw(DrawActionBase *action, Viewport *port)
00165 {
00166     if (_face == 0)
00167         initText();
00168 
00169     Real32  pw = Real32(port->getPixelWidth ());
00170     Real32  ph = Real32(port->getPixelHeight());
00171 
00172     if(pw < 1 || ph < 1)
00173         return;
00174 
00175     GLboolean    light = glIsEnabled(GL_LIGHTING);
00176 
00177     GLint   fill[2];
00178     glGetIntegerv(GL_POLYGON_MODE, fill);
00179     glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
00180 
00181     GLboolean    depth = glIsEnabled(GL_DEPTH_TEST);
00182     glDisable(GL_DEPTH_TEST);
00183 
00184     GLboolean    colmat = glIsEnabled(GL_COLOR_MATERIAL);
00185     glDisable(GL_COLOR_MATERIAL);
00186 
00187     glMatrixMode(GL_MODELVIEW);
00188     glPushMatrix();
00189     glLoadIdentity();
00190 
00191     glMatrixMode(GL_PROJECTION);
00192     glPushMatrix();
00193     glLoadIdentity();
00194 
00195     Real32  aspect = pw / ph;
00196     Real32  size = getSize();
00197 
00198     glOrtho(-0.5, -0.5 + ph / size * aspect, 0.5 - ph / size, 0.5, 0, 1);
00199 
00200     glAlphaFunc(GL_NOTEQUAL, 0);
00201     glEnable(GL_ALPHA_TEST);
00202     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
00203     glEnable(GL_BLEND);
00204 
00205     // draw text
00206     std::vector < std::string > stat;
00207 
00208     StatCollector   *col = &getCollector();
00209     StatElem        *el;
00210 
00211     if(getElementIDs().size() != 0)
00212     {
00213         for(UInt32 i = 0; i < getElementIDs().size(); ++i)
00214         {
00215             Int32 id(getElementIDs()[i]);
00216             el = ((id >= 0) ? col->getElem(id) : 0);
00217 
00218             stat.resize(stat.size() + 1);
00219             std::vector < std::string >::iterator str = stat.end() - 1;
00220 
00221             const char  *format = NULL;
00222             if(i < getFormats().size() && getFormats()[i].length())
00223             {
00224               format = getFormats()[i].c_str();
00225             }
00226 
00227             if (el)
00228                 el->putToString(*str, format);
00229             else
00230                 *str = format;
00231         }
00232     }
00233     else    // fallback, show all elements
00234     {
00235         for(UInt32 i = 0; i < col->getNumOfElems(); ++i)
00236         {
00237             el = col->getElem(i, false);
00238             if(el)
00239             {
00240                 std::string desc(el->getDesc()->getName().str()), eltext;
00241                 
00242                 el->putToString(eltext);
00243                 desc = desc + " : " + eltext;
00244                 
00245                 stat.resize(stat.size() + 1);
00246                 std::vector < std::string >::iterator str = stat.end() - 1;
00247                 *str = desc;
00248             }
00249         }
00250     }
00251 
00252     TextLayoutParam layoutParam;
00253     layoutParam.majorAlignment = TextLayoutParam::ALIGN_BEGIN;
00254     layoutParam.minorAlignment = TextLayoutParam::ALIGN_BEGIN;
00255     TextLayoutResult layoutResult;
00256     _face->layout(stat, layoutParam, layoutResult);
00257 
00258     _texchunk->activate(action);
00259 
00260     glColor4fv((GLfloat *) getColor().getValuesRGBA());
00261 
00262     glBegin(GL_QUADS);
00263     UInt32 i, numGlyphs = layoutResult.getNumGlyphs();
00264     for(i = 0; i < numGlyphs; ++i)
00265     {
00266         const TextTXFGlyph &glyph = _face->getTXFGlyph(layoutResult.indices[i]);
00267         Real32 width = glyph.getWidth();
00268         Real32 height = glyph.getHeight();
00269         // No need to draw invisible glyphs
00270         if ((width <= 0.f) || (height <= 0.f))
00271             continue;
00272 
00273         // Calculate coordinates
00274         const Vec2f &pos = layoutResult.positions[i];
00275         Real32 posLeft = pos.x();
00276         Real32 posTop = pos.y();
00277         Real32 posRight = pos.x() + width;
00278         Real32 posBottom = pos.y() - height;
00279         Real32 texCoordLeft = glyph.getTexCoord(TextTXFGlyph::COORD_LEFT);
00280         Real32 texCoordTop = glyph.getTexCoord(TextTXFGlyph::COORD_TOP);
00281         Real32 texCoordRight = glyph.getTexCoord(TextTXFGlyph::COORD_RIGHT);
00282         Real32 texCoordBottom = glyph.getTexCoord(TextTXFGlyph::COORD_BOTTOM);
00283 
00284         // lower left corner
00285         glTexCoord2f(texCoordLeft, texCoordBottom);
00286         glVertex2f(posLeft, posBottom);
00287 
00288         // lower right corner
00289         glTexCoord2f(texCoordRight, texCoordBottom);
00290         glVertex2f(posRight, posBottom);
00291 
00292         // upper right corner
00293         glTexCoord2f(texCoordRight, texCoordTop);
00294         glVertex2f(posRight, posTop);
00295 
00296         // upper left corner
00297         glTexCoord2f(texCoordLeft, texCoordTop);
00298         glVertex2f(posLeft, posTop);
00299     }
00300     glEnd();
00301 
00302     _texchunk->deactivate(action);
00303 
00304     glDisable(GL_ALPHA_TEST);
00305     glDisable(GL_BLEND);
00306 
00307     glMatrixMode(GL_PROJECTION);
00308     glPopMatrix();
00309 
00310     glMatrixMode(GL_MODELVIEW);
00311     glPopMatrix();
00312 
00313     if(depth == GL_TRUE)
00314         glEnable(GL_DEPTH_TEST);
00315     if(light == GL_TRUE)
00316         glEnable(GL_LIGHTING);
00317     if(colmat == GL_TRUE)
00318         glEnable(GL_COLOR_MATERIAL);
00319 
00320     glPolygonMode(GL_FRONT_AND_BACK, fill[0]);
00321 }
00322 
00323 /*-------------------------------------------------------------------------*/
00324 /*                              cvs id's                                   */
00325 #ifdef __sgi
00326 #pragma set woff 1174
00327 #endif
00328 #ifdef OSG_LINUX_ICC
00329 #pragma warning(disable : 177)
00330 #endif
00331 namespace
00332 {
00333 static char cvsid_cpp[] = "@(#)$Id: OSGSimpleStatisticsForeground.cpp,v 1.11 2002/08/07 04:04:13 vossg Exp $";
00334 static char cvsid_hpp[] = OSGSIMPLESTATISTICSFOREGROUND_HEADER_CVSID;
00335 static char cvsid_inl[] = OSGSIMPLESTATISTICSFOREGROUND_INLINE_CVSID;
00336 }

Generated on Thu Aug 25 04:10:10 2005 for OpenSG by  doxygen 1.4.3