[partly fixed]Collada vertex colors ignored

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
sf17k
Posts: 6
Joined: Sat Mar 15, 2014 6:04 am

[partly fixed]Collada vertex colors ignored

Post by sf17k »

I was wondering what file format to use to achieve untextured, colored polygon meshes. I made a simple mesh in Blender and vertex painted it. Then I discovered that .obj doesn't support vertex colors, so I tried Collada (.dae) but couldn't get it to load the colors! Then I decided to try all the other formats that Blender can export, and .ply was the one that succeeded. I can work with that, but .ply is a very basic format and I'm afraid it'll be missing important features.

I did a bunch of research, and it turns out vertex colors are poorly supported in general, not just in Irrlicht. Blender itself is unable to import Collada vertex color data, even though it seems to export correctly! (I checked the export by opening my .dae file in a text editor, and reading the Collada 1.5 specification.) It seems this is not a feature that many people require.

Looking at the Irrlicht Collada loader source confirmed my suspicions that the colors are being ignored by the importer.

Irrlicht 1.8.1
Blender 2.69
CuteAlien
Admin
Posts: 9651
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Collada vertex colors ignored

Post by CuteAlien »

Hi - can you post an example collada file with vertex colors somewhere? If you can add the Blender file as well - even better!
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
sf17k
Posts: 6
Joined: Sat Mar 15, 2014 6:04 am

Re: Collada vertex colors ignored

Post by sf17k »

Note: I get a warning about a "library_controllers" XML tag when loading the .dae file, but I think it's an unrelated problem with blender's exporting. I got the same warning in another tool, and manually deleting the (empty) tag fixes it.

The mesh files:
https://dl.dropboxusercontent.com/u/305 ... mesh.blend
https://dl.dropboxusercontent.com/u/305 ... stmesh.dae
https://dl.dropboxusercontent.com/u/305 ... stmesh.ply

A program which uses them:

Code: Select all

#include "irrlicht.h"
using namespace irr;
 
int main()
{
    IrrlichtDevice *device = createDevice(video::EDT_OPENGL, core::dimension2d<u32>(512, 256));
    video::IVideoDriver *driver = device->getVideoDriver();
    scene::ISceneManager *smgr = device->getSceneManager();
 
    smgr->addCameraSceneNode();
 
    scene::ISceneNode *ply = smgr->addMeshSceneNode(smgr->getMesh("testmesh.ply"), 0, -1, core::vector3df(-1.5,0,2.5));
    ply->setMaterialFlag(video::EMF_LIGHTING, 0);
 
    scene::ISceneNode *dae = smgr->addMeshSceneNode(smgr->getMesh("testmesh.dae"), 0, -1, core::vector3df(+1.5,0,2.5));
    dae->setMaterialFlag(video::EMF_LIGHTING, 0);
 
    while (device->run())
    if (device->isWindowActive()) {
        driver->beginScene(true, true, 0);
        smgr->drawAll();
        driver->endScene();
    }
 
    device->drop();
    return 0;
}
Output:
Image
CuteAlien
Admin
Posts: 9651
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Collada vertex colors ignored

Post by CuteAlien »

Thanks. I've added it to Irrlicht svn trunk in revision 4727). I hope it works correct.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
sf17k
Posts: 6
Joined: Sat Mar 15, 2014 6:04 am

Re: [fixed]Collada vertex colors ignored

Post by sf17k »

Thank you! It now works correctly until I add a material to the mesh in blender. Then the material's diffuse color overwrites the colors of all vertexes. I expected diffuse color to only set the material's DiffuseColor without overwriting individual vertexes' colors.

This code shows up twice in CColladaFileLoader.cpp:

Code: Select all

SceneManager->getMeshManipulator()->setVertexColors(&tmpmesh,material->Mat.DiffuseColor);
And corresponding lines for alpha (though I understand this less):

Code: Select all

SceneManager->getMeshManipulator()->setVertexColorAlpha(&tmpmesh, core::floor32(material->Transparency*255.0f));
If I comment these out, I get the behavior I expected. But I'm not 100% sure what's intended here.

EDIT: Okay, I think I've wrapped my mind around some things.

In an Irrlicht material, the default setting for ColorMaterial is ECM_DIFFUSE. This overrides the per-mesh DiffuseColor with per-vertex colors. For me that led to confusing rendering because although DiffuseColor is overridden, AmbientColor, SpecularColor, and EmissiveColor are not. The rendered result was a senseless mix of vertex colors and material colors.

In light of this, I think it would make more sense for the Collada importer to simply set ColorMaterial to ECM_NONE instead of overwriting.

In fact, ECM_NONE may be a saner default in general. It's more confusing to debug partly wrong vertex colors than having no vertex colors at all and being forced to discover the ColorMaterial option. E.g. if you add ambient lighting, you have to know to set ECM_DIFFUSE_AND_AMBIENT or else the colors will be wonky.
CuteAlien
Admin
Posts: 9651
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: [fixed]Collada vertex colors ignored

Post by CuteAlien »

Changing default settings for SMaterial is probably not an option. This seems to have been around for a while, so modifying it would suddenly change the look of materials in all applications using the engine. Not a good idea.

I'm not sure - does it work now for you? Otherwise - I see that one place where DiffuseColor set is out-defined, so not quite sure where it even get's the value from. From the effects section? Sorry, if I sound a little lost - I'm not the original author (and who understands Collada entirely...) and even less familiar with the material settings in Blender. Which means I need an example for everything that might be wrong. And then I start figuring out what's going on in the code and compare it with Collada documentation to see if stuff makes some sense.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
sf17k
Posts: 6
Joined: Sat Mar 15, 2014 6:04 am

Re: [fixed]Collada vertex colors ignored

Post by sf17k »

Yeah, I was confused myself. Maybe I can explain more clearly with some examples.

In blender, a mesh starts with no material, but one can be added. A material has diffuse/ambient/specular/emissive colors and shininess, among other things. Blender lets you set all these. They get exported into Collada and imported by Irrlicht as SMaterial settings. If the mesh has no material then these settings don't exist in the Collada file, so Irrlicht sets some default values for them.

ColorMaterial sets which of diffuse/ambient/specular/emissive is overridden by vertex colors.

To demonstrate, I made a sphere with a vertex-painted pattern on it. Not a texture! I exported two versions, with and without a purple material (meaning, the diffuse color is purple).

Then I made a program that renders four variations of each. There is a white light above and bluish ambient lighting. I ran this app with three versions of Irrlicht: Before and after your fix, and after additional changes.

Irrlicht 1.8.1

This shows the original bug: vertex colors completely ignored.

Image

First fix: Revision 4727

