DrawV.cpp

Go to the documentation of this file.
00001 /******************************************************************************
00002 *       SOFA, Simulation Open-Framework Architecture, version 1.0 beta 3      *
00003 *                (c) 2006-2008 MGH, INRIA, USTL, UJF, CNRS                    *
00004 *                                                                             *
00005 * This library is free software; you can redistribute it and/or modify it     *
00006 * under the terms of the GNU Lesser General Public License as published by    *
00007 * the Free Software Foundation; either version 2.1 of the License, or (at     *
00008 * your option) any later version.                                             *
00009 *                                                                             *
00010 * This library is distributed in the hope that it will be useful, but WITHOUT *
00011 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or       *
00012 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License *
00013 * for more details.                                                           *
00014 *                                                                             *
00015 * You should have received a copy of the GNU Lesser General Public License    *
00016 * along with this library; if not, write to the Free Software Foundation,     *
00017 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.          *
00018 *******************************************************************************
00019 *                               SOFA :: Modules                               *
00020 *                                                                             *
00021 * Authors: The SOFA Team and external contributors (see Authors.txt)          *
00022 *                                                                             *
00023 * Contact information: contact@sofa-framework.org                             *
00024 ******************************************************************************/
00025 #include <sofa/component/visualmodel/DrawV.h>
00026 #include <sofa/helper/gl/RAII.h>
00027 #include <sofa/helper/vector.h>
00028 #include <sofa/core/ObjectFactory.h>
00029 #include <sstream>
00030 
00031 namespace sofa
00032 {
00033 
00034 namespace component
00035 {
00036 
00037 namespace visualmodel
00038 {
00039 
00040 using namespace sofa::defaulttype;
00041 
00042 SOFA_DECL_CLASS(DrawV)
00043 
00044 int DrawVClass = core::RegisterObject("Visual model displaying velocity vectors for OpenGL display")
00045 .add< DrawV >()
00046 ;
00047 
00048 DrawV::DrawV()
00049 : castShadow( initData(&castShadow, false, "castShadow", "True if object cast shadows") )
00050 , useAlpha  ( initData(&useAlpha  , false, "useAlpha"  , "True if velocity displayed using alpha blending") )
00051 , vscale    ( initData(&vscale    ,   1.0, "vscale"    , "Scaling of veloity vectors") )
00052 {
00053 }
00054 
00055 void DrawV::initVisual()
00056 {
00057 }
00058 
00059 void DrawV::updateVisual()
00060 {
00061 }
00062 
00063 bool DrawV::isTransparent()
00064 {
00065     return useAlpha.getValue();
00066 }
00067 
00068 void DrawV::drawVisual()
00069 {
00070     if (!isTransparent())
00071     internalDraw();
00072 }
00073 
00074 void DrawV::drawTransparent()
00075 {
00076     if (isTransparent())
00077     internalDraw();
00078 }
00079 
00080 void DrawV::drawShadow()
00081 {
00082     if (!isTransparent() && castShadow.getValue())
00083     {
00084     //std::cout << "drawShadow for "<<getName()<<std::endl;
00085     internalDraw();
00086     }
00087 }
00088 
00089 float DrawV::getVScale()
00090 {
00091     return (float)vscale.getValue();
00092     //return 1.0f;
00093     //return getContext()->getDt();
00094 }
00095 
00096 void DrawV::internalDraw()
00097 {
00098     if (!getContext()->getShowVisualModels()) return;
00099 
00100     glDisable(GL_LIGHTING);
00101 
00102     if (isTransparent()) {
00103     glEnable(GL_BLEND);
00104     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
00105     glDepthMask(GL_FALSE);
00106     }
00107 
00108     const unsigned int nbp = inputX.size();
00109     const float vscale = getVScale();
00110     glBegin(GL_LINES);
00111     for (unsigned int i=0;i<nbp;i++)
00112     {
00113     const Coord& p = inputX[i];
00114     const Coord p1 = inputX[i] + inputV[i]*vscale;
00115     const Coord p2 = inputX[i] - inputV[i]*vscale;
00116     glColor4f(1.0, 0.5, 0.5, 1.0);
00117     glVertex3fv(p.ptr());
00118     glColor4f(1.0, 0.5, 0.5, 0.0);
00119     glVertex3fv(p1.ptr());
00120     glColor4f(0.5, 1.0, 0.5, 1.0);
00121     glVertex3fv(p.ptr());
00122     glColor4f(0.5, 1.0, 0.5, 0.0);
00123     glVertex3fv(p2.ptr());
00124     }
00125     glEnd();
00126 
00127     glDisable(GL_LIGHTING);
00128 
00129     if (isTransparent()) {
00130     glDisable(GL_BLEND);
00131     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
00132     //glBlendFunc(GL_ONE, GL_ZERO);
00133     glDepthMask(GL_TRUE);
00134     }
00135 }
00136 
00137 bool DrawV::addBBox(double* minBBox, double* maxBBox)
00138 {
00139     const unsigned int nbp = inputX.size();
00140     const float vscale = getVScale();
00141     for (unsigned int i=0; i<nbp; i++)
00142     {
00143     const Coord& p = inputX[i];
00144     const Coord p1 = inputX[i]+inputV[i]*vscale;
00145     const Coord p2 = inputX[i]-inputV[i]*vscale;
00146     for (int c=0;c<3;c++)
00147     {
00148         if (p[c] > maxBBox[c]) maxBBox[c] = p[c];
00149         if (p[c] < minBBox[c]) minBBox[c] = p[c];
00150         if (p1[c] > maxBBox[c]) maxBBox[c] = p1[c];
00151         if (p1[c] < minBBox[c]) minBBox[c] = p1[c];
00152         if (p2[c] > maxBBox[c]) maxBBox[c] = p2[c];
00153         if (p2[c] < minBBox[c]) minBBox[c] = p2[c];
00154     }
00155   }  
00156   return true;
00157 }
00158 
00159 } // namespace visualmodel
00160 
00161 } // namespace component
00162 
00163 } // namespace sofa
00164 

Generated on Sun Nov 23 03:47:59 2008 for Sofa by  doxygen 1.5.7.1