Trouble with cube lighting and shadows

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.
Post Reply
laleksic
Posts: 5
Joined: Sun Jun 26, 2016 8:19 am

Trouble with cube lighting and shadows

Post by laleksic »

Edit: Thanks Vectrotek, mongoose7 and aaammmsterdddam for the help! My lighting problem is fixed and was due to incorrect normals in my cube model. Though I am still having trouble with the shadows and would very much appreciate some help with that.

Hi,

I'm having some trouble with lighting and shadows in relation to cube scene nodes. What I have set up is a test scene consisting entirely of cube scene nodes, and a single light circling above.

Image
Screenshot of lighting problem (shadows disabled). Each cube scene node is getting shaded individually, instead of the light shading the scene as a whole.

https://www.youtube.com/watch?v=mShkG9zUryw
Video of shadow problem (lighting disabled). A video was necessary to demonstrate the problem with the shadows, as in addition to the fact that they look totally wrong, they also disappear entirely when looked at from certain angles.

It is my guess that both problems are caused by the fact that the scene's architecture is made up of many cube scene nodes instead of one big mesh. I have tried to use LonesomeDucky's Mesh Combiner (http://irrlicht.sourceforge.net/forum/v ... =6&t=39598) to combine all the cubes into a single mesh, but with no success. The same problems are present. I am certain I am using the Mesh Combiner correctly, so the problem is not there.

I would like to avoid having to model whole scenes in Blender (unsure if that would actually fix my problems), as I'm aiming for making a small game whose level architecture is similar to Wolfenstein 3d (levels made entirely of equally shaped cubes), only with a 3rd dimension. I have already coded a system that renders and handles collision detection for such levels in Irrlicht from simple info in .txt files.

So, I'd appreciate it if someone could point me in the right direction to a solution that can coexist with what I've got so far. If necessary I will post my source code.

Thanks in advance,
L. Aleksic
Last edited by laleksic on Mon Jun 27, 2016 11:41 am, edited 1 time in total.
mongoose7
Posts: 1227
Joined: Wed Apr 06, 2011 12:13 pm

Re: Trouble with cube lighting and shadows

Post by mongoose7 »

It looks like your normals are not correct. How did you generate the cubes? If you used the Irrlicht geometry maker, then I'm here to tell you that it makes spheres and not cubes. You will have to make your own cubes. You will need 24 vertices, four for each face. (I think Irrlicht uses 12 vertices!) Then you can assign normals and UV-coordinates.
laleksic
Posts: 5
Joined: Sun Jun 26, 2016 8:19 am

Re: Trouble with cube lighting and shadows

Post by laleksic »

mongoose7 wrote:It looks like your normals are not correct. How did you generate the cubes? If you used the Irrlicht geometry maker, then I'm here to tell you that it makes spheres and not cubes. You will have to make your own cubes. You will need 24 vertices, four for each face. (I think Irrlicht uses 12 vertices!) Then you can assign normals and UV-coordinates.
Thanks for the quick reply and the help! I did use Irrlicht's addCubeSceneNode to make the cubes, and yes, after making my own 24-vertex cubes in Blender, there is an improvement, though it's not right yet.
Image
I removed the textures so as to better see the shading. The tops of the cubes are perfect, though the sides are still wrong. I guess I should have mentioned, I am pretty much totally new to both Irrlicht and 3d modelling/programming. I'm still not sure exactly what vertex normals are and how they affect lighting.

Image
In any case here is how the normals are positioned in my new cube. Blender's "Recalculate normals" does nothing, and I'm still looking for a way to rotate the normals inside Blender (except just flipping their direction), but in the meantime, how are they even supposed to be rotated for the lighting to work?

In any case, thanks for setting me on the right track :)!

Edit: Okay, so I looked up vertex normals and understand what they are.
http://www.opengl-tutorial.org/beginners-tutorials/tutorial-8-basic-shading/ wrote:...we call the normal of a vertex the combination of the normals of the surroundings triangles.
So I cannot rotate them, and with 24 vertices, no two cube faces share a vertex, so the normal of each vertex corresponds to the normal of the cube face to which it belongs, so what is the problem?

Edit 2: Oh, and the new cube did nothing whatsoever to help the shadow problem.

Edit 3: If I'm to trust this image found by googling "cube vertex normals", my cube's vertex normals should be alright :? .
Image
Vectrotek
Competition winner
Posts: 1087
Joined: Sat May 02, 2015 5:05 pm

Re: Trouble with cube lighting and shadows

Post by Vectrotek »

Between Blender and Irrlicht your model should exist in a specific 3D format.. Which is it?
What sometimes happens, is that when Blender exports an object for use in other programs
it "Merges" the vertices, meaning that the vertex for a given corner, is turned from three different
vertices (three normal's) into one vertex (and one normal).
This sometimes happens because the exporter thinks it is doing us a favor optimizing for file size.
This sometimes happens when blender exports as "*.x" or "b3d".
Your Blender Cube is correct because it shows Three Normals eminating from a corner vertex.

Irrlicht has a method of "Recalculating Normals" which also may result in Vertices being merged
and resulting in one "averaged" normal. (I think)

Give us more info on your workflow when getting the object from Blender into Irrlicht.
Blender exports an "*.obj" fine, so if that was the case then Irrlicht had optimised your
data causing Three Vertices and Three Normals to become One Vertex and One Normal.

It would be helpfull if you show us the line in your code that loads this model.
laleksic
Posts: 5
Joined: Sun Jun 26, 2016 8:19 am

Re: Trouble with cube lighting and shadows

Post by laleksic »

Vectrotek wrote:Between Blender and Irrlicht your model should exist in a specific 3D format.. Which is it?
What sometimes happens, is that when Blender exports an object for use in other programs
it "Merges" the vertices, meaning that the vertex for a given corner, is turned from three different
vertices (three normal's) into one vertex (and one normal).
This sometimes happens because the exporter thinks it is doing us a favor optimizing for file size.
This sometimes happens when blender exports as "*.x" or "b3d".
Your Blender Cube is correct because it shows Three Normals eminating from a corner vertex.

Irrlicht has a method of "Recalculating Normals" which also may result in Vertices being merged
and resulting in one "averaged" normal. (I think)

Give us more info on your workflow when getting the object from Blender into Irrlicht.
Blender exports an "*.obj" fine, so if that was the case then Irrlicht had optimised your
data causing Three Vertices and Three Normals to become One Vertex and One Normal.

It would be helpfull if you show us the line in your code that loads this model.
I see. I exported it from Blender as an .obj, so that should be alright. And here goes all of the code which has anything to do with the cube nodes.

Code: Select all

...
//Here I import the cube. Nothing fancy.
 
            IMesh *temp_mesh = smgr->getMesh("cube.obj");
...
 
//lvl is a 3d array of custom structs which hold info on each cube in the level's 3d grid
//node is IMeshSceneNode*
 
                lvl[i][j][k].node = smgr->addMeshSceneNode(temp_mesh); //addCubeSceneNode();
                lvl[i][j][k].node->setPosition(vector3df(
                                            BLOCK_DIMENSION/2+BLOCK_DIMENSION*k,
                                            BLOCK_DIMENSION/2+BLOCK_DIMENSION*i,
                                            BLOCK_DIMENSION/2+BLOCK_DIMENSION*j)
                                            );
                lvl[i][j][k].node->setScale(vector3df(
                                        BLOCK_DIMENSION/10.f,
                                        BLOCK_DIMENSION/10.f,
                                        BLOCK_DIMENSION/10.f)
                                        );
 
                lvl[i][j][k].node->addShadowVolumeSceneNode();
 
//The only thing I guess might be responsible for influencing the normals is the next line, though commenting it out has no effects other than making the light dimmer. I also recall one of the irrlicht tutorials said that if I want shadows or lighting with a scaled model, I should normalize the normals.
                lvl[i][j][k].node->setMaterialFlag(video::EMF_NORMALIZE_NORMALS, true);
 
                lvl[i][j][k].node->setMaterialFlag(EMF_LIGHTING, true);
                lvl[i][j][k].node->setMaterialTexture( 0, driver->getTexture("metalface.jpg") );
 
//Here I create triangle selectors for each cube and add them to a meta selector (world_collision) for collision.
                lvl[i][j][k].selector=smgr->createTriangleSelectorFromBoundingBox(lvl[i][j][k].node);
                world_collision->addTriangleSelector(lvl[i][j][k].selector);
                lvl[i][j][k].selector->drop();
...
Thanks!

Edit: The tops of the cubes are getting shaded alright. The sides are now the only problem. Why would that be so?

Edit: Here's the model: http://www.filedropper.com/cube_2

Edit: Found Irrlicht's method of recalculating normals and tried adding it to my code, even though my normals are alright, and recalculating would merge vertices and screw it up, I wanted to see what would happen.

Code: Select all

smgr->getMeshManipulator()->recalculateNormals(temp_mesh);
Makes no difference :? . The top side of the cube still shades okay, the rest are still messed up.
mongoose7
Posts: 1227
Joined: Wed Apr 06, 2011 12:13 pm

Re: Trouble with cube lighting and shadows

Post by mongoose7 »

Err, cube.obj has no normals! It is a text file so you can look at it yourself. Here is one of the 'face' lines

f 2/1 1/2 4/3 3/4

See, it specifies four vertices with their UV coordinates, but no normals. There are also no 'normal' lines in the file. Keep working with Blender until you get the normals in the file.
Cube_
Posts: 1010
Joined: Mon Oct 24, 2011 10:03 pm
Location: 0x45 61 72 74 68 2c 20 69 6e 20 74 68 65 20 73 6f 6c 20 73 79 73 74 65 6d

Re: Trouble with cube lighting and shadows

Post by Cube_ »

in blender you need to check the "export normals" checkbox in the object exporter.
It's unchecked by default.
"this is not the bottleneck you are looking for"
Vectrotek
Competition winner
Posts: 1087
Joined: Sat May 02, 2015 5:05 pm

Re: Trouble with cube lighting and shadows

Post by Vectrotek »

Export Options..
Image
Vectrotek
Competition winner
Posts: 1087
Joined: Sat May 02, 2015 5:05 pm

Re: Trouble with cube lighting and shadows

Post by Vectrotek »

Far Below is what the OBJ (ascii format) should look like..
See the Face Index Definition Lines.. "f 11/1/1 13/2/1 15/3/1 9/4/1"..
There are now THREE indices of which one is the Normal Index..
You now should have "vn" along with "v" and "vt"..
Debatable, but avoid using Quads in your models, rather use Triangles..
(this example is still quads)
Reason for this, among others comes into play when you start playing with Normal Maps etc..
Also, Triangles are better because:

Code: Select all

lvl[i][j][k].selector=smgr->createTriangleSelectorFromBoundingBox(lvl[i][j][k].node);
I'm not too sure, but for Triangle Selector to work Irrlicht may need to convert from Quads to Triangles which
again may mess with your Geometric Data..
Another thing is when Irrlicht generates shadows it is probably better to have your data as Triangles.
In Blender, select: (Mesh - Faces - Triangulate Faces) before export..
(you may want to keep your development version models as quads though)

"*.obj"..

Code: Select all

 
# Blender v2.76 (sub 0) OBJ File: ''
# http://www.blender.org
mtllib cube_002.mtl
o Cube
v 0.000000 0.000000 0.000000
v 0.000000 1.000000 0.000000
v 0.000000 0.000000 -1.000000
v 0.000000 1.000000 -1.000000
v 1.000000 0.000000 0.000000
v 1.000000 1.000000 0.000000
v 1.000000 0.000000 -1.000000
v 1.000000 1.000000 -1.000000
v 0.000000 0.000000 0.000000
v 0.000000 0.000000 0.000000
v 0.000000 1.000000 0.000000
v 0.000000 1.000000 0.000000
v 0.000000 1.000000 -1.000000
v 0.000000 1.000000 -1.000000
v 0.000000 0.000000 -1.000000
v 0.000000 0.000000 -1.000000
v 1.000000 1.000000 -1.000000
v 1.000000 1.000000 -1.000000
v 1.000000 0.000000 -1.000000
v 1.000000 0.000000 -1.000000
v 1.000000 1.000000 0.000000
v 1.000000 1.000000 0.000000
v 1.000000 0.000000 0.000000
v 1.000000 0.000000 0.000000
vt 0.333333 0.333333
vt 0.333333 0.000000
vt 0.666667 0.000000
vt 0.666667 0.333333
vt 0.000000 0.666667
vt 0.000000 0.333333
vt 0.333333 0.666667
vt 0.000000 0.000000
vt 0.666667 0.666667
vt 1.000000 0.333333
vt 1.000000 0.000000
vt 0.333333 1.000000
vt 0.000000 1.000000
vn -1.000000 0.000000 0.000000
vn 0.000000 0.000000 -1.000000
vn 1.000000 0.000000 0.000000
vn 0.000000 0.000000 1.000000
vn 0.000000 -1.000000 0.000000
vn 0.000000 1.000000 0.000000
usemtl S_001
s off
f 11/1/1 13/2/1 15/3/1 9/4/1
f 14/5/2 17/6/2 19/1/2 16/7/2
f 18/6/3 21/8/3 23/2/3 20/1/3
f 22/1/4 12/4/4 10/9/4 24/7/4
f 1/10/5 3/4/5 7/3/5 5/11/5
f 6/5/6 8/7/6 4/12/6 2/13/6
mongoose7
Posts: 1227
Joined: Wed Apr 06, 2011 12:13 pm

Re: Trouble with cube lighting and shadows

Post by mongoose7 »

When Irrlicht loads OBJ files it converts quads into triangles, so this will not be a problem.
laleksic
Posts: 5
Joined: Sun Jun 26, 2016 8:19 am

Re: Trouble with cube lighting and shadows

Post by laleksic »

Thank you everyone for the help! Lighting works like a charm now :D. Much appreciated. It was a very stupid and simple mistake on my part. Thanks for the help and the patience. Though, if it's not too much hassle, could anybody shed a light on my problem with the shadows? They still look wrong and act odd, such as being cast on the sky/void and disappearing when looked at from odd angles. Now that my cube's normals are fixed, should I try the Mesh Combiner again?
Image
Image
mongoose7
Posts: 1227
Joined: Wed Apr 06, 2011 12:13 pm

Re: Trouble with cube lighting and shadows

Post by mongoose7 »

You've got a lot of cubes there. You wouldn't mind cutting it back to just a few?

On another angle, the shadow method counts the number of triangles a light ray passes through to judge whether an intersection is inside or outside the mesh. Since touching cubes have a face in common, I don't know whether it would correctly count the two faces. Presumably it does since they are rendered separately. You can try the mesh combiner, or try leaving a gap between the cubes, or reduce the problem to a small number of cubes.

I think your skybox may have lighting enabled?
laleksic
Posts: 5
Joined: Sun Jun 26, 2016 8:19 am

Re: Trouble with cube lighting and shadows

Post by laleksic »

Well, I actually intended the actual levels to be larger and thus have even more cubes then this quick mock up test map. Though if necessary I would switch to more smaller levels.

As for the shadows, I had a similar thought, though I would have guessed that the inside doubled faces were part of the problem. The mesh combiner doesn't remove them. However as .obj are actually .txt files, I could code a system which generates a single level mesh from the level info instead of drawing many cubes to a 3d grid of sorts. This singular mesh would have no inside faces obviously.

Though it's too much of a hassle if the shadows won't work with it, so I'd ask one final question: Has anybody made with irrlicht a scene where a large singular map mesh casts shadows on itself in such a way? Is it feasible? If not, I'll most probably give up on the shadows. I don't really care for fancy effects, though I thought I'd try everything Irrlicht has to offer.
mongoose7
Posts: 1227
Joined: Wed Apr 06, 2011 12:13 pm

Re: Trouble with cube lighting and shadows

Post by mongoose7 »

I've never used stencil shadows but, theoretically, self-shadowing is possible. Surely you can set up a test, if you are interested in shadows. You can probably google for "shadow volumes" or "stencil shadows" if you want to understand the technique.
Post Reply