In the top row, vertex colors are applied correctly. ECM_DIFFUSE only shows the pattern on diffuse-lit areas. ECM_AMBIENT only shows it on the bottom (no, the light didn't get brighter, the sphere is just not being patterned on top). ECM_DIFFUSE_AND_AMBIENT gives the most natural result (which, on a side note, makes it seem like a saner default than ECM_DIFFUSE).

But in the bottom row, when a material is present, the pattern is missing. It has been completely overwritten by the purple diffuse color!

Image

Second fix

This is after I commented out the code that overwrites the vertex colors. It's what I expect to happen. Well, mostly. I'm actually not sure why the AmbientColor is black on the bottom row, since ambient is set to 100% in blender. Of course, you see the dim blue again in the last two spheres, because AmbientColor is being (correctly) overridden there.

Image

Now, does this need to be fixed? I don't actually know. My original goal before I got so caught up was to combine vertex colors with textures, so I guess I'll look into that next... But I hope this explains the problem I was perceiving.

In any case, I learned a lot about Irrlicht! :P

Source code

Code: Select all

#include <irrlicht.h>
using namespace irr;
using namespace core;
using namespace scene;
using namespace video;
 
IrrlichtDevice *device;
IVideoDriver *driver;
ISceneManager *smgr;
 
void addMesh(const char *path, vector3df pos, u8 colorMaterial){
    ISceneNode *node = smgr->addMeshSceneNode(smgr->getMesh(path), 0, -1, pos);
    node->setMaterialFlag(EMF_LIGHTING, 1);
    node->getMaterial(0).ColorMaterial = colorMaterial;
}
 
void addVariations(const char *path, float yPos){
    addMesh(path, vector3df(-4.5, yPos, 5), ECM_NONE);
    addMesh(path, vector3df(-1.5, yPos, 5), ECM_DIFFUSE);
    addMesh(path, vector3df(+1.5, yPos, 5), ECM_AMBIENT);
    addMesh(path, vector3df(+4.5, yPos, 5), ECM_DIFFUSE_AND_AMBIENT);
}
 
int main(){
    device = createDevice(EDT_OPENGL, dimension2d<u32>(512, 256));
    driver = device->getVideoDriver();
    smgr = device->getSceneManager();
    smgr->addCameraSceneNode();
 
    // white light from above
    smgr->addLightSceneNode(0, vector3df(0,50,-10));
    // bluish ambient
    smgr->setAmbientLight(SColorf(0.1f,0.2f,0.3f));
 
    addVariations("no_mat.dae", +1.5f);
    addVariations("mat.dae", -1.5f);
 
    while (device->run())
    if (device->isWindowActive()) {
        driver->beginScene(true, true, 0);
        smgr->drawAll();
        driver->endScene();
    }
 
    device->drop();
    return 0;
}
Mesh files

Collada without material: https://dl.dropboxusercontent.com/u/305 ... no_mat.dae
Collada with material: https://dl.dropboxusercontent.com/u/305 ... st/mat.dae
Blender: https://dl.dropboxusercontent.com/u/305 ... /mat.blend
hendu
Posts: 2600
Joined: Sat Dec 18, 2010 12:53 pm

Re: [fixed]Collada vertex colors ignored

Post by hendu »

My original goal before I got so caught up was to combine vertex colors with textures, so I guess I'll look into that next...
You should write a shader for that. Gives you complete leeway on how to combine them.
CuteAlien
Admin
Posts: 9651
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: [fixed]Collada vertex colors ignored

Post by CuteAlien »

I can't reproduce your second result here when commenting out the setVertexColors lines. Unfortunately I don't know if those 2 places where setVertexColor (and setVertexColorAlpha) are used should really affect the vertex colors or if maybe one of them should instead set the SMaterial colors. I tried understanding the Collada documenation - but as usual when it comes to Collada this is nothing to be understandable by giving it a quick look. Figuring out what a material really means when the way Materials are handled is not even the same in Irrlicht/Collada/Blender is pretty hard :-(

So not really sure what to do now - I won't find the time to dig into this deep enough to understand the correct solution. I hope Hybrid can find some time to look at this - he knows more about materials and how they are supposed to behave and might have an idea.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
sf17k
Posts: 6
Joined: Sat Mar 15, 2014 6:04 am

Re: [fixed]Collada vertex colors ignored

Post by sf17k »

CuteAlien wrote:I can't reproduce your second result here when commenting out the setVertexColors lines. Unfortunately I don't know if those 2 places where setVertexColor (and setVertexColorAlpha) are used should really affect the vertex colors or if maybe one of them should instead set the SMaterial colors.
Ah, strange. Well, I guess the reason it bothered me is because I spent so long twiddling with settings trying to get the pattern to show up, on the assumption that the vertex colors were still there. From a newbie perspective, it's very confusing.

Yes, Collada is mind-boggling. Maybe I'd use something else, if I could. Thanks for trying, though! I can work with the first fix for now.
hendu wrote:You should write a shader for that. Gives you complete leeway on how to combine them.
This'll help, thanks.

EDIT: For completeness' sake, here's the patch that worked for me:
https://dl.dropboxusercontent.com/u/305 ... .cpp.patch

I compiled with:

Code: Select all

scons -f source/Irrlicht/SConstruct platform=win32-mingw
CuteAlien
Admin
Posts: 9651
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: [partly fixed]Collada vertex colors ignored

Post by CuteAlien »

Thanks for the patch and the testcases. It might even be correct, thought I wonder why the vertex-color was then set in the first place as it's not the obvious thing to code. Guess I'll have to wriggle my way once more through the Collada docs some time.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
sf17k
Posts: 6
Joined: Sat Mar 15, 2014 6:04 am

Re: [partly fixed]Collada vertex colors ignored

Post by sf17k »

Just a note for future reference: I've been hanging out on the Blender IRC. Collada (along with FBX) are currently major topics of discussion there. Few people want to work on supporting them because of their complexity, but they are important formats. It seems the best bet for games at this time would be to use a custom format instead (this is common practice).
Post Reply