C++ File Changing

I am working with artoolkit, I want to be able to change the loaded vrml when I press a button. The default code has 2 working keyboard inputs, "q" quits, "c" changing the resolution.


The program uses a Dat files to specify the vrmls to load as well as the transformations made to them.
This is the code for the dat file, it shows that it loads 2 other dat files which locate the vrml files. I have changed the dat file so it loads 2 vrml files to the same pattern and I have found that it only displays the first vrml file in the list, so I need to change the code so it displays only one file then stop displaying the first vrml file and loads the next vrml file when I input a single key or command.

[code]
#the number of patterns to be recognized
2

#pattern 1
VRML Wrl/vrml_1.dat
Data/patt.hiro
80.0
0.0 0.0

#pattern 2
VRML Wrl/vrml_2.dat
Data/patt.hiro
80.0
0.0 0.0
[/code]



A change already made to the default code is the camera which isn't working at the moment, but doesn't affect the rest of the code:

[code]
int mode;
switch (key) {
case 0x1B: // Quit.
case 'Q':
case 'q':
Quit();
break;
case 'e': camera_x = camera_x + step;
break;
case 'r': camera_x = camera_x - step;
break;
case 'f': camera_y = camera_y + step;
break;
case 'd': camera_y = camera_y - step;
break;
case 's': camera_z = camera_z + step;
break;
case 'a': camera_z = camera_z - step;
break;
[/code]


Apart from the code for the camera everything else is the default code.


[code]// ============================================================================
// Includes
// ============================================================================

#ifdef _WIN32
# include
#endif
#include
#include
#include

#ifdef __APPLE__
# include
#else
# include
#endif

#include
#include
#include // arParamDisp()
#include
#include
#include

#include "object.h"

double camera_x = 0.0, camera_y = 0.0, camera_z = 0.00, step = 2;

// ============================================================================
// Constants
// ============================================================================

#define VIEW_SCALEFACTOR 0.025 // 1.0 ARToolKit unit becomes 0.025 of my OpenGL units.
#define VIEW_SCALEFACTOR_1 1.0 // 1.0 ARToolKit unit becomes 1.0 of my OpenGL units.
#define VIEW_SCALEFACTOR_4 4.0 // 1.0 ARToolKit unit becomes 4.0 of my OpenGL units.
#define VIEW_DISTANCE_MIN 4.0 // Objects closer to the camera than this will not be displayed.
#define VIEW_DISTANCE_MAX 4000.0 // Objects further away from the camera than this will not be displayed.


// ============================================================================
// Global variables
// ============================================================================

// Preferences.
static int prefWindowed = TRUE;
static int prefWidth = 640; // Fullscreen mode width.
static int prefHeight = 480; // Fullscreen mode height.
static int prefDepth = 32; // Fullscreen mode bit depth.
static int prefRefresh = 0; // Fullscreen mode refresh rate. Set to 0 to use default rate.

// Image acquisition.
static ARUint8 *gARTImage = NULL;

// Marker detection.
static int gARTThreshhold = 100;
static long gCallCountMarkerDetect = 0;

// Transformation matrix retrieval.
static int gPatt_found = FALSE; // At least one marker.

// Drawing.
static ARParam gARTCparam;
static ARGL_CONTEXT_SETTINGS_REF gArglSettings = NULL;

// Object Data.
static ObjectData_T *gObjectData;
static int gObjectDataCount;

// ============================================================================
// Functions
// ============================================================================

static int setupCamera(const char *cparam_name, char *vconf, ARParam *cparam)
{
ARParam wparam;
int xsize, ysize;

// Open the video path.
if (arVideoOpen(vconf) < 0) {
fprintf(stderr, "setupCamera(): Unable to open connection to camera.
");
return (FALSE);
}

// Find the size of the window.
if (arVideoInqSize(&xsize, &ysize) < 0) return (FALSE);
fprintf(stdout, "Camera image size (x,y) = (%d,%d)
", xsize, ysize);

// Load the camera parameters, resize for the window and init.
if (arParamLoad(cparam_name, 1, &wparam) < 0) {
fprintf(stderr, "setupCamera(): Error loading parameter file %s for camera.
", cparam_name);
return (FALSE);
}
arParamChangeSize(&wparam, xsize, ysize, cparam);
fprintf(stdout, "*** Camera Parameter ***
");
arParamDisp(cparam);

arInitCparam(cparam);

if (arVideoCapStart() != 0) {
fprintf(stderr, "setupCamera(): Unable to begin camera data capture.
");
return (FALSE);
}

return (TRUE);
}

static int setupMarkersObjects(char *objectDataFilename)
{
// Load in the object data - trained markers and associated bitmap files.
if ((gObjectData = read_VRMLdata(objectDataFilename, &gObjectDataCount)) == NULL) {
fprintf(stderr, "setupMarkersObjects(): read_VRMLdata returned error !!
");
return (FALSE);
}

printf("Object count = %d
", gObjectDataCount);

return (TRUE);
}

// Report state of ARToolKit global variables arFittingMode,
// arImageProcMode, arglDrawMode, arTemplateMatchingMode, arMatchingPCAMode.
static void debugReportMode(void)
{
if(arFittingMode == AR_FITTING_TO_INPUT ) {
fprintf(stderr, "FittingMode (Z): INPUT IMAGE
");
} else {
fprintf(stderr, "FittingMode (Z): COMPENSATED IMAGE
");
}

if( arImageProcMode == AR_IMAGE_PROC_IN_FULL ) {
fprintf(stderr, "ProcMode (X) : FULL IMAGE
");
} else {
fprintf(stderr, "ProcMode (X) : HALF IMAGE
");
}

if (arglDrawModeGet(gArglSettings) == AR_DRAW_BY_GL_DRAW_PIXELS) {
fprintf(stderr, "DrawMode (C) : GL_DRAW_PIXELS
");
} else if (arglTexmapModeGet(gArglSettings) == AR_DRAW_TEXTURE_FULL_IMAGE) {
fprintf(stderr, "DrawMode (C) : TEXTURE MAPPING (FULL RESOLUTION)
");
} else {
fprintf(stderr, "DrawMode (C) : TEXTURE MAPPING (HALF RESOLUTION)
");
}

if( arTemplateMatchingMode == AR_TEMPLATE_MATCHING_COLOR ) {
fprintf(stderr, "TemplateMatchingMode (M) : Color Template
");
} else {
fprintf(stderr, "TemplateMatchingMode (M) : BW Template
");
}

if( arMatchingPCAMode == AR_MATCHING_WITHOUT_PCA ) {
fprintf(stderr, "MatchingPCAMode (P) : Without PCA
");
} else {
fprintf(stderr, "MatchingPCAMode (P) : With PCA
");
}
}

static void Quit(void)
{
arglCleanup(gArglSettings);
arVideoCapStop();
arVideoClose();
#ifdef _WIN32
CoUninitialize();
#endif
exit(0);
}

static void Keyboard(unsigned char key, int x, int y)
{
int mode;
switch (key) {
case 0x1B: // Quit.
case 'Q':
case 'q':
Quit();
break;
case 'e': camera_x = camera_x + step;
break;
case 'r': camera_x = camera_x - step;
break;
case 'f': camera_y = camera_y + step;
break;
case 'd': camera_y = camera_y - step;
break;
case 's': camera_z = camera_z + step;
break;
case 'a': camera_z = camera_z - step;
break;
case 'C':
case 'c':
mode = arglDrawModeGet(gArglSettings);
if (mode == AR_DRAW_BY_GL_DRAW_PIXELS) {
arglDrawModeSet(gArglSettings, AR_DRAW_BY_TEXTURE_MAPPING);
arglTexmapModeSet(gArglSettings, AR_DRAW_TEXTURE_FULL_IMAGE);
} else {
mode = arglTexmapModeGet(gArglSettings);
if (mode == AR_DRAW_TEXTURE_FULL_IMAGE) arglTexmapModeSet(gArglSettings, AR_DRAW_TEXTURE_HALF_IMAGE);
else arglDrawModeSet(gArglSettings, AR_DRAW_BY_GL_DRAW_PIXELS);
}
fprintf(stderr, "*** Camera - %f (frame/sec)
", (double)gCallCountMarkerDetect/arUtilTimer());
gCallCountMarkerDetect = 0;
arUtilTimerReset();
debugReportMode();
break;
case '?':
case '/':
printf("Keys:
");
printf(" q or [esc] Quit demo.
");
printf(" c Change arglDrawMode and arglTexmapMode.
");
printf(" ? or / Show this help.
");
printf("
Additionally, the ARVideo library supplied the following help text:
");
arVideoDispOption();
break;
default:
break;
}
}

static void Idle(void)
{
static int ms_prev;
int ms;
float s_elapsed;
ARUint8 *image;

ARMarkerInfo *marker_info; // Pointer to array holding the details of detected markers.
int marker_num; // Count of number of markers detected.
int i, j, k;

// Find out how long since Idle() last ran.
ms = glutGet(GLUT_ELAPSED_TIME);
s_elapsed = (float)(ms - ms_prev) * 0.001;
if (s_elapsed < 0.01f) return; // Don't update more often than 100 Hz.
ms_prev = ms;

// Update drawing.
arVrmlTimerUpdate();

// Grab a video frame.
if ((image = arVideoGetImage()) != NULL) {
gARTImage = image; // Save the fetched image.
gPatt_found = FALSE; // Invalidate any previous detected markers.

gCallCountMarkerDetect++; // Increment ARToolKit FPS counter.

// Detect the markers in the video frame.
if (arDetectMarker(gARTImage, gARTThreshhold, &marker_info, &marker_num) < 0) {
exit(-1);
}

// Check for object visibility.

for (i = 0; i < gObjectDataCount; i++) {

// Check through the marker_info array for highest confidence
// visible marker matching our object's pattern.
k = -1;
for (j = 0; j < marker_num; j++) {
if (marker_info[j].id == gObjectData[i].id) {
if( k == -1 ) k = j; // First marker detected.
else if (marker_info[k].cf < marker_info[j].cf) k = j; // Higher confidence marker detected.
}
}

if (k != -1) {
// Get the transformation between the marker and the real camera.
//fprintf(stderr, "Saw object %d.
", i);
if (gObjectData[i].visible == 0) {
arGetTransMat(&marker_info[k],
gObjectData[i].marker_center, gObjectData[i].marker_width,
gObjectData[i].trans);
} else {
arGetTransMatCont(&marker_info[k], gObjectData[i].trans,
gObjectData[i].marker_center, gObjectData[i].marker_width,
gObjectData[i].trans);
}
gObjectData[i].visible = 1;
gPatt_found = TRUE;
} else {
gObjectData[i].visible = 0;
}
}

// Tell GLUT to update the display.
glutPostRedisplay();
}
}

//
// This function is called on events when the visibility of the
// GLUT window changes (including when it first becomes visible).
//
static void Visibility(int visible)
{
if (visible == GLUT_VISIBLE) {
glutIdleFunc(Idle);
} else {
glutIdleFunc(NULL);
}
}

//
// This function is called when the
// GLUT window is resized.
//
static void Reshape(int w, int h)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glViewport(0, 0, (GLsizei) w, (GLsizei) h);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

// Call through to anyone else who needs to know about window sizing here.
}

//
// This function is called when the window needs redrawing.
//
static void Display(void)
{
int i;
GLdouble p[16];
GLdouble m[16];

// Select correct buffer for this context.
glDrawBuffer(GL_BACK);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear the buffers for new frame.

arglDispImage(gARTImage, &gARTCparam, 1.0, gArglSettings); // zoom = 1.0.
arVideoCapNext();
gARTImage = NULL; // Image data is no longer valid after calling arVideoCapNext().

if (gPatt_found) {
// Projection transformation.
arglCameraFrustumRH(&gARTCparam, VIEW_DISTANCE_MIN, VIEW_DISTANCE_MAX, p);
glMatrixMode(GL_PROJECTION);
glLoadMatrixd(p);
glMatrixMode(GL_MODELVIEW);

// Viewing transformation.
glLoadIdentity();
// Lighting and geometry that moves with the camera should go here.
// (I.e. must be specified before viewing transformations.)
//none

glRotatef(camera_x, 1.0,0.0,0.0);
glRotatef(camera_y, 0.0,1.0,0.0);
glRotatef(camera_z, 0.0,0.0,1.0);
glPushMatrix();

// All other lighting and geometry goes here.
// Calculate the camera position for each object and draw it.
for (i = 0; i < gObjectDataCount; i++) {
if ((gObjectData[i].visible != 0) && (gObjectData[i].vrml_id >= 0)) {
//fprintf(stderr, "About to draw object %i
", i);
arglCameraViewRH(gObjectData[i].trans, m, VIEW_SCALEFACTOR_4);
glLoadMatrixd(m);

arVrmlDraw(gObjectData[i].vrml_id);
}
}
} // gPatt_found

// Any 2D overlays go here.
//none

glutSwapBuffers();
}

int main(int argc, char** argv)
{
int i;
char glutGamemode[32];
const char *cparam_name =
"Data/camera_para.dat";
#ifdef _WIN32
char *vconf = "Data\WDM_camera_flipV.xml";
#else
char *vconf = "";
#endif
char objectDataFilename[] = "Data/object_data_vrml";

// ----------------------------------------------------------------------------
// Library inits.
//

glutInit(&argc, argv);

// ----------------------------------------------------------------------------
// Hardware setup.
//

if (!setupCamera(cparam_name, vconf, &gARTCparam)) {
fprintf(stderr, "main(): Unable to set up AR camera.
");
exit(-1);
}

#ifdef _WIN32
CoInitialize(NULL);
#endif

// ----------------------------------------------------------------------------
// Library setup.
//

// Set up GL context(s) for OpenGL to draw into.
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
if (!prefWindowed) {
if (prefRefresh) sprintf(glutGamemode, "%ix%i:%i@%i", prefWidth, prefHeight, prefDepth, prefRefresh);
else sprintf(glutGamemode, "%ix%i:%i", prefWidth, prefHeight, prefDepth);
glutGameModeString(glutGamemode);
glutEnterGameMode();
} else {
glutInitWindowSize(gARTCparam.xsize, gARTCparam.ysize);
glutCreateWindow(argv[0]);
}

// Setup argl library for current context.
if ((gArglSettings = arglSetupForCurrentContext()) == NULL) {
fprintf(stderr, "main(): arglSetupForCurrentContext() returned error.
");
exit(-1);
}
debugReportMode();
arUtilTimerReset();

if (!setupMarkersObjects(objectDataFilename)) {
fprintf(stderr, "main(): Unable to set up AR objects and markers.
");
Quit();
}

// Test render all the VRML objects.
fprintf(stdout, "Pre-rendering the VRML objects...");
fflush(stdout);
glEnable(GL_TEXTURE_2D);
for (i = 0; i < gObjectDataCount; i++) {
arVrmlDraw(gObjectData[i].vrml_id);
}
glDisable(GL_TEXTURE_2D);
fprintf(stdout, " done
");

// Register GLUT event-handling callbacks.
// NB: Idle() is registered by Visibility.
glutDisplayFunc(Display);
glutReshapeFunc(Reshape);
glutVisibilityFunc(Visibility);
glutKeyboardFunc(Keyboard);

glutMainLoop();

return (0);
}
[/code]

The section I believe I need to change is in the "static void display(void)" section

[code]for (i = 0; i < gObjectDataCount; i++) {
if ((gObjectData[i].visible != 0) && (gObjectData[i].vrml_id >= 0)) {
//fprintf(stderr, "About to draw object %i
", i);
arglCameraViewRH(gObjectData[i].trans, m, VIEW_SCALEFACTOR_4);
glLoadMatrixd(m);

arVrmlDraw(gObjectData[i].vrml_id);
}
[/code]

I'd really appreciate any help with this, just say if you need more information regarding this
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories