Problem with Directional Light

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
egrath
Posts: 28
Joined: Wed May 13, 2009 8:15 am
Location: Austria
Contact:

Problem with Directional Light

Post by egrath »

Hello,

i have a little problem regarding the Lighting of a custom scene node:

1.) The Scene Node in Question represents a Terrain
2.) Lighting is achieved using a single Directional light.

The problem is that only parts of the scene are lit. Take a look at the following image:

Image
(In the first step i draw the entire terrain shaded and then everything again in wireframe mode)

All vertices of the terrain are of color SColorf( 1.0f, 1.0f, 0.0f, 1.0f ). The magenta colored lines in the image show the normals of each vertex. The light is set up in the following way:

Code: Select all

    ILightSceneNode *light = m_Scene->addLightSceneNode( m_Scene->getRootSceneNode() );
    light->setLightType( ELT_DIRECTIONAL );
    light->setRotation( vector3df( 45.0f, 45.0f, 0.0f ));
    light->getLightData().AmbientColor = SColorf( 0.2f, 0.2f, 0.2f );
    light->getLightData().DiffuseColor = SColorf( 0.8f, 0.8f, 0.8f );
    m_Scene->setAmbientLight( SColorf( 0.2f,0.2f, 0.2f, 1.0f ));
Any ideas whats causing the problem? Maybe there are problems with my normals calculation. I use the following method to calculate the normals for each vertex:

Code: Select all

bool NormalBuilder::generateNormals()
{
    for( int index = 0; index < m_NumVertices; index ++ )
    {
        S3DVertex &vert = m_Vertices[index];

        // Get all triangles which contain this vertice
        vector<triangle3df> *tris = new vector<triangle3df>();
        for( int j = 0; j < m_NumIndices; j += 3 )
        {
            if( m_Indices[j] == index || m_Indices[j+1] == index || m_Indices[j+2] == index )
            {
                tris->push_back( triangle3df( m_Vertices[m_Indices[j]].Pos, m_Vertices[m_Indices[j+1]].Pos, m_Vertices[m_Indices[j+2]].Pos ));
            }
        }

        vector3df norm( 0, 0, 0 );
        vector<triangle3df>::iterator iter;
        for( iter = tris->begin(); iter != tris->end(); iter ++ )
        {
            triangle3df tri = *iter;
            vector3df n = tri.pointB.crossProduct( tri.pointC ).normalize();
            norm += n.normalize();
        }

        vert.Normal = vert.Pos + norm.normalize();
    }

    return true;
}
Thanks,
Egon

/edit: You can find the complete Project here:

http://rapidshare.de/files/47543788/IrrTest8.zip.html
Post Reply