Page 1 of 1

how to use lights effects with driver.draw2DImage

Posted: Tue Sep 23, 2008 11:49 am
by AmigaIrr
Hello

I use driver.draw2DImage to draw a 2d tile map

i want tu use lights effects off this tiles...

How can i do this ?

Posted: Tue Sep 23, 2008 11:57 am
by JP
Hmm probably there's no way at all that you could do that...

Best thing would be to write a scene node which draws a tile for you (or all the tiles) and then you can use the lighting on the node.

Posted: Tue Sep 23, 2008 12:37 pm
by AmigaIrr
Thanks

But, how to do this ?

Posted: Tue Sep 23, 2008 1:18 pm
by rogerborg
Image

Code: Select all

#include <irrlicht.h>
using namespace irr;
using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;

#ifdef _IRR_WINDOWS_
#pragma comment(lib, "Irrlicht.lib")
#endif

int main()
{
   IrrlichtDevice * device =
      createDevice( video::EDT_OPENGL, dimension2d<s32>(640, 480), 32);

   IVideoDriver * driver = device->getVideoDriver();
   ISceneManager * smgr = device->getSceneManager();
   IGUIEnvironment * guienv = device->getGUIEnvironment();

   // Create an orthogonal (orthographic projection) camera
   ICameraSceneNode * camera = smgr->addCameraSceneNode(0, vector3df(0,0,-10), vector3df(0,0,0));
   matrix4 orthoMatrix;
   orthoMatrix.buildProjectionMatrixOrthoLH(20.f, 15.f, 5.f, 100.f);

    camera->setProjectionMatrix(orthoMatrix, true);      // SVN / 1.5
    //   camera->setProjectionMatrix(orthoMatrix);    // <= 1.4.2
    //   camera->setIsOrthogonal(true);


   // Create a simple quad mesh
   SMeshBuffer * buffer = new SMeshBuffer();
   S3DVertex vertices[4];
   vertices[0] = S3DVertex(0.5f, 0.5f, 0.0f, 0.0f, 0.0f, -1.0f, SColor(0, 255, 255, 255), 1.0f, 1.0f);
   vertices[1] = S3DVertex(0.5f, -0.5f, 0.0f, 0.0f, 0.0f, -0.5f, SColor(0, 255, 255, 255), 0.5f, 0.0f);
   vertices[2] = S3DVertex(-0.5f, -0.5f, 0.0f, 0.0f, 0.0f, -0.5f, SColor(0, 255, 255, 255), 0.0f, 0.0f);
   vertices[3] = S3DVertex(-0.5f, 0.5f, 0.0f, 0.0f, 0.0f, -0.5f, SColor(0, 255, 255, 255), 0.0f, 0.5f);
   u16 indices[6] = { 0, 1, 2, 0, 2, 3 };
   buffer->append(vertices, 4, indices, 6);

   SMesh * mesh = new SMesh();
   mesh->addMeshBuffer(buffer);
   buffer->drop();
   // That's our quad mesh created

   // Fill the screen with nodes that use this quad mesh. We are using an ortho camera
   // that defines a 20 x 15 view over 1x1 nodes, so we need 20 x 15 nodes.
   const u32 MAX_NODES = 20 * 15;

   IMeshSceneNode * nodes[MAX_NODES];
   
   for(int index = 0; index < MAX_NODES; ++index)
    {
        nodes[index] = smgr->addMeshSceneNode(mesh);

        if (nodes[index])
        {
            nodes[index]->setMaterialFlag(EMF_LIGHTING, true);
            ITexture * texture;

            u32 nodeX = index % 20;
            u32 nodeY = index / 20;
            nodes[index]->setPosition(vector3df((f32)nodeX - 9.5f, (f32)nodeY - 7.f, 0.f));

            if((nodeX + nodeY) % 2)
                texture = driver->getTexture("../../media/wall.bmp");
            else
                texture = driver->getTexture("../../media/detailmap3.jpg");

            nodes[index]->setMaterialTexture( 0, texture);
        }

    }

    mesh->drop();
    mesh = 0;

    // Move a light over the scene
    IBillboardSceneNode * billboard = smgr->addBillboardSceneNode();
    if(billboard)
    {
        billboard->setMaterialType(video::EMT_TRANSPARENT_ADD_COLOR );
        billboard->setMaterialTexture(0, driver->getTexture("../../media/particle.bmp"));
        billboard->setMaterialFlag(video::EMF_LIGHTING, false);
        billboard->setMaterialFlag(video::EMF_ZBUFFER, false);
        billboard->setSize(core::dimension2d<f32>(1.0f, 1.0f));

        ISceneNodeAnimator * animator = smgr->createFlyCircleAnimator(vector3df(0, 0, -1.f), 7.f, 0.001f, vector3df(0, 0, -1));
        if(animator)
        {
            billboard->addAnimator(animator);
            animator->drop();
            animator = 0;
        }

        (void)smgr->addLightSceneNode(billboard, vector3df(0, 0, 0), SColorf(1.f, 1.f, 0.f, 1.f), 5.f);
    }

    smgr->setAmbientLight(SColorf(0.25f, 0.25f, 0.5f, 1.0f));


    while(device->run())
    {
        driver->beginScene(true, true, SColor(255,100,101,140));
        smgr->drawAll();
        driver->endScene();
    }

    device->drop();

    return 0;
} 

Posted: Tue Sep 23, 2008 6:16 pm
by AmigaIrr
thanks


but :

I use a big picture and picking small tiles.

is this possible with this system ?

Posted: Tue Sep 23, 2008 6:18 pm
by BlindSide
AmigaIrr wrote:is this possible with this system ?
Be strong! The only obstacle is yourself.

Posted: Thu Sep 25, 2008 6:28 am
by AmigaIrr
BlindSide wrote:
AmigaIrr wrote:is this possible with this system ?
Be strong! The only obstacle is yourself.


?

Posted: Thu Sep 25, 2008 7:26 am
by hybrid
Means: Yes, just do it.

Posted: Thu Sep 25, 2008 8:44 am
by rogerborg
AmigaIrr wrote:I use a big picture and picking small tiles.

is this possible with this system ?
Anything is possible, with the exception of compassionate conservatism.

Can you be a bit more specific with your question? What is it about the (very basic) quad based tiling shown about that concerns you?

Posted: Thu Sep 25, 2008 9:57 am
by AmigaIrr
sorry, i'm french...


i want to use a sprite map (thath contains a lots of tiles (ex: 32*32) and i get with a loop all tiles i need.

exemple :

For y=0 To nbtilesy
For x=0 To nbtilesx
i=layer0[xdecarte,ydecarte] ' numero de la tile
driver.draw2DRectangle(_SCOLOR(100,55,55,255), _RECTI(debmapx+(x*tilesize)+8, debmapy+(y*tilesize)+8,debmapx+(x*tilesize)+20, debmapy+(y*tilesize)+20))

Next
Next


how tu use the mesh function to do this ?

Posted: Thu Sep 25, 2008 10:17 am
by rogerborg
AmigaIrr wrote:sorry, i'm french...
Well, better you than me.
AmigaIrr wrote:i want to use a sprite map (thath contains a lots of tiles (ex: 32*32) and i get with a loop all tiles i need.

Code: Select all

		For y=0 To nbtilesy
			For x=0 To nbtilesx
				i=layer0[xdecarte,ydecarte] ' numero de la tile
driver.draw2DRectangle(_SCOLOR(100,55,55,255), _RECTI(debmapx+(x*tilesize)+8, debmapy+(y*tilesize)+8,debmapx+(x*tilesize)+20, debmapy+(y*tilesize)+20))

			Next
		Next
I'm sorry, I don't really understand the connection between the text and the pseudocode. You don't appear to be using "i", and you are drawing coloured rectangles, not sprite images.

The sample code that I provided above is only intended to demonstrate a very simple way of drawing tiles using textured quads and an orthographic camera, to show that it's possible.

Each quad uses the entirety of a single texture. If you want to texture each quad with a portion of a larger sprite map, then you'll have to change the UV co-ordinates of the vertices making up each quad. You could do that in several ways, either by creating a different SMeshBuffer and SMesh for each scene node, or by using a custom scene node that draws itself, and can adjust the UVs of its vertices as necessary.

Example 03.CustomSceneNode shows you how to create a custom scene node that draws itself, and hopefully changing the UV coordinates of a S3DVertex should be obvious to someone who's been using Irrlicht for over three and a half years.