Attaching directional light node to createFlyCircleAnimator?

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
pandoragami
Posts: 226
Joined: Wed Jan 26, 2011 5:37 pm
Contact:

Attaching directional light node to createFlyCircleAnimator?

Post by pandoragami »

Hello,

I'm trying to use this http://irrlicht.sourceforge.net/docu/example008.html tutorial on FX in the following code which does work with

Code: Select all

 
anim = smgr->createRotationAnimator (    irr::core::vector3df(0.0f, 1.0f, 0.0f) );
 
But not with the createFlyCircleAnimator method as explained in example 8.

Here's the complete code

Code: Select all

 
 
#include "irrlicht.h"
#include <cstdlib>
#include <iostream>
#include <cmath>
 
using namespace irr;
 
class EventReceiver : public irr::IEventReceiver
{
    public:
 
    virtual bool OnEvent(const irr::SEvent& event);
    virtual bool IsKeyDown( irr::EKEY_CODE keyCode) const;
    EventReceiver();
 
private:
 
    bool KeyIsDown[ irr::KEY_KEY_CODES_COUNT];
};
bool EventReceiver::OnEvent(const irr::SEvent& event)
{
 
    if (event.EventType == irr::EET_KEY_INPUT_EVENT)
        KeyIsDown[event.KeyInput.Key] = event.KeyInput.PressedDown;
 
    return false;
}
bool EventReceiver::IsKeyDown( irr::EKEY_CODE keyCode) const
{
    return KeyIsDown[keyCode];
}
 
EventReceiver::EventReceiver()
{
    for ( irr::u32 i = 0; i < irr::KEY_KEY_CODES_COUNT; ++i)
        KeyIsDown[i] = false;
}
 
scene::SMeshBuffer Buffer;
video::IVideoDriver* driver;
void init_indices()//this is supposed to create a triangle polygon. The indices represent the points of the vertices and the point zero is the beginning and endpoint.
{
    int index = Buffer.Indices.size();
 
     Buffer.Indices.push_back(     index/3);
     Buffer.Indices.push_back( 2+index/3);
     Buffer.Indices.push_back( 3+index/3);
 
     Buffer.Indices.push_back( 2+index/3);
     Buffer.Indices.push_back( 1+index/3);
     Buffer.Indices.push_back( 3+index/3);
 
     Buffer.Indices.push_back( 1+index/3);
     Buffer.Indices.push_back(     index/3);
     Buffer.Indices.push_back( 3+index/3);
 
     Buffer.Indices.push_back( 2+index/3);
     Buffer.Indices.push_back(     index/3);
     Buffer.Indices.push_back( 1+index/3);
 
 
}
void init_points( float x, float y, float z)
{
    //video::SColor color( 0, 255, 0, 0);
 
    Buffer.Vertices.push_back( video::S3DVertex(     x,     y,   .1+z, 1,1,0,   video::SColor(255,    0,255,255), 0, 1));
    Buffer.Vertices.push_back( video::S3DVertex(  .1+x,    y,      z, 1,0,0,  video::SColor(255,255,    0,255), 1, 1));
    Buffer.Vertices.push_back( video::S3DVertex(     x, y+.1,     z, 0,1,1, video::SColor(255,255,255,     0), 1, 0));
    Buffer.Vertices.push_back(video::S3DVertex(       x,    y,      z, 0,0,1,   video::SColor(255,    0,255,     0), 0, 0));
 
    init_indices();
}
 
void render()
{
   video::SMaterial material;
   material.Wireframe = false;
   material.Lighting = true;
   material.BackfaceCulling = true;
   material.DiffuseColor = irr::video::SColor( 255, 0, 0, 255);
   material.SpecularColor = irr::video::SColor( 255, 0, 0, 255);
   material.setTexture( 0, driver->getTexture("../../media/particlewhite.bmp") );
   material.setFlag( irr::video::EMF_FOG_ENABLE, true);
   material.NormalizeNormals = true;
   driver->setMaterial( material);
 
   driver->drawVertexPrimitiveList
                                        (
                                            Buffer.getVertices(),
                                            Buffer.getVertexCount(),
                                            Buffer.getIndices(),
                                            Buffer.Indices.size()/3,
                                            video::EVT_STANDARD,
                                            irr::scene::EPT_TRIANGLES,
                                            video::EIT_16BIT
                                        );
}
 
int main()
{
    EventReceiver receiver;
    IrrlichtDevice *device = createDevice( irr::video::EDT_OPENGL,
                                       irr::core::dimension2d< irr::u32>( 1024, 768),
                                       16,
                                       false,
                                       false,
                                       false,
                                       &receiver);
 
    if (device == 0)
        return 1;
 
    device->setWindowCaption(L"IRRLICHT");
    driver = device->getVideoDriver();
    scene::ISceneManager* smgr = device->getSceneManager();
    driver->setTextureCreationFlag( irr::video::ETCF_CREATE_MIP_MAPS, true);
 
    for( float x = -5; x < 5; x+= .1)
        for( float y = -5; y < 5; y+= .1)
            init_points( x, y, 0);
 
    std::cout<<"Buffer.getVertices(). "<<Buffer.getVertices();
    std::cout<<", Buffer.getVertexCount(). "<<Buffer.getVertexCount();
    std::cout<<", Buffer.getIndices(). "<<Buffer.getIndices();
    std::cout<<", Buffer.Indices.size()/3. "<<Buffer.Indices.size()/3<<std::endl;
//
    irr::scene::ISceneNode* node = 0;
    irr::scene::ILightSceneNode* light_node = 0;
 
    node = smgr->addEmptySceneNode( 0,-1);
    light_node = smgr->addLightSceneNode();
    light_node->setPosition( irr::core::vector3df(0,0,1));
 
    irr::video::SLight & slight = light_node->getLightData();
    slight.Type = irr::video::ELT_DIRECTIONAL;
 
    //node->addChild( light_node);
    light_node->setParent( node);
 
    irr::scene::ISceneNodeAnimator* anim = 0;
    ///*
    anim = smgr->createFlyCircleAnimator
                                    (
                                        irr::core::vector3df(0.0f, 0.0f, 0.0f),//center
                                        15.f,//radius
                                        0.001f,//speed
                                        irr::core::vector3df(0.0f, 1.0f, 0.0f),//direction
                                        0.0f,//start position
                                        0.0f //radius ellipsoid
                                    );
    //*/
 
    //anim = smgr->createRotationAnimator   (    irr::core::vector3df(0.0f, 1.0f, 0.0f) );
 
    node->addAnimator(anim);
    anim->drop();
 
    // attach billboard to light\

    node = smgr->addBillboardSceneNode( node, irr::core::dimension2d<irr::f32>(1, 1));
    node->setMaterialFlag( irr::video::EMF_LIGHTING, false);
    node->setMaterialType( irr::video::EMT_TRANSPARENT_ADD_COLOR);
    node->setMaterialTexture(0, driver->getTexture("../../media/particlewhite.bmp"));
//
    irr::scene::ICameraSceneNode* cam = smgr->addCameraSceneNode();
    cam->setTarget( core::vector3df(0,0,0));
    cam->setPosition( core::vector3df(0,0,-25));
    device->getCursorControl()->setVisible( false);
 
    u32 frames=0;
    while(device->run())
    {
        driver->beginScene( true, true, video::SColor( 0, 0, 0, 0));
        render();
        smgr->drawAll();
 
        driver->endScene();
        if (++frames==100)
        {
            core::stringw str = L"Irrlicht Engine [";
            str += driver->getName();
            str += L"] FPS: ";
            str += (s32)driver->getFPS();
 
            //pLight->setRotation( irr::core::vector3df( x_rot, 0, 0));
 
            device->setWindowCaption(str.c_str());
            frames=0;
        }
 
        //x_rot += 0.05f;
    }
 
    device->drop();
 
    return 0;
}
 
 
BTW are these two lines equivalent?

Code: Select all

 
  node->addChild( light_node);
    light_node->setParent( node);
 
pandoragami
Posts: 226
Joined: Wed Jan 26, 2011 5:37 pm
Contact:

Re: Attaching directional light node to createFlyCircleAnima

Post by pandoragami »

I think I narrowed it down to a few things, now it almost works. Basically in the code below, on line 137 where the directional lighting is declared,

Code: Select all

 
slight.Type = irr::video::ELT_DIRECTIONAL;//this is the problem
 
it somehow prevents the light from working with the animator :? . In the game loop the direction of the light is output
once every one hundred frames

Code: Select all

 
core::vector3df temp_dir = slight.Direction;
            std::cout<<temp_dir.X<<","<<temp_dir.Y<<","<<temp_dir.Z<<std::endl;
 
But this always shows 0,0,1 for some reason.

If line 137 is commented out then the scene almost works but the direction is still 0,0,1. I'm guessing that the light is attached to the animator but then why doesn't the direction change?

Code: Select all

 
#include "irrlicht.h"
#include <cstdlib>
#include <iostream>
#include <cmath>
 
using namespace irr;
 
class EventReceiver : public irr::IEventReceiver
{
    public:
 
    virtual bool OnEvent(const irr::SEvent& event);
    virtual bool IsKeyDown( irr::EKEY_CODE keyCode) const;
    EventReceiver();
 
private:
 
    bool KeyIsDown[ irr::KEY_KEY_CODES_COUNT];
};
bool EventReceiver::OnEvent(const irr::SEvent& event)
{
 
    if (event.EventType == irr::EET_KEY_INPUT_EVENT)
        KeyIsDown[event.KeyInput.Key] = event.KeyInput.PressedDown;
 
    return false;
}
bool EventReceiver::IsKeyDown( irr::EKEY_CODE keyCode) const
{
    return KeyIsDown[keyCode];
}
 
EventReceiver::EventReceiver()
{
    for ( irr::u32 i = 0; i < irr::KEY_KEY_CODES_COUNT; ++i)
        KeyIsDown[i] = false;
}
 
scene::SMeshBuffer Buffer;
video::IVideoDriver* driver;
void init_indices()//this is supposed to create a triangle polygon. The indices represent the points of the vertices and the point zero is the beginning and endpoint.
{
    int index = Buffer.Indices.size();
 
     Buffer.Indices.push_back(     index/3);
     Buffer.Indices.push_back( 2+index/3);
     Buffer.Indices.push_back( 3+index/3);
 
     Buffer.Indices.push_back( 2+index/3);
     Buffer.Indices.push_back( 1+index/3);
     Buffer.Indices.push_back( 3+index/3);
 
     Buffer.Indices.push_back( 1+index/3);
     Buffer.Indices.push_back(     index/3);
     Buffer.Indices.push_back( 3+index/3);
 
     Buffer.Indices.push_back( 2+index/3);
     Buffer.Indices.push_back(     index/3);
     Buffer.Indices.push_back( 1+index/3);
 
 
}
void init_points( float x, float y, float z)
{
    //video::SColor color( 0, 255, 0, 0);
 
    Buffer.Vertices.push_back( video::S3DVertex(     x,     y,   .1+z, 1,1,0,   video::SColor(255,    0,255,255), 0, 1));
    Buffer.Vertices.push_back( video::S3DVertex(  .1+x,    y,      z, 1,0,0,  video::SColor(255,255,    0,255), 1, 1));
    Buffer.Vertices.push_back( video::S3DVertex(     x, y+.1,     z, 0,1,1, video::SColor(255,255,255,     0), 1, 0));
    Buffer.Vertices.push_back(video::S3DVertex(       x,    y,      z, 0,0,1,   video::SColor(255,    0,255,     0), 0, 0));
 
    init_indices();
}
 
void render()
{
   video::SMaterial material;
   material.Wireframe = false;
   material.Lighting = true;
   material.BackfaceCulling = true;
   material.DiffuseColor = irr::video::SColor( 255, 0, 0, 255);
   material.SpecularColor = irr::video::SColor( 255, 0, 0, 255);
   material.setTexture( 0, driver->getTexture("../../media/particlewhite.bmp") );
   material.setFlag( irr::video::EMF_FOG_ENABLE, true);
   material.NormalizeNormals = true;
   driver->setMaterial( material);
 
   driver->drawVertexPrimitiveList
                                        (
                                            Buffer.getVertices(),
                                            Buffer.getVertexCount(),
                                            Buffer.getIndices(),
                                            Buffer.Indices.size()/3,
                                            video::EVT_STANDARD,
                                            irr::scene::EPT_TRIANGLES,
                                            video::EIT_16BIT
                                        );
}
 
int main()
{
    EventReceiver receiver;
    IrrlichtDevice *device = createDevice( irr::video::EDT_OPENGL,
                                       irr::core::dimension2d< irr::u32>( 1024, 768),
                                       16,
                                       false,
                                       false,
                                       false,
                                       &receiver);
 
    if (device == 0)
        return 1;
 
    device->setWindowCaption(L"IRRLICHT");
    driver = device->getVideoDriver();
    scene::ISceneManager* smgr = device->getSceneManager();
    driver->setTextureCreationFlag( irr::video::ETCF_CREATE_MIP_MAPS, true);
 
    for( float x = -5; x < 5; x+= .1)
        for( float y = -5; y < 5; y+= .1)
            init_points( x, y, 0);
 
    std::cout<<"Buffer.getVertices(). "<<Buffer.getVertices();
    std::cout<<", Buffer.getVertexCount(). "<<Buffer.getVertexCount();
    std::cout<<", Buffer.getIndices(). "<<Buffer.getIndices();
    std::cout<<", Buffer.Indices.size()/3. "<<Buffer.Indices.size()/3<<std::endl;
//
    irr::scene::ISceneNode* node = 0;
    irr::scene::ILightSceneNode* light_node = 0;
 
    node = smgr->addEmptySceneNode( 0,-1);
    light_node = smgr->addLightSceneNode(0, core::vector3df(0,0,0),
        video::SColorf(1.0f, 0.6f, 0.7f, 1.0f), 800.0f);
    light_node->setPosition( irr::core::vector3df(0,0,250));
 
    irr::video::SLight & slight = light_node->getLightData();
    slight.Type = irr::video::ELT_DIRECTIONAL;//this is the problem
 
    light_node->setParent( node);
    irr::scene::ISceneNodeAnimator* anim = 0;
 
    anim = smgr->createFlyCircleAnimator
                                    (
                                        core::vector3df( 0, 0, 0), 250.0f
                                    );
 
        node->addAnimator(anim);
        anim->drop();
 
    // attach billboard to light\

    node = smgr->addBillboardSceneNode( node, irr::core::dimension2d<irr::f32>(50, 50));
    node->setMaterialFlag( irr::video::EMF_LIGHTING, false);
    node->setMaterialType( irr::video::EMT_TRANSPARENT_ADD_COLOR);
    node->setMaterialTexture(0, driver->getTexture("../../media/particlewhite.bmp"));
//
    irr::scene::ICameraSceneNode* cam = smgr->addCameraSceneNode();
    cam->setTarget( core::vector3df( 0, 0, 0));
    cam->setPosition( core::vector3df( 0, 0,-25));
    device->getCursorControl()->setVisible( false);
 
    u32 frames=0;
    while(device->run())
    {
        driver->beginScene( true, true, video::SColor( 0, 0, 0, 0));
        render();
        smgr->drawAll();
 
        driver->endScene();
        if (++frames==100)
        {
            core::stringw str = L"Irrlicht Engine [";
            str += driver->getName();
            str += L"] FPS: ";
            str += (s32)driver->getFPS();
 
            //pLight->setRotation( irr::core::vector3df( x_rot, 0, 0));
 
            core::vector3df temp_dir = slight.Direction;
            std::cout<<temp_dir.X<<","<<temp_dir.Y<<","<<temp_dir.Z<<std::endl;
 
            device->setWindowCaption(str.c_str());
            frames=0;
        }
 
        //x_rot += 0.05f;
    }
 
    device->drop();
 
    return 0;
}
 
Seven
Posts: 1030
Joined: Mon Nov 14, 2005 2:03 pm

Re: Attaching directional light node to createFlyCircleAnima

Post by Seven »

Code: Select all

 
//! Enumeration for different types of lights
enum E_LIGHT_TYPE
{
    //! point light, it has a position in space and radiates light in all directions
    ELT_POINT,
    //! spot light, it has a position in space, a direction, and a limited cone of influence
    ELT_SPOT,
    //! directional light, coming from a direction from an infinite distance
    ELT_DIRECTIONAL,
 
    //! Only used for counting the elements of this enum
    ELT_COUNT
};
 

I dont think that a directional light does what you are wanting. It seems like you want something like a flashlight that has a position and a direction with a cone of light.
pandoragami
Posts: 226
Joined: Wed Jan 26, 2011 5:37 pm
Contact:

Re: Attaching directional light node to createFlyCircleAnima

Post by pandoragami »

Seven wrote:

Code: Select all

 
//! Enumeration for different types of lights
enum E_LIGHT_TYPE
{
    //! point light, it has a position in space and radiates light in all directions
    ELT_POINT,
    //! spot light, it has a position in space, a direction, and a limited cone of influence
    ELT_SPOT,
    //! directional light, coming from a direction from an infinite distance
    ELT_DIRECTIONAL,
 
    //! Only used for counting the elements of this enum
    ELT_COUNT
};
 

I dont think that a directional light does what you are wanting. It seems like you want something like a flashlight that has a position and a direction with a cone of light.

I know I thought of that too, but then I don't want to digress too much and just ask a simpler question, how would I then pass a variable to a shader that changes, namely the direction. I've looked at a couple of tutorials ( such as this one http://learningwebgl.com/blog/?p=684) that use directional light for shading because it is simpler to use than point light.

The way it's explained is that point lights requires more calculation due to smaller changes of incident light on a surface where as directional light is all the same from one point.

So basically the entire point of this was to setup toon-shading which is a tutorial here http://www.lighthouse3d.com/tutorials/g ... version-i/

and I tried to setup that in this code ( modified example 10) to try using the directional light with the toon shading.

Code: Select all

 
 
/** Example 010 Shaders
*/
#include <irrlicht.h>
#include <iostream>
 
using namespace irr;
 
 
IrrlichtDevice* device = 0;
bool UseHighLevelShaders = false;
bool UseCgShaders = false;
 
irr::scene::ILightSceneNode* light_node = NULL;
 
class MyShaderCallBack : public video::IShaderConstantSetCallBack
{
public:
 
    virtual void OnSetConstants(video::IMaterialRendererServices* services,
            s32 userData)
    {
        video::IVideoDriver* driver = services->getVideoDriver();
 
        //light direction
        if(light_node)
        {
            irr::video::SLight  & slight = light_node->getLightData();
            core::vector3df temp_dir = slight.Direction;
            std::cout<<"shader: "<<temp_dir.X<<","<<temp_dir.Y<<","<<temp_dir.Z<<std::endl;
 
            core::vector3df dir = slight.Direction;
 
            if (UseHighLevelShaders)
                services->setVertexShaderConstant(" lightDir", reinterpret_cast<f32*>(&dir), 3);
            else
                services->setVertexShaderConstant(reinterpret_cast<f32*>(&dir), 8, 1);
        }
 
    }
};
 
 
 
int main()
{
    video::E_DRIVER_TYPE driverType;
    UseHighLevelShaders = true;
    UseCgShaders =false;
 
    // create device
    device = createDevice(irr::video::EDT_OPENGL, core::dimension2d<u32>(640, 480));
 
    if (device == 0)
        return 1; // could not create selected driver.
 
    video::IVideoDriver* driver = device->getVideoDriver();
    scene::ISceneManager* smgr = device->getSceneManager();
    gui::IGUIEnvironment* gui = device->getGUIEnvironment();
 
 
    // Make sure we don't try Cg without support for it
    if (UseCgShaders && !driver->queryFeature(video::EVDF_CG))
    {
        printf("Warning: No Cg support, disabling.\n");
        UseCgShaders=false;
    }
 
    
    io::path vsFileName; // filename for the vertex shader
    io::path psFileName; // filename for the pixel shader
 
    driverType = video::EDT_OPENGL;
    psFileName = "opengl.frag";
    vsFileName = "opengl.vert";
 
 
 
    if (!driver->queryFeature(video::EVDF_PIXEL_SHADER_1_1) &&
        !driver->queryFeature(video::EVDF_ARB_FRAGMENT_PROGRAM_1))
    {
        device->getLogger()->log("WARNING: Pixel shaders disabled "\
            "because of missing driver/hardware support.");
        psFileName = "";
    }
 
    if (!driver->queryFeature(video::EVDF_VERTEX_SHADER_1_1) &&
        !driver->queryFeature(video::EVDF_ARB_VERTEX_PROGRAM_1))
    {
        device->getLogger()->log("WARNING: Vertex shaders disabled "\
            "because of missing driver/hardware support.");
        vsFileName = "";
    }
 
    // create materials
 
    video::IGPUProgrammingServices* gpu = driver->getGPUProgrammingServices();
    s32 newMaterialType1 = 0;
    s32 newMaterialType2 = 0;
 
    if (gpu)
    {
        MyShaderCallBack* mc = new MyShaderCallBack();
 
        // create the shaders depending on if the user wanted high level
        // or low level shaders:
 
        if (UseHighLevelShaders)
        {
            // Choose the desired shader type. Default is the native
            // shader type for the driver, for Cg pass the special
            // enum value EGSL_CG
            const video::E_GPU_SHADING_LANGUAGE shadingLanguage =
                UseCgShaders ? video::EGSL_CG:video::EGSL_DEFAULT;
 
            // create material from high level shaders (hlsl, glsl or cg)
 
            newMaterialType1 = gpu->addHighLevelShaderMaterialFromFiles(
                vsFileName, "vertexMain", video::EVST_VS_1_1,
                psFileName, "pixelMain", video::EPST_PS_1_1,
                mc, video::EMT_SOLID, 0, shadingLanguage);
 
            newMaterialType2 = gpu->addHighLevelShaderMaterialFromFiles(
                vsFileName, "vertexMain", video::EVST_VS_1_1,
                psFileName, "pixelMain", video::EPST_PS_1_1,
                mc, video::EMT_TRANSPARENT_ADD_COLOR, 0 , shadingLanguage);
        }
        else
        {
            // create material from low level shaders (asm or arb_asm)
 
            newMaterialType1 = gpu->addShaderMaterialFromFiles(vsFileName,
                psFileName, mc, video::EMT_SOLID);
 
            newMaterialType2 = gpu->addShaderMaterialFromFiles(vsFileName,
                psFileName, mc, video::EMT_TRANSPARENT_ADD_COLOR);
        }
 
        mc->drop();
    }
 
    irr::scene::ISceneNode* node = 0;
 
    node = smgr->addEmptySceneNode( 0,-1);
    light_node = smgr->addLightSceneNode();
    light_node->setPosition( irr::core::vector3df( 0,100, 0));
    light_node->setVisible( true);
    light_node->enableCastShadow( false);
 
    irr::video::SLight  & slight = light_node->getLightData();
    slight.Type = irr::video::ELT_DIRECTIONAL;
    slight.Radius = 200.0f;
 
    slight.AmbientColor = irr::video::SColorf(1.0f,0.0f,0.0f,0.0f);
    slight.SpecularColor = irr::video::SColorf(1.0f,1.0f,1.0f,0.0f);
    slight.DiffuseColor = irr::video::SColorf(1.0f,1.0f,1.0f,0.0f);
 
    //node->addChild( light_node);
    light_node->setParent( node);
    irr::scene::ISceneNodeAnimator* anim = 0;
 
    anim = smgr->createRotationAnimator ( irr::core::vector3df( 0.0f, 0.3f, 0.0f));
        node->addAnimator(anim);
        anim->drop();
 
    // attach billboard to light\

    node = smgr->addBillboardSceneNode( node, irr::core::dimension2d<irr::f32>(1, 1));
    node->setMaterialFlag( irr::video::EMF_LIGHTING, true);
    node->setMaterialType( irr::video::EMT_TRANSPARENT_ADD_COLOR);
    node->setMaterialTexture(0, driver->getTexture("../../media/particlewhite.bmp"));
//
    scene::ISceneNode* cubenode = smgr->addCubeSceneNode(50);
    cubenode->setPosition( core::vector3df(0,0,0));
    cubenode->setMaterialTexture( 0, driver->getTexture("../../media/wall.bmp"));
    //cubenode->setMaterialFlag(video::EMF_LIGHTING, false);
    //cubenode->setMaterialFlag(video::EMF_BLEND_OPERATION, true);
    cubenode->setMaterialFlag(video::EMF_NORMALIZE_NORMALS, true);
    cubenode->setMaterialType((video::E_MATERIAL_TYPE)newMaterialType2);
 
    scene::ICameraSceneNode* cam = smgr->addCameraSceneNodeFPS();
    cam->setPosition(core::vector3df(0,0,-150));
    cam->setTarget(core::vector3df(0,0,0));
    device->getCursorControl()->setVisible(false);
 
    int lastFPS = -1;
    int counter = 0;
    int limit = 100;
 
    while(device->run())
        if (device->isWindowActive())
    {
        driver->beginScene(true, true, video::SColor(255,0,0,0));
        smgr->drawAll();
        driver->endScene();
 
        int fps = driver->getFPS();
 
        if (lastFPS != fps)
        {
            core::stringw str = L"Irrlicht Engine - Vertex and pixel shader example [";
            str += driver->getName();
            str += "] FPS:";
            str += fps;
 
            device->setWindowCaption(str.c_str());
            lastFPS = fps;
        }
 
        if( counter > limit)
        {
            core::vector3df temp_dir = slight.Direction;
            //std::cout<<temp_dir.X<<","<<temp_dir.Y<<","<<temp_dir.Z<<std::endl;
            counter = 0;
        }
 
        counter++;
    }
 
    device->drop();
 
    return 0;
}
 
 
this is the opengl.vert

Code: Select all

 
uniform vec3 lightDir;
varying float intensity;
 
void main()
{
    intensity = dot(lightDir,gl_Normal);    
    
    //vec3 lightDir = normalize(vec3(gl_LightSource[0].position));
    //intensity = dot(lightDir,gl_Normal);
 
    gl_Position = ftransform();
}
 
and the opengl.frag

Code: Select all

 
varying float intensity;
 
void main()
{
    vec4 color;
    if (intensity > 0.95)
 
        color = vec4(1.0,0.5,0.5,1.0);
    else if (intensity > 0.5)
        color = vec4(0.6,0.3,0.3,1.0);
    else if (intensity > 0.25)
        color = vec4(0.4,0.2,0.2,1.0);
    else
        color = vec4(0.2,0.1,0.1,1.0);
    gl_FragColor = color;
 
}
 


I did get the directional light to work, it extracts it in the shader callback

Code: Select all

 
irr::video::SLight  & slight = light_node->getLightData();
            core::vector3df temp_dir = slight.Direction;
            std::cout<<"shader: "<<temp_dir.X<<","<<temp_dir.Y<<","<<temp_dir.Z<<std::endl;
 
but the shader itself doesn't seem to take the lightDir variable to calculate the intensity. If you comment out the two lines

Code: Select all

//vec3 lightDir = normalize(vec3(gl_LightSource[0].position));
    //intensity = dot(lightDir,gl_Normal);
then it works but not the way I want it, because the directional light changes position and direction and the shading on the cube doesn't, it just stays the same. Am I not using the right normal? Maybe I should be using a different normal other than the gl_normal?
pandoragami
Posts: 226
Joined: Wed Jan 26, 2011 5:37 pm
Contact:

Re: Attaching directional light node to createFlyCircleAnima

Post by pandoragami »

Now I found the problem, there was a blank space in the code where the unique identifier for the light direction is passed here

Code: Select all

 
if (UseHighLevelShaders)
                services->setVertexShaderConstant(" lightDir", reinterpret_cast<f32*>(&dir), 3);
 
That extra whitespace before the the lightDir messes things up.
Post Reply