Problem with material diffuse color

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
arras
Posts: 1622
Joined: Mon Apr 05, 2004 8:35 am
Location: Slovakia
Contact:

Problem with material diffuse color

Post by arras »

Material diffuse color does not seem to influence color of node at all. Ambient color and specular color works well.

Final diffuse color should be calculated from Light.DiffuseColor * Material.DiffuseColor. Yet if I set light diffuse to rgb[1,1,1] and material diffuse to rgb[1,0,0] resulting color is still white. Am I doing something wrong?

Here is test case:

Code: Select all

#include <irrlicht.h>
using namespace irr;

int main()
{
   IrrlichtDevice *device = createDevice(video::EDT_OPENGL,
      core::dimension2d<u32>(640, 480), 16, false, false, false, 0);

   video::IVideoDriver* driver = device->getVideoDriver();
   scene::ISceneManager* smgr = device->getSceneManager();

   scene::ISceneNode *sphere = smgr->addSphereSceneNode(2.f);
   sphere->getMaterial(0).AmbientColor = video::SColor(0, 0, 0, 255);
   sphere->getMaterial(0).DiffuseColor = video::SColor(255, 0, 0, 255);
   sphere->getMaterial(0).SpecularColor = video::SColor(0, 0, 0, 255);
   sphere->getMaterial(0).Shininess = 0.f;

   scene::ICameraSceneNode* camera = smgr->addCameraSceneNode();
   camera->setPosition(core::vector3df(0,10,-10));
   camera->setTarget(core::vector3df(0,0,0));

   scene::ILightSceneNode *light = smgr->addLightSceneNode(0, core::vector3df(-40,80,0));
   video::SLight *ldata = &light->getLightData();
   ldata->AmbientColor = video::SColorf(0.f, 0.f, 0.f, 0.1f);
   ldata->DiffuseColor = video::SColorf(1.f, 1.0f, 1.0f, 1.f);
   ldata->SpecularColor = video::SColorf(0.f, 0.f, 0.f, 0.f);
   ldata->Attenuation = core::vector3df(1,0,0);

   smgr->setAmbientLight(video::SColorf(0.f, 0.f, 0.f, 1.f));

   while(device->run())
   {
      driver->beginScene(true, true, video::SColor(0,200,200,255));
      smgr->drawAll();
      driver->endScene();
   }

   device->drop();

   return 0;
}
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

Default for Irrlicht is to take vertex color as diffuse. Check SMaterial.ColorMaterial
arras
Posts: 1622
Joined: Mon Apr 05, 2004 8:35 am
Location: Slovakia
Contact:

Post by arras »

Thanks hybrid. I did miss that change between Irrlicht versions. It is actually very useful one.

However I have small issue with GLSL shader.
Normally gl_FrontMaterial and gl_BackMaterial parameters store all material properties including ambient, diffuse and specular. Parameter gl_FrontLightProduct and gl_BackLightProduct store precalculated result of material and light color multiplication. This works fine if Material.ColorMaterial is set to ECM_NONE and material use its diffuse instead of vertex color. But in all other cases, like if default ECM_DIFFUSE is used, OpenGL parameters are set to white. I know I can write shader using gl_Color but that will make code little bit less universal.

Can Irrlicht send correct colors to OpenGL parameters I have mentioned above depending on ColorMaterial state?
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

I don't think so, because the gl_ things are OpenGL defaults. We don't do anything special here, so it's what OpenGL seems to intend for this case. Maybe BlindSide or some other shader guy knows more. But for me it sounds as a feature.
arras
Posts: 1622
Joined: Mon Apr 05, 2004 8:35 am
Location: Slovakia
Contact:

Post by arras »

Well, it might very well be it is. But there is one thing that makes me wonder about it a little bit: by default, those values store material properties (by default in OpenGL). But if ECM_DIFFUSE (default in Irrlicht) is used, its is neither vertex color, nor material diffuse which is stored in gl_FrontMaterial.diffuse. It is set to white regardless of material settings. That would suggest that something somewhere (driver?) did notice diffuse calculation changed in Irrlicht and quietly have send different values to OpenGL. Well if that is possible ...perhaps sending correct value is possible as well.

