3#include "Topology/HeightField.h"
5#include "Mapping/HeightFieldToTriangleSet.h"
6#include "GLSurfaceVisualModule.h"
8#define STB_IMAGE_IMPLEMENTATION
9#include <stb/stb_image.h>
13 template<typename TDataType>
14 LandScape<TDataType>::LandScape()
15 : ParametricModel<TDataType>()
17 auto heights = std::make_shared<HeightField<TDataType>>();
18 this->stateHeightField()->setDataPtr(heights);
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);
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);
31 auto callback = std::make_shared<FCallBackFunc>(std::bind(&LandScape<TDataType>::callbackTransform, this));
33 this->varLocation()->attach(callback);
34 this->varScale()->attach(callback);
35 this->varRotation()->attach(callback);
37 auto callbackLoadFile = std::make_shared<FCallBackFunc>(std::bind(&LandScape<TDataType>::callbackLoadFile, this));
38 this->varFileName()->attach(callbackLoadFile);
41 template<typename TDataType>
42 LandScape<TDataType>::~LandScape()
46 template<typename TDataType>
47 void LandScape<TDataType>::resetStates()
52 template<typename Real, typename Coord>
53 __global__ void LS_Transform(
55 DArray2D<Real> heights,
58 unsigned int i = blockIdx.x * blockDim.x + threadIdx.x;
59 unsigned int j = blockIdx.y * blockDim.y + threadIdx.y;
61 uint nx = heights.nx();
62 uint ny = heights.ny();
65 Real y = heights(i, j);
67 disp(i, j) = Coord(0, y * scale.y, 0);
71 template<typename TDataType>
72 void LandScape<TDataType>::callbackTransform()
74 auto scale = this->varScale()->getValue();
75 auto loc = this->varLocation()->getValue();
77 auto topo = this->stateHeightField()->getDataPtr();
79 uint nx = mInitialHeights.nx();
80 uint nz = mInitialHeights.ny();
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));
86 auto& disp = topo->getDisplacement();
88 cuExecute2D(make_uint2(mInitialHeights.nx(), mInitialHeights.ny()),
95 template<typename TDataType>
96 void LandScape<TDataType>::callbackLoadFile()
98 const std::string& mapPath = this->varFileName()->getValue().string();
99 if (mapPath != fileName) {
103 stbi_set_flip_vertically_on_load(true);
105 float* data = stbi_loadf(fileName.c_str(), &w, &h, &comp, STBI_default);
107 CArray2D<Real> hLand(w, h);
109 for (int x0 = 0; x0 < w; x0++)
111 for (int y0 = 0; y0 < h; y0++)
113 int idx = (y0 * w + x0) * comp;
115 hLand(x0, y0) = data[idx];
119 mInitialHeights.assign(hLand);
125 DEFINE_CLASS(LandScape);