Lighting Problems

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Kortalh
Posts: 10
Joined: Wed Feb 23, 2011 10:52 pm

Lighting Problems

Post by Kortalh »

Hello. :) I've tried searching around a bit, but couldn't seem to find a solution to my problem.

I'm working on a tile-based system, both to make maps easily reconfigurable, and because the game involves square-based movement.

All has been going well so far, and I've come up with a small level:
Image
(Don't mind the lazy models & textures -- they're just placeholders 'til I get the code running right)

However, when I tried adding lighting to the level, everything went to hell. This screenshot has about 5-6 lights in it, and as you can see, the light hits the tiles strangely and results in a very ugly looking map.
Image

My initial reaction was that, if I added ambient lighting, the darkest areas wouldn't look so bad and the odd shading might not be a big deal.

However, I was originally exporting from Blender into .obj models -- and through a very frustrating bit of trial and error, I eventually realized that .obj models don't pick up ambient lighting for some reason.

So, with that in mind, I redid the models, this time exporting them in the .x format.

The good news? .x models do pick up ambient lighting correctly. The bad news? That's about all they seem to pick up correctly.
Image

The lights are either 100% white or disabled. No amount of tweaking the colors or radius would make them any dimmer. Also, as you can see, the shadows around the character models are about 4 times bigger than they ought to be (I tried NORMALIZE_NORMALS to no success).


So, at this point, I'm completely stumped. I don't know if I'm just overlooking something, or if I should try a different model format, or what. Clearly, other people aren't having the same problems (or, if they are, I couldn't find their posts).

Does anyone have any suggestions?
mongoose7
Posts: 1227
Joined: Wed Apr 06, 2011 12:13 pm

Post by mongoose7 »

It sure looks like a problem with normals. Could you try turning on normals debugging and see where they are pointing?
Kortalh
Posts: 10
Joined: Wed Feb 23, 2011 10:52 pm

Post by Kortalh »

Will do. How do I turn it on? :)
serengeor
Posts: 1712
Joined: Tue Jan 13, 2009 7:34 pm
Location: Lithuania

Post by serengeor »

Kortalh wrote:Will do. How do I turn it on? :)
http://irrlicht.sourceforge.net/docu/cl ... c139bf9de7
Working on game: Marrbles (Currently stopped).
mongoose7
Posts: 1227
Joined: Wed Apr 06, 2011 12:13 pm

Post by mongoose7 »

Specifically,

Code: Select all

node->setDebugDataVisible(irr::scene::EDS_NORMALS);
Kortalh
Posts: 10
Joined: Wed Feb 23, 2011 10:52 pm

Post by Kortalh »

I'm not really sure what I'm looking at, so I took several different screenshots.

This first one has debugging enabled for everything:
Image[/img]

This one has it enabled just for the ground tiles:
Image

This one has it enabled just for the walls:
Image

This one has it enabled just for the characters:
Image


Hopefully those are useful.
mongoose7
Posts: 1227
Joined: Wed Apr 06, 2011 12:13 pm

Post by mongoose7 »

Is this correct? - For each ground tile there are four vertices, not shared with the adjacent ground tiles? At each of these locations there are normals pointing straight up (correctly) and normals pointing along the edges (incorrectly)?

From the initial image, it looks as if the normals at the north-east corners of the tiles are correctly aligned (pointing up), but the normals for the other three vertices are horizontal instead of vertical.

Is it possible to just set all these normals to (0, 1, 0)?

Or not use normals at all, though the effect would depend on the "winding" of the vertices.
Kortalh
Posts: 10
Joined: Wed Feb 23, 2011 10:52 pm

Post by Kortalh »

Well, I'm actually using cubes for the tiles, rather than planes, so that the user could go up or down a level without the tiles vanishing. So the tiles have 8 vertices.

Also note that it gives the same pure-white lighting effect to walls as it does to the ground tiles.


I'm essentially just copying the code from tutorial 8, and I notice they use a .x file there too without any issues. So I wonder if maybe I rewrote the code incorrectly?

Here's the relevant parts of my code, maybe you can spot something I did wrong?


Setting up the device...

Code: Select all

	// Set up the device parameters
	irr::SIrrlichtCreationParameters params;
	params.DriverType = EDT_DIRECT3D9; //EDT_OPENGL;
	params.WindowSize = dimension2d<u32>(1024,768);
	params.Bits = 32;
	params.Fullscreen = false;
	params.Stencilbuffer = true;
	params.Vsync = false;
	params.AntiAlias = true;
	params.EventReceiver = &events;
	// Create the device
	IrrlichtDevice *device = createDeviceEx(params); 
Adding a light node...

Code: Select all

	scene::ISceneNode* node = 0;
	// Create light
	node = scene->addLightSceneNode(0, core::vector3df(10,3,-7), video::SColorf(1.0f,1.0f,1.0f,1.0f), 4.0f);
Creating a tile...

Code: Select all

	IAnimatedMeshSceneNode* tile;
	tileMeshname = "mymedia/brick_wall.x";
	tileTexturename = "mymedia/brick_wall.tga";

	// Load the model
	tile = scene->addAnimatedMeshSceneNode(scene->getMesh(tileMeshname));
	// Apply a texture
	tile->setMaterialTexture( 0, driver->getTexture(tileTexturename) );
	// Disable lighting
	tile->setMaterialFlag(video::EMF_LIGHTING, true);
	// Reposition the model
	tile->setPosition(core::vector3df(posX,posZ,posY));
	tile->setRotation(core::vector3df(0,rotation,0));

Creating a character...

Code: Select all

	IAnimatedMeshSceneNode* char;

	tileMeshname = "mymedia/character_cube.x";
	tileTexturename = "mymedia/human_skin.tga";

	// Load the model
	tile = scene->addAnimatedMeshSceneNode(scene->getMesh(tileMeshname));
	// Apply a texture
	tile->setMaterialTexture( 0, driver->getTexture(tileTexturename) );
	// Disable lighting
	tile->setMaterialFlag(video::EMF_LIGHTING, true);
	// Reposition the model
	tile->setPosition(core::vector3df(posX,posZ,posY));
	tile->setRotation(core::vector3df(0,rotation,0));

	tile->addShadowVolumeSceneNode();
	// Set shadow color
	scene->setShadowColor(video::SColor(50,0,0,0));

And drawing the scene...

Code: Select all

	// Begin the scene
	driver->beginScene(true, true, video::SColor(255,0,0,0));
	// Draw the scene
	scene->drawAll();
	// End the scene
	driver->endScene();
mongoose7
Posts: 1227
Joined: Wed Apr 06, 2011 12:13 pm

Post by mongoose7 »

I guess there is a problem with brickwall.x. Open it up and search for "MeshNormals".
Kortalh
Posts: 10
Joined: Wed Feb 23, 2011 10:52 pm

Post by Kortalh »

Here's the Mesh and MeshNormals from both the wall and ground models. They're from the newest version of Blender, 2.57, using the included exporter addon.

I'm not sure what I'm looking at here, either, but hopefully you can make sense of it. :D


brick_wall.x

Code: Select all

    Mesh { //Cube_001 Mesh
      24;
      -1.000000; 1.000000;-1.000000;,
      -1.000000;-1.000000;-1.000000;,
       1.000000;-1.000000;-1.000000;,
       1.000000; 1.000000;-1.000000;,
       0.999999;-1.000001; 1.000000;,
      -1.000000;-1.000000; 1.000000;,
      -1.000000; 1.000000; 1.000000;,
       1.000000; 0.999999; 1.000000;,
       1.000000;-1.000000;-1.000000;,
       0.999999;-1.000001; 1.000000;,
       1.000000; 0.999999; 1.000000;,
       1.000000; 1.000000;-1.000000;,
      -1.000000;-1.000000;-1.000000;,
      -1.000000;-1.000000; 1.000000;,
       0.999999;-1.000001; 1.000000;,
       1.000000;-1.000000;-1.000000;,
      -1.000000; 1.000000;-1.000000;,
      -1.000000; 1.000000; 1.000000;,
      -1.000000;-1.000000; 1.000000;,
      -1.000000;-1.000000;-1.000000;,
      -1.000000; 1.000000; 1.000000;,
      -1.000000; 1.000000;-1.000000;,
       1.000000; 1.000000;-1.000000;,
       1.000000; 0.999999; 1.000000;;
      6;
      4;0;1;2;3;,
      4;4;5;6;7;,
      4;8;9;10;11;,
      4;12;13;14;15;,
      4;16;17;18;19;,
      4;20;21;22;23;;
      MeshNormals { //Cube_001 Normals
        24;
         0.000000; 0.000000;-1.000000;,
         0.000000; 0.000000;-1.000000;,
         0.000000; 0.000000;-1.000000;,
         0.000000; 0.000000;-1.000000;,
         0.000000;-0.000000; 1.000000;,
         0.000000;-0.000000; 1.000000;,
         0.000000;-0.000000; 1.000000;,
         0.000000;-0.000000; 1.000000;,
         1.000000;-0.000000; 0.000000;,
         1.000000;-0.000000; 0.000000;,
         1.000000;-0.000000; 0.000000;,
         1.000000;-0.000000; 0.000000;,
        -0.000000;-1.000000;-0.000000;,
        -0.000000;-1.000000;-0.000000;,
        -0.000000;-1.000000;-0.000000;,
        -0.000000;-1.000000;-0.000000;,
        -1.000000; 0.000000;-0.000000;,
        -1.000000; 0.000000;-0.000000;,
        -1.000000; 0.000000;-0.000000;,
        -1.000000; 0.000000;-0.000000;,
         0.000000; 1.000000; 0.000000;,
         0.000000; 1.000000; 0.000000;,
         0.000000; 1.000000; 0.000000;,
         0.000000; 1.000000; 0.000000;;
        6;
        4;0;1;2;3;,
        4;4;5;6;7;,
        4;8;9;10;11;,
        4;12;13;14;15;,
        4;16;17;18;19;,
        4;20;21;22;23;;
      } //End of Cube_001 Normals
ground_tile.x

Code: Select all

    Mesh { //Cube_001 Mesh
      24;
      -1.000000; 1.000000;-1.000000;,
      -1.000000;-1.000000;-1.000000;,
       1.000000;-1.000000;-1.000000;,
       1.000000; 1.000000;-1.000000;,
       0.999999;-1.000001; 1.000000;,
      -1.000000;-1.000000; 1.000000;,
      -1.000000; 1.000000; 1.000000;,
       1.000000; 0.999999; 1.000000;,
       1.000000;-1.000000;-1.000000;,
       0.999999;-1.000001; 1.000000;,
       1.000000; 0.999999; 1.000000;,
       1.000000; 1.000000;-1.000000;,
      -1.000000;-1.000000;-1.000000;,
      -1.000000;-1.000000; 1.000000;,
       0.999999;-1.000001; 1.000000;,
       1.000000;-1.000000;-1.000000;,
      -1.000000; 1.000000;-1.000000;,
      -1.000000; 1.000000; 1.000000;,
      -1.000000;-1.000000; 1.000000;,
      -1.000000;-1.000000;-1.000000;,
      -1.000000; 1.000000; 1.000000;,
      -1.000000; 1.000000;-1.000000;,
       1.000000; 1.000000;-1.000000;,
       1.000000; 0.999999; 1.000000;;
      6;
      4;0;1;2;3;,
      4;4;5;6;7;,
      4;8;9;10;11;,
      4;12;13;14;15;,
      4;16;17;18;19;,
      4;20;21;22;23;;
      MeshNormals { //Cube_001 Normals
        24;
         0.000000; 0.000000;-1.000000;,
         0.000000; 0.000000;-1.000000;,
         0.000000; 0.000000;-1.000000;,
         0.000000; 0.000000;-1.000000;,
         0.000000;-0.000000; 1.000000;,
         0.000000;-0.000000; 1.000000;,
         0.000000;-0.000000; 1.000000;,
         0.000000;-0.000000; 1.000000;,
         1.000000;-0.000000; 0.000000;,
         1.000000;-0.000000; 0.000000;,
         1.000000;-0.000000; 0.000000;,
         1.000000;-0.000000; 0.000000;,
        -0.000000;-1.000000;-0.000000;,
        -0.000000;-1.000000;-0.000000;,
        -0.000000;-1.000000;-0.000000;,
        -0.000000;-1.000000;-0.000000;,
        -1.000000; 0.000000;-0.000000;,
        -1.000000; 0.000000;-0.000000;,
        -1.000000; 0.000000;-0.000000;,
        -1.000000; 0.000000;-0.000000;,
         0.000000; 1.000000; 0.000000;,
         0.000000; 1.000000; 0.000000;,
         0.000000; 1.000000; 0.000000;,
         0.000000; 1.000000; 0.000000;;
        6;
        4;0;1;2;3;,
        4;4;5;6;7;,
        4;8;9;10;11;,
        4;12;13;14;15;,
        4;16;17;18;19;,
        4;20;21;22;23;;
      } //End of Cube_001 Normals
mongoose7
Posts: 1227
Joined: Wed Apr 06, 2011 12:13 pm

Post by mongoose7 »

Your normals are OK. For example, the last four lines under Mesh define the top of the cube:

Code: Select all

      -1.000000; 1.000000; 1.000000;, 
      -1.000000; 1.000000;-1.000000;, 
       1.000000; 1.000000;-1.000000;, 
       1.000000; 0.999999; 1.000000;;
and the last 4 lines under MeshNormals

Code: Select all

         0.000000; 1.000000; 0.000000;, 
         0.000000; 1.000000; 0.000000;, 
         0.000000; 1.000000; 0.000000;, 
         0.000000; 1.000000; 0.000000;;
show the normals pointing up. So I don't know what is going on.

It is curious, though, that the normals for the characters appear detached from the mesh. The wall normals also appear detached from the vertices.

If you are desperate, you could try exporting as a .obj file to see if it makes a difference. I'm sorry that I cannot make any sense of your problem.

Some other points. Try it without scaling and rotating, to see if it makes a difference. Also, you added a texture to the material for the node. I don't know how this reacts with an animated mesh - they normally have their textures defined outside of Irrlicht. The materials are set up by the X-file loader.

When texturing, the vertex colour is normally multiplied with the texture, so it is a good idea to have white as the vertex colour, but you haven't done anything out od the ordinary so this shouldn't be the problem.

You could also try loading the cube(s) as a static mesh but I'm not sure how to do this. Or you could try Irrlicht's cube mesh scene node.
greenya
Posts: 1012
Joined: Sun Jan 21, 2007 1:46 pm
Location: Ukraine
Contact:

Post by greenya »

I don't know why, but i also noticed that Irrlicht renders lighting in OpenGL is much better than in Direct3D8/9.
mongoose7
Posts: 1227
Joined: Wed Apr 06, 2011 12:13 pm

Post by mongoose7 »

Is that just because the OpenGL lighting model is different from the Direct3D one (eg light attenuation) or do you think Irrlicht serves OpenGL better than it does Direct3D?
Radikalizm
Posts: 1215
Joined: Tue Jan 09, 2007 7:03 pm
Location: Leuven, Belgium

Post by Radikalizm »

mongoose7 wrote:Is that just because the OpenGL lighting model is different from the Direct3D one (eg light attenuation) or do you think Irrlicht serves OpenGL better than it does Direct3D?
Yes, the fixed function lighting in OpenGL differs from the lighting in D3D
Only way to avoid this would be to write your own lighting shaders I suppose
greenya
Posts: 1012
Joined: Sun Jan 21, 2007 1:46 pm
Location: Ukraine
Contact:

Post by greenya »

mongoose7 wrote:Is that just because the OpenGL lighting model is different from the Direct3D one (eg light attenuation) or do you think Irrlicht serves OpenGL better than it does Direct3D?
I just want to say that, if you made something that looks OK in OpenGL and uses lighting, than if you switch video driver to Direct3D you will get much worse result. I confirm this on ATI and NVidia video cards. I don't know how to fix this but i think this is a bug. Because different drivers renders differently simple things.
Post Reply