Irrlicht use fixed rendering pipeline after all and fixed pipeline renders correct diffuse in this case. Shader use basically those very same values that fixed pipeline does. Or so I think ;)
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

Well, could be driver related issue, or just some wrong setup in Irrlicht maybe. I'll move this to bug forum, but I guess that it might take somewhat longer to find the proper behavior (and a fix if necessary).
arras
Posts: 1622
Joined: Mon Apr 05, 2004 8:35 am
Location: Slovakia
Contact:

Post by arras »

Ok, thanks hybrid. I would say, whoever is author of ColorMaterial code, he will probably have some idea of what's going on.
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

Well, I'm the only author for that part in Irrlicht, but I have no idea about what the shader specs think would be correct here, nor where I have to change a bit to make it different.
arras
Posts: 1622
Joined: Mon Apr 05, 2004 8:35 am
Location: Slovakia
Contact:

Post by arras »

I might try to get BlindSide interested in this then. Perhaps he might know something. I do know a little bit about GLSL but OpenGL stuff itself is beyond my capabilities.

All I know is from articles like this one: http://www.clockworkcoders.com/oglsl/tutorial5.htm

here is citation:
Material Parameters:
You can access the values you set in C++ with "glMaterial*" with gl_FrontMaterial and gl_BackMaterial.

Code: Select all

struct gl_MaterialParameters
{
 vec4 emission;    // Ecm
 vec4 ambient;     // Acm
 vec4 diffuse;     // Dcm
 vec4 specular;    // Scm
 float shininess;  // Srm
};
uniform gl_MaterialParameters gl_FrontMaterial;
uniform gl_MaterialParameters gl_BackMaterial;
Light Source Paramters:
You can access light source parameters defined in your OpenGL Program with the pre-defined uniform variable "gl_LightSource". In the C++ application you simply define values with "glLight*".

Code: Select all

struct gl_LightSourceParameters {
 vec4 ambient;              // Acli
 vec4 diffuse;              // Dcli
 vec4 specular;             // Scli
 vec4 position;             // Ppli
 vec4 halfVector;           // Derived: Hi
 vec3 spotDirection;        // Sdli
 float spotExponent;        // Srli
 float spotCutoff;          // Crli
                            // (range: [0.0,90.0], 180.0)
 float spotCosCutoff;       // Derived: cos(Crli)
                            // (range: [1.0,0.0],-1.0)
 float constantAttenuation; // K0
 float linearAttenuation;   // K1
 float quadraticAttenuation;// K2
};

uniform gl_LightSourceParameters gl_LightSource[gl_MaxLights];
Derived state from products of light and material:

Code: Select all

struct gl_LightModelProducts
{
  vec4 sceneColor; // Derived. Ecm + Acm * Acs
};
uniform gl_LightModelProducts gl_FrontLightModelProduct;
uniform gl_LightModelProducts gl_BackLightModelProduct;


struct gl_LightProducts {
 vec4 ambient;    // Acm * Acli 
 vec4 diffuse;    // Dcm * Dcli
 vec4 specular;   // Scm * Scli
};
uniform gl_LightProducts gl_FrontLightProduct[gl_MaxLights];
uniform gl_LightProducts gl_BackLightProduct[gl_MaxLights];
This should be from OpenGL Shading Language specification. I would say glMaterial, glLight and others are set somewhere in driver.

btw: there might be similar issues in DirectX, but I do not use HLSL so I can't test it.
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

Well, the things which are covered by the ColorMaterial setting are not set by glMaterial, but via the vertex color. So I guess you need to check the color material setting, and either use glFrontMaterial or the vertex color in your shader.
arras
Posts: 1622
Joined: Mon Apr 05, 2004 8:35 am
Location: Slovakia
Contact:

Post by arras »

Ok, thanks for help hybrid. I'll do that.
Post Reply