With thanks to this thread I would like to post the learning code I developed to make the lib I am about to make.
This is my whole test.cpp, it is a merge of ARToolKit's simpletest.c and irrlicht's Hello World. Minus the girl I made a billboard node display the webcam input from the artoolkit.
a Youtube video of this code in action:
http://www.youtube.com/watch?v=tk8xPPeqi40
Code: Select all
#include <irrlicht/irrlicht.h>
#include <AR/video.h>
#include <AR/param.h>
#include <AR/ar.h>
using namespace irr;
using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;
#ifdef _WIN32
char *vconf = "Data¥¥WDM_camera_flipV.xml";
#else
char *vconf = "";
#endif
int xsize, ysize;
int thresh = 100;
int count = 0;
char *cparam_name = "ardata/camera_para.dat";
ARParam cparam;
char *patt_name = "ardata/patt.hiro";
int patt_id;
double patt_width = 80.0;
double patt_center[2] = {0.0, 0.0};
double patt_trans[3][4];
IrrlichtDevice *device;
IVideoDriver* driver;
ISceneManager* smgr;
void initAR();
ITexture* create_ITexture_from_ARimage(ARUint8 *ardata, int width, int height);
ITexture* update_ITexture_from_ARimage(ITexture *dest, ARUint8 *ardata, int width, int height);
int main()
{
//init code ripped from ARToolkit's simpleTest.c
initAR();
device = createDevice( video::EDT_OPENGL, dimension2d<s32>(640, 480), 16, false, false, false, 0);
device->setWindowCaption(L"Hello World! - Irrlicht Engine Demo");
driver = device->getVideoDriver();
smgr = device->getSceneManager();
//our billboard
IBillboardSceneNode* our_bill_node = smgr->addBillboardSceneNode(NULL, dimension2d<f32>(20.0f, 20.0f), vector3df(10,0,0));
our_bill_node->setMaterialFlag(video::EMF_LIGHTING, false);
ICameraSceneNode* camera = smgr->addCameraSceneNodeFPS();
camera->setPosition(vector3df(40,0,0));
camera->setTarget(vector3df(0,0,0));
int lastFPS = -1;
ARUint8 *dataPtr;
if( (dataPtr = (ARUint8 *)arVideoGetImage()) == NULL )
printf("no image loaded\n");
ITexture* ARimage = create_ITexture_from_ARimage(dataPtr, xsize, ysize);
our_bill_node->setMaterialTexture( 0, ARimage );
while(device->run())
{
if(dataPtr = (ARUint8 *)arVideoGetImage())
update_ITexture_from_ARimage(ARimage, dataPtr, xsize, ysize);
driver->beginScene(true, true, SColor(255,100,101,140));
smgr->drawAll();
//guienv->drawAll();
driver->endScene();
int fps = driver->getFPS();
if (lastFPS != fps)
{
core::stringw str = L"Irrlicht Engine [";
str += driver->getName();
str += "] FPS:";
str += fps;
device->setWindowCaption(str.c_str());
lastFPS = fps;
}
}
device->drop();
return 0;
}
ITexture* update_ITexture_from_ARimage(ITexture *dest, ARUint8 *ardata, int width, int height)
{
u8* pixels;
pixels = (u8*)(dest->lock());
if( pixels )
{
int max_pixels = width * height;
for(int i=0;i<max_pixels;i++)
{
*pixels = *ardata;
pixels++; ardata++;
*pixels = *ardata;
pixels++; ardata++;
*pixels = *ardata;
pixels++; ardata++;
pixels++;
}
dest->unlock();
}
else
{
printf("some hellish error\n");
}
return dest;
}
ITexture* create_ITexture_from_ARimage(ARUint8 *ardata, int width, int height)
{
ITexture* m_poTileTexture = driver->addTexture(core::dimension2d<s32>(width, height), "TileTex", video::ECF_A1R5G5B5);
ITexture* m_poTileNormalMap = driver->addTexture(core::dimension2d<s32>(width, height), "NormTex", video::ECF_A1R5G5B5);
// read the pixels directly into the texture
u8* pixels;
pixels = (u8*)(m_poTileTexture->lock());
if( pixels )
{
int max_pixels = width * height;
for(int i=0;i<max_pixels;i++)
{
*pixels = *ardata;
pixels++; ardata++;
*pixels = *ardata;
pixels++; ardata++;
*pixels = *ardata;
pixels++; ardata++;
pixels++;
}
m_poTileTexture->unlock();
}
else
{
printf("some hellish error\n");
}
return m_poTileTexture;
}
void initAR()
{
//nearly verbatum rip from ARToolKit's simpleTest.c, minus the "g" stuff that is used for integrating with glut
ARParam wparam;
/* open the video path */
if( arVideoOpen( vconf ) < 0 ) exit(0);
/* find the size of the window */
if( arVideoInqSize(&xsize, &ysize) < 0 ) exit(0);
printf("Image size (x,y) = (%d,%d)\n", xsize, ysize);
/* set the initial camera parameters */
if( arParamLoad(cparam_name, 1, &wparam) < 0 ) {
printf("Camera parameter load error !!\n");
exit(0);
}
arParamChangeSize( &wparam, xsize, ysize, &cparam );
arInitCparam( &cparam );
printf("*** Camera Parameter ***\n");
arParamDisp( &cparam );
if( (patt_id=arLoadPatt(patt_name)) < 0 ) {
printf("pattern load error !!\n");
exit(0);
}
arVideoCapStart();
}
[/url]