DrawV.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
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
00085 internalDraw();
00086 }
00087 }
00088
00089 float DrawV::getVScale()
00090 {
00091 return (float)vscale.getValue();
00092
00093
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
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 }
00160
00161 }
00162
00163 }
00164