Camera Space Light Animator

Post those lines of code you feel like sharing or find what you require for your project here; or simply use them as tutorials.
Post Reply
Klunk
Posts: 264
Joined: Mon Jan 10, 2011 5:21 pm

Camera Space Light Animator

Post by Klunk »

a simple light animator that keeps the light fixed in camera space and always looking at the origin. Like having an object tracking torch just above your right shoulder (if you want it on the left add a minus sign to the csoffset.X initializer ). For those who know it, it will mimic the 3ds max single default light.

code, (i know its just a snippet)

Code: Select all

#include <irrlicht.h>
#include <math.h>
 
using namespace irr;
using namespace core;
using namespace scene;
 
class CMaxDefLightAnim : public ISceneNodeAnimator
{
public:
 
        CMaxDefLightAnim(ICameraSceneNode* cam) : camera(cam), csoffset(0.5 * sin(PI/6.0), 0.5 * cos(PI/6.0),-cos(PI/6.0)) {}
        void animateNode(ISceneNode* node, u32 timeMs)
        {
                matrix4 camTM, offsetTM;
                vector3df wsoffset;
                camera->getViewMatrix().getInverse(camTM);
                camTM.setTranslation(vector3df(0.0,0.0,0.0));
                camTM.transformVect(wsoffset,csoffset);
                TransformFromDirVector(-wsoffset,  offsetTM);
                node->setRotation(offsetTM.getRotationDegrees());
        }
        virtual void serializeAttributes(io::IAttributes* out, io::SAttributeReadWriteOptions* options=0) const {}
        virtual void deserializeAttributes(io::IAttributes* in, io::SAttributeReadWriteOptions* options=0) {}
        virtual ESCENE_NODE_ANIMATOR_TYPE getType() const { return ESNAT_UNKNOWN; }
        virtual ISceneNodeAnimator* createClone(ISceneNode* node, ISceneManager* newManager=0) { return new CMaxDefLightAnim(camera); }
 
private:
 
        void TransformFromDirVector(const vector3df& dir, matrix4& mat)
        {
                vector3df up(0.0f,1.0f,0.0f), zaxis, xaxis, yaxis;
                zaxis = dir;
                zaxis.normalize();
                xaxis = up.crossProduct(zaxis);
                xaxis.normalize();
                yaxis = zaxis.crossProduct(xaxis);
                mat[0] = xaxis.X; mat[1] = xaxis.Y; mat[2] = xaxis.Z; mat[3] = 0;
                mat[4] = yaxis.X; mat[5] = yaxis.Y; mat[6] = yaxis.Z; mat[7] = 0;
                mat[8] = zaxis.X; mat[9] = zaxis.Y; mat[10] = zaxis.Z; mat[11] = 0;
                mat[12] = 0; mat[13] = 0; mat[14] = 0; mat[15] = 1;
        }
        vector3df                       csoffset;
        ICameraSceneNode*       camera;
};

usage.

Code: Select all

ICameraSceneNode* cam = smgr->addCameraSceneNodeMaya();
ILightSceneNode* lnode = smgr->addLightSceneNode(smgr->getRootSceneNode(),vector3df(0,0,0));
lnode->setLightType(ELT_DIRECTIONAL);
 
ISceneNodeAnimator* anim = new CMaxDefLightAnim(cam);
if(anim)
{
        lnode->addAnimator(anim);
        anim->drop();
}
Last edited by Klunk on Sat Jun 30, 2012 1:25 am, edited 4 times in total.
randomMesh
Posts: 1186
Joined: Fri Dec 29, 2006 12:04 am

Re: Camera Space Light Animator

Post by randomMesh »

Please don't use the using namespace directive in headers, as it will pollute your namespace and can lead to name clashes.
You include the C header (math.h) rather than the C++ one (cmath).
"Whoops..."
Klunk
Posts: 264
Joined: Mon Jan 10, 2011 5:21 pm

Re: Camera Space Light Animator

Post by Klunk »

oh no, what shall I do. :roll:
randomMesh
Posts: 1186
Joined: Fri Dec 29, 2006 12:04 am

Re: Camera Space Light Animator

Post by randomMesh »

Easy. Fix your code.
"Whoops..."
Klunk
Posts: 264
Joined: Mon Jan 10, 2011 5:21 pm

Re: Camera Space Light Animator

Post by Klunk »

yeah, no.
Klunk
Posts: 264
Joined: Mon Jan 10, 2011 5:21 pm

Re: Camera Space Light Animator

Post by Klunk »

I have a question about my own code, is there any advantage binding this sort of thing to a fixed frame rate/update rate ? with something like

Code: Select all

void CMaxDefLightAnim::animateNode(ISceneNode* node, u32 timeMs)
{
    if((timeMs - oldtime) * updaterate >= 1000)
    {
           //... animator code
 
           oldtime = timeMs;
    }
}
where oldtime is a class member
Mel
Competition winner
Posts: 2293
Joined: Wed May 07, 2008 11:40 am
Location: Granada, Spain

Re: Camera Space Light Animator

Post by Mel »

Is there any advantage over linking the light to the camera as a child node, and move the camera on its own?
"There is nothing truly useless, it always serves as a bad example". Arthur A. Schmitt
Klunk
Posts: 264
Joined: Mon Jan 10, 2011 5:21 pm

Re: Camera Space Light Animator

Post by Klunk »

the light would work differently, it's more of a sort of "orientation constraint" to the camera.
Post Reply