00001
00002
00003
00004 #include "TAseLoader.h"
00005
00006
00007
00008
00009
00010
00011 #define sqr(x) x*x
00012
00013
00014
00015 #define str2int(string) atoi(string)
00016 #define str2long(string) atol(string)
00017 #define str2float(string) atof(string)
00018
00019
00020
00021 uint no_create(char *file)
00022 {
00023 FILE *fp;
00024 fp = fopen(file,"r");
00025 if(fp != NULL)
00026 {
00027
00028 return 1;
00029 }
00030 else
00031 {
00032
00033 return 0;
00034 }
00035 fclose(fp);
00036 }
00037
00038
00039
00040
00041
00042
00043
00044 uint TAseLoader::LoadFile(char *filename)
00045 {
00046
00047 int current_obj = 0;
00048 int current_cam = 0;
00049 int current_light = 0;
00050 int current_shape = 0;
00051 int current_vertex = 0;
00052 int current_trin = 0;
00053 int current_f_norm = 0;
00054 int current_tvertex = 0;
00055 int current_tface = 0;
00056 int hm_tvertex = 0;
00057 int *tvertex;
00058 int NumOfTVertex;
00059 int NumOfTFaces;
00060
00061 char buffer[256];
00062 string ss,tmp;
00063 ifstream ase_file;
00064
00065
00066 SceneInfo *info;
00067
00068
00069 Vertex3D **tmp_vertex;
00070 Vertex3D **tmp_vertex_normal;
00071 Vertex3D **tmp_tvertex;
00072 Triangle **tmp_tfaces;
00073 Triangle **tmp_trin;
00074
00075
00076 info = ReadSceneInfo(filename);
00077 if(info == NULL)
00078 return 0;
00079
00080
00081 ase_scene = (Scene3D *)malloc(sizeof(Scene3D));
00082 ase_scene->objs = (Object3D *)malloc(info->ObjsNum*sizeof(Object3D));
00083 ase_scene->cams = (Camera3D *)malloc(info->CamsNum*sizeof(Camera3D));
00084 ase_scene->lights = (Light3D *)malloc(info->LightsNum*sizeof(Light3D));
00085 ase_scene->shapes = (Shape3D *)malloc(info->ShapesNum*sizeof(Shape3D));
00086
00087 tmp_vertex_normal = (Vertex3D **)malloc(info->ObjsNum*sizeof(Vertex3D *));
00088 tmp_vertex = (Vertex3D **)malloc(info->ObjsNum*sizeof(Vertex3D *));
00089 tmp_tvertex = (Vertex3D **)malloc(info->ObjsNum*sizeof(Vertex3D *));
00090 tmp_tfaces = (Triangle **)malloc(info->ObjsNum*sizeof(Triangle *));
00091 tmp_trin = (Triangle **)malloc(info->ObjsNum*sizeof(Triangle *));
00092 tvertex = (int *)malloc(info->ObjsNum*sizeof(int));
00093
00094
00095 ase_scene->NumberOfObjects = info->ObjsNum;
00096 ase_scene->NumberOfCameras = info->CamsNum;
00097 ase_scene->NumberOfLights = info->LightsNum;
00098 ase_scene->NumberOfShapes = info->ShapesNum;
00099
00100 ase_file.open(filename,ios::in);
00101
00102 while(!ase_file.eof())
00103 {
00104 ase_file.getline(buffer,256);
00105 ss = buffer;
00106
00107
00108 if(ss.find("*COMMENT",0) != string::npos)
00109 {
00110 tmp = ss.substr(ss.find("\"",0)+1,ss.rfind("\"",ss.length())-ss.find("\"",0)-1);
00111 ascii_version = (char *)malloc((ss.rfind("\"",ss.length())-ss.find("\"",0)-1)*sizeof(char));
00112 strcpy(ascii_version,tmp.c_str());
00113 ascii_version[strlen(ascii_version)] = '\0';
00114 tmp.erase();
00115 }
00116
00117
00118 if(ss.find(GOBJECT,0) != string::npos && current_obj != info->ObjsNum)
00119 {
00120
00121 bool objs_read = false;
00122 bool node_name = false;
00123
00124 while(objs_read == false)
00125 {
00126 ase_file.getline(buffer,256);
00127 ss = buffer;
00128
00129 if(ss.find("*NODE_NAME",0) != string::npos && node_name == false)
00130 {
00131 tmp = ss.substr(ss.find("\"",0)+1,ss.rfind("\"",ss.length())-ss.find("\"",0)-1);
00132 ase_scene->objs[current_obj].name = (char *)malloc( (ss.rfind("\"",ss.length())-ss.find("\"",0)) * sizeof(char) );
00133 strcpy(ase_scene->objs[current_obj].name,tmp.c_str());
00134 ase_scene->objs[current_obj].name[strlen(ase_scene->objs[current_obj].name)] = '\0';
00135 tmp.erase();
00136 node_name = true;
00137 }
00138
00139
00140 if(ss.find(NUM_VERTEX,0) != string::npos)
00141 {
00142 tmp = ss.substr(ss.find(NUM_VERTEX,0)+strlen(NUM_VERTEX),ss.length());
00143 ase_scene->objs[current_obj].NumOfVertex = str2int(tmp.c_str());
00144 tmp_vertex[current_obj] = (Vertex3D *)malloc(ase_scene->objs[current_obj].NumOfVertex*sizeof(Vertex3D));
00145 tmp_vertex_normal[current_obj] = (Vertex3D *)malloc(ase_scene->objs[current_obj].NumOfVertex*sizeof(Vertex3D));
00146 }
00147
00148 if(ss.find(NUM_FACES,0) != string::npos)
00149 {
00150 tmp = ss.substr(ss.find(NUM_FACES,0)+strlen(NUM_FACES),ss.length());
00151 ase_scene->objs[current_obj].NumOfFaces = str2int(tmp.c_str());
00152 ase_scene->objs[current_obj].faces = (Face *)malloc(ase_scene->objs[current_obj].NumOfFaces*sizeof(Face));
00153 tmp_trin[current_obj] = (Triangle *)malloc(ase_scene->objs[current_obj].NumOfFaces*sizeof(Triangle));
00154 }
00155
00156 if(ss.find(NUM_TVERTEX,0) != string::npos)
00157 {
00158 tmp = ss.substr(ss.find(NUM_TVERTEX,0)+strlen(NUM_TVERTEX),ss.length());
00159 NumOfTVertex = str2int(tmp.c_str());
00160 tmp_tvertex[current_obj] = (Vertex3D *)malloc(NumOfTVertex*sizeof(Vertex3D));
00161 }
00162
00163 if(ss.find(NUM_TFACES,0) != string::npos)
00164 {
00165 tmp = ss.substr(ss.find(NUM_TFACES,0)+strlen(NUM_TFACES),ss.length());
00166 NumOfTFaces = str2int(tmp.c_str());
00167 tmp_tfaces[current_obj] = (Triangle *)malloc(NumOfTFaces*sizeof(Triangle));
00168 }
00169
00170 if(ss.find(VERTEX,0) != string::npos)
00171 {
00172 while(current_vertex != ase_scene->objs[current_obj].NumOfVertex)
00173 {
00174 ase_file.getline(buffer,256);
00175 Vertex3D vertex = ReadVertex(buffer);
00176 tmp_vertex[current_obj][current_vertex].x = vertex.x;
00177 tmp_vertex[current_obj][current_vertex].y = vertex.y;
00178 tmp_vertex[current_obj][current_vertex].z = vertex.z;
00179 current_vertex++;
00180 }
00181 }
00182
00183 if(ss.find(FACE,0) != string::npos)
00184 {
00185 while(current_trin != ase_scene->objs[current_obj].NumOfFaces)
00186 {
00187 ase_file.getline(buffer,256);
00188 Triangle trin = ReadTriangle(buffer);
00189 tmp_trin[current_obj][current_trin].a = trin.a;
00190 tmp_trin[current_obj][current_trin].b = trin.b;
00191 tmp_trin[current_obj][current_trin].c = trin.c;
00192 current_trin++;
00193 }
00194 }
00195
00196 if(ss.find(TVERTEX,0) != string::npos)
00197 {
00198 while(current_tvertex != NumOfTVertex)
00199 {
00200 ase_file.getline(buffer,256);
00201 Vertex3D vertex = ReadUV(buffer);
00202 tmp_tvertex[current_obj][current_tvertex].x = vertex.x;
00203 tmp_tvertex[current_obj][current_tvertex].y = vertex.y;
00204 tmp_tvertex[current_obj][current_tvertex].z = vertex.z;
00205 current_tvertex++;
00206 }
00207 tvertex[hm_tvertex] = current_obj;
00208 hm_tvertex++;
00209 }
00210
00211 if(ss.find(TFACE,0) != string::npos)
00212 {
00213 while(current_tface != NumOfTFaces)
00214 {
00215 ase_file.getline(buffer,256);
00216 Triangle trin = ReadUVFaces(buffer);
00217 tmp_tfaces[current_obj][current_tface].a = trin.a;
00218 tmp_tfaces[current_obj][current_tface].b = trin.b;
00219 tmp_tfaces[current_obj][current_tface].c = trin.c;
00220 current_tface++;
00221 }
00222 ase_scene->objs[current_obj].tex_read = true;
00223 }
00224
00225 if(ss.find("*MESH_FACENORMAL",0) != string::npos)
00226 {
00227 while(current_f_norm != ase_scene->objs[current_obj].NumOfFaces)
00228 {
00229 Vertex3D vertex;
00230
00231
00232 vertex = ReadFaceNormal(buffer);
00233 ase_scene->objs[current_obj].faces[current_f_norm].face_normal.x = vertex.x;
00234 ase_scene->objs[current_obj].faces[current_f_norm].face_normal.y = vertex.y;
00235 ase_scene->objs[current_obj].faces[current_f_norm].face_normal.z = vertex.z;
00236
00237 ase_file.getline(buffer,256);
00238
00239 vertex = ReadVertexNormal(buffer);
00240 ase_scene->objs[current_obj].faces[current_f_norm].vertex_normal[0].x = vertex.x;
00241 ase_scene->objs[current_obj].faces[current_f_norm].vertex_normal[0].y = vertex.y;
00242 ase_scene->objs[current_obj].faces[current_f_norm].vertex_normal[0].z = vertex.z;
00243
00244 ase_file.getline(buffer,256);
00245
00246 vertex = ReadVertexNormal(buffer);
00247 ase_scene->objs[current_obj].faces[current_f_norm].vertex_normal[1].x = vertex.x;
00248 ase_scene->objs[current_obj].faces[current_f_norm].vertex_normal[1].y = vertex.y;
00249 ase_scene->objs[current_obj].faces[current_f_norm].vertex_normal[1].z = vertex.z;
00250
00251 ase_file.getline(buffer,256);
00252
00253 vertex = ReadVertexNormal(buffer);
00254 ase_scene->objs[current_obj].faces[current_f_norm].vertex_normal[2].x = vertex.x;
00255 ase_scene->objs[current_obj].faces[current_f_norm].vertex_normal[2].y = vertex.y;
00256 ase_scene->objs[current_obj].faces[current_f_norm].vertex_normal[2].z = vertex.z;
00257
00258 ase_file.getline(buffer,256);
00259 current_f_norm++;
00260 }
00261 objs_read = true;
00262 normals_read = true;
00263 }
00264 else if(ss.find("*PROP_RECVSHADOW",0) != string::npos)
00265 {
00266 normals_read = false;
00267 objs_read = true;
00268 if(ase_scene->objs[current_obj].tex_read != true)
00269 ase_scene->objs[current_obj].tex_read = false;
00270 }
00271
00272 if(current_trin == ase_scene->objs[current_obj].NumOfFaces && objs_read == true)
00273 {
00274 current_f_norm = 0;
00275 current_trin = 0;
00276 current_vertex = 0;
00277 current_tvertex = 0;
00278 current_tface = 0;
00279 current_obj++;
00280 }
00281 }
00282 }
00283
00284
00285 if(ss.find(COBJECT,0) != string::npos && current_cam != info->CamsNum)
00286 {
00287
00288 int cams_read = 0;
00289 bool node_name = false;
00290 bool camera_read = false;
00291
00292 while(camera_read != true)
00293 {
00294 ase_file.getline(buffer,256);
00295 ss = buffer;
00296
00297
00298 if(ss.find("*NODE_NAME",0) != string::npos && node_name == false)
00299 {
00300 tmp = ss.substr(ss.find("\"",0)+1,ss.rfind("\"",ss.length())-ss.find("\"",0)-1);
00301 ase_scene->cams[current_cam].name = (char *)malloc( (ss.rfind("\"",ss.length())-ss.find("\"",0)) * sizeof(char) );
00302 strcpy(ase_scene->cams[current_cam].name,tmp.c_str());
00303 ase_scene->cams[current_cam].name[strlen(ase_scene->cams[current_cam].name)] = '\0';
00304 tmp.erase();
00305 node_name = true;
00306 }
00307
00308
00309 if(ss.find("*TM_POS",0) != string::npos && cams_read != 2)
00310 {
00311 Vertex3D vertex = ReadTmPos(buffer);
00312
00313 if(cams_read == 1)
00314 {
00315 ase_scene->cams[current_cam].center.x = vertex.x;
00316 ase_scene->cams[current_cam].center.y = vertex.y;
00317 ase_scene->cams[current_cam].center.z = vertex.z;
00318 cams_read++;
00319 }
00320
00321 if(cams_read == 0)
00322 {
00323 ase_scene->cams[current_cam].eye.x = vertex.x;
00324 ase_scene->cams[current_cam].eye.y = vertex.y;
00325 ase_scene->cams[current_cam].eye.z = vertex.z;
00326 cams_read++;
00327 }
00328 }
00329
00330 if(ss.find("*CAMERA_NEAR",0) != string::npos)
00331 {
00332 float _Near = ReadCameraNear(buffer);
00333 ase_scene->cams[current_cam].Near = _Near;
00334 }
00335
00336 if(ss.find("*CAMERA_FAR",0) != string::npos)
00337 {
00338 float _Far = ReadCameraFar(buffer);
00339 ase_scene->cams[current_cam].Far = _Far;
00340 }
00341
00342 if(ss.find("*CAMERA_FOV",0) != string::npos)
00343 {
00344 float fov = ReadCameraFov(buffer);
00345 ase_scene->cams[current_cam].fov = fov;
00346 cams_read = 3;
00347 }
00348
00349 if(cams_read == 3)
00350 {
00351 camera_read = true;
00352 node_name = false;
00353 current_cam++;
00354 }
00355 }
00356 cams_read = 0;
00357 }
00358
00359
00360
00361 if(ss.find(LOBJECT,0) != string::npos && current_light != info->LightsNum)
00362 {
00363 int pos_count = 0;
00364 bool light_read = false;
00365 bool node_name = false;
00366 while(light_read != true)
00367 {
00368 ase_file.getline(buffer,256);
00369 ss = buffer;
00370
00371
00372 if(ss.find("*NODE_NAME",0) != string::npos && node_name == false)
00373 {
00374 tmp = ss.substr(ss.find("\"",0)+1,ss.rfind("\"",ss.length())-ss.find("\"",0)-1);
00375 ase_scene->lights[current_light].name = (char *)malloc( (ss.rfind("\"",ss.length())-ss.find("\"",0)) * sizeof(char) );
00376 strcpy(ase_scene->lights[current_light].name,tmp.c_str());
00377 ase_scene->lights[current_light].name[strlen(ase_scene->lights[current_light].name)] = '\0';
00378 tmp.erase();
00379 node_name = true;
00380 }
00381
00382 if(ss.find("*LIGHT_TYPE",0) != string::npos)
00383 {
00384 ase_scene->lights[current_light].type = ReadLightType(buffer);
00385 }
00386
00387 if(ss.find("*TM_POS",0) != string::npos)
00388 {
00389 if(ase_scene->lights[current_light].type == SPOT)
00390 {
00391 if(pos_count == 0)
00392 {
00393 Vertex3D vertex;
00394 vertex = ReadTmPos(buffer);
00395 ase_scene->lights[current_light].eye.x = vertex.x;
00396 ase_scene->lights[current_light].eye.y = vertex.y;
00397 ase_scene->lights[current_light].eye.z = vertex.z;
00398 }
00399
00400 if(pos_count == 1)
00401 {
00402 Vertex3D vertex;
00403 vertex = ReadTmPos(buffer);
00404 ase_scene->lights[current_light].center.x = vertex.x;
00405 ase_scene->lights[current_light].center.y = vertex.y;
00406 ase_scene->lights[current_light].center.z = vertex.z;
00407 }
00408 pos_count++;
00409 }
00410 if(ase_scene->lights[current_light].type == OMNI)
00411 {
00412 Vertex3D vertex;
00413 vertex = ReadTmPos(buffer);
00414 ase_scene->lights[current_light].eye.x = vertex.x;
00415 ase_scene->lights[current_light].eye.y = vertex.y;
00416 ase_scene->lights[current_light].eye.z = vertex.z;
00417 pos_count = 2;
00418 }
00419 }
00420
00421 if(ss.find("*LIGHT_COLOR",0) != string::npos)
00422 {
00423 ColorRGB color = ReadLightColor(buffer);
00424 ase_scene->lights[current_light].color.r = color.r;
00425 ase_scene->lights[current_light].color.g = color.g;
00426 ase_scene->lights[current_light].color.b = color.b;
00427
00428 if(ase_scene->lights[current_light].type == OMNI)
00429 pos_count = 3;
00430 }
00431
00432 if(ss.find("*LIGHT_FALLOFF",0) != string::npos)
00433 {
00434 float falloff = ReadLightFallOff(buffer);
00435 ase_scene->lights[current_light].falloff = falloff;
00436
00437 pos_count = 3;
00438 }
00439
00440 if(pos_count == 3)
00441 light_read = true;
00442 }
00443 current_light++;
00444 }
00445
00446
00447 if(ss.find("*SHAPEOBJECT",0) != string::npos && current_shape != info->ShapesNum)
00448 {
00449 bool shape_read = false;
00450 bool node_name = false;
00451 while(shape_read != true)
00452 {
00453 ase_file.getline(buffer,256);
00454 ss = buffer;
00455
00456 if(ss.find("*NODE_NAME",0) != string::npos && node_name == false)
00457 {
00458 tmp = ss.substr(ss.find("\"",0)+1,ss.rfind("\"",ss.length())-ss.find("\"",0)-1);
00459 ase_scene->shapes[current_shape].name = (char *)malloc( (ss.rfind("\"",ss.length())-ss.find("\"",0)) * sizeof(char) );
00460 strcpy(ase_scene->shapes[current_shape].name,tmp.c_str());
00461 ase_scene->shapes[current_shape].name[strlen(ase_scene->shapes[current_shape].name)] = '\0';
00462 tmp.erase();
00463 node_name = true;
00464 }
00465
00466 if(ss.find("*SHAPE_VERTEXCOUNT",0) != string::npos)
00467 {
00468 int shape_vnum = ReadShapeVertexCount(buffer);
00469 int counter = 0;
00470 int knot_c = 0;
00471 int seek_pos = ase_file.tellg();
00472
00473 ase_scene->shapes[current_shape].NumOfKnot = 0;
00474
00475 while(counter < shape_vnum)
00476 {
00477 ase_file.getline(buffer,256);
00478 ss = buffer;
00479 if(ss.find("*SHAPE_VERTEX_KNOT",0) != string::npos)
00480 {
00481 ase_scene->shapes[current_shape].NumOfKnot++;
00482 }
00483 counter++;
00484 }
00485
00486 ase_scene->shapes[current_shape].knot = (Vertex3D *)malloc( ase_scene->shapes[current_shape].NumOfKnot * sizeof(Vertex3D));
00487
00488 ase_file.seekg(seek_pos);
00489 counter = 0;
00490 while(counter < shape_vnum)
00491 {
00492 ase_file.getline(buffer,256);
00493 ss = buffer;
00494 if(ss.find("*SHAPE_VERTEX_KNOT",0) != string::npos)
00495 {
00496 Vertex3D vertex;
00497 vertex = ReadShapeKNOT(buffer);
00498 ase_scene->shapes[current_shape].knot[knot_c].x = vertex.x;
00499 ase_scene->shapes[current_shape].knot[knot_c].y = vertex.y;
00500 ase_scene->shapes[current_shape].knot[knot_c].z = vertex.z;
00501 knot_c++;
00502 }
00503 counter++;
00504 }
00505 shape_read = true;
00506 }
00507 }
00508 current_shape++;
00509 }
00510
00511 }
00512
00513
00514
00515 ase_file.close();
00516
00517
00518 for(current_obj = 0; current_obj < ase_scene->NumberOfObjects ; current_obj++)
00519 {
00520 for(int x = 0; x < ase_scene->objs[current_obj].NumOfFaces ; x++)
00521 {
00522
00523 ase_scene->objs[current_obj].faces[x].vertex[0].x = tmp_vertex[current_obj][tmp_trin[current_obj][x].a].x;
00524 ase_scene->objs[current_obj].faces[x].vertex[0].y = tmp_vertex[current_obj][tmp_trin[current_obj][x].a].y;
00525 ase_scene->objs[current_obj].faces[x].vertex[0].z = tmp_vertex[current_obj][tmp_trin[current_obj][x].a].z;
00526
00527
00528 ase_scene->objs[current_obj].faces[x].vertex[1].x = tmp_vertex[current_obj][tmp_trin[current_obj][x].b].x;
00529 ase_scene->objs[current_obj].faces[x].vertex[1].y = tmp_vertex[current_obj][tmp_trin[current_obj][x].b].y;
00530 ase_scene->objs[current_obj].faces[x].vertex[1].z = tmp_vertex[current_obj][tmp_trin[current_obj][x].b].z;
00531
00532
00533 ase_scene->objs[current_obj].faces[x].vertex[2].x = tmp_vertex[current_obj][tmp_trin[current_obj][x].c].x;
00534 ase_scene->objs[current_obj].faces[x].vertex[2].y = tmp_vertex[current_obj][tmp_trin[current_obj][x].c].y;
00535 ase_scene->objs[current_obj].faces[x].vertex[2].z = tmp_vertex[current_obj][tmp_trin[current_obj][x].c].z;
00536
00537 if(ase_scene->objs[current_obj].tex_read == true)
00538 {
00539
00540 ase_scene->objs[current_obj].faces[x].tex_coords[0].u = tmp_tvertex[current_obj][tmp_tfaces[current_obj][x].a].x;
00541 ase_scene->objs[current_obj].faces[x].tex_coords[0].v = tmp_tvertex[current_obj][tmp_tfaces[current_obj][x].a].y;
00542
00543
00544 ase_scene->objs[current_obj].faces[x].tex_coords[1].u = tmp_tvertex[current_obj][tmp_tfaces[current_obj][x].b].x;
00545 ase_scene->objs[current_obj].faces[x].tex_coords[1].v = tmp_tvertex[current_obj][tmp_tfaces[current_obj][x].b].y;
00546
00547
00548 ase_scene->objs[current_obj].faces[x].tex_coords[2].u = tmp_tvertex[current_obj][tmp_tfaces[current_obj][x].c].x;
00549 ase_scene->objs[current_obj].faces[x].tex_coords[2].v = tmp_tvertex[current_obj][tmp_tfaces[current_obj][x].c].y;
00550 }
00551 }
00552 }
00553
00554
00555 for(int x = 0; x < info->ObjsNum ; x++)
00556 {
00557 free(tmp_vertex[x]);
00558 free(tmp_trin[x]);
00559 free(tmp_vertex_normal[x]);
00560 }
00561 for(int y = 0; y < hm_tvertex ; y++)
00562 {
00563 free(tmp_tvertex[tvertex[y]]);
00564 free(tmp_tfaces[tvertex[y]]);
00565 }
00566 free(tmp_vertex);
00567 free(tmp_tvertex);
00568 free(tmp_tfaces);
00569 free(tmp_trin);
00570 free(tmp_vertex_normal);
00571 free(info);
00572 return 1;
00573 }
00574
00575
00576
00577 Vertex3D TAseLoader::ReadVertex(char *lstring)
00578 {
00579 Vertex3D vertex;
00580 string ss,floats;
00581 ss = lstring;
00582 floats = ss.substr(ss.find("\t",ss.find("*MESH_VERTEX"))+1,ss.length());
00583
00584 vertex.x = str2float(floats.substr(0,floats.find("\t")).c_str());
00585 vertex.z = -1*str2float(floats.substr(floats.find("\t"),floats.rfind("\t",floats.length())).c_str());
00586 vertex.y = str2float(floats.substr(floats.rfind("\t",floats.length()),floats.length()).c_str());
00587 return vertex;
00588 }
00589
00590
00591
00592 Triangle TAseLoader::ReadTriangle(char *lstring)
00593 {
00594 Triangle trin;
00595 string ss;
00596 ss = lstring;
00597 trin.a = str2int(ss.substr(ss.find("A:")+2,ss.find("B:",0)-ss.find("A:",0)-2).c_str());
00598 trin.b = str2int(ss.substr(ss.find("B:")+2,ss.find("C:",0)-ss.find("B:",0)-2).c_str());
00599 trin.c = str2int(ss.substr(ss.find("C:")+2,ss.find("AB:",0)-ss.find("C:",0)-2).c_str());
00600 return trin;
00601 }
00602
00603
00604
00605 Vertex3D TAseLoader::ReadVertexNormal(char *lstring)
00606 {
00607 Vertex3D vertex;
00608 string ss,floats;
00609 ss = lstring;
00610 floats = ss.substr(ss.find("\t",ss.find("*MESH_VERTEXNORMAL"))+1,ss.length());
00611
00612 vertex.x = str2float(floats.substr(0,floats.find("\t",0)).c_str());
00613 vertex.z = -1*str2float(floats.substr(floats.find("\t")+1,floats.rfind("\t",floats.length())-floats.find("\t")).c_str());
00614 vertex.y = str2float(floats.substr(floats.rfind("\t",floats.length())+1,ss.length()).c_str());
00615 return vertex;
00616 }
00617
00618
00619
00620 Vertex3D TAseLoader::ReadUV(char *lstring)
00621 {
00622 Vertex3D vertex;
00623 string ss,floats;
00624 ss = lstring;
00625 floats = ss.substr(ss.find("\t",ss.find(TVERTEX))+1,ss.length());
00626 vertex.x = str2float(floats.substr(0,floats.find("\t")).c_str());
00627 vertex.y = str2float(floats.substr(floats.find("\t"),floats.rfind("\t",floats.length())).c_str());
00628 vertex.z = str2float(floats.substr(floats.rfind("\t",floats.length()),floats.length()).c_str());
00629 return vertex;
00630 }
00631
00632
00633
00634 Triangle TAseLoader::ReadUVFaces(char *lstring)
00635 {
00636 Triangle tri;
00637 string ss,floats;
00638 ss = lstring;
00639 floats = ss.substr(ss.find("\t",ss.find(TFACE))+1,ss.length());
00640 tri.a = str2int(floats.substr(0,floats.find("\t")).c_str());
00641 tri.b = str2int(floats.substr(floats.find("\t"),floats.rfind("\t",floats.length())).c_str());
00642 tri.c = str2int(floats.substr(floats.rfind("\t",floats.length()),floats.length()).c_str());
00643 return tri;
00644 }
00645
00646
00647
00648 Vertex3D TAseLoader::ReadFaceNormal(char *lstring)
00649 {
00650 Vertex3D vertex;
00651 string ss,floats;
00652 ss = lstring;
00653 floats = ss.substr(ss.find("\t",ss.find("*MESH_FACENORMAL"))+1,ss.length());
00654
00655 vertex.x = str2float(floats.substr(0,floats.find("\t",0)).c_str());
00656 vertex.z = -1*str2float(floats.substr(floats.find("\t")+1,floats.rfind("\t",floats.length())-1-floats.find("\t")).c_str());
00657 vertex.y = str2float(floats.substr(floats.rfind("\t",floats.length())+1,ss.length()).c_str());
00658 return vertex;
00659 }
00660
00661
00662
00663 Vertex3D TAseLoader::ReadTmPos(char *lstring)
00664 {
00665 Vertex3D vertex;
00666 string ss,floats;
00667 ss = lstring;
00668 floats = ss.substr(ss.find("*TM_POS")+strlen("*TM_POS"),ss.length());
00669
00670 vertex.x = str2float(floats.substr(0,floats.find("\t",0)).c_str());
00671 vertex.z = -1*str2float(floats.substr(floats.find("\t")+1,floats.rfind("\t",floats.length())-1-floats.find("\t")).c_str());
00672 vertex.y = str2float(floats.substr(floats.rfind("\t",floats.length())+1,ss.length()).c_str());
00673 return vertex;
00674 }
00675
00676
00677
00678 int TAseLoader::ReadLightType(char *lstring)
00679 {
00680 string ss;
00681 ss = lstring;
00682 if(strcmp(ss.substr(ss.find("*LIGHT_TYPE",0)+1+strlen("*LIGHT_TYPE"),ss.length()).c_str(),"Target") == 0)
00683 return SPOT;
00684 else
00685 return OMNI;
00686 }
00687
00688 ColorRGB TAseLoader::ReadLightColor(char *lstring)
00689 {
00690 ColorRGB color;
00691 string ss,floats;
00692 ss = lstring;
00693 floats = ss.substr(ss.find("*LIGHT_COLOR",0)+strlen("*LIGHT_COLOR")+1,ss.length()).c_str();
00694 color.r = str2float(floats.substr(0,floats.find("\t",0)).c_str());
00695 color.g = str2float(floats.substr(floats.find("\t")+1,floats.rfind("\t",floats.length())-1-floats.find("\t")).c_str());
00696 color.b = str2float(floats.substr(floats.rfind("\t",floats.length())+1,ss.length()).c_str());
00697 return color;
00698 }
00699
00700 float TAseLoader::ReadLightFallOff(char *lstring)
00701 {
00702 float falloff;
00703 string ss;
00704 ss = lstring;
00705 falloff = str2float(ss.substr(ss.find("*LIGHT_FALLOFF",0)+strlen("*LIGHT_FALLOFF")+1,ss.length()).c_str());
00706 return falloff;
00707 }
00708
00709 float TAseLoader::ReadCameraFov(char *lstring)
00710 {
00711 float fov;
00712 string ss;
00713 ss = lstring;
00714 fov = str2float(ss.substr(ss.find("*CAMERA_FOV",0)+strlen("*CAMERA_FOV")+1,ss.length()).c_str());
00715 return fov;
00716 }
00717
00718 float TAseLoader::ReadCameraNear(char *lstring)
00719 {
00720 float Near;
00721 string ss;
00722 ss = lstring;
00723 Near = str2float(ss.substr(ss.find("*CAMERA_NEAR",0)+strlen("*CAMERA_NEAR")+1,ss.length()).c_str());
00724 return Near;
00725 }
00726
00727 float TAseLoader::ReadCameraFar(char *lstring)
00728 {
00729 float Far;
00730 string ss;
00731 ss = lstring;
00732 Far = str2float(ss.substr(ss.find("*CAMERA_FAR",0)+strlen("*CAMERA_FAR")+1,ss.length()).c_str());
00733 return Far;
00734 }
00735
00736 uint TAseLoader::ReadShapeVertexCount(char *lstring)
00737 {
00738 string ss;
00739 ss = lstring;
00740 return str2int(ss.substr(ss.find("*SHAPE_VERTEXCOUNT",0)+strlen("*SHAPE_VERTEXCOUNT"),ss.length()).c_str());
00741 }
00742
00743 Vertex3D TAseLoader::ReadShapeKNOT(char *lstring)
00744 {
00745 Vertex3D vertex;
00746 string ss,floats;
00747 ss = lstring;
00748 floats = ss.substr(ss.find("\t",ss.find("*SHAPE_VERTEX_KNOT")+strlen("*SHAPE_VERTEX_KNOT")+1)+1,ss.length());
00749
00750 vertex.x = str2float(floats.substr(0,floats.find("\t")).c_str());
00751 vertex.z = -1*str2float(floats.substr(floats.find("\t"),floats.rfind("\t",floats.length())).c_str());
00752 vertex.y = str2float(floats.substr(floats.rfind("\t",floats.length()),floats.length()).c_str());
00753 return vertex;
00754 }
00755
00756
00757
00758 SceneInfo* TAseLoader::ReadSceneInfo(char *filename)
00759 {
00760 char buffer[256];
00761 string ss,tmp;
00762 ifstream ase_file;
00763
00764
00765 SceneInfo *info;
00766
00767 if (no_create(filename) == 0)
00768 {
00769 cout << "Couldn't find or open file: " << filename << endl;
00770 return NULL;
00771 }
00772
00773 info = (SceneInfo *)malloc(sizeof(SceneInfo));
00774
00775 info->ObjsNum = 0;
00776 info->CamsNum = 0;
00777 info->LightsNum = 0;
00778 info->ShapesNum = 0;
00779
00780 ase_file.open(filename,ios::in);
00781
00782 ase_file.getline(buffer,256);
00783 ss = buffer;
00784
00785 if(ss.find(ASEHEADER,0) == string::npos)
00786 {
00787 cout << "The file " << filename << " isn't ASciiExport(2.0) file format" << endl;
00788 return NULL;
00789 }
00790
00791 while(!ase_file.eof())
00792 {
00793 ase_file.getline(buffer,256);
00794 ss = buffer;
00795 if(ss.find(GOBJECT,0) != string::npos)
00796 info->ObjsNum++;
00797 if(ss.find(COBJECT,0) != string::npos)
00798 info->CamsNum++;
00799 if(ss.find(LOBJECT,0) != string::npos)
00800 info->LightsNum++;
00801 if(ss.find("*SHAPEOBJECT") != string::npos)
00802 info->ShapesNum++;
00803 }
00804
00805 return info;
00806 }
00807
00808
00809
00810 void TAseLoader::DumpInfoToFile(char *file)
00811 {
00812 ofstream out_file;
00813 out_file.open(file,ios::out);
00814
00815 out_file << "####################\n";
00816 out_file << "# TAseLoader::v0.1 #\n";
00817 out_file << "####################\n\n";
00818 out_file << "Scene general data:\n";
00819 out_file << "OBJECTS [" << setw(3) << setfill('.') << ase_scene->NumberOfObjects << "]\n";
00820 out_file << "CAMERAS [" << setw(3) << setfill('.') << ase_scene->NumberOfCameras << "]\n";
00821 out_file << "LIGHTS [" << setw(3) << setfill('.') << ase_scene->NumberOfLights << "]\n";
00822 out_file << "SHAPES [" << setw(3) << setfill('.') << ase_scene->NumberOfShapes << "]\n";
00823
00824 out_file << "\n";
00825
00826 if(ase_scene->NumberOfObjects != 0)
00827 {
00828 out_file << "Dumping Objects data :จจ):\n\n";
00829 for(int current_obj = 0; current_obj < ase_scene->NumberOfObjects ; current_obj++)
00830 {
00831 out_file << setw(strlen(ase_scene->objs[current_obj].name)+5) << setfill('*') << "\n";
00832 out_file << "* " << ase_scene->objs[current_obj].name << " *\n";
00833 out_file << setw(strlen(ase_scene->objs[current_obj].name)+5) << setfill('*') << "\n";
00834 out_file << "\n";
00835
00836 for(int x = 0; x < ase_scene->objs[current_obj].NumOfFaces; x++)
00837 {
00838 out_file << "FACE [" << setw(6) << setfill('.') << x << "]\n";
00839
00840 out_file << "fn[X]: " << setw(15) << setfill(' ') << ase_scene->objs[current_obj].faces[x].face_normal.x;
00841 out_file << " [Y]: " << setw(15) << setfill(' ') << ase_scene->objs[current_obj].faces[x].face_normal.y;
00842 out_file << " [Z]: " << setw(15) << setfill(' ') << ase_scene->objs[current_obj].faces[x].face_normal.z;
00843
00844 out_file << "\n";
00845
00846 out_file << "n1[X]: " << setw(15) << setfill(' ') << ase_scene->objs[current_obj].faces[x].vertex_normal[0].x;
00847 out_file << " [Y]: " << setw(15) << setfill(' ') << ase_scene->objs[current_obj].faces[x].vertex_normal[0].y;
00848 out_file << " [Z]: " << setw(15) << setfill(' ') << ase_scene->objs[current_obj].faces[x].vertex_normal[0].z;
00849
00850 out_file << "\n";
00851
00852 if(ase_scene->objs[current_obj].tex_read == true)
00853 {
00854 out_file << "t1[U]: " << setw(15) << setfill(' ') << ase_scene->objs[current_obj].faces[x].tex_coords[0].u;
00855 out_file << " [V]: " << setw(15) << setfill(' ') << ase_scene->objs[current_obj].faces[x].tex_coords[0].v;
00856
00857 out_file << "\n";
00858 }
00859
00860 out_file << "v1[X]: " << setw(15) << setfill(' ') << ase_scene->objs[current_obj].faces[x].vertex[0].x;
00861 out_file << " [Y]: " << setw(15) << setfill(' ') << ase_scene->objs[current_obj].faces[x].vertex[0].y;
00862 out_file << " [Z]: " << setw(15) << setfill(' ') << ase_scene->objs[current_obj].faces[x].vertex[0].z;
00863
00864 out_file << "\n";
00865
00866 out_file << "n2[X]: " << setw(15) << setfill(' ') << ase_scene->objs[current_obj].faces[x].vertex_normal[1].x;
00867 out_file << " [Y]: " << setw(15) << setfill(' ') << ase_scene->objs[current_obj].faces[x].vertex_normal[1].y;
00868 out_file << " [Z]: " << setw(15) << setfill(' ') << ase_scene->objs[current_obj].faces[x].vertex_normal[1].z;
00869
00870 out_file << "\n";
00871
00872 if(ase_scene->objs[current_obj].tex_read == true)
00873 {
00874 out_file << "t2[U]: " << setw(15) << setfill(' ') << ase_scene->objs[current_obj].faces[x].tex_coords[1].u;
00875 out_file << " [V]: " << setw(15) << setfill(' ') << ase_scene->objs[current_obj].faces[x].tex_coords[1].v;
00876
00877 out_file << "\n";
00878 }
00879
00880 out_file << "v2[X]: " << setw(15) << setfill(' ') << ase_scene->objs[current_obj].faces[x].vertex[1].x;
00881 out_file << " [Y]: " << setw(15) << setfill(' ') << ase_scene->objs[current_obj].faces[x].vertex[1].y;
00882 out_file << " [Z]: " << setw(15) << setfill(' ') << ase_scene->objs[current_obj].faces[x].vertex[1].z;
00883
00884 out_file << "\n";
00885
00886 out_file << "n3[X]: " << setw(15) << setfill(' ') << ase_scene->objs[current_obj].faces[x].vertex_normal[2].x;
00887 out_file << " [Y]: " << setw(15) << setfill(' ') << ase_scene->objs[current_obj].faces[x].vertex_normal[2].y;
00888 out_file << " [Z]: " << setw(15) << setfill(' ') << ase_scene->objs[current_obj].faces[x].vertex_normal[2].z;
00889
00890 out_file << "\n";
00891
00892 if(ase_scene->objs[current_obj].tex_read == true)
00893 {
00894 out_file << "t3[U]: " << setw(15) << setfill(' ') << ase_scene->objs[current_obj].faces[x].tex_coords[2].u;
00895 out_file << " [V]: " << setw(15) << setfill(' ') << ase_scene->objs[current_obj].faces[x].tex_coords[2].v;
00896
00897 out_file << "\n";
00898 }
00899
00900 out_file << "v3[X]: " << setw(15) << setfill(' ') << ase_scene->objs[current_obj].faces[x].vertex[2].x;
00901 out_file << " [Y]: " << setw(15) << setfill(' ') << ase_scene->objs[current_obj].faces[x].vertex[2].y;
00902 out_file << " [Z]: " << setw(15) << setfill(' ') << ase_scene->objs[current_obj].faces[x].vertex[2].z;
00903
00904 out_file << "\n\n";
00905 }
00906 }
00907 }
00908
00909 if(ase_scene->NumberOfCameras != 0)
00910 {
00911 out_file << "Dumping Cameras data :จจ):\n\n";
00912
00913 for(int current_cam = 0; current_cam < ase_scene->NumberOfCameras ; current_cam++)
00914 {
00915 out_file << setw(strlen(ase_scene->cams[current_cam].name)+5) << setfill('*') << "\n";
00916 out_file << "* " << ase_scene->cams[current_cam].name << " *\n";
00917 out_file << setw(strlen(ase_scene->cams[current_cam].name)+5) << setfill('*') << "\n";
00918
00919 out_file << "EYE [CAMERA POSITION]" << "\n";
00920 out_file << " [X]: " << setw(15) << setfill(' ') << ase_scene->cams[current_cam].eye.x;
00921 out_file << " [Y]: " << setw(15) << setfill(' ') << ase_scene->cams[current_cam].eye.y;
00922 out_file << " [Z]: " << setw(15) << setfill(' ') << ase_scene->cams[current_cam].eye.z;
00923
00924 out_file << "\n";
00925
00926 out_file << "CENTER [CAMERAS POINT OF VIEW]" << "\n";
00927 out_file << " [X]: " << setw(15) << setfill(' ') << ase_scene->cams[current_cam].center.x;
00928 out_file << " [Y]: " << setw(15) << setfill(' ') << ase_scene->cams[current_cam].center.y;
00929 out_file << " [Z]: " << setw(15) << setfill(' ') << ase_scene->cams[current_cam].center.z;
00930 out_file << "\n\n";
00931 }
00932 }
00933
00934 if(ase_scene->NumberOfLights != 0)
00935 {
00936 out_file << "Dumping Lights data :จจ):\n\n";
00937
00938 for(int current_light = 0; current_light < ase_scene->NumberOfLights ; current_light++)
00939 {
00940 out_file << setw(strlen(ase_scene->lights[current_light].name)+5) << setfill('*') << "\n";
00941 out_file << "* " << ase_scene->lights[current_light].name << " *\n";
00942 out_file << setw(strlen(ase_scene->lights[current_light].name)+5) << setfill('*') << "\n";
00943
00944 if(ase_scene->lights[current_light].type == SPOT)
00945 out_file << "LIGHT TYPE: spot" << "\n";
00946 if(ase_scene->lights[current_light].type == OMNI)
00947 out_file << "LIGHT TYPE: omni" << "\n";
00948
00949 out_file << "EYE [LIGHT POSITION]" << "\n";
00950 out_file << " [X]: " << setw(15) << setfill(' ') << ase_scene->lights[current_light].eye.x;
00951 out_file << " [Y]: " << setw(15) << setfill(' ') << ase_scene->lights[current_light].eye.y;
00952 out_file << " [Z]: " << setw(15) << setfill(' ') << ase_scene->lights[current_light].eye.z;
00953
00954 out_file << "\n";
00955
00956 if(ase_scene->lights[current_light].type == SPOT)
00957 {
00958 out_file << "CENTER [LIGHT ILUMINATION POINT]" << "\n";
00959 out_file << " [X]: " << setw(15) << setfill(' ') << ase_scene->lights[current_light].center.x;
00960 out_file << " [Y]: " << setw(15) << setfill(' ') << ase_scene->lights[current_light].center.y;
00961 out_file << " [Z]: " << setw(15) << setfill(' ') << ase_scene->lights[current_light].center.z;
00962 out_file << "\n\n";
00963 }
00964 }
00965 }
00966
00967 if(ase_scene->NumberOfShapes != 0)
00968 {
00969 out_file << "Dumping Shapes data :จจ):\n\n";
00970
00971 for(int current_shape = 0; current_shape < ase_scene->NumberOfShapes ; current_shape++)
00972 {
00973 out_file << setw(strlen(ase_scene->shapes[current_shape].name)+5) << setfill('*') << "\n";
00974 out_file << "* " << ase_scene->shapes[current_shape].name << " *\n";
00975 out_file << setw(strlen(ase_scene->shapes[current_shape].name)+5) << setfill('*') << "\n";
00976
00977 for(int knot_num = 0 ; knot_num < ase_scene->shapes[current_shape].NumOfKnot ; knot_num++)
00978 {
00979 out_file << "\nKNOT " << knot_num << "\n";
00980 out_file << " [X]: " << setw(15) << setfill(' ') << ase_scene->shapes[current_shape].knot[knot_num].x;
00981 out_file << " [Y]: " << setw(15) << setfill(' ') << ase_scene->shapes[current_shape].knot[knot_num].y;
00982 out_file << " [Z]: " << setw(15) << setfill(' ') << ase_scene->shapes[current_shape].knot[knot_num].z;
00983 }
00984 out_file << "\n";
00985 }
00986 }
00987
00988 out_file.close();
00989 }
00990
00991
00992
00993
00994 Scene3D* TAseLoader::GetScene(void)
00995 {
00996 return ase_scene;
00997 }
00998
00999 Object3D* TAseLoader::GetObject(int obj_num)
01000 {
01001 if(ase_scene->NumberOfObjects >= obj_num)
01002 return &ase_scene->objs[obj_num];
01003 else
01004 return NULL;
01005 }
01006
01007 Camera3D* TAseLoader::GetCamera(int cam_num)
01008 {
01009 if(ase_scene->NumberOfCameras >= cam_num)
01010 return &ase_scene->cams[cam_num];
01011 else
01012 return NULL;
01013 }
01014
01015 Light3D* TAseLoader::GetLight(int light_num)
01016 {
01017 if(ase_scene->NumberOfLights >= light_num)
01018 return &ase_scene->lights[light_num];
01019 else
01020 return NULL;
01021 }
01022
01023 Shape3D* TAseLoader::GetShape(int shape_num)
01024 {
01025 if(ase_scene->NumberOfShapes >= shape_num)
01026 return &ase_scene->shapes[shape_num];
01027 else
01028 return NULL;
01029 }
01030
01031 uint TAseLoader::GetObjectsNum(void)
01032 {
01033 return ase_scene->NumberOfObjects;
01034 }
01035
01036 uint TAseLoader::GetCamerasNum(void)
01037 {
01038 return ase_scene->NumberOfCameras;
01039 }
01040
01041 uint TAseLoader::GetLightsNum(void)
01042 {
01043 return ase_scene->NumberOfLights;
01044 }
01045
01046 char* TAseLoader::GetAsciiVersion(void)
01047 {
01048 return ascii_version;
01049 }