PeriDyno 1.0.0
Loading...
Searching...
No Matches
VtkFluidVisualModule.cpp
Go to the documentation of this file.
2
3// framework
4#include <Node.h>
5#include <SceneGraph.h>
6#include <Topology/TriangleSet.h>
7
8#include <vtkActor.h>
9#include <vtkProperty.h>
10#include <vtkRenderer.h>
11#include <vtkOpenGLVertexBufferObjectGroup.h>
12#include <vtkPolyData.h>
13#include <vtkOpenGLVertexBufferObject.h>
14#include <vtkOpenGLIndexBufferObject.h>
15#include <vtkVolume.h>
16
17#include <vtkOpenGLFluidMapper.h>
18
19#include "SceneGraphFactory.h"
20
21#ifdef _WIN32
22 #include <windows.h>
23#endif
24#include <cuda_gl_interop.h>
25
26using namespace dyno;
27
28class FluidMapper : public vtkOpenGLFluidMapper
29{
30public:
32 {
33 // create psedo data, required by the vtkOpenGLPolyDataMapper to render content
34 vtkNew<vtkPoints> points;
35
37 Vec3f bbox0 = scn->getLowerBound();
38 Vec3f bbox1 = scn->getUpperBound();
39 points->InsertNextPoint(bbox0[0], bbox0[1], bbox0[2]);
40 points->InsertNextPoint(bbox1[0], bbox1[1], bbox1[2]);
41
42 vtkNew<vtkPolyData> polyData;
43 polyData->SetPoints(points);
44 SetInputData(polyData);
45 }
46
47 void Update() override
48 {
49 vtkOpenGLFluidMapper::Update();
50
51 // hack for VBO update...
52 vtkOpenGLVertexBufferObject* vertexBuffer = this->VBOs->GetVBO("vertexMC");
53
54 // check whether the vertex buffer is ready...
55 if (vertexBuffer == NULL)
56 return;
57
58 //printf("update\n");
59 // update the VBO build time, so vtk framework will not write to VBO
60 this->VBOBuildTime.Modified();
61
62 if (!m_module->isInitialized())
63 return;
64
65 if (m_module->isDirty())
66 {
67 auto node = m_module->getParent();
68 //auto pSet = std::dynamic_pointer_cast<dyno::PointSet<dyno::DataType3f>>(node->getTopologyModule());
69 auto pSet = m_module->inPointSet()->getDataPtr();
70 auto verts = pSet->getPoints();
71
72 cudaError_t error;
73
74 // only initialize once
75 if (!m_initialized)
76 {
77 printf("Intialize\n");
78 m_initialized = true;
79
80 vtkNew<vtkPoints> points;
81 points->SetNumberOfPoints(verts.size());
82 vertexBuffer->UploadDataArray(points->GetData());
83
84 // create memory mapper for CUDA
85 error = cudaGraphicsGLRegisterBuffer(&m_cudaVBO, vertexBuffer->GetHandle(), cudaGraphicsRegisterFlagsWriteDiscard);
86 //printf("%s\n", cudaGetErrorName(error));
87 }
88
89 // copy vertex memory
90 {
91 size_t size;
92 void* cudaPtr = 0;
93
94 // upload vertex
95 error = cudaGraphicsMapResources(1, &m_cudaVBO);
96 //printf("%s\n", cudaGetErrorName(error));
97 error = cudaGraphicsResourceGetMappedPointer(&cudaPtr, &size, m_cudaVBO);
98 //printf("%s\n", cudaGetErrorName(error));
99 error = cudaMemcpy(cudaPtr, verts.begin(), verts.size() * sizeof(float) * 3, cudaMemcpyDeviceToDevice);
100 //printf("%s\n", cudaGetErrorName(error));
101 error = cudaGraphicsUnmapResources(1, &m_cudaVBO);
102 //printf("%s\n", cudaGetErrorName(error));
103 }
104
106 vtkIdType numPts = verts.size();
107 this->GLHelperDepthThickness.IBO->IndexCount = static_cast<size_t>(numPts);
108 }
109
110
111 }
112
113
114
115private:
117 cudaGraphicsResource* m_cudaVBO;
118 bool m_initialized = false;
119
120};
121
123
125{
126 this->setName("fluid_renderer");
127 m_volume = vtkVolume::New();
128
129 FluidMapper* fluidMapper = new FluidMapper(this);
130
131 fluidMapper->SetParticleRadius(0.01f);
132 fluidMapper->SetSurfaceFilterIterations(3);
133 fluidMapper->SetSurfaceFilterRadius(3);
134
135 fluidMapper->SetSurfaceFilterMethod(vtkOpenGLFluidMapper::FluidSurfaceFilterMethod::NarrowRange);
136 fluidMapper->SetDisplayMode(vtkOpenGLFluidMapper::FluidDisplayMode::TransparentFluidVolume);
137 fluidMapper->SetAttenuationColor(0.8f, 0.2f, 0.15f);
138 fluidMapper->SetAttenuationScale(1.0f);
139 fluidMapper->SetOpaqueColor(0.0f, 0.0f, 0.9f);
140 fluidMapper->SetParticleColorPower(0.1f);
141 fluidMapper->SetParticleColorScale(0.57f);
142 fluidMapper->SetAdditionalReflection(0.0f);
143 fluidMapper->SetRefractiveIndex(1.33f);
144 fluidMapper->SetRefractionScale(0.07f);
145
146 m_volume->SetMapper(fluidMapper);
147}
148
#define IMPLEMENT_CLASS_COMMON(name, func)
Definition Object.h:73
void Update() override
cudaGraphicsResource * m_cudaVBO
FluidMapper(VtkFluidVisualModule *v)
dyno::VtkFluidVisualModule * m_module
void setName(std::string name)
Definition Module.cpp:187
static SceneGraphFactory * instance()
std::shared_ptr< SceneGraph > active()
This is an implementation of AdditiveCCD based on peridyno.
Definition Array.h:25
Vector< float, 3 > Vec3f
Definition Vector3D.h:93