From 50b2706783e64a4cc17362e20fbb68fe05990cd3 Mon Sep 17 00:00:00 2001 From: Pavel Rojtberg Date: Sat, 16 Feb 2019 17:21:33 +0100 Subject: [PATCH] restore terrain in sample using LegacyTerrainLoader --- samples/include/CaelumDemo.h | 20 +++--- samples/include/LegacyTerrainLoader.h | 90 +++++++++++++++++++++++++++ samples/resources/CaelumSample.cg | 13 ++-- 3 files changed, 110 insertions(+), 13 deletions(-) create mode 100644 samples/include/LegacyTerrainLoader.h diff --git a/samples/include/CaelumDemo.h b/samples/include/CaelumDemo.h index 61f608d..95613ad 100644 --- a/samples/include/CaelumDemo.h +++ b/samples/include/CaelumDemo.h @@ -4,6 +4,7 @@ #include "CaelumDemoCommon.h" #include "ExampleApplication.h" +#include "LegacyTerrainLoader.h" class CaelumSampleFrameListener : public OgreBites::InputListener { @@ -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); @@ -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); } }; diff --git a/samples/include/LegacyTerrainLoader.h b/samples/include/LegacyTerrainLoader.h new file mode 100644 index 0000000..29770df --- /dev/null +++ b/samples/include/LegacyTerrainLoader.h @@ -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 +#include +#include + +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(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; +} diff --git a/samples/resources/CaelumSample.cg b/samples/resources/CaelumSample.cg index 1eaa0cb..cce6301 100644 --- a/samples/resources/CaelumSample.cg +++ b/samples/resources/CaelumSample.cg @@ -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 @@ -203,9 +204,15 @@ 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; @@ -213,8 +220,6 @@ void MainFP #endif #if TWO_LIGHTS - float3 normal = normalize(iNormal); - // Accumulate two lights float4 light_colour = float4(0, 0, 0, 0);