From 16c2ea3a751500733bb69b7571c1a6061e6396b3 Mon Sep 17 00:00:00 2001 From: scrawl Date: Sat, 14 Jul 2012 11:13:38 +0200 Subject: [PATCH] terrain colourmap --- apps/openmw/mwrender/terrain.cpp | 3 +++ apps/openmw/mwrender/terrainmaterial.cpp | 20 +++++++++++++++---- apps/openmw/mwrender/terrainmaterial.hpp | 9 +++++---- extern/shiny | 2 +- files/materials/terrain.shader | 25 +++++++++++++++++++++++- 5 files changed, 49 insertions(+), 10 deletions(-) diff --git a/apps/openmw/mwrender/terrain.cpp b/apps/openmw/mwrender/terrain.cpp index 67cee435c7..b1d9dee124 100644 --- a/apps/openmw/mwrender/terrain.cpp +++ b/apps/openmw/mwrender/terrain.cpp @@ -191,6 +191,9 @@ namespace MWRender y*(mLandSize-1), mLandSize); + mActiveProfile->setGlobalColourMapEnabled(true); + mActiveProfile->setGlobalColourMap (terrain, vertex->getName()); + //this is a hack to get around the fact that Ogre seems to //corrupt the global colour map leading to rendering errors //MaterialPtr mat = terrain->getMaterial(); diff --git a/apps/openmw/mwrender/terrainmaterial.cpp b/apps/openmw/mwrender/terrainmaterial.cpp index ae04306f6f..f8551a3fd5 100644 --- a/apps/openmw/mwrender/terrainmaterial.cpp +++ b/apps/openmw/mwrender/terrainmaterial.cpp @@ -31,6 +31,7 @@ namespace MWRender TerrainMaterial::Profile::Profile(Ogre::TerrainMaterialGenerator* parent, const Ogre::String& name, const Ogre::String& desc) : Ogre::TerrainMaterialGenerator::Profile(parent, name, desc) + , mGlobalColourMap(false) { } @@ -56,11 +57,17 @@ namespace MWRender createPass(); return Ogre::MaterialManager::getSingleton().getByName(matName); + } - /* - Ogre::MaterialPtr m = Ogre::MaterialManager::getSingleton().create(matName, Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME); - return m; - */ + void TerrainMaterial::Profile::setGlobalColourMapEnabled (bool enabled) + { + mGlobalColourMap = enabled; + mParent->_markChanged(); + } + + void TerrainMaterial::Profile::setGlobalColourMap (Ogre::Terrain* terrain, const std::string& name) + { + sh::Factory::getInstance ().setTextureAlias (terrain->getMaterialName () + "_colourMap", name); } int TerrainMaterial::Profile::getLayersPerPass () const @@ -76,6 +83,11 @@ namespace MWRender p->setProperty ("vertex_program", sh::makeProperty(new sh::StringValue("terrain_vertex"))); p->setProperty ("fragment_program", sh::makeProperty(new sh::StringValue("terrain_fragment"))); + + p->mShaderProperties.setProperty ("colour_map", sh::makeProperty(new sh::BooleanValue(mGlobalColourMap))); + + sh::MaterialInstanceTextureUnit* colourMap = p->createTextureUnit ("colourMap"); + colourMap->setProperty ("texture_alias", sh::makeProperty(mMaterial->getName() + "_colourMap")); } Ogre::MaterialPtr TerrainMaterial::Profile::generateForCompositeMap(const Ogre::Terrain* terrain) diff --git a/apps/openmw/mwrender/terrainmaterial.hpp b/apps/openmw/mwrender/terrainmaterial.hpp index 73bbd7e076..2ee0224fde 100644 --- a/apps/openmw/mwrender/terrainmaterial.hpp +++ b/apps/openmw/mwrender/terrainmaterial.hpp @@ -47,10 +47,6 @@ namespace MWRender class Profile : public Ogre::TerrainMaterialGenerator::Profile { - protected: - Ogre::TerrainMaterialGenerator* mParent; - Ogre::String mName; - Ogre::String mDesc; public: Profile(Ogre::TerrainMaterialGenerator* parent, const Ogre::String& name, const Ogre::String& desc); virtual ~Profile(); @@ -69,9 +65,14 @@ namespace MWRender virtual void requestOptions(Ogre::Terrain* terrain); + void setGlobalColourMapEnabled(bool enabled); + void setGlobalColourMap (Ogre::Terrain* terrain, const std::string& name); + private: sh::MaterialInstance* mMaterial; + bool mGlobalColourMap; + void createPass (int index=0); int getLayersPerPass () const; diff --git a/extern/shiny b/extern/shiny index 1c25aca082..5cf02ac0c3 160000 --- a/extern/shiny +++ b/extern/shiny @@ -1 +1 @@ -Subproject commit 1c25aca082214beddd05fa9b8adf481c7299cf0e +Subproject commit 5cf02ac0c39115fcf7f69eea7a79c9c6ad7dbd71 diff --git a/files/materials/terrain.shader b/files/materials/terrain.shader index 8b186def39..bb776d6914 100644 --- a/files/materials/terrain.shader +++ b/files/materials/terrain.shader @@ -3,8 +3,11 @@ #define FOG @shGlobalSettingBool(fog) #define MRT @shGlobalSettingBool(mrt_output) +#define COLOUR_MAP @shPropertyBool(colour_map) + @shAllocatePassthrough(1, depth) +@shAllocatePassthrough(2, UV) #ifdef SH_VERTEX_SHADER @@ -26,6 +29,7 @@ shOutputPosition = shMatrixMult(viewProjMatrix, worldPos); @shPassthroughAssign(depth, shOutputPosition.z); + @shPassthroughAssign(UV, uv0); } @@ -34,11 +38,20 @@ SH_BEGIN_PROGRAM +#if COLOUR_MAP + shSampler2D(colourMap) +#endif + +#if FOG + shUniform(float3, fogColor) @shAutoConstant(fogColor, fog_colour) + shUniform(float4, fogParams) @shAutoConstant(fogParams, fog_params) +#endif @shPassthroughFragmentInputs #if MRT shDeclareMrtOutput(1) + shUniform(float, far) @shAutoConstant(far, far_clip_distance) #endif @@ -47,12 +60,22 @@ { float depth = @shPassthroughReceive(depth); + float2 UV = @shPassthroughReceive(UV); shOutputColour(0) = float4(1,0,0,1); + +#if COLOUR_MAP + shOutputColour(0).rgb *= shSample(colourMap, UV).rgb; +#endif + +#if FOG + float fogValue = shSaturate((depth - fogParams.y) * fogParams.w); + shOutputColour(0).xyz = shLerp (shOutputColour(0).xyz, fogColor, fogValue); +#endif #if MRT - //shOutputColour(1) = float4(1,1,1,1); + shOutputColour(1) = float4(depth / far,1,1,1); #endif }