Skip to content

Commit 919f424

Browse files
committed
restore terrain in sample using LegacyTerrainLoader
1 parent b53d7eb commit 919f424

3 files changed

Lines changed: 115 additions & 13 deletions

File tree

samples/include/CaelumDemo.h

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include "CaelumDemoCommon.h"
66
#include "ExampleApplication.h"
7+
#include "LegacyTerrainLoader.h"
78

89
class CaelumSampleFrameListener : public OgreBites::InputListener
910
{
@@ -119,8 +120,8 @@ class CaelumSampleApplication : public ExampleApplication
119120
// Start the camera on a hill in the middle of the terrain
120121
// looking towards Z+ (north).
121122
// Sun should rise in the east(left) and set in the west(right).
122-
mCamera->setPosition (Vector3 (775, 100, 1997));
123-
mCamera->lookAt (Vector3 (0, 0, 0));
123+
mCamera->setPosition (Vector3 (775, 100, 997));
124+
mCamera->lookAt (Vector3 (775, 99, 1000));
124125

125126
mCamera->setNearClipDistance(5);
126127

@@ -132,13 +133,13 @@ class CaelumSampleApplication : public ExampleApplication
132133

133134
void createScene ()
134135
{
135-
mSceneMgr->getRootSceneNode()->attachObject(
136-
mSceneMgr->createEntity("House", "TudorHouse.mesh"));
137-
// needs porting to new terrain system
138-
#if 0
136+
137+
SceneNode* houseNode = mSceneMgr->getRootSceneNode()->createChildSceneNode();
138+
houseNode->setPosition(Vector3 (775, 60, 1150));
139+
houseNode->setScale(0.05, 0.05, 0.05);
140+
houseNode->yaw(Degree(45));
141+
houseNode->attachObject(mSceneMgr->createEntity("House", "TudorHouse.mesh"));
139142
// Put some terrain in the scene
140-
std::string terrain_cfg("CaelumDemoTerrain.cfg");
141-
mSceneMgr->setWorldGeometry (terrain_cfg);
142-
#endif
143+
loadLegacyTerrain("CaelumDemoTerrain.cfg", mSceneMgr);
143144
}
144145
};
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
-----------------------------------------------------------------------------
3+
This source file is part of OGRE
4+
(Object-oriented Graphics Rendering Engine)
5+
For the latest info, see http://www.ogre3d.org/
6+
7+
Copyright (c) 2000-2009 Torus Knot Software Ltd
8+
Also see acknowledgements in Readme.html
9+
10+
You may use this sample code for anything you like, it is not covered by the
11+
same license as the rest of the engine.
12+
-----------------------------------------------------------------------------
13+
*/
14+
15+
#include <OgreTerrain.h>
16+
#include <OgreTerrainGroup.h>
17+
#include <OgreConfigFile.h>
18+
19+
namespace Ogre
20+
{
21+
class CustomMatProfile : public Ogre::TerrainMaterialGenerator::Profile
22+
{
23+
MaterialPtr mMaterial;
24+
bool mIsInit;
25+
bool mNormalMapRequired;
26+
public:
27+
CustomMatProfile(const String& matName) : Profile(NULL, "", ""), mIsInit(false), mNormalMapRequired(true)
28+
{
29+
auto terrainGlobals = TerrainGlobalOptions::getSingletonPtr();
30+
mMaterial =
31+
MaterialManager::getSingleton().getByName(matName, terrainGlobals->getDefaultResourceGroup());
32+
}
33+
34+
bool isVertexCompressionSupported() const { return false; }
35+
36+
void setNormalMapRequired(bool enable) { mNormalMapRequired = enable; }
37+
38+
MaterialPtr generate(const Terrain* terrain)
39+
{
40+
if (!mIsInit && mNormalMapRequired)
41+
{
42+
// Get default pass
43+
Pass *p = mMaterial->getTechnique(0)->getPass(0);
44+
45+
// Add terrain's global normalmap to renderpass so the fragment program can find it.
46+
p->createTextureUnitState()->_setTexturePtr(terrain->getTerrainNormalMap());
47+
48+
}
49+
mIsInit = true;
50+
51+
return mMaterial;
52+
}
53+
MaterialPtr generateForCompositeMap(const Terrain* terrain)
54+
{
55+
return terrain->_getCompositeMapMaterial();
56+
}
57+
void updateCompositeMap(const Terrain* terrain, const Rect& rect) {}
58+
void setLightmapEnabled(bool enabled) {}
59+
uint8 getMaxLayers(const Terrain* terrain) const { return 0; }
60+
61+
void updateParams(const MaterialPtr& mat, const Terrain* terrain) {}
62+
void updateParamsForCompositeMap(const MaterialPtr& mat, const Terrain* terrain) {}
63+
void requestOptions(Terrain* terrain)
64+
{
65+
terrain->_setLightMapRequired(false);
66+
terrain->_setCompositeMapRequired(false);
67+
terrain->_setNormalMapRequired(mNormalMapRequired);
68+
}
69+
};
70+
} // namespace Ogre
71+
72+
inline Ogre::TerrainGroup* loadLegacyTerrain(const Ogre::String& cfgFileName, Ogre::SceneManager* sceneMgr)
73+
{
74+
using namespace Ogre;
75+
76+
ConfigFile cfg;
77+
cfg.loadFromResourceSystem(cfgFileName, RGN_DEFAULT);
78+
79+
auto terrainGlobals = TerrainGlobalOptions::getSingletonPtr();
80+
if(!terrainGlobals)
81+
terrainGlobals = new TerrainGlobalOptions();
82+
83+
const String& customMatName = cfg.getSetting("CustomMaterialName");
84+
85+
if(!customMatName.empty())
86+
{
87+
auto profile = new CustomMatProfile(customMatName);
88+
profile->setNormalMapRequired(StringConverter::parseBool(cfg.getSetting("VertexNormals")));
89+
terrainGlobals->getDefaultMaterialGenerator()->setActiveProfile(profile);
90+
}
91+
92+
auto terrainGroup = new TerrainGroup(sceneMgr);
93+
terrainGroup->loadLegacyTerrain(cfgFileName);
94+
95+
return terrainGroup;
96+
}

samples/resources/CaelumSample.cg

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ void MainFP
144144
uniform sampler mainTexture : register(s0),
145145
#if TERRAIN
146146
uniform sampler detailTexture : register(s1),
147+
uniform sampler normalTexture : register(s2),
147148
#endif
148149
#endif
149150

@@ -203,18 +204,22 @@ void MainFP
203204
oColour += baseColour * derived_scene_colour;
204205
#endif
205206

206-
#if ONE_LIGHT
207-
float3 normal = normalize(iNormal);
207+
#if ONE_LIGHT || TWO_LIGHTS
208+
#if TERRAIN
209+
float3 normal = tex2D(normalTexture, iTexcoord).rgb * 2 - 1;
210+
#else
211+
float3 normal = normalize(iNormal);
212+
#endif
213+
#endif
208214

215+
#if ONE_LIGHT
209216
float diffuse_factor = max(0, dot(float4(normal, 1), light_position_view_space));
210217
float4 light_colour = diffuse_factor * derived_light_diffuse_colour * shadowing;
211218

212219
oColour += baseColour * light_colour;
213220
#endif
214221

215222
#if TWO_LIGHTS
216-
float3 normal = normalize(iNormal);
217-
218223
// Accumulate two lights
219224
float4 light_colour = float4(0, 0, 0, 0);
220225

0 commit comments

Comments
 (0)