VISH  0.2
BoundingVolume.cpp

Draw the bounding box of some VObject, if that one provides the respective information as an interface.

#include <ocean/plankton/VPipeline.hpp>
#include <ocean/GLvish/VGLRenderObject.hpp>
#include <ocean/eagle/PhysicalSpace.hpp>
#include <ocean/eagle/GL/EagleGL.hpp>
#include <ocean/GLvish/BoundingBox.hpp>
#include <ocean/GLvish/GLFonts.hpp>

using namespace Wizt;
using namespace Eagle;

namespace
{


class   BoundingBoxRenderer : public VGLRenderObject
{
public:
        TypedSlot<VBoundingBox>         myData;
        TypedSlot<double>               Thickness,
                                        FontSize;

        TypedSlot<GLFontManager>        MyFonts;
        
        BoundingBoxRenderer(const string&name, int p, const RefPtr<VCreationPreferences>&VP)
        : VGLRenderObject(name, p, VP)
        , myData( this, "source", VBoundingBox() )
        , Thickness(this, "thickness", 20.0, NullPtr(), 5)
        , FontSize(this, "fontsize", 1.0, NullPtr(), 0)
        , MyFonts(this, "fonts", Empty(), 10 )
        {
                FontSize.setProperty("max",10.0);
        }

        override bool update(VRequest&Context, double precision);

        override void render(VGLRenderContext&Context) const;

static string createChildname(const string&parent_name)
        {
                return "BoundingVolumeOf" + parent_name;
        }
};


bool    BoundingBoxRenderer::update(VRequest&Context, double precision)
{
VBoundingBox    BBox;
        myData << Context >> BBox;
        if (!BBox)
        {
                resetBBox( Context );
                return true;
        }

        setBoundingBall(Context, BBox);
        return true;
}


void    BoundingBoxRenderer::render(VGLRenderContext&Context) const
{
VBoundingBox    IB;
        myData << Context >> IB;
        if (!IB)
                return; 

using namespace Eagle::PhysicalSpace; 

point   P0 = IB->center(),
        P1 = IB->center(); 

RefPtr<BoundingBox> BB = IB; 
        if (BB)
        {
                P0 = BB->mincoord(); 
                P1 = BB->maxcoord();
        }
        else
        {
                for(int i=0; i<P0.SIZE; i++)
                {
                        P0[i] -= IB->radius(); 
                        P1[i] += IB->radius();
                }
        } 

double  FontScale = 1.0;
        FontSize << Context >> FontScale;
        if (FontScale>0.0)
        {
                if (GLFontManager*V = GLFontManagerCreator::getFontManager() )
                {
                RefPtr<GLFontManager::Font> MyFont = V->newFont("arial.ttf;Vera.ttf",';'); 
                        if (MyFont)
                        {
                                MyFont->setSize(2);
                                {
                                float front_emission[4] = { 0.3, 0.2, 0.1, 0.0 };
                                float front_ambient[4]  = { 0.2, 0.2, 0.2, 0.0 };
                                float front_diffuse[4]  = { 0.95, 0.95, 0.8, 0.0 };
                                float front_specular[4] = { 0.6, 0.6, 0.6, 0.0 };
                                        glMaterialfv(GL_FRONT, GL_EMISSION, front_emission);
                                        glMaterialfv(GL_FRONT, GL_AMBIENT, front_ambient);
                                        glMaterialfv(GL_FRONT, GL_DIFFUSE, front_diffuse);
                                        glMaterialfv(GL_FRONT, GL_SPECULAR, front_specular);
                                        glMaterialf(GL_FRONT, GL_SHININESS, 16.0);
                                        glColor4fv(front_diffuse);

                                        glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_FALSE);
                                        glColorMaterial(GL_FRONT, GL_DIFFUSE);
                                        glEnable(GL_COLOR_MATERIAL); 
                                        glDisable( GL_TEXTURE_2D); 
                                        glEnable( GL_DEPTH_TEST); 
                                        glDisable( GL_BLEND);
                
                                } 
                                glEnable( GL_NORMALIZE );

                                glColor3f( 1.0, 1.0, 1.0);

                        float   llx, lly, llz, urx, ury, urz;
                        char    coords[2048]; 

                                sprintf(coords,"(%lg,%lg,%lg)", P0[0], P0[1], P0[2] );
                                glPushMatrix(); 
                                glTranslate( P0 );
                                MyFont->getBBox( coords, llx, lly, llz, urx, ury, urz); 
                                glTranslatef( -FontScale*(urx-llx)/2, -(ury-lly), 0);
                                glScalef(FontScale, FontScale, FontScale );
                                MyFont->render(coords); 
                                glPopMatrix();

                                sprintf(coords,"(%lg,%lg,%lg)", P1[0], P1[1], P1[2] );
                                glPushMatrix(); 
                                glTranslate( P1 ); 
                                MyFont->getBBox( coords, llx, lly, llz, urx, ury, urz); 
                                glTranslatef( -FontScale*(urx-llx)/2, 0,0); 
                                glScalef(FontScale, FontScale, FontScale );
                                MyFont->render(coords); 
                                glPopMatrix();
                        } 
                        else
                                puts("font not found");
                } 
                else
                        puts("no font manager");

        }


//              printf("BBOX: %lg,%lg,%lg <-> %lg,%lg,%lg\n",
//                     P0[0], P0[1], P0[2],
//                     P1[0], P1[1], P1[2] ); 

double  width = 20;
        Thickness << Context >> width; 

point   P000 ( P0.x(), P0.y(), P0.z() ),
        P001 ( P0.x(), P0.y(), P1.z() ),
        P010 ( P0.x(), P1.y(), P0.z() ),
        P011 ( P0.x(), P1.y(), P1.z() ),
        P100 ( P1.x(), P0.y(), P0.z() ),
        P101 ( P1.x(), P0.y(), P1.z() ),
        P110 ( P1.x(), P1.y(), P0.z() ),
        P111 ( P1.x(), P1.y(), P1.z() ); 
 

/*
 This code is inspired by
 http://www.bluevoid.com/opengl/sig00/advanced00/notes/node290.html
*/ 
         glLineWidth(width/10.0);
         glEnable(GL_LINE_SMOOTH);

         glDisable(GL_LIGHTING);
         glEnable(GL_DEPTH_TEST); 

         glColor3f( 0.9, 0.7, 0.6); 
         glBegin( GL_LINES ); 

         glVertex( P000 );      glVertex( P010 ); 
         glVertex( P010 );      glVertex( P011 ); 
         glVertex( P011 );      glVertex( P001 ); 
         glVertex( P001 );      glVertex( P000 ); 

         glVertex( P100 );      glVertex( P110 ); 
         glVertex( P110 );      glVertex( P111 ); 
         glVertex( P111 );      glVertex( P101 ); 
         glVertex( P101 );      glVertex( P100 ); 

         glVertex( P000 );      glVertex( P100 ); 
         glVertex( P010 );      glVertex( P110 ); 
         glVertex( P011 );      glVertex( P111 ); 
         glVertex( P001 );      glVertex( P101 ); 

         glEnd(); 

         glEnable(GL_POINT_SMOOTH);
         glPointSize(width/10.0);
         glBegin( GL_POINTS );
         glVertex( P000 ); glVertex( P010 ); glVertex( P011 ); glVertex( P001 );
         glVertex( P100 ); glVertex( P110 ); glVertex( P111 ); glVertex( P101 );
         glEnd();
}

static  VSink< AcceptList<VBoundingBox>, BoundingBoxRenderer> ThisIsMyGreatCreator("Display/BoundingVolume", ObjectQuality::MATURE);

}