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