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 SOFA_GPU_CUDA_CUDAVISUALMODEL_H
00026 #define SOFA_GPU_CUDA_CUDAVISUALMODEL_H
00027
00028 #include <sofa/core/VisualModel.h>
00029 #include <sofa/core/componentmodel/topology/BaseMeshTopology.h>
00030 #include <sofa/core/componentmodel/behavior/State.h>
00031 #include "CudaTypes.h"
00032
00033 namespace sofa
00034 {
00035
00036 namespace gpu
00037 {
00038
00039 namespace cuda
00040 {
00041
00042 template<class DataTypes>
00043 class CudaKernelsCudaVisualModel;
00044
00045 }
00046
00047 }
00048
00049
00050 namespace component
00051 {
00052
00053 namespace visualmodel
00054 {
00055
00056 template <class TDataTypes>
00057 class CudaVisualModel : public core::VisualModel
00058 {
00059 public:
00060
00061 typedef TDataTypes DataTypes;
00062 typedef typename DataTypes::VecCoord VecCoord;
00063 typedef typename DataTypes::VecDeriv VecDeriv;
00064 typedef typename DataTypes::Coord Coord;
00065 typedef typename DataTypes::Deriv Deriv;
00066 typedef typename DataTypes::Real Real;
00067
00068 typedef core::componentmodel::topology::BaseMeshTopology::PointID Index;
00069 typedef core::componentmodel::topology::BaseMeshTopology::Triangle Triangle;
00070 typedef core::componentmodel::topology::BaseMeshTopology::SeqTriangles SeqTriangles;
00071 typedef core::componentmodel::topology::BaseMeshTopology::Quad Quad;
00072 typedef core::componentmodel::topology::BaseMeshTopology::SeqQuads SeqQuads;
00073
00074 typedef core::componentmodel::behavior::State<DataTypes> TState;
00075
00076 typedef gpu::cuda::CudaKernelsCudaVisualModel<DataTypes> Kernels;
00077
00078 TState* state;
00079 core::componentmodel::topology::BaseMeshTopology* topology;
00080 bool needUpdateTopology;
00081 gpu::cuda::CudaVector<Triangle> triangles;
00082 gpu::cuda::CudaVector<Quad> quads;
00083
00084 VecCoord fnormals;
00085 VecCoord vnormals;
00086
00087 int nbElement;
00088 int nbVertex;
00089 int nbElementPerVertex;
00090
00091 gpu::cuda::CudaVector<int> velems;
00092
00093 Data<defaulttype::Vec4f> matAmbient, matDiffuse, matSpecular, matEmissive;
00094 Data<float> matShininess;
00095 Data<bool> useVBO;
00096 Data<bool> computeNormals;
00097
00098 CudaVisualModel()
00099 : state(NULL), topology(NULL), needUpdateTopology(true), nbElement(0), nbVertex(0), nbElementPerVertex(0)
00100 , matAmbient ( initData( &matAmbient, defaulttype::Vec4f(0.1f,0.1f,0.1f,0.0f), "ambient", "material ambient color") )
00101 , matDiffuse ( initData( &matDiffuse, defaulttype::Vec4f(0.8f,0.8f,0.8f,1.0f), "diffuse", "material diffuse color and alpha") )
00102 , matSpecular ( initData( &matSpecular, defaulttype::Vec4f(1.0f,1.0f,1.0f,0.0f), "specular", "material specular color") )
00103 , matEmissive ( initData( &matEmissive, defaulttype::Vec4f(0.0f,0.0f,0.0f,0.0f), "emissive", "material emissive color") )
00104 , matShininess( initData( &matShininess, 45.0f, "shininess", "material specular shininess") )
00105 , useVBO( initData( &useVBO, false, "useVBO", "true to activate Vertex Buffer Object") )
00106 , computeNormals( initData( &computeNormals, false, "computeNormals", "true to compute smooth normals") )
00107 {}
00108
00109 virtual void init();
00110 virtual void reinit();
00111 virtual void drawVisual();
00112 virtual void updateVisual();
00113
00114 protected:
00115
00116 void updateTopology();
00117 void updateNormals();
00118
00119 void initV(int nbe, int nbv, int nbelemperv)
00120 {
00121 nbElement = nbe;
00122 nbVertex = nbv;
00123 nbElementPerVertex = nbelemperv;
00124 int nbloc = (nbVertex+BSIZE-1)/BSIZE;
00125 velems.resize(nbloc*nbElementPerVertex*BSIZE);
00126 for (unsigned int i=0;i<velems.size();i++)
00127 velems[i] = 0;
00128 }
00129
00130 void setV(int vertex, int num, int index)
00131 {
00132 int bloc = vertex/BSIZE;
00133 int b_x = vertex%BSIZE;
00134 velems[ bloc*BSIZE*nbElementPerVertex
00135 + num*BSIZE
00136 + b_x
00137 ] = index+1;
00138 }
00139 };
00140
00141 }
00142
00143 }
00144
00145 }
00146
00147 #endif