void VirtualEnvironmentFinal(void) { int i; if (objects) { // Unload all objects. for (i = 0; i < objectCount; i++) { arOSGUnloadModel(VirtualEnvironment_AROSG, i); } free(objects); objects = NULL; objectCount = 0; } // Cleanup all-model state. if (VirtualEnvironment_AROSG) { arOSGFinal(VirtualEnvironment_AROSG); VirtualEnvironment_AROSG = NULL; } }
int VirtualEnvironmentInit(const char *objectListFile) { int numObjects; FILE *fp; char buf[MAXPATHLEN]; char objectFullpath[MAXPATHLEN]; int i; ARdouble translation[3], rotation[4], scale[3]; int lightingFlag, markerIndex; ARLOGd("Initialising Virtual Environment.\n"); // One-time OSG initialization. if (!VirtualEnvironment_AROSG) { VirtualEnvironment_AROSG = arOSGInit(); if (!VirtualEnvironment_AROSG) { ARLOGe("Error: unable to init arOSG library.\n"); return (0); } } // Locate and open the objects description file. if ((fp = fopen(objectListFile, "r")) == NULL) { ARLOGe("Error: unable to open object data file '%s'.\n", objectListFile); perror(NULL); goto bail1; } else { ARLOGe("Error: open object data file '%s'.\n", objectListFile); } // First line is number of objects to read. numObjects = 0; get_buff(buf, MAXPATHLEN, fp, 1); if (sscanf(buf, "%d", &numObjects) != 1) { ARLOGe("Error: unable to read number of objects to load from object data file.\n"); goto bail2; } // Allocate space for the objects. if (objects) { free(objects); objects = NULL; objectCount = 0; } objects = (VEObject*)calloc(numObjects, sizeof(VEObject)); if (!objects) { goto bail2; } ARLOGd("Reading %d objects.\n", numObjects); for (i = 0; i < numObjects; i++) { // Read in all info relating to the object. // Read model file path (relative to objects description file). if (!get_buff(buf, MAXPATHLEN, fp, 1)) { ARLOGe("Error: unable to read model file name from object data file.\n"); goto bail3; } if (!arUtilGetDirectoryNameFromPath(objectFullpath, objectListFile, sizeof(objectFullpath), 1)) // Get directory prefix, with path separator. { goto bail3; } strncat(objectFullpath, buf, sizeof(objectFullpath) - strlen(objectFullpath) - 1); // Add name of file to open. // Read translation. get_buff(buf, MAXPATHLEN, fp, 1); #ifdef ARDOUBLE_IS_FLOAT if (sscanf(buf, "%f %f %f", &translation[0], &translation[1], &translation[2]) != 3) #else if (sscanf(buf, "%lf %lf %lf", &translation[0], &translation[1], &translation[2]) != 3) #endif { goto bail3; } // Read rotation. get_buff(buf, MAXPATHLEN, fp, 1); #ifdef ARDOUBLE_IS_FLOAT if (sscanf(buf, "%f %f %f %f", &rotation[0], &rotation[1], &rotation[2], &rotation[3]) != 4) #else if (sscanf(buf, "%lf %lf %lf %lf", &rotation[0], &rotation[1], &rotation[2], &rotation[3]) != 4) #endif { goto bail3; } // Read scale. get_buff(buf, MAXPATHLEN, fp, 1); #ifdef ARDOUBLE_IS_FLOAT if (sscanf(buf, "%f %f %f", &scale[0], &scale[1], &scale[2]) != 3) #else if (sscanf(buf, "%lf %lf %lf", &scale[0], &scale[1], &scale[2]) != 3) #endif { goto bail3; } // Look for optional tokens. A blank line marks end of options. lightingFlag = 1; markerIndex = -1; while (get_buff(buf, MAXPATHLEN, fp, 0) && (buf[0] != '\0')) { if (strncmp(buf, "LIGHTING", 8) == 0) { if (sscanf(&(buf[8]), " %d", &lightingFlag) != 1) { ARLOGe("Error in object file: LIGHTING token must be followed by an integer >= 0. Discarding.\n"); } } else if (strncmp(buf, "MARKER", 6) == 0) { if (sscanf(&(buf[6]), " %d", &markerIndex) != 1) { ARLOGe("Error in object file: MARKER token must be followed by an integer > 0. Discarding.\n"); } else { markerIndex--; // Marker numbers are zero-indexed, but in the config file they're 1-indexed. } } // Unknown tokens are ignored. } // Now attempt to load objects. ARLOGd("Reading object data file %s.\n", objectFullpath); objects[i].modelIndex = arOSGLoadModel2(VirtualEnvironment_AROSG, objectFullpath, translation, rotation, scale); if (objects[i].modelIndex < 0) { ARLOGe("Error attempting to read object data file %s.\n", objectFullpath); goto bail4; } // Set optional properties. arOSGSetModelLighting(VirtualEnvironment_AROSG, objects[i].modelIndex, lightingFlag); // If a valid marker index has been specified, save it. if (markerIndex >= 0 /*&& markerIndex < markersCount]*/) { arOSGSetModelVisibility(VirtualEnvironment_AROSG, objects[i].modelIndex, FALSE); // Objects tied to markers will not be initially visible. objects[i].markerIndex = markerIndex; } else { arOSGSetModelVisibility(VirtualEnvironment_AROSG, objects[i].modelIndex, TRUE); // All other objects will be initially visible. objects[i].markerIndex = -1; } objectCount++; } ARLOGd("Virtual Environment initialised.\n"); fclose(fp); return (objectCount); bail4: for (i--; i >= 0; i--) { arOSGUnloadModel(VirtualEnvironment_AROSG, i); } bail3: free(objects); objects = NULL; objectCount = 0; bail2: fclose(fp); bail1: if (VirtualEnvironment_AROSG) { arOSGFinal(VirtualEnvironment_AROSG); VirtualEnvironment_AROSG = NULL; } #ifdef DEBUG ARLOGe("Virtual Environment initialisation failed.\n"); #endif return (0); }