CudaVisualModel.h

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 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 } // namespace cuda
00046 
00047 } // namespace gpu
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     //typedef gpu::cuda::CudaVectorTypes<TCoord,TDeriv,TReal> DataTypes;
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 // start of the bloc
00135               + num*BSIZE                     // offset to the element
00136               + b_x                           // offset to the vertex
00137               ] = index+1;
00138     }
00139 };
00140 
00141 } // namespace visualmodel
00142 
00143 } // namespace component
00144 
00145 } // namespace sofa
00146 
00147 #endif

Generated on Sun Nov 23 04:05:45 2008 for Sofa by  doxygen 1.5.7.1