Skip to content

Commit

Permalink
restore terrain in sample using LegacyTerrainLoader
Browse files Browse the repository at this point in the history
  • Loading branch information
paroj committed Feb 26, 2019
1 parent eb78d74 commit 569bdec
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 13 deletions.
19 changes: 10 additions & 9 deletions samples/include/CaelumDemo.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include "CaelumDemoCommon.h"
#include "ExampleApplication.h"
#include "LegacyTerrainLoader.h"

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

mCamera->setNearClipDistance(5);

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

void createScene ()
{
mSceneMgr->getRootSceneNode()->attachObject(
mSceneMgr->createEntity("House", "TudorHouse.mesh"));
// needs porting to new terrain system
#if 0

SceneNode* houseNode = mSceneMgr->getRootSceneNode()->createChildSceneNode();
houseNode->setPosition(Vector3 (775, 60, 1150));
houseNode->setScale(0.05, 0.05, 0.05);
houseNode->yaw(Degree(45));
houseNode->attachObject(mSceneMgr->createEntity("House", "TudorHouse.mesh"));
// Put some terrain in the scene
std::string terrain_cfg("CaelumDemoTerrain.cfg");
mSceneMgr->setWorldGeometry (terrain_cfg);
#endif
loadLegacyTerrain("CaelumDemoTerrain.cfg", mSceneMgr);
}
};
99 changes: 99 additions & 0 deletions samples/include/LegacyTerrainLoader.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
-----------------------------------------------------------------------------
This source file is part of OGRE
(Object-oriented Graphics Rendering Engine)
For the latest info, see http://www.ogre3d.org/
Copyright (c) 2000-2009 Torus Knot Software Ltd
Also see acknowledgements in Readme.html
You may use this sample code for anything you like, it is not covered by the
same license as the rest of the engine.
-----------------------------------------------------------------------------
*/

#include <OgreTerrain.h>
#include <OgreTerrainGroup.h>
#include <OgreConfigFile.h>

namespace Ogre
{
class CustomMatProfile : public Ogre::TerrainMaterialGenerator::Profile
{
MaterialPtr mMaterial;
bool mIsInit;
bool mNormalMapRequired;
public:
CustomMatProfile(const String& matName) : Profile(NULL, "", ""), mIsInit(false), mNormalMapRequired(true)
{
auto terrainGlobals = TerrainGlobalOptions::getSingletonPtr();
mMaterial =
MaterialManager::getSingleton().getByName(matName, terrainGlobals->getDefaultResourceGroup());
}

bool isVertexCompressionSupported() const { return false; }

void setNormalMapRequired(bool enable) { mNormalMapRequired = enable; }

MaterialPtr generate(const Terrain* terrain)
{
if (!mIsInit && mNormalMapRequired)
{
// Get default pass
Pass *p = mMaterial->getTechnique(0)->getPass(0);

// Add terrain's global normalmap to renderpass so the fragment program can find it.
p->createTextureUnitState()->_setTexturePtr(terrain->getTerrainNormalMap());

}
mIsInit = true;

return mMaterial;
}
MaterialPtr generateForCompositeMap(const Terrain* terrain)
{
return terrain->_getCompositeMapMaterial();
}
void updateCompositeMap(const Terrain* terrain, const Rect& rect) {}
void setLightmapEnabled(bool enabled) {}
uint8 getMaxLayers(const Terrain* terrain) const { return 0; }

void updateParams(const MaterialPtr& mat, const Terrain* terrain) {}
void updateParamsForCompositeMap(const MaterialPtr& mat, const Terrain* terrain) {}
void requestOptions(Terrain* terrain)
{
terrain->_setLightMapRequired(false);
terrain->_setCompositeMapRequired(false);
terrain->_setNormalMapRequired(mNormalMapRequired);
}
};
} // namespace Ogre

inline Ogre::TerrainGroup* loadLegacyTerrain(const Ogre::String& cfgFileName, Ogre::SceneManager* sceneMgr)
{
using namespace Ogre;

auto terrainGroup = new TerrainGroup(sceneMgr);

ConfigFile cfg;
cfg.loadFromResourceSystem(cfgFileName, terrainGroup->getResourceGroup());

auto terrainGlobals = TerrainGlobalOptions::getSingletonPtr();
if(!terrainGlobals)
terrainGlobals = new TerrainGlobalOptions();

const String& customMatName = cfg.getSetting("CustomMaterialName");

if(!customMatName.empty())
{
auto profile = new CustomMatProfile(customMatName);
profile->setNormalMapRequired(StringConverter::parseBool(cfg.getSetting("VertexNormals")));
terrainGlobals->getDefaultMaterialGenerator()->setActiveProfile(profile);
}

#if OGRE_VERSION >= ((1 << 16) | (11 << 8) | 6)
terrainGroup->loadLegacyTerrain(cfg);
#endif

return terrainGroup;
}
13 changes: 9 additions & 4 deletions samples/resources/CaelumSample.cg
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ void MainFP
uniform sampler mainTexture : register(s0),
#if TERRAIN
uniform sampler detailTexture : register(s1),
uniform sampler normalTexture : register(s2),
#endif
#endif

Expand Down Expand Up @@ -203,18 +204,22 @@ void MainFP
oColour += baseColour * derived_scene_colour;
#endif

#if ONE_LIGHT
float3 normal = normalize(iNormal);
#if ONE_LIGHT || TWO_LIGHTS
#if TERRAIN
float3 normal = tex2D(normalTexture, iTexcoord).rgb * 2 - 1;
#else
float3 normal = normalize(iNormal);
#endif
#endif

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

oColour += baseColour * light_colour;
#endif

#if TWO_LIGHTS
float3 normal = normalize(iNormal);

// Accumulate two lights
float4 light_colour = float4(0, 0, 0, 0);

Expand Down

0 comments on commit 569bdec

Please sign in to comment.