Skip to content

Commit

Permalink
Merge pull request #1194 from fredroy/add_rendersph
Browse files Browse the repository at this point in the history
[SofaSphFluid] Add sprite-based point render
  • Loading branch information
epernod committed Oct 25, 2019
2 parents a625bbc + 2760685 commit 55c4930
Show file tree
Hide file tree
Showing 13 changed files with 1,449 additions and 6 deletions.
9 changes: 8 additions & 1 deletion applications/plugins/SofaSphFluid/CMakeLists.txt
Expand Up @@ -2,6 +2,7 @@ cmake_minimum_required(VERSION 3.1)
project(SofaSphFluid VERSION 1.0)

find_package(SofaBase REQUIRED)
sofa_find_package(SofaOpenglVisual QUIET)

set(PLUGIN_SPH_SRC_DIR src/SofaSphFluid)
set(HEADER_FILES
Expand All @@ -17,7 +18,9 @@ set(HEADER_FILES
${PLUGIN_SPH_SRC_DIR}/SPHFluidSurfaceMapping.h
${PLUGIN_SPH_SRC_DIR}/SPHFluidSurfaceMapping.inl
${PLUGIN_SPH_SRC_DIR}/SpatialGridContainer.h
${PLUGIN_SPH_SRC_DIR}/SpatialGridContainer.inl
${PLUGIN_SPH_SRC_DIR}/SpatialGridContainer.inl
${PLUGIN_SPH_SRC_DIR}/OglFluidModel.h
${PLUGIN_SPH_SRC_DIR}/OglFluidModel.inl
)

set(SOURCE_FILES
Expand All @@ -28,6 +31,7 @@ set(SOURCE_FILES
${PLUGIN_SPH_SRC_DIR}/SPHFluidForceField.cpp
${PLUGIN_SPH_SRC_DIR}/SPHFluidSurfaceMapping.cpp
${PLUGIN_SPH_SRC_DIR}/SpatialGridContainer.cpp
${PLUGIN_SPH_SRC_DIR}/OglFluidModel.cpp
)

set(README_FILES
Expand All @@ -43,6 +47,9 @@ target_compile_definitions(${PROJECT_NAME} PRIVATE "-DSOFA_BUILD_SPH_FLUID")
# Link the plugin library to its dependencies (other libraries).
target_link_libraries(${PROJECT_NAME} SofaBaseTopology SofaBaseMechanics)

if(SofaOpenglVisual_FOUND)
target_link_libraries(${PROJECT_NAME} SofaOpenglVisual)
endif()

target_include_directories(${PROJECT_NAME} PUBLIC "$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src>")
target_include_directories(${PROJECT_NAME} PUBLIC "$<BUILD_INTERFACE:${CMAKE_BINARY_DIR}/include>")
Expand Down
29 changes: 29 additions & 0 deletions applications/plugins/SofaSphFluid/examples/OglFluidModel_SPH.scn
@@ -0,0 +1,29 @@
<Node dt="0.01" gravity="0 -20 0.0" >
<RequiredPlugin name="SofaOpenglVisual"/>
<RequiredPlugin pluginName="SofaSphFluid"/>

<VisualStyle displayFlags="hideBehaviorModels hideForceFields hideCollisionModels" />


<Node name="SPH" >

<EulerExplicit symplectic="1" />
<RegularGrid nx="5" ny="400" nz="5" xmin="-3.0" xmax="0" ymin="-3" ymax="36" zmin="-3.0" zmax="0" />
<MechanicalObject name="MModel" />
<!-- A topology is used here just to set initial particles positions. It is a bad idea because this object has no real topology, but it works... -->
<UniformMass name="M1" vertexMass="1" />
<SpatialGridContainer cellWidth="0.75" />
<SPHFluidForceField radius="0.745" density="15" kernelType="1" viscosityType="2" viscosity="10" pressure="1000" surfaceTension="-1000" printLog="0" />
<!-- The following force fields handle collision with walls and an inclined floor -->
<PlaneForceField normal="1 0 0" d="-4" />
<PlaneForceField normal="-1 0 0" d="-4" />
<PlaneForceField normal="0.5 1 0.1" d="-4" />
<PlaneForceField normal="0 0 1" d="-10" />
<PlaneForceField normal="0 0 -1" d="-10" />
</Node>
<Node name="Fluid" >
<OglFluidModel template="Vec3d" position="@../SPH/MModel.position"
debugFBO="9"
spriteRadius="0.5" spriteThickness="0.015" spriteBlurRadius="10" spriteBlurScale="10" spriteBlurDepthFalloff="1" />
</Node>
</Node>
@@ -0,0 +1,19 @@
<?xml version="1.0" ?>
<Camera version="1.0">
<!--Vector of 3 reals (x, y, z)-->
<position value="11.1214 19.2346 7.1181" />
<!--Quaternion (x, y, z, w)-->
<orientation value="-0.414638 0.447079 0.306899 0.730759" />
<!--Vector of 3 reals (x, y, z)-->
<lookAt value="3.64651 2.7371 2.31377" />
<!--Real-->
<fieldOfView value="45" />
<!--Real-->
<distance value="18.7383" />
<!--Real-->
<zNear value="1.76364" />
<!--Real-->
<zFar value="53.5667" />
<!--Int (0 -> Perspective, 1 -> Orthographic)-->
<projectionType value="Perspective" />
</Camera>
@@ -0,0 +1,21 @@
#include <SofaSphFluid/OglFluidModel.inl>

#include <sofa/core/ObjectFactory.h>

namespace sofa
{
namespace component
{
namespace visualmodel
{

SOFA_DECL_CLASS(OglFluidModel)

int OglFluidModelClass = sofa::core::RegisterObject("Particle model for OpenGL display, using glsl")
.add< OglFluidModel<sofa::defaulttype::Vec3Types> >();

template class SOFA_SPH_FLUID_API OglFluidModel<sofa::defaulttype::Vec3Types>;

}
}
}
98 changes: 98 additions & 0 deletions applications/plugins/SofaSphFluid/src/SofaSphFluid/OglFluidModel.h
@@ -0,0 +1,98 @@
#pragma once

#include <SofaSphFluid/config.h>

#include <sofa/core/visual/VisualModel.h>
#include <sofa/helper/gl/FrameBufferObject.h>
#include <sofa/helper/gl/GLSLShader.h>
#include <sofa/defaulttype/VecTypes.h>
#include <sofa/defaulttype/RGBAColor.h>

namespace sofa
{
namespace component
{
namespace visualmodel
{
using namespace sofa::defaulttype;
/**
* \brief Render volume using particles
*
*/

// http://developer.download.nvidia.com/presentations/2010/gdc/Direct3D_Effects.pdf

template<class DataTypes>
class SOFA_SPH_FLUID_API OglFluidModel : public core::visual::VisualModel
{
public:
SOFA_CLASS(OglFluidModel, core::visual::VisualModel);
//typedef ExtVec3fTypes DataTypes;
typedef typename DataTypes::Coord Coord;
typedef typename DataTypes::VecCoord VecCoord;
typedef typename DataTypes::Deriv Deriv;
typedef typename DataTypes::VecDeriv VecDeriv;
typedef typename DataTypes::Real Real;

private:
Data< VecCoord > m_positions;
VecCoord m_previousPositions;

GLuint m_posVBO;
helper::gl::FrameBufferObject* m_spriteDepthFBO;
helper::gl::FrameBufferObject* m_spriteThicknessFBO;
helper::gl::FrameBufferObject* m_spriteNormalFBO;
helper::gl::FrameBufferObject* m_spriteBlurDepthHFBO;
helper::gl::FrameBufferObject* m_spriteBlurDepthVFBO;
helper::gl::FrameBufferObject* m_spriteBlurThicknessHFBO;
helper::gl::FrameBufferObject* m_spriteBlurThicknessVFBO;
helper::gl::FrameBufferObject* m_spriteShadeFBO;

helper::gl::GLSLShader m_spriteShader;
helper::gl::GLSLShader m_spriteNormalShader;
helper::gl::GLSLShader m_spriteBlurDepthShader;
helper::gl::GLSLShader m_spriteBlurThicknessShader;
helper::gl::GLSLShader m_spriteShadeShader;

void drawSprites(const core::visual::VisualParams* vparams);
void updateVertexBuffer();
protected:
OglFluidModel();
virtual ~OglFluidModel();
public:
Data<unsigned int> d_debugFBO;
Data<float> d_spriteRadius;
Data<float> d_spriteThickness;
Data<unsigned int> d_spriteBlurRadius;
Data<float> d_spriteBlurScale;
Data<float> d_spriteBlurDepthFalloff;
Data<sofa::defaulttype::RGBAColor> d_spriteDiffuseColor;


void init();
void initVisual();
void fwdDraw(core::visual::VisualParams*);
void bwdDraw(core::visual::VisualParams*);
void drawVisual(const core::visual::VisualParams* vparams);
void computeBBox(const core::ExecParams* params, bool onlyVisible = false);

virtual void updateVisual();

static std::string templateName(const OglFluidModel<DataTypes>* = NULL)
{
return DataTypes::Name();
}

virtual std::string getTemplateName() const
{
return templateName(this);
}

};

}

}

}

0 comments on commit 55c4930

Please sign in to comment.