Per pixel lighting in custom scene node

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
Proxx
Posts: 6
Joined: Sat Mar 02, 2013 3:57 pm

Per pixel lighting in custom scene node

Post by Proxx »

Hi,
I am new to this forum, so first of all: Hello beginners and experts of Irrlicht ;-)

I have played around with Irrlicht now a couple of days and use the .NET wrapper Irrlicht Lime. Now, I would like to get a deeper understanding in per pixel lighting in conjunction with custom scene node.

In my project, I have already implemented a custom scene node that is called "Floor". This is simply a node that gets some "From" and "To" Vector3Di and creates a plane with a texture. Here is some code:

Code: Select all

 
 
public class Floor : SceneNode
{
    public Floor(SceneNode parent, SceneManager smgr, int id, string name, Vector3Di from, Vector3Di to)
        : base(parent, smgr, id)
        {
            ...
        }
 
    ...
}
 
To build up the geometry, I have a method BuildGeometry that looks like this and works fine:

Code: Select all

 
private void BuildGeometry(Vector3Di from, Vector3Di to)
{
    var min = new Vector3Di(Math.Min(from.X, to.X), Math.Min(from.Y, to.Y), Math.Min(from.Z, to.Z));
    var max = new Vector3Di(Math.Max(from.X, to.X), Math.Max(from.Y, to.Y), Math.Max(from.Z, to.Z));
 
    var numU = (max - min).X;
    var numV = (max - min).Z;
 
    this.vertices = new Vertex3D[4];
 
    this.vertices[0] = new Vertex3D(min.X * 50, from.Y * 50, min.Z * 50, 0, 1, 0, new Color(0, 255, 255), 0, 0);
    this.vertices[1] = new Vertex3D(max.X * 50, from.Y * 50, min.Z * 50, 0, 1, 0, new Color(0, 255, 255), 0, numU);
    this.vertices[2] = new Vertex3D(max.X * 50, from.Y * 50, max.Z * 50, 0, 1, 0, new Color(0, 255, 255), numV, numU);
    this.vertices[3] = new Vertex3D(min.X * 50, from.Y * 50, max.Z * 50, 0, 1, 0, new Color(0, 255, 255), numV, 0);
}
 
To setup the material, I set the following parameters in a method called SetupMaterial

Code: Select all

 
private void SetupMaterial(SceneManager sceneManager, string name)
{
    this.material.Lighting = false;
    this.material.BackfaceCulling = true;
 
    var texture = sceneManager.VideoDriver.GetTexture("textures/" + name + ".png");
    var normalMap = sceneManager.VideoDriver.GetTexture("textures/" + name + "_normal.png");
 
    this.material.SetTexture(0, texture);
    this.material.SetTexture(1, normalMap);
    this.material.Type = MaterialType.ParallaxMapSolid;
}
 
Ok, enough of code. Here is my current result:
Image

As you can see in the enlarged image on the top right side, parallax mapping seems to work. But as I look at the whole plane, there seems to be something missing. The shading seems to be Gouraud shading. I expected a "per pixel lighting", or a shading that looks like the one in 3ds max when I press on "render". How can I achieve a better shading with parallax mapping?
I have read something about Tangents but I dont know how use them. My vertices are of type Vertex3D. Should I use Vertex3DTangents? If so, how to draw them? driver.DrawVertexPrimitiveList(...) only takes Vertex3D vertices.

Thanks for answers!
Vir
Posts: 14
Joined: Thu Feb 21, 2013 10:00 pm

Re: Per pixel lighting in custom scene node

Post by Vir »

You need lights in order to get the parallax-mapped textures looking how you want them to. If you have no lights in 3DS Max, the renderer will assume a default light that's positioned near where the camera is. In your program, you've apparently just turned lighting off completely for your material, which will naturally not look as realistic as it would have if there were dynamic lights in the scene.
Proxx
Posts: 6
Joined: Sat Mar 02, 2013 3:57 pm

Re: Per pixel lighting in custom scene node

Post by Proxx »

Thanks for your reply. I have a light in my scene and animated it. Its turning around in a 100 unit circle. So when I run my program I can see the edges oft my floor turning darker and lighter, depending on the current lights position. The material is behaving like gouraud shaded with additional (very weak) parallax mapping.
What else do I need to set?

EDIT:

I have changed one of my code lines from

Code: Select all

 
this.material.Type = MaterialType.ParallaxMapSolid;
 
to

Code: Select all

 
this.SetMaterialType(MaterialType.ParallaxMapSolid);
 
Now, the MaterialType seems to be not setted anymore. Do I have to implement my own SetMaterialType method? As far as I saw, the SceneNode class does not include a material, but why does it have the SetMaterialType method?

EDIT2:

I reverted the last change and simplified the SetupGeometry method into this

Code: Select all

 
private void SetupGeometry()
{
    var meshBuffer = MeshBuffer.Create(VertexType.Standard, IndexType._16Bit);
 
    var vertices = new Vertex3D[4];
    vertices[0] = new Vertex3D(-300, 0, -300, 0, 1, 0, new Color(0), 0, 0);
    vertices[1] = new Vertex3D(300, 0, -300, 0, 1, 0, new Color(0), 0, 1);
    vertices[2] = new Vertex3D(300, 0, 300, 0, 1, 0, new Color(0), 1, 1);
    vertices[3] = new Vertex3D(-300, 0, 300, 0, 1, 0, new Color(0), 1, 0);
 
    var indices = new ushort[] { 2, 1, 0, 0, 3, 2 };
    meshBuffer.Append(vertices, indices);
 
    this.mesh = Mesh.Create();
    this.mesh.AddMeshBuffer(meshBuffer);
    this.mesh = this.SceneManager.MeshManipulator.CreateMeshWithTangents(this.mesh);
 
For drawing, I call now

Code: Select all

 
    driver.DrawMeshBuffer(this.mesh.GetMeshBuffer(0));
 
This should add Tangents to my geometry, but I still dont get "per pixel lighting" but gouraud. Any ideas?
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Re: Per pixel lighting in custom scene node

Post by hybrid »

Please check example 11. You can set the strength of the parallax bump, maybe it's just 0. Or it's your normal map, which is not as expected by Irrlicht.
Proxx
Posts: 6
Joined: Sat Mar 02, 2013 3:57 pm

Re: Per pixel lighting in custom scene node

Post by Proxx »

I took the code of example 11 as a base for the parallax mapping in my application. I now "fixed" the gouraud shading by dividing my ground plane into more triangles, it was only two at the beginning.
I also got more into the material stuff now, thanks for your help. :-)
Post Reply