From 8e683c2e05fac7c8aad3e95b9c756bdeb9dbd55f Mon Sep 17 00:00:00 2001 From: scrawl Date: Thu, 5 Jul 2012 01:38:33 +0200 Subject: [PATCH] vertex colors --- components/nifogre/ogre_nif_loader.cpp | 14 ++++++++++---- components/nifogre/ogre_nif_loader.hpp | 3 ++- files/materials/core.h | 8 +++++++- files/materials/objects.shader | 17 +++++++++++++++++ 4 files changed, 36 insertions(+), 6 deletions(-) diff --git a/components/nifogre/ogre_nif_loader.cpp b/components/nifogre/ogre_nif_loader.cpp index d1102087d..eb1714b61 100644 --- a/components/nifogre/ogre_nif_loader.cpp +++ b/components/nifogre/ogre_nif_loader.cpp @@ -226,7 +226,7 @@ void NIFLoader::createMaterial(const String &name, const Vector &emissive, float glossiness, float alpha, int alphaFlags, float alphaTest, - const String &texName) + const String &texName, bool vertexColor) { if (texName.empty()) return; @@ -246,6 +246,9 @@ void NIFLoader::createMaterial(const String &name, instance->setProperty ("diffuseMap", sh::makeProperty(texName)); + if (vertexColor) + instance->setProperty ("has_vertex_colour", sh::makeProperty(new sh::BooleanValue(true))); + // Add transparency if NiAlphaProperty was present if (alphaFlags != -1) { @@ -272,7 +275,6 @@ void NIFLoader::createMaterial(const String &name, } - /* if (!texName.empty()) { @@ -668,6 +670,8 @@ void NIFLoader::handleNiTriShape(NiTriShape *shape, int flags, BoundsFinder &bou NiTexturingProperty *t = NULL; NiMaterialProperty *m = NULL; NiAlphaProperty *a = NULL; + // can't make any sense of these values, so ignoring them for now + //NiVertexColorProperty *v = NULL; // Scan the property list for material information PropertyList &list = shape->props; @@ -685,6 +689,8 @@ void NIFLoader::handleNiTriShape(NiTriShape *shape, int flags, BoundsFinder &bou m = static_cast(pr); else if (pr->recType == RC_NiAlphaProperty) a = static_cast(pr); + //else if (pr->recType == RC_NiVertexColorProperty) + //v = static_cast(pr); } // Texture @@ -755,7 +761,7 @@ void NIFLoader::handleNiTriShape(NiTriShape *shape, int flags, BoundsFinder &bou { //std::cout << "new"; createMaterial(material, d->ambient, d->diffuse, d->specular, d->emissive, - d->glossiness, d->alpha, alphaFlags, alphaTest, texName); + d->glossiness, d->alpha, alphaFlags, alphaTest, texName, shape->data->colors.length != 0); MaterialMap.insert(std::make_pair(texName,material)); } } @@ -771,7 +777,7 @@ void NIFLoader::handleNiTriShape(NiTriShape *shape, int flags, BoundsFinder &bou } createMaterial(material, one, one, zero, zero, 0.0, 1.0, - alphaFlags, alphaTest, texName); + alphaFlags, alphaTest, texName, shape->data->colors.length != 0); } } } // End of material block, if(!hidden) ... diff --git a/components/nifogre/ogre_nif_loader.hpp b/components/nifogre/ogre_nif_loader.hpp index d73948fa8..fadb30af7 100644 --- a/components/nifogre/ogre_nif_loader.hpp +++ b/components/nifogre/ogre_nif_loader.hpp @@ -142,7 +142,8 @@ class NIFLoader : Ogre::ManualResourceLoader const Nif::Vector &emissive, float glossiness, float alpha, int alphaFlags, float alphaTest, - const Ogre::String &texName); + const Ogre::String &texName, + bool vertexColor); void findRealTexture(Ogre::String &texName); diff --git a/files/materials/core.h b/files/materials/core.h index 20127d3de..fe0172cd9 100644 --- a/files/materials/core.h +++ b/files/materials/core.h @@ -15,6 +15,8 @@ #define shOutput(type, name) , out type name : TEXCOORD@shCounter(2) #define shNormalInput(type) , in type normal : NORMAL + + #define shColourInput(type) , in type colour : COLOR #ifdef SH_VERTEX_SHADER @@ -40,7 +42,7 @@ #define SH_BEGIN_PROGRAM \ void main( \ - out float4 oColor : COLOR + out float4 oColor0 : COLOR #define SH_START_PROGRAM \ ) \ @@ -69,7 +71,9 @@ #define shMatrixMult(m, v) m * v + // automatically recognized by ogre when the input name equals this #define shInputPosition vertex + #define shOutputPosition gl_Position #define shOutputColor(num) oColor##num @@ -78,7 +82,9 @@ #define shInput(type, name) in type name; #define shOutput(type, name) out type name; + // automatically recognized by ogre when the input name equals this #define shNormalInput(type) in type normal; + #define shColourInput(type) in type colour; #ifdef SH_VERTEX_SHADER diff --git a/files/materials/objects.shader b/files/materials/objects.shader index 9ec09840d..9a78bb455 100644 --- a/files/materials/objects.shader +++ b/files/materials/objects.shader @@ -12,6 +12,8 @@ #define NEED_DEPTH #endif +#define HAS_VERTEXCOLOR @shPropertyBool(has_vertex_colour) + #ifdef SH_VERTEX_SHADER SH_BEGIN_PROGRAM @@ -22,6 +24,10 @@ shOutput(float4, normalPassthrough) #ifdef NEED_DEPTH shOutput(float, depthPassthrough) +#endif +#if HAS_VERTEXCOLOR + shColourInput(float4) + shOutput(float4, colorPassthrough) #endif SH_START_PROGRAM { @@ -31,6 +37,10 @@ #ifdef NEED_DEPTH depthPassthrough = shOutputPosition.z; #endif + +#if HAS_VERTEXCOLOR + colorPassthrough = colour; +#endif } #else @@ -50,13 +60,20 @@ #if FOG shUniform(float3 fogColor) @shAutoConstant(fogColor, fog_colour) shUniform(float4 fogParams) @shAutoConstant(fogParams, fog_params) +#endif +#ifdef HAS_VERTEXCOLOR + shInput(float4, colorPassthrough) #endif SH_START_PROGRAM { //shOutputColor(0) = float4((normalize(normalPassthrough.xyz)+float3(1.0,1.0,1.0)) / 2.f, 1.0); shOutputColor(0) = shSample(diffuseMap, UV); +#if HAS_VERTEXCOLOR + shOutputColor(0).xyz *= colorPassthrough.xyz; +#endif + #if FOG float fogValue = shSaturate((depthPassthrough - fogParams.y) * fogParams.w); shOutputColor(0).xyz = shLerp (shOutputColor(0).xyz, fogColor, fogValue);