[bug] parsing of specular color in OGRE material files

You discovered a bug in the engine, and you are sure that it is not a problem of your code? Just post it in here. Please read the bug posting guidelines first.
Post Reply
Duane
Posts: 5
Joined: Mon Jun 10, 2013 5:43 pm

[bug] parsing of specular color in OGRE material files

Post by Duane »

In OGRE material files, specular colors are expressed as "R G B Shininess".

The current parser for this line uses a function that will parse R G B colors, then attempts to parse an additional number if it's in the same line, which in interprets as A.

Since speculars *also* have a fourth value, but it's not alpha.

The code that parses speculars subsequently attempts to parse another number to get the shininess, but since it's already parsed, it will instead pull in the next token. In most cases, this is a texture_unit structure, which means that most materials with specular highlights will fail to parse the texture.

A typical specular line looks like:

Code: Select all

specular 0.8 0.8 0.8 5.5
Here is the current code that parses colors, note the "getMaterialToken(file, token, true);" which tries to parse a fourth number:

Code: Select all

 
bool COgreMeshFileLoader::readColor(io::IReadFile* file, video::SColor& col)
{
    core::stringc token;
 
    getMaterialToken(file, token);
    if (token!="vertexcolour")
    {
        video::SColorf col_f;
        col_f.r=core::fast_atof(token.c_str());
        getMaterialToken(file, token);
        col_f.g=core::fast_atof(token.c_str());
        getMaterialToken(file, token);
        col_f.b=core::fast_atof(token.c_str());
        getMaterialToken(file, token, true);
        if (token.size())
            col_f.a=core::fast_atof(token.c_str());
        else
            col_f.a=1.0f;
        if ((col_f.r==0.0f)&&(col_f.g==0.0f)&&(col_f.b==0.0f))
            col.set(255,255,255,255);
        else
            col=col_f.toSColor();
        return false;
    }
    return true;
}
 
Here is the code that parses speculars (COgreMeshFileLoader::readPass):

Code: Select all

 
        else if (token=="specular")
        {
            pass.SpecularTokenColor=readColor(file, pass.Material.SpecularColor);
            getMaterialToken(file, token);
            pass.Material.Shininess=core::fast_atof(token.c_str());
        }
 
One possible fix would be to add an optional parameter to readColor() that suppresses the attempt to read the fourth value.
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Re: [bug] parsing of specular color in OGRE material files

Post by hybrid »

Oh, we cannot even use the value read in the alpha atribute as it seems as it gets converted to something unusable in this situation. So yes, we probably need a three components color read then.
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Re: [bug] parsing of specular color in OGRE material files

Post by hybrid »

Ok, checked also some ogre files before touching the code. And I actually only have files where specular contains 5 values. 4 RGB values (i.e. range 0..1) and one other, which seems to be the shininess. So all seems ok. Where did you get the hint that specular only has 3 color elements?
Duane
Posts: 5
Joined: Mon Jun 10, 2013 5:43 pm

Re: [bug] parsing of specular color in OGRE material files

Post by Duane »

This was in the Sinbad.material file for the stock Sinbad model that comes in the OGRE distribution.

Here's the section that was failing:

Code: Select all

 
 
material Sinbad/Gold
{
    receive_shadows on
    technique
    {
        pass
        {
            ambient 0.75 0.75 0.75
            diffuse 0.8 0.8 0.8 1
            specular 0.3 0.3 0.2 5.5
            
            texture_unit
            {
                texture sinbad_clothes.tga
            }
        }
    }
}
 
I just checked the parser code in OGRE, and apparently the specular line can have two ("vertexcolour" Shininess), four (R G B Shininess) or five (R G B A Shininess) parameters.
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Re: [bug] parsing of specular color in OGRE material files

Post by hybrid »

Ahh, I removed the material files from the ogre SDK, that's why I didn't find those examples. Looks like it becomes a little tricker with these variable parameters. Have to think how we can go ahead.
Duane
Posts: 5
Joined: Mon Jun 10, 2013 5:43 pm

Re: [bug] parsing of specular color in OGRE material files

Post by Duane »

The quick (but very, very unclean) hack is to let the color function parse the four values it currently does, then try to parse another value on the same line in the specular parser, much like the color parser does. If it fails, then copy the parsed alpha from the material to shininess and set the alpha to 1, otherwise use the newly parsed value for shininess.

OGRE's code basically parses the entire line then conditions on the number of parameters it finds.
Post Reply