Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 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,9 @@ class CaelumSampleApplication : public ExampleApplication
// looking towards Z+ (north).
// Sun should rise in the east(left) and set in the west(right).
mCameraNode = mSceneMgr->getRootSceneNode()->createChildSceneNode();
mCameraNode->setPosition (Vector3 (775, 100, 1997));
mCameraNode->lookAt (Vector3 (0, 0, 0), Node::TS_PARENT);
mCameraNode->setPosition (Vector3 (775, 100, 997));
mCameraNode->setFixedYawAxis(true);
mCameraNode->lookAt (Vector3 (775, 99, 1000), Node::TS_PARENT);
mCameraNode->attachObject(mCamera);

mCamera->setNearClipDistance(5);
Expand All @@ -133,13 +135,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);
}
};
90 changes: 90 additions & 0 deletions samples/include/LegacyTerrainLoader.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
-----------------------------------------------------------------------------
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 ManualTerrainMaterial : public Ogre::TerrainMaterialGenerator
{
MaterialPtr mMaterial;
bool mIsInit;
bool mNormalMapRequired;
public:
ManualTerrainMaterial(const String& matName) : mIsInit(false), mNormalMapRequired(true)
{
auto terrainGlobals = TerrainGlobalOptions::getSingletonPtr();
mMaterial =
MaterialManager::getSingleton().getByName(matName, terrainGlobals->getDefaultResourceGroup());
}

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

MaterialPtr generate(const Terrain* terrain) override
{
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;
}

bool isVertexCompressionSupported() const override { return false; }

void requestOptions(Terrain* terrain) override
{
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 matgen = std::make_shared<ManualTerrainMaterial>(customMatName);
matgen->setNormalMapRequired(StringConverter::parseBool(cfg.getSetting("VertexNormals")));
terrainGlobals->setDefaultMaterialGenerator(matgen);
}

#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