7. Implementation

CWorld.cpp

//for ART background
extern "C" {
#include <AR/gsub.h>
#include <AR/video.h>
#include <AR/param.h>
#include <AR/ar.h>
#include <GL/gl.h>
#include <GL/glut.h>
}
#include <AR/ARFrameGrabber.h> //for ART with FrameGrabber (<=2.53)
#include "SoBackground.h"
int xsize, ysize;
int thresh = 100;
int count, art = 0;
char *cparam_name = "camera_para.dat";
char *patt_name = "patt.hiro";
int patt_id;
int patt_width = 80.0;
double patt_center[2] = {0.0, 0.0};
double patt_trans[3][4];
ARParam cparam;
ARFrameGrabber camera;
long *dataPtr;
SoBackgroundImage *bkgd;
// ART 2.53
static bool setupAR( void )
{
ARParam wparam;
CoInitialize(NULL);
xsize = camera.GetWidth(); ysize = camera.GetHeight(); //xsize = 320; ysize = 240;
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");
return(false);
}
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");
return (false);
}
camera.Init(0,xsize,ysize);
printf("AR setup FINISHED.\n");
return (true);
}
void CWorld::tickFunc(void* data, SoSensor* psensor) {
// ART: grab a video frame
camera.SetFlippedImage(false);
camera.GrabFrame();
if( (dataPtr = camera.GetBuffer()) == NULL ) {
arUtilSleep(2);
return;
}
long bufferSize = camera.GetBufferSize();
unsigned char *ptr = (new unsigned char[bufferSize]) + bufferSize - 1;
unsigned char *pTemp = (unsigned char*) dataPtr;
for (int index = 0; index < (bufferSize/4); index++)
{
unsigned char r = *(pTemp++);
unsigned char g = *(pTemp++);
unsigned char b = *(pTemp++);
unsigned char a = *(pTemp++);
*(ptr--) = b;
*(ptr--) = a;
*(ptr--) = r;
*(ptr--) = g;
}
xsize = camera.GetWidth(); ysize = camera.GetHeight();
//OI: Display camera image
bkgd->image.setValue(SbVec2s(xsize,ysize), 4, (ARUint8 *) dataPtr);
/* //ART: marker detection if needed
ARMarkerInfo *marker_info;
int marker_num;
int j, k;
if(!(arDetectMarker((ARUint8*) ptr, thresh, &marker_info, &marker_num) < 0 )) {
printf("Marker detected.\t");
}
// check for object visibility
k = -1;
for( j = 0; j < marker_num; j++ ) {
if( patt_id == marker_info[j].id ) {
if( k == -1 ) k = j;
else if( marker_info[k].cf < marker_info[j].cf ) k = j;
}
}
if( k == -1 ) {
//argSwapBuffers();
return;
}
// get the transformation between the marker and the real camera
arGetTransMat(&marker_info[k], patt_center, patt_width, patt_trans);
//ART code for graphics rendering
//
// [...]
//
*/
CWorld* self = (CWorld*)data;
if(self) {
self->tick();
}
}
//*** CWORLD ***
void CWorld::start(int updatesPerSecond{
// NEW ARToolKit
if (!setupAR()) {
printf("Init not successfull. EXITING.\n");
exit(-1);
}
//
m_pCLK = new SoTimerSensor(tickFunc, this);
m_pCLK->setInterval(1.0/(float)updatesPerSecond);
m_pCLK->schedule();
SoXt::show(m_wWindow);
SoXt::mainLoop();
}
CWorld::CWorld(String windowname) {
.
.
.
// NEW ARToolKit
bkgd = new SoBackgroundImage();
bkgd->style.setValue("STRETCH"); // take window size and fill the entire background
m_pRoot->addChild(bkgd);
// end
.
.
.
/* // VRML Background Node if needed
SoVRMLBackground* pbackground = new SoVRMLBackground;
pbackground->groundColor.setValue(SbVec3f(0,1,0));
pbackground->skyColor.setValue(SbVec3f(0,1,0));
m_pRoot->addChild(pbackground);
SoDirectionalLight* pLight = new SoDirectionalLight();
pLight->direction.setValue(SbVec3f(1.0,0.0,-1.0));
SoOrthographicCamera* pCamera = new SoOrthographicCamera();
pCamera->position.setValue(SbVec3f(0.0,(float) 0.4,(float) 3.0));
pCamera->height.setValue((float) 0.8);
m_pRoot->addChild(pLight);
m_pRoot->addChild(pCamera);
*/
.
.
.
}
CWorld::cleanup(void) {
if(m_pCLK) m_pCLK->unschedule();
if(m_pObjectList) delete m_pObjectList;
// NEW ARToolKit
CoUninitialize(); // close video path
//
}


Link to SoBackgroundImage.cxx