00001 #include <AR/arvrml.h> 00002 #include "arViewer_openvrml-0.14.3.h" 00003 #include <iostream> 00004 #include <vector> 00005 #include <string> 00006 #ifdef __APPLE__ 00007 # include <GLUT/glut.h> 00008 #else 00009 # include <GL/glut.h> 00010 #endif 00011 #include <stdio.h> 00012 #include <string.h> 00013 00014 #define AR_VRML_MAX 100 00015 00016 static arVrmlViewer *viewer[AR_VRML_MAX]; 00017 static int init = 1; 00018 static int vrID = -1; 00019 00020 static char *get_buff( char *buf, int n, FILE *fp ); 00021 00022 00023 int arVrmlLoadFile(const char *file) 00024 { 00025 00026 FILE *fp; 00027 openvrml::browser * myBrowser = 0; 00028 char buf[256], buf1[256]; 00029 char buf2[256]; 00030 int id; 00031 int i; 00032 00033 if( init ) { 00034 for( i = 0; i < AR_VRML_MAX; i++ ) viewer[i] = NULL; 00035 init = 0; 00036 } 00037 for( i = 0; i < AR_VRML_MAX; i++ ) { 00038 if( viewer[i] == NULL ) break; 00039 } 00040 if( i == AR_VRML_MAX ) return -1; 00041 id = i; 00042 00043 00044 if( (fp=fopen(file, "r")) == NULL ) return -1; 00045 00046 get_buff(buf, 256, fp); 00047 if( sscanf(buf, "%s", buf1) != 1 ) {fclose(fp); return -1;} 00048 for( i = 0; file[i] != '\0'; i++ ) buf2[i] = file[i]; 00049 for( ; i >= 0; i-- ) { 00050 if( buf2[i] == '/' ) break; 00051 } 00052 buf2[i+1] = '\0'; 00053 sprintf(buf, "%s%s", buf2, buf1); 00054 00055 myBrowser = new openvrml::browser(std::cout, std::cerr); 00056 if( !myBrowser) {fclose(fp); return -1;} 00057 00058 std::vector<std::string> uri(1, buf); 00059 std::vector<std::string> parameter; 00060 myBrowser->load_url(uri, parameter); 00061 00062 viewer[id] = new arVrmlViewer(*myBrowser); 00063 if(!viewer[id]) 00064 { 00065 delete myBrowser; 00066 fclose(fp); 00067 return -1; 00068 } 00069 strcpy( viewer[id]->filename, buf ); 00070 00071 get_buff(buf, 256, fp); 00072 if( sscanf(buf, "%lf %lf %lf", &viewer[id]->translation[0], 00073 &viewer[id]->translation[1], &viewer[id]->translation[2]) != 3 ) { 00074 delete viewer[id]; 00075 viewer[id] = NULL; 00076 fclose(fp); 00077 return -1; 00078 } 00079 00080 get_buff(buf, 256, fp); 00081 if( sscanf(buf, "%lf %lf %lf %lf", &viewer[id]->rotation[0], 00082 &viewer[id]->rotation[1], &viewer[id]->rotation[2], &viewer[id]->rotation[3]) != 4 ) { 00083 delete viewer[id]; 00084 viewer[id] = NULL; 00085 fclose(fp); 00086 return -1; 00087 } 00088 00089 get_buff(buf, 256, fp); 00090 if( sscanf(buf, "%lf %lf %lf", &viewer[id]->scale[0], &viewer[id]->scale[1], 00091 &viewer[id]->scale[2]) != 3 ) { 00092 delete viewer[id]; 00093 viewer[id] = NULL; 00094 fclose(fp); 00095 return -1; 00096 } 00097 fclose(fp); 00098 00099 return id; 00100 } 00101 00102 int arVrmlFree( int id ) 00103 { 00104 if( viewer[id] == NULL ) return -1; 00105 00106 delete viewer[id]; 00107 viewer[id] = NULL; 00108 00109 if( vrID == id ) { 00110 vrID = -1; 00111 } 00112 00113 return 0; 00114 } 00115 00116 int arVrmlTimerUpdate() 00117 { 00118 int i; 00119 00120 for( i = 0; i < AR_VRML_MAX; i++ ) { 00121 if( viewer[i] == NULL ) continue; 00122 viewer[i]->timerUpdate(); 00123 } 00124 return 0; 00125 } 00126 00127 int arVrmlDraw( int id ) 00128 { 00129 if( viewer[id] == NULL ) return -1; 00130 viewer[id]->redraw(); 00131 return 0; 00132 } 00133 00134 int arVrmlSetInternalLight( int flag ) 00135 { 00136 int i; 00137 00138 if( flag ) { 00139 for( i = 0; i < AR_VRML_MAX; i++ ) { 00140 if( viewer[i] == NULL ) continue; 00141 viewer[i]->setInternalLight(true); 00142 } 00143 } 00144 else { 00145 for( i = 0; i < AR_VRML_MAX; i++ ) { 00146 if( viewer[i] == NULL ) continue; 00147 viewer[i]->setInternalLight(false); 00148 } 00149 } 00150 00151 return 0; 00152 } 00153 00154 static char *get_buff( char *buf, int n, FILE *fp ) 00155 { 00156 char *ret, buf1[256]; 00157 00158 for(;;) { 00159 ret = fgets( buf, n, fp ); 00160 if( ret == NULL ) return(NULL); 00161 if( sscanf(buf, "%s", buf1) != 1 ) continue; 00162 00163 if( buf1[0] != '#' ) return(ret); 00164 } 00165 }