CudaTetrahedralVisualModel.inl

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 #ifndef OGLTETRAHEDRALMODEL_INL_
00026 #define OGLTETRAHEDRALMODEL_INL_
00027 
00028 #include "CudaTetrahedralVisualModel.h"
00029 
00030 #include <sofa/helper/gl/GLSLShader.h>
00031 
00032 namespace sofa
00033 {
00034 namespace component
00035 {
00036 namespace visualmodel
00037 {
00038 
00039 using namespace sofa::defaulttype;
00040 
00041 template<class TCoord, class TDeriv, class TReal>
00042 OglTetrahedralModel< gpu::cuda::CudaVectorTypes<TCoord,TDeriv,TReal> >::OglTetrahedralModel()
00043 : needUpdateTopology(true)
00044 , depthTest(initData(&depthTest, (bool) true, "depthTest", "Set Depth Test"))
00045 , blending(initData(&blending, (bool) true, "blending", "Set Blending"))
00046 , useVBO( initData( &useVBO, false, "useVBO", "true to activate Vertex Buffer Object") )
00047 {
00048 }
00049 
00050 template<class TCoord, class TDeriv, class TReal>
00051 OglTetrahedralModel< gpu::cuda::CudaVectorTypes<TCoord,TDeriv,TReal> >::~OglTetrahedralModel()
00052 {
00053 }
00054 
00055 template<class TCoord, class TDeriv, class TReal>
00056 void OglTetrahedralModel< gpu::cuda::CudaVectorTypes<TCoord,TDeriv,TReal> >::init()
00057 {
00058     sofa::core::objectmodel::BaseContext* context = this->getContext();
00059     context->get(topo);
00060     context->get(nodes);
00061 
00062     
00063     if (!nodes)
00064     {
00065         std::cerr << "No mecha." << std::endl;
00066         return;
00067     }
00068     
00069     if (!topo)
00070     {
00071         std::cerr << "No topo." << std::endl;
00072         return;
00073     }
00074     
00075     updateTopology();
00076 }
00077 
00078 template<class TCoord, class TDeriv, class TReal>
00079 void OglTetrahedralModel< gpu::cuda::CudaVectorTypes<TCoord,TDeriv,TReal> >::drawTransparent()
00080 {
00081     //if (!getContext()->getShowVisualModels()) return;
00082 
00083 //  glDisable(GL_CULL_FACE);
00084 //  glBegin(GL_LINES_ADJACENCY_EXT);
00085 //      glVertex3f(5.0,0.0,0.0);
00086 //      glVertex3f(0.0,0.0,0.0);
00087 //      glVertex3f(0.0,-5.0,0.0);
00088 //      glVertex3f(2.5,-2.5,-3.0);
00089 //  glEnd();
00090     
00091     if(blending.getValue())
00092         glEnable(GL_BLEND);
00093     if(depthTest.getValue())
00094         glDepthMask(GL_FALSE);
00095     
00096     glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
00097     
00098 #ifdef GL_LINES_ADJACENCY_EXT
00099     VecCoord& x = *nodes->getX();
00100 
00101     bool vbo = useVBO.getValue();
00102 
00103     GLuint vbo_x = vbo ? x.bufferRead(true) : 0;
00104     if (vbo_x)
00105     {
00106     glBindBuffer(GL_ARRAY_BUFFER, vbo_x);
00107     glVertexPointer (3, (sizeof(Real)==sizeof(double))?GL_DOUBLE:GL_FLOAT, sizeof(Coord), NULL);
00108     }
00109     else
00110     glVertexPointer (3, (sizeof(Real)==sizeof(double))?GL_DOUBLE:GL_FLOAT, sizeof(Coord), x.hostRead());
00111 
00112     glEnableClientState(GL_VERTEX_ARRAY);
00113     glDisableClientState(GL_NORMAL_ARRAY);
00114     glDisableClientState(GL_COLOR_ARRAY);
00115     glDisableClientState(GL_TEXTURE_COORD_ARRAY);
00116 
00117     if (tetras.size() > 0)
00118     {
00119     GLuint vbo_t = vbo ? tetras.bufferRead(true) : 0;
00120     if (vbo_t)
00121     {
00122         glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbo_t);
00123         glDrawElements(GL_LINES_ADJACENCY_EXT, tetras.size() * 4, GL_UNSIGNED_INT, NULL);
00124     }
00125     else
00126         glDrawElements(GL_LINES_ADJACENCY_EXT, tetras.size() * 4, GL_UNSIGNED_INT, tetras.hostRead());
00127     }
00128 
00129     if (vbo)
00130     {
00131     glBindBuffer(GL_ARRAY_BUFFER, 0);
00132     glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
00133     }
00134 #endif
00135     glDisable(GL_BLEND);
00136     glDepthMask(GL_TRUE);
00137 }
00138 
00139 template<class TCoord, class TDeriv, class TReal>
00140 bool OglTetrahedralModel< gpu::cuda::CudaVectorTypes<TCoord,TDeriv,TReal> >::addBBox(double* minBBox, double* maxBBox)
00141 {
00142     const core::componentmodel::topology::BaseMeshTopology::SeqTetras& vec = topo->getTetras();
00143     core::componentmodel::topology::BaseMeshTopology::SeqTetras::const_iterator it;
00144     VecCoord& x = *nodes->getX();
00145     Coord v;
00146     
00147     for(it = vec.begin() ; it != vec.end() ; it++)
00148     {
00149             for (unsigned int i=0 ; i< 4 ; i++){
00150                 v = x[(*it)[i]];
00151                 
00152                 if (minBBox[0] > v[0]) minBBox[0] = v[0];
00153                 if (minBBox[1] > v[1]) minBBox[1] = v[1];
00154                 if (minBBox[2] > v[2]) minBBox[2] = v[2];
00155                 if (maxBBox[0] < v[0]) maxBBox[0] = v[0];
00156                 if (maxBBox[1] < v[1]) maxBBox[1] = v[1];
00157                 if (maxBBox[2] < v[2]) maxBBox[2] = v[2];
00158             }
00159     }
00160     return true;
00161 }
00162 
00163 }
00164 }
00165 }
00166 
00167 #endif //OGLTETRAHEDRALMODEL_H_

Generated on Sun Nov 23 04:03:20 2008 for Sofa by  doxygen 1.5.7.1