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 #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
00082
00083
00084
00085
00086
00087
00088
00089
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_