PeriDyno 1.0.0
Loading...
Searching...
No Matches
LandScape.cu
Go to the documentation of this file.
1#include "LandScape.h"
2
3#include "Topology/HeightField.h"
4
5#include "Mapping/HeightFieldToTriangleSet.h"
6#include "GLSurfaceVisualModule.h"
7
8#define STB_IMAGE_IMPLEMENTATION
9#include <stb/stb_image.h>
10
11namespace dyno
12{
13 template<typename TDataType>
14 LandScape<TDataType>::LandScape()
15 : ParametricModel<TDataType>()
16 {
17 auto heights = std::make_shared<HeightField<TDataType>>();
18 this->stateHeightField()->setDataPtr(heights);
19
20 auto mapper = std::make_shared<HeightFieldToTriangleSet<DataType3f>>();
21 //mapper->varTranslation()->setValue(Vec3f(-128.0f, -5.0f, -128.0f));
22 this->stateHeightField()->promoteOuput()->connect(mapper->inHeightField());
23 this->graphicsPipeline()->pushModule(mapper);
24
25 auto sRender = std::make_shared<GLSurfaceVisualModule>();
26 //sRender->setColor(Color(0.57f, 0.4f, 0.3f));
27 sRender->varUseVertexNormal()->setValue(true);
28 mapper->outTriangleSet()->connect(sRender->inTriangleSet());
29 this->graphicsPipeline()->pushModule(sRender);
30
31 auto callback = std::make_shared<FCallBackFunc>(std::bind(&LandScape<TDataType>::callbackTransform, this));
32
33 this->varLocation()->attach(callback);
34 this->varScale()->attach(callback);
35 this->varRotation()->attach(callback);
36
37 auto callbackLoadFile = std::make_shared<FCallBackFunc>(std::bind(&LandScape<TDataType>::callbackLoadFile, this));
38 this->varFileName()->attach(callbackLoadFile);
39 }
40
41 template<typename TDataType>
42 LandScape<TDataType>::~LandScape()
43 {
44 }
45
46 template<typename TDataType>
47 void LandScape<TDataType>::resetStates()
48 {
49 callbackTransform();
50 }
51
52 template<typename Real, typename Coord>
53 __global__ void LS_Transform(
54 DArray2D<Coord> disp,
55 DArray2D<Real> heights,
56 Coord scale)
57 {
58 unsigned int i = blockIdx.x * blockDim.x + threadIdx.x;
59 unsigned int j = blockIdx.y * blockDim.y + threadIdx.y;
60
61 uint nx = heights.nx();
62 uint ny = heights.ny();
63 if (i < nx && j < ny)
64 {
65 Real y = heights(i, j);
66
67 disp(i, j) = Coord(0, y * scale.y, 0);
68 }
69 }
70
71 template<typename TDataType>
72 void LandScape<TDataType>::callbackTransform()
73 {
74 auto scale = this->varScale()->getValue();
75 auto loc = this->varLocation()->getValue();
76
77 auto topo = this->stateHeightField()->getDataPtr();
78
79 uint nx = mInitialHeights.nx();
80 uint nz = mInitialHeights.ny();
81
82 topo->setExtents(mInitialHeights.nx(), mInitialHeights.ny());
83 topo->setGridSpacing(1);
84 topo->setOrigin(Coord(-0.5 * nx * scale.x + loc.x, loc.y, -0.5 * nz * scale.z + loc.z));
85
86 auto& disp = topo->getDisplacement();
87
88 cuExecute2D(make_uint2(mInitialHeights.nx(), mInitialHeights.ny()),
89 LS_Transform,
90 disp,
91 mInitialHeights,
92 scale);
93 }
94
95 template<typename TDataType>
96 void LandScape<TDataType>::callbackLoadFile()
97 {
98 const std::string& mapPath = this->varFileName()->getValue().string();
99 if (mapPath != fileName) {
100 fileName = mapPath;
101
102 int w, h, comp;
103 stbi_set_flip_vertically_on_load(true);
104
105 float* data = stbi_loadf(fileName.c_str(), &w, &h, &comp, STBI_default);
106
107 CArray2D<Real> hLand(w, h);
108
109 for (int x0 = 0; x0 < w; x0++)
110 {
111 for (int y0 = 0; y0 < h; y0++)
112 {
113 int idx = (y0 * w + x0) * comp;
114
115 hLand(x0, y0) = data[idx];
116 }
117 }
118
119 mInitialHeights.assign(hLand);
120
121 delete data;
122 }
123 }
124
125 DEFINE_CLASS(LandScape);
126}