3#include "Mapping/HeightFieldToTriangleSet.h"
5#include "GLSurfaceVisualModule.h"
9 template<typename TDataType>
10 Ocean<TDataType>::Ocean()
11 : OceanBase<TDataType>()
13 auto heights = std::make_shared<HeightField<TDataType>>();
14 this->stateHeightField()->setDataPtr(heights);
16 auto mapper = std::make_shared<HeightFieldToTriangleSet<DataType3f>>();
17 this->stateHeightField()->connect(mapper->inHeightField());
18 this->graphicsPipeline()->pushModule(mapper);
20 auto sRender = std::make_shared<GLSurfaceVisualModule>();
21 sRender->setColor(Color(0, 0.2, 1.0));
22 sRender->varUseVertexNormal()->setValue(true);
23 mapper->outTriangleSet()->connect(sRender->inTriangleSet());
24 this->graphicsPipeline()->pushModule(sRender);
27 template<typename TDataType>
28 Ocean<TDataType>::~Ocean()
30 this->varExtentX()->setRange(1, 10);
31 this->varExtentZ()->setRange(1, 10);
34 template<typename TDataType>
35 void Ocean<TDataType>::resetStates()
37 auto patch = this->getOceanPatch();
39 auto Nx = this->varExtentX()->getValue();
40 auto Nz = this->varExtentZ()->getValue();
42 auto patchHeights = patch->stateHeightField()->getDataPtr();
43 auto oceanHeights = this->stateHeightField()->getDataPtr();
45 Real h = patchHeights->getGridSpacing();
46 oceanHeights->setExtents(Nx * patchHeights->width(), Nz * patchHeights->height());
47 oceanHeights->setGridSpacing(h);
48 oceanHeights->setOrigin(patchHeights->getOrigin());
50 Real level = this->varWaterLevel()->getValue();
52 //Initialize the height field for the ocean
53 DArray2D<Coord>& patchDisp = patchHeights->getDisplacement();
54 cuExecute2D(make_uint2(patchDisp.nx(), patchDisp.ny()),
56 oceanHeights->getDisplacement(),
61 template<typename Real, typename Coord>
62 __global__ void O_InitOceanWave(
63 DArray2D<Coord> oceanVertex,
64 DArray2D<Coord> displacement,
65 Real level) //Water level
67 int i = threadIdx.x + blockIdx.x * blockDim.x;
68 int j = threadIdx.y + blockIdx.y * blockDim.y;
70 int width = displacement.nx();
71 int height = displacement.ny();
73 if (i < width && j < height)
75 Coord D_ij = displacement(i, j);
77 int tiledX = oceanVertex.nx() / displacement.nx();
78 int tiledY = oceanVertex.ny() / displacement.ny();
79 for (int t = 0; t < tiledX; t++)
81 for (int s = 0; s < tiledY; s++)
83 int nx = i + t * width;
84 int ny = j + s * height;
86 oceanVertex(nx, ny) = D_ij;
87 oceanVertex(nx, ny).y += level;
93 template<typename Real, typename Coord>
94 __global__ void O_AddOceanTrails(
95 DArray2D<Coord> oceanVertex,
96 DArray2D<Coord> waveDisp,
99 int i = threadIdx.x + blockIdx.x * blockDim.x;
100 int j = threadIdx.y + blockIdx.y * blockDim.y;
102 int width = waveDisp.nx();
103 int height = waveDisp.ny();
105 if (i < width && j < height)
107 Coord D_ij = waveDisp(i, j);
111 int tiledX = oceanVertex.nx() / waveDisp.nx();
112 int tiledY = oceanVertex.ny() / waveDisp.ny();
114 //TODO: correct the position
118 oceanVertex(nx, ny) += D_ij;
122 template<typename TDataType>
123 void Ocean<TDataType>::updateStates()
125 Real level = this->varWaterLevel()->getValue();
127 auto patch = this->getOceanPatch();
129 auto patchHeights = patch->stateHeightField()->getDataPtr();
131 auto oceanHeights = this->stateHeightField()->getDataPtr();
133 //Initialize the height field for the ocean
134 DArray2D<Coord>& patchDisp = patchHeights->getDisplacement();
135 cuExecute2D(make_uint2(patchDisp.nx(), patchDisp.ny()),
137 oceanHeights->getDisplacement(),
141 //Add capillary waves
142 auto& waves = this->getCapillaryWaves();
143 for (int i = 0; i < waves.size(); i++) {
144 auto wave = waves[i]->stateHeightField()->getDataPtr();
145 auto h = waves[i]->varWaterLevel()->getValue();
147 auto& waveDisp = wave->getDisplacement();
149 cuExecute2D(make_uint2(waveDisp.nx(), waveDisp.ny()),
151 oceanHeights->getDisplacement(),