ambient lighting -- problem and headache

Problems building or running the engine, queries about how to use features etc.
User avatar
paul424
Gnome
Posts: 377
Joined: Thu May 24, 2012 7:16 pm
x 18

ambient lighting -- problem and headache

Post by paul424 »

Ogre Version: : 1 12 13:
Operating System: :Linux Tumbleweed:
Render System: :OpenGL3+

I try recently to make two markers for OD, one of them being Outliner in shaders ( works) the other being the ambient light made brighter.
Problem is not all creatures reacts to such ambient increase :

Code: Select all

 
            for(int ii =0 ; ii < myBrighter->getTechnique(myBrighter->getNumTechniques() - 1)->getNumPasses(); ++ii)
            {
                Ogre::ColourValue cv = myBrighter->getTechnique(myBrighter->getNumTechniques() - 1)->getPass(ii)->getAmbient();
                cv = cv * 8.0;
                myBrighter->getTechnique(myBrighter->getNumTechniques() - 1)->getPass(ii)->setAmbient(cv);
            }

For example the PitDeamon with such material does not SEEM react to such ambient change, although it has clearly defined the ambient quantity in the script :

Code: Select all

// PitDemon genrated by blender2ogre 0.6.0
import RTSS/NormalMapping_MultiPass from "RTShaderSystem.material"
material PitDemon : RTSS/NormalMapping_MultiPass
{
    receive_shadows on

technique
{
    pass lighting
    {
        // Override the normal map.
        rtshader_system
        {
            lighting_stage normal_map PitDemonNormal.png
        }
    }

    pass decal
    {
        ambient 0.8 0.8 0.8 1.0
        diffuse 0.65 0.65 0.65 1.0
        specular 0.1 0.1 0.1 0.0 2.0
        emissive 0.0 0.0 0.0 0.0

        texture_unit decalmap
        {
            texture PitDemon.png
            tex_address_mode wrap
            scale 1.0 1.0
            colour_op modulate
        }
    }
}
}

At the same time the Knight SEEMS increasing it's ambient fine : while there is no ambient quantity in it's script :

Code: Select all

// Knight
import RTSS/NormalMapping_MultiPass from "RTShaderSystem.material"
material Knight RTSS/NormalMapping_MultiPass
{
    technique
    {
        pass
        {
            texture_unit decalmap
            {
                texture Knight.png
            }
        }
    }
}

material Knight/TWOSIDE RTSS/NormalMapping_MultiPass
{
    technique
    {
        pass
        {
            texture_unit decalmap
            {
                texture Knight.png
            }
        }
    }
}

How do I get uniform treatment of ambient from Creatures's scripts using RTShader system ?

What are default ambient , specular, diffuse, emissive value in this system when not defined ?

User avatar
paul424
Gnome
Posts: 377
Joined: Thu May 24, 2012 7:16 pm
x 18

Re: ambient lighting -- problem and headache

Post by paul424 »

Seems the

Code: Select all

            rtshader_system
            {
                lighting_stage normal_map CultistNormal.png
            }

is the culprit but not the only one. For sure I need a normal texture mapping for most of the Creatures models. How do I deal with that ?

User avatar
paul424
Gnome
Posts: 377
Joined: Thu May 24, 2012 7:16 pm
x 18

Re: ambient lighting -- problem and headache

Post by paul424 »

I slowly leaning towards to writing custom shaders for all the creatures :D
The general template could be :

Vertex Shader :

Code: Select all





#version 330 core
#extension GL_ARB_explicit_uniform_location : enable
#extension GL_ARB_shading_language_include : enable

uniform    mat4 projectionMatrix;
uniform    mat4 viewMatrix;
uniform    mat4 worldMatrix;
uniform    mat4 lightMatrix;
layout (location = 0) in vec4 position;
layout (location = 2)  in vec3 normal;
layout (location = 14) in vec3 tangent;
 
layout (location = 8) in vec2 uv0;
layout (location = 8) in vec2 uv1;
out vec2 out_UV0;
out vec2 out_UV1;
out vec3 FragPos;
out vec4 VertexPos; 

out mat3 TBN;
 
 
void main() {
    // compute world space position, tangent, bitangent
    vec3 P = (worldMatrix * position).xyz;
    vec3 T = normalize(vec3(worldMatrix * vec4(tangent, 0.0)));
    vec3 B = normalize(vec3(worldMatrix * vec4(cross(tangent, normal), 0.0))); 
    vec3 N = cross(B, T);
    TBN = mat3(T, B, N);
 
gl_Position = projectionMatrix * viewMatrix * vec4(P, 1.0);
FragPos = P;
 
out_UV0 = uv0;
out_UV1 = uv1;
VertexPos = lightMatrix * position;
}  

Fragment Shader:

Code: Select all

,
#version 330  core
#define epsilon 0.00001

uniform sampler2D decalmap;
uniform sampler2D normalmap;
uniform sampler2D shadowmap;

uniform vec4 ambientLightColour;
uniform vec4 lightDiffuseColour; 
uniform vec4 lightSpecularColour;
uniform vec4 lightPos;
uniform vec4 cameraPosition;
uniform float ambient;
uniform bool shadowingEnabled;
in vec2 out_UV0;
in vec2 out_UV1;
in vec3 FragPos;
in vec4 VertexPos; 
in mat3 TBN;
 
out vec4 color;

void main (void)  
{ // compute Normal vec3 Normal = texture(normalmap, out_UV1.st).rgb; Normal.xyz = 2 * Normal.xyz - (1.0,1.0,1.0); Normal = normalize(TBN * Normal); vec4 shadow = vec4(1.0, 1.0, 1.0,1.0); vec4 tmpVertexPos = VertexPos; // compute shadowmap if(shadowingEnabled){ if(tmpVertexPos.z > epsilon ){ tmpVertexPos /= tmpVertexPos.w; shadow = texture(shadowmap, tmpVertexPos.xy); } } // compute lightDir vec3 lightDir = normalize(lightPos.xyz - FragPos*lightPos.w); // compute Specular vec3 viewDirection = normalize( cameraPosition.xyz - FragPos); vec3 reflectedLightDirection = normalize(reflect(-1.0*lightDir.xyz,Normal)); float spec = max(dot(reflectedLightDirection, viewDirection ), 0.0) ; spec = pow(spec,16); vec3 specular = spec * lightSpecularColour.rgb; // compute Diffuse float diff = max(dot(lightDir,Normal), 0.0); vec3 diffuse = diff * lightDiffuseColour.rgb; vec3 result; // precompute the lighting term vec3 lightingTerm = (diffuse + specular + ambientLightColour.rgb * ambient )*shadow.rgb; vec3 texelColor = texture(decalmap, out_UV0.st).rgb; result = lightingTerm * texelColor; color = vec4(result.xyz, 1.0); }

and material script would be :

Code: Select all


// Troll
// import RTSS/NormalMapping_MultiPass from "RTShaderSystem.material"


vertex_program myTrollVertexShader glsl
{
  source Creature.vert
    default_params
    {
        param_named_auto projectionMatrix projection_matrix
        param_named_auto viewMatrix view_matrix
        param_named_auto worldMatrix world_matrix
        param_named_auto lightMatrix texture_worldviewproj_matrix
    }
  
} fragment_program myTrollFragmentShader glsl { source Creature.frag default_params { param_named_auto ambientLightColour ambient_light_colour 10.0 param_named_auto lightDiffuseColour light_diffuse_colour 0.0 param_named_auto lightSpecularColour light_specular_colour 0.0 param_named_auto lightPos light_position 0.0 0.0 0.0 0.0 param_named_auto cameraPosition camera_position param_named shadowingEnabled bool false
} } material Troll //: RTSS/NormalMapping_MultiPass {
technique { pass { vertex_program_ref myTrollVertexShader { } fragment_program_ref myTrollFragmentShader { param_named ambient float 1.0 param_named decalmap int 0 param_named normalmap int 1 param_named shadowmap int 2 } // We use blending, so that we can see the underlying texture. texture_unit decalmap { texture Troll.png } texture_unit normalmap { texture TrollNormal.png } texture_unit shadowmap { content_type shadow tex_address_mode clamp filtering bilinear } } } }

The only problem is how do I get the value of float ambient from the code side ?
I know if I want to set its value it's setNamedConstant, like

myBrighter->getTechnique(myBrighter->getNumTechniques() - 1)->getPass(ii)->getFragmentProgramParameters()->setNamedConstant("ambient",Ogre::Real(100.0));

but there is no getNamedConstant ! :( How do I read value like that ?

User avatar
paul424
Gnome
Posts: 377
Joined: Thu May 24, 2012 7:16 pm
x 18

Re: ambient lighting -- problem and headache

Post by paul424 »

To futher complicate things some of the models does not react to light position at all ! For example I have Knight and Adventurer :

Knight seems to receive light position, while Adventurer does not !!!!!

Those are the same materials copied over :

Code: Select all

// Knight
// import RTSS/NormalMapping_MultiPass from "RTShaderSystem.material"


vertex_program myKnightVertexShader glsl
{
  source Creature.vert
    default_params
    {
        param_named_auto projectionMatrix projection_matrix
        param_named_auto viewMatrix view_matrix
        param_named_auto worldMatrix world_matrix
        param_named_auto lightMatrix texture_worldviewproj_matrix
    }
  
} fragment_program myKnightFragmentShader glsl { source Creature.frag default_params { param_named_auto ambientLightColour ambient_light_colour param_named_auto lightDiffuseColour light_diffuse_colour 0.0 param_named_auto lightSpecularColour light_specular_colour 0.0 param_named_auto lightPos light_position 0.0 0.0 0.0 0.0 param_named_auto cameraPosition camera_position param_named shadowingEnabled bool false
} } material Knight //: RTSS/NormalMapping_MultiPass {
technique { pass { vertex_program_ref myKnightVertexShader { } fragment_program_ref myKnightFragmentShader { param_named ambient float3 1.0 1.0 1.0 param_named decalmap int 0 param_named normalmap int 1 param_named shadowmap int 2 } // We use blending, so that we can see the underlying texture. texture_unit decalmap { texture Knight.png } texture_unit normalmap { texture UniversalNormal.png } texture_unit shadowmap { content_type shadow tex_address_mode clamp filtering bilinear } } } } material Knight/TWOSIDE //: RTSS/NormalMapping_MultiPass {
technique { pass { vertex_program_ref myKnightVertexShader { } fragment_program_ref myKnightFragmentShader { param_named ambient float3 1.0 1.0 1.0 param_named decalmap int 0 param_named normalmap int 1 param_named shadowmap int 2 } // We use blending, so that we can see the underlying texture. texture_unit decalmap { texture Knight.png } texture_unit normalmap { texture UniversalNormal.png } texture_unit shadowmap { content_type shadow tex_address_mode clamp filtering bilinear } } } }

Code: Select all

// Adventurer
// import RTSS/NormalMapping_MultiPass from "RTShaderSystem.material"


vertex_program myAdventurerVertexShader glsl
{
  source Creature.vert
    default_params
    {
        param_named_auto projectionMatrix projection_matrix
        param_named_auto viewMatrix view_matrix
        param_named_auto worldMatrix world_matrix
        param_named_auto lightMatrix texture_worldviewproj_matrix
    }
  
} fragment_program myAdventurerFragmentShader glsl { source Creature.frag default_params { param_named_auto ambientLightColour ambient_light_colour param_named_auto lightDiffuseColour light_diffuse_colour 0.0 param_named_auto lightSpecularColour light_specular_colour 0.0 param_named_auto lightPos light_position 0.0 0.0 0.0 0.0 param_named_auto cameraPosition camera_position param_named shadowingEnabled bool false
} } material Adventurer //: RTSS/NormalMapping_MultiPass {
technique { pass { vertex_program_ref myAdventurerVertexShader { } fragment_program_ref myAdventurerFragmentShader { param_named ambient float3 1.0 1.0 1.0 param_named decalmap int 0 param_named normalmap int 1 param_named shadowmap int 2 } // We use blending, so that we can see the underlying texture. texture_unit decalmap { texture Adventurer.png } texture_unit normalmap { texture UniversalNormal.png } texture_unit shadowmap { content_type shadow tex_address_mode clamp filtering bilinear } } } }
User avatar
paul424
Gnome
Posts: 377
Joined: Thu May 24, 2012 7:16 pm
x 18

Re: ambient lighting -- problem and headache

Post by paul424 »


On the right hand side a model which recieves shading , on left which doesn't...... All on the same map and SceneManager session !
Shouldn't the above code print all the lights from a scene , if no how do I do it ?
Asking because whatever I do it returns the empty list.

Code: Select all

    else if (args[1].compare("lights") == 0)
    {
        // Loop over the list of ligths and print them.
        stringStr << "Existing lights \n";
        const Ogre::LightList &ll = ODFrameListener::getSingletonPtr()->getCameraManager()->getCamera("RTS")->getLights();
        OD_LOG_INF("The size of LigthList is " + Helper::toString(ll.size()));
        int ii =0 ;
        for (auto light : ll )
        {
            stringStr << ii << ": " << light->getName() << " \n" ;
            ++ii;
        }


}

On the second skirmish game -- the same run --- it prepares those two models correctly :

The same is with the opening scene -- once the Troll is shaded correctly only, once the Knight and the rat only !
Why does the user of correct shaders is so random and seems to depend on humor of Ogre3d engine ?

EDIT : I Think I get the pattern : the first instant of my custom model shader is a lame one -- it does not pass the light position properly.
On the second instance everything is fine

User avatar
paul424
Gnome
Posts: 377
Joined: Thu May 24, 2012 7:16 pm
x 18

Re: ambient lighting -- problem and headache

Post by paul424 »

Uhh the problem was how the tangents are (re) - generated --- it seems OpenDungeons was playing russian rullet by upgrading the mesh after the createEntity call. My solution is :

Code: Select all

  
if(!Ogre::MeshManager::getSingleton().resourceExists(meshName,"Graphics")) Ogre::MeshManager::getSingleton().load(meshName,"Graphics");
Ogre::MeshPtr meshPtr = Ogre::MeshManager::getSingleton().getByName(meshName,"Graphics"); unsigned short src, dest; if (!meshPtr->suggestTangentVectorBuildParams(Ogre::VES_TANGENT, src, dest)) { meshPtr->buildTangentVectors(Ogre::VES_TANGENT, src, dest); } std::string creatureName = curCreature->getOgreNamePrefix() + curCreature->getName(); Ogre::Entity* ent = mSceneManager->createEntity(creatureName, meshPtr);

which solves the problem of first Instance being only sensitive to ambient light, that is a mesh not having defined Normals.

User avatar
paul424
Gnome
Posts: 377
Joined: Thu May 24, 2012 7:16 pm
x 18

Re: ambient lighting -- problem and headache

Post by paul424 »

Still I stay with my orginal question then --- How do I get the value of material parameter ? getNamedConstant doesn't exist !
How do you solve such problems as this ? Do you keep a mirrored value on the entity site on the cpp source code ? like Creature::mBrightnessLevel ? and then I can alternate it and compare as I wish ? .....

paroj
OGRE Team Member
OGRE Team Member
Posts: 2093
Joined: Sun Mar 30, 2014 2:51 pm
x 1129

Re: ambient lighting -- problem and headache

Post by paroj »