Eye toy project

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.
YoJi
Posts: 12
Joined: Sat Mar 11, 2006 7:32 am

Post by YoJi »

vitek wrote: Your code should look something like the makeColorKeyTexture code in CNullDriver.cpp. If you wanted to support scaling the texture, there is code for that in CImage.cpp.
Thanks to your answer but ,,, :( i don't understand how to change the pixel data of the texture ... I don't know any CImage.cpp and CNullDriver.cpp ( I am very bad :( )

this is my code :

Code: Select all

Imagebool=-1;   
    int lastFPS = -1;
    ITexture * TextureWebcam = driver->addTexture(core::dimension2d<s32>(VMAX, HMAX), "My Webcam", video::ECF_A8R8G8B8); ;
    IplImage * frame;
    u32 now;
    const u32 camera_fps = 30;
    const u32 ms_at_camera_fps = 1000 / camera_fps;
    u32 then = device->getTimer()->getRealTime();
    IImage * ImageWebcam;
    while(device->run())
    if (device->isWindowActive())
    {

    
 
        //DraGon.BougeDragon(smgr, driver, dragon);
        
        /** Dessin de la Scene **/
    	driver->beginScene(true, true, SColor(0,0,0,0)); 
        

        smgr->drawAll();
        
        if (Imagebool!=-1)
           driver->draw2DImage(TextureWebcam, position2d<s32>(351, 30));
        
    	guienv->drawAll();
    	
        
        now = device->getTimer()->getRealTime(); 
        if (ms_at_camera_fps < (now - then))
        {
            then = now;

            void* lock = TextureWebcam->lock();
            if (lock)
            {
                // Mise à jour des pixels de la texture
                
                /** On Envoie La WebCam =D **/        
                RecupFrame(guienv,driver,smgr,device,capture,TextureWebcam,frame);
                ImageWebcam=WebCam.AfficheImage(Image1,321,0,driver,TextureWebcam,ImageWebcam);
                if (ImageWebcam!=NULL)
                   memcpy(lock, ImageWebcam, sizeof (ImageWebcam)); 
                TextureWebcam->unlock();
            }
        } 
        
        driver->endScene();
        
    	/** Pour récuperer le FPS **/
    	int fps = driver->getFPS();
    	if (lastFPS != fps)        // Pour eviter d'afficher toujours le meme FPS si celui ci reste inchangé
		{
			stringw str = L"Irrlicht Engine - BiGMaC's Adventure ["; str += driver->getName();str += "] FPS:";str += fps;
            device->setWindowCaption(str.c_str());
			lastFPS = fps;
		}
    }
And the AfficheImage function :

Code: Select all

IImage * Webcam::AfficheImage(Image * ImageAAfficher, int x, int y, IVideoDriver* driver,ITexture * TextureWebcam,IImage * ImageWebcam)
{
	int i, j;
    LPBYTE DonneeWebcam = new BYTE[4*VMAX*HMAX];
    
	// Parcoure le tbl
	for(i=0; i<HMAX; i++)
		for(j=0; j<VMAX; j++)
		{
			DonneeWebcam[4*VMAX*(HMAX-1-i)+4*j+2] = ImageAAfficher->Couleur(HMAX-i-1,j,'r');
			DonneeWebcam[4*VMAX*(HMAX-1-i)+4*j+1] = ImageAAfficher->Couleur(HMAX-i-1,j,'g');
			DonneeWebcam[4*VMAX*(HMAX-1-i)+4*j]   = ImageAAfficher->Couleur(HMAX-i-1,j,'b');
		}
    ImageWebcam = driver->createImageFromData(ECF_A8R8G8B8, dimension2d<s32>(VMAX,HMAX), DonneeWebcam,true); 	

    delete (DonneeWebcam);
    return ImageWebcam;  
    	
}
sdi2000
Posts: 129
Joined: Thu Aug 25, 2005 12:19 pm
Location: Berlin, DE
Contact:

Post by sdi2000 »

ok stop... :D
nobody is bad!
Image1 is the grabed cam pic?
so do this.

Code: Select all

 //in your render loop
      now = device->getTimer()->getRealTime();
        if (ms_at_camera_fps < (now - then))
        {
            then = now;
            copyPixel(TextureWebcam, Image1);
        } 

...
...
...

void copyPixel(ITexture *dest, Image * ImageAAfficher)
{
  
      void *lockedTextureBuffer = dest->lock();
      
      if(!lockedTextureBuffer)
        return;
      
      //has your ImageAAfficher a resize function?
      //if it is so then use the following two function calls to determine the new size and then resize
      int newv = getTextureSizeFromImageSize(VMAX);
      int newh = getTextureSizeFromImageSize(HMAX);

      //here scale if ur object has a resize function and copy pixel by pixel to
     //the locked buffer and dont forget to add an extra pixel for the apha 
    //chanel and ignore the following lines beside dest->unlock();
      //u must call unlock, if not no changes on the texture was made
      
      //if no resize functions exist try this?? i dont know if it is bug free, but i think it is a good template =)
      //a simple scale/resize function


			float xscale = (float)VMAX / (float)newv;
			float yscale = (float)HMAX / (float)newh;
			int width = VMAX;

			for (int y=0; y<newh; ++y) 
			{
				for (int x=0; x<newv; ++x) 
				{
    			unsigned char tmp1[4];
					int xIndex = (int)((float)x*xscale);
					int yIndex = (int)((float)y*yscale);
					tmp1[0] = ImageAAfficher->Couleur(xIndex,yIndex*width,'b');
					tmp1[1] = ImageAAfficher->Couleur(xIndex,yIndex*width,'g');
					tmp1[2] = ImageAAfficher->Couleur(xIndex,yIndex*width,'r');
					tmp1[3] = 0; //to 32bit... a extra bit for da alpha chanel
					memcpy(lockedTextureBuffer[x + y*newv], tmp1, sizeof(tmp1));
				}
			}
			dest->unlock();
}


int getTextureSizeFromImageSize(int _size)
{
   int ts = 0x01;

   while(ts < _size)
      ts <<= 1;

   if (ts > _size && ts > 64)
      ts >>= 1;

   return ts;
}

:D
YoJi
Posts: 12
Joined: Sat Mar 11, 2006 7:32 am

Post by YoJi »

thanks to your code !!! :)

but he don't like : lockedTextureBuffer[x + y*newv]

"pointer of type `void *' used in arithmetic "
sdi2000
Posts: 129
Joined: Thu Aug 25, 2005 12:19 pm
Location: Berlin, DE
Contact:

Post by sdi2000 »

cast the void pointer to a unsinged char *

like

Code: Select all

 unsingned char *lockedBuffer = (unsigned char*) texture->lock();
YoJi
Posts: 12
Joined: Sat Mar 11, 2006 7:32 am

Post by YoJi »

i don't understand why TextureWebcam->lock() return a void * because if i want to change imagedata is in 2D so a void ** no ?


Or maybe is the image in line ?
sdi2000
Posts: 129
Joined: Thu Aug 25, 2005 12:19 pm
Location: Berlin, DE
Contact:

Post by sdi2000 »

the function TextureWebcam->lock()
returns an array with the pixelcolor data like
[g][r][a][b1][g1][r1][a1]...
now u must replace the color values with ur color values.
then call the TextureWebcam->unlock()
and the irrlicht engine copies the color value array into the texture image
and nothing more :D
YoJi
Posts: 12
Joined: Sat Mar 11, 2006 7:32 am

Post by YoJi »

this is that i do :

Code: Select all

for (int y=0; y<HMAX; ++y)
                     {
                        for (int x=0; x<VMAX; ++x)
                        {
                           unsigned char tmp1[4];
                           tmp1[0] = Image1->Couleur(y,x,'b');
                           tmp1[1] = Image1->Couleur(y,x,'g');
                           tmp1[2] = Image1->Couleur(y,x,'r');
                           tmp1[3] = 0; //to 32bit... a extra bit for da alpha chanel
                           memcpy((void*)lock[y + x*VMAX], tmp1, sizeof(tmp1));
                        }
                     } 

but executable crash :s
sdi2000
Posts: 129
Joined: Thu Aug 25, 2005 12:19 pm
Location: Berlin, DE
Contact:

Post by sdi2000 »

once again :D
resize ur image!!!!!!
if u dont resize ur image u get an access violation error!!!!
YoJi
Posts: 12
Joined: Sat Mar 11, 2006 7:32 am

Post by YoJi »

I have the same error :

Code: Select all


            unsigned char* lock =(unsigned char*) TextureWebcam->lock();
            if (lock)
            {
                int newv = getTextureSizeFromImageSize(VMAX);
                int newh = getTextureSizeFromImageSize(HMAX);
                
                float xscale = (float)VMAX / (float)newv;
                float yscale = (float)HMAX / (float)newh;
                int width = VMAX;  
                //Mise à jour des pixels de la texture
                
                /** On Envoie La WebCam =D **/        
                EnvoyezLaWebcam(guienv,driver,smgr,device,capture,TextureWebcam,frame);
                //ImageWebcam=WebCam.AfficheImage(Image1,321,0,driver,TextureWebcam,ImageWebcam);
                
                     for (int y=0; y<newh; ++y)
                     {
                        for (int x=0; x<newv; ++x)
                        {
                           unsigned char tmp1[4];
                           tmp1[0] = Image1->Couleur(y,x,'b');
                           tmp1[1] = Image1->Couleur(y,x,'g');
                           tmp1[2] = Image1->Couleur(y,x,'r');
                           tmp1[3] = 0; //to 32bit... a extra bit for da alpha chanel
                           //memcpy((void*)lock[y + x*newv], tmp1, sizeof(tmp1));
                        }
                     } 
                
                
                if (ImageWebcam!=NULL)

                   memcpy(lock, ImageWebcam, sizeof (ImageWebcam)); 
                TextureWebcam->unlock();


            }

If i remove the memcpy i don't have crash
sdi2000
Posts: 129
Joined: Thu Aug 25, 2005 12:19 pm
Location: Berlin, DE
Contact:

Post by sdi2000 »

hey...
do the ways we have you told
but before
learn c++ and more about 2d graphics algorithm
a link for algorithms to scale a pic...

http://www.compuphase.com/graphic/scale2.htm

another link
http://astronomy.swin.edu.au/~pbourke/colour/bicubic/
(poul bourke is a great man)

http://www.cs.wisc.edu/graphics/Courses ... caling.htm

read the api docs of irrlicht

and learn more c++ =)
seriously it is important to know, how to use c++
:shock:
YoJi
Posts: 12
Joined: Sat Mar 11, 2006 7:32 am

Post by YoJi »

Hello , i have always the texture data problem, and a problem with 3D animation :

I have a node and i want to (for exemple) rotate him to an angle of 30°, i can use a node->setRotate() but the node will be teleported.

In opengl we make a loop with a variable on the glRotatef() and after every rotate we redraw the scene... How we do that in Irrlicht



This is that i make :

Code: Select all

    if (dragon)
    {   
        int X=0;
        vector3df CoorDragon=dragon->getPosition(); 
        vector3df translate = vector3df(1,0,0);
        while (X!=100)
        {
            
            CoorDragon=dragon->getPosition(); 
            dragon->setPosition(CoorDragon+translate);
            
            dragon->updateAbsolutePosition(); 
            dragon->render(); 
            X++;
        }   
    }

Anybody know the solution of my mistake?
saulotmalo
Posts: 54
Joined: Wed Oct 04, 2006 6:56 am

Post by saulotmalo »

I'm having the same speed problems using opencv... is there anyway of solve it? i've read the entire thread and don't get the answer.

I've tried to use a thred for managing the camera input but it doesn't work when the camera reads it stops... also i read only a time of each second but the framerate drops ...
Post Reply