-
Notifications
You must be signed in to change notification settings - Fork 0
/
OgrePagedGeometrySample.cpp
239 lines (202 loc) · 8.37 KB
/
OgrePagedGeometrySample.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#include "OgrePagedGeometrySample.h"
void OgrePagedGeometrySampleApp::createTerrain()
{
using namespace Ogre;
mTerrainGlobals = OGRE_NEW TerrainGlobalOptions();
mTerrainGroup = OGRE_NEW TerrainGroup(mSceneMgr, Terrain::ALIGN_X_Z, 513,3200);
mTerrainGroup->setOrigin(Ogre::Vector3(mTerrainGroup->getTerrainWorldSize() / 2,0,mTerrainGroup->getTerrainWorldSize() / 2));
// Configure global
mTerrainGlobals->setMaxPixelError(8);
// testing composite map
mTerrainGlobals->setCompositeMapDistance(3000);
//matProfile->setLightmapEnabled(false);
// Important to set these so that the terrain knows what to use for derived (non-realtime) data
//mTerrainGlobals->setLightMapDirection(mLight->getDerivedDirection());
Ogre::Light * light = mSceneMgr->getLight("Sun");
mTerrainGlobals->setCompositeMapAmbient(mSceneMgr->getAmbientLight());
//mTerrainGlobals->setCompositeMapAmbient(ColourValue::Red);
mTerrainGlobals->setCompositeMapDiffuse(light->getDiffuseColour());
mTerrainGlobals->setLightMapDirection(light->getDerivedDirection());
// Configure default import settings for if we use imported image
Terrain::ImportData& defaultimp = mTerrainGroup->getDefaultImportSettings();
defaultimp.terrainSize = 513;
defaultimp.worldSize = 3200;
defaultimp.inputScale = 80;
defaultimp.minBatchSize = 33;
defaultimp.maxBatchSize = 65;
// textures
defaultimp.layerList.resize(3);
defaultimp.layerList[0].worldSize = 513;
defaultimp.layerList[0].textureNames.push_back("dirt_grayrocky_diffusespecular.dds");
defaultimp.layerList[0].textureNames.push_back("dirt_grayrocky_normalheight.dds");
defaultimp.layerList[1].worldSize = 30;
defaultimp.layerList[1].textureNames.push_back("grass_green-01_diffusespecular.dds");
defaultimp.layerList[1].textureNames.push_back("grass_green-01_normalheight.dds");
defaultimp.layerList[2].worldSize = 200;
defaultimp.layerList[2].textureNames.push_back("growth_weirdfungus-03_diffusespecular.dds");
defaultimp.layerList[2].textureNames.push_back("growth_weirdfungus-03_normalheight.dds");
Image img;
img.load("densitymap.png", ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
mTerrainGroup->defineTerrain(0, 0,&img);
mTerrainGroup->loadAllTerrains(true);
TerrainGroup::TerrainIterator ti = mTerrainGroup->getTerrainIterator();
while(ti.hasMoreElements())
{
Terrain* terrain = ti.getNext()->instance;
TerrainLayerBlendMap* blendMap0 = terrain->getLayerBlendMap(1);
TerrainLayerBlendMap* blendMap1 = terrain->getLayerBlendMap(2);
Ogre::Real minHeight0 = 70;
Ogre::Real fadeDist0 = 40;
Ogre::Real minHeight1 = 70;
Ogre::Real fadeDist1 = 15;
float * pBlend0 = blendMap0->getBlendPointer();
float* pBlend1 = blendMap1->getBlendPointer();
for (Ogre::uint16 y = 0; y < terrain->getLayerBlendMapSize(); ++y)
{
for (Ogre::uint16 x = 0; x < terrain->getLayerBlendMapSize(); ++x)
{
Ogre::Real tx, ty;
blendMap0->convertImageToTerrainSpace(x, y, &tx, &ty);
Ogre::Real height = terrain->getHeightAtTerrainPosition(tx, ty);
Ogre::Real val = (height - minHeight0) / fadeDist0;
val = Ogre::Math::Clamp(val, (Ogre::Real)0, (Ogre::Real)1);
*pBlend0++ = val;
val = (height - minHeight1) / fadeDist1;
val = Ogre::Math::Clamp(val, (Ogre::Real)0, (Ogre::Real)1);
*pBlend1++ = val;
}
}
blendMap0->dirty();
blendMap1->dirty();
blendMap0->update();
blendMap1->update();
}
mTerrainGroup->freeTemporaryResources();
}
inline float getTerrainHeight(float x, float z, void *userData)
{
return ((Ogre::TerrainGroup *)userData)->getHeightAtWorldPosition(x,0,z) + 1.5;
}
void OgrePagedGeometrySampleApp::createScene()
{
mCameraMan->setTopSpeed(200);
mWindow->getViewport(0)->setBackgroundColour(ColourValue(0.47f, 0.67f, 0.96f));
mSceneMgr->setFog(FOG_LINEAR, mWindow->getViewport(0)->getBackgroundColour(), 0, 100, 700);
mSceneMgr->setSkyBox(true, "3D-Diggers/SkyBox", 2000);
Light *light = mSceneMgr->createLight("Sun");
light->setType(Light::LT_DIRECTIONAL);
light->setDirection(Vector3(0.0f, -0.5f, 1.0f));
mSceneMgr->setAmbientLight(ColourValue(1, 1, 1));
createTerrain();
createPagedGeometry();
}
void OgrePagedGeometrySampleApp::createPagedGeometry()
{
using namespace Forests;
using namespace Ogre;
//Trees
mTreesMgr = new PagedGeometry(mCameraMan->getCamera(),150);
mTreesMgr->addDetailLevel<WindBatchPage>(150,50);
mTreesMgr->addDetailLevel<ImpostorPage>(1000,100);
TreeLoader2D * treeLoader = new TreeLoader2D(mTreesMgr,TBounds(0,0,mTerrainGroup->getTerrainWorldSize(),mTerrainGroup->getTerrainWorldSize()));
mTreesMgr->setPageLoader(treeLoader);
treeLoader->setHeightFunction(&getTerrainHeight,(void *)mTerrainGroup);
treeLoader->setColorMap("terrain_lightmap.jpg");
Entity * tree1 = mSceneMgr->createEntity("tree1","tree2.mesh");
Entity * tree2 = mSceneMgr->createEntity("tree2","fir14_25.mesh");
mTreesMgr->setCustomParam(tree1->getName(), "windFactorX", 15);
mTreesMgr->setCustomParam(tree1->getName(), "windFactorY", 0.01f);
mTreesMgr->setCustomParam(tree2->getName(), "windFactorX", 32);
mTreesMgr->setCustomParam(tree2->getName(), "windFactorY", 0.01f);
Ogre::Degree yaw;
Ogre::Vector3 pos;
for (int i=0;i<2400;i++)
{
pos.x = Math::RangeRandom(0, mTerrainGroup->getTerrainWorldSize());
pos.z = Math::RangeRandom(0, mTerrainGroup->getTerrainWorldSize());
yaw = Degree(Math::RangeRandom(0,360));
if (i % 3 == 0)
{
treeLoader->addTree(tree1,pos,yaw,Math::RangeRandom(0.6f,0.9f));
}
else
{
treeLoader->addTree(tree1,pos,yaw);
}
}
//Bushes
mBushMgr = new PagedGeometry(mCameraMan->getCamera(),150);
mBushMgr->addDetailLevel<WindBatchPage>(100,50);
mBushMgr->addDetailLevel<ImpostorPage>(160,50);
TreeLoader2D * bushLoader = new TreeLoader2D(mBushMgr,TBounds(0,0,mTerrainGroup->getTerrainWorldSize(),mTerrainGroup->getTerrainWorldSize()));
mBushMgr->setPageLoader(bushLoader);
bushLoader->setColorMap("terrain_lightmap.jpg");
bushLoader->setHeightFunction(&getTerrainHeight,(void *)mTerrainGroup);
Entity *fern = mSceneMgr->createEntity("Fern", "farn1.mesh");
Entity *plant = mSceneMgr->createEntity("Plant", "plant2.mesh");
Entity *mushroom = mSceneMgr->createEntity("Mushroom", "shroom1_3.mesh");
mBushMgr->setCustomParam(fern->getName(), "factorX", 1);
mBushMgr->setCustomParam(fern->getName(), "factorY", 0.01);
mBushMgr->setCustomParam(plant->getName(), "factorX", 0.6);
mBushMgr->setCustomParam(plant->getName(), "factorY", 0.02);
for (int i = 0; i < 2000; i++)
{
yaw = Degree(Math::RangeRandom(0, 360));
pos.x = Math::RangeRandom(0, mTerrainGroup->getTerrainWorldSize());
pos.z = Math::RangeRandom(0, mTerrainGroup->getTerrainWorldSize());
if (i % 3 == 0)
{
bushLoader->addTree(fern,pos,yaw,Math::RangeRandom(0.6f,0.9f));
}
else if (i % 3 == 1)
{
bushLoader->addTree(plant,pos,yaw,Math::RangeRandom(0.6f,0.9f));
}
else
{
bushLoader->addTree(mushroom,pos,yaw,Math::RangeRandom(0.6f,0.9f));
}
}
//Grass
mGrassMgr = new PagedGeometry(mCameraMan->getCamera(),70);
mGrassMgr->addDetailLevel<GrassPage>(100,300);
GrassLoader * grassLoader = new GrassLoader(mGrassMgr);
mGrassMgr->setPageLoader(grassLoader);
grassLoader->setHeightFunction(&getTerrainHeight,(void *)mTerrainGroup);
GrassLayer * grassLayer = grassLoader->addLayer("grass");
grassLayer->setMinimumSize(2.0f, 2.0f);
grassLayer->setMaximumSize(2.5f, 2.5f);
grassLayer->setAnimationEnabled(true);
grassLayer->setSwayDistribution(7.0f);
grassLayer->setSwayLength(0.23f);
grassLayer->setSwaySpeed(0.5f);
grassLayer->setDensity(2.0f);
grassLayer->setRenderTechnique(GRASSTECH_QUAD);
grassLayer->setFadeTechnique(FADETECH_GROW);
grassLayer->setColorMap("terrain_texture2.jpg");
grassLayer->setDensityMap("densitymap.png");
grassLayer->setMapBounds(TBounds(0,0,mTerrainGroup->getTerrainWorldSize(),mTerrainGroup->getTerrainWorldSize()));
}
void OgrePagedGeometrySampleApp::frameRenderingQueued(const Ogre::FrameEvent &evt)
{
Ogre::Vector3 pos = mCameraMan->getCamera()->getPosition();
if (pos.y <= mTerrainGroup->getHeightAtWorldPosition(pos) + 4)
{
pos.y = mTerrainGroup->getHeightAtWorldPosition(pos) + 4;
}
//pos.y = mTerrainGroup->getHeightAtWorldPosition(pos) + 60;
mCameraMan->getCamera()->setPosition(pos);
mTreesMgr->update();
mBushMgr->update();
mGrassMgr->update();
}
void OgrePagedGeometrySampleApp::destroyScene()
{
delete mTreesMgr->getPageLoader();
delete mBushMgr->getPageLoader();
delete mGrassMgr->getPageLoader();
delete mTreesMgr;
delete mBushMgr;
delete mGrassMgr;
mSceneMgr->destroyAllEntities();
}