char OBJ_Load(char* filename,entity_t* entity) { static vec3_t tmpVertices[DE_USHRT_MAX]; ushort num_vertices=0; static vec2_t tmpTextureCoor[DE_USHRT_MAX]; ushort num_textuCoor=0; int idata[9]; int i; ushort indiceOfCurrentVertex; filehandle_t* objFile ; obj_t* obj; objFile = FS_OpenFile(filename, "rt"); if (!objFile) { printf("Could not Load OBJ: '%s'.\n",filename); return 0; } OBJ_InitVerticeCache(); obj = (obj_t*)entity->model; obj->vertices = (obj_vertex_t*)calloc(DE_USHRT_MAX, sizeof(obj_vertex_t)); obj->num_vertices = 0; obj->indices = (ushort*)calloc(DE_USHRT_MAX, sizeof(ushort)); obj->num_indices = 0; LE_pushLexer(); LE_init(objFile); while (LE_hasMoreData()) { LE_readToken(); if (!strcmp("v",LE_getCurrentToken())) { tmpVertices[num_vertices][0] = LE_readReal(); tmpVertices[num_vertices][1] = LE_readReal(); tmpVertices[num_vertices][2] = LE_readReal(); num_vertices++; } else if (!strcmp("vt",LE_getCurrentToken())) { tmpTextureCoor[num_textuCoor][0] = LE_readReal(); tmpTextureCoor[num_textuCoor][1] = 1- LE_readReal(); num_textuCoor++; } else if (!strcmp("vn",LE_getCurrentToken())) { for(i=0;i<3;i++) LE_readReal(); //Drop it (like it's hot) } else if (!strcmp("f",LE_getCurrentToken())) { LE_SetWhiteCharValue('/', 1); for(i=0;i<9;i++) { idata[i] = LE_readReal(); idata[i]--; } #ifdef OBJ_FACE_CCW for(i=0;i<3;i++)//OBJ Standard: Couter-clockwise #else for(i=2;i>=0;i--) //Bugged expoter. #endif { indiceOfCurrentVertex = GetCacheVertex(idata[3*i],tmpTextureCoor[idata[3*i+1]]); //indiceOfCurrentVertex = obj->num_indices; if (indiceOfCurrentVertex+1 > obj->num_vertices) obj->num_vertices = indiceOfCurrentVertex+1; vectorCopy (tmpVertices[idata[3*i]] , obj->vertices[indiceOfCurrentVertex].position); //for(i=0;i<3;i++) //{ // printf("Vertices: %d, %d, %d.\n",obj->vertices[indiceOfCurrentVertex].position[0],obj->vertices[indiceOfCurrentVertex].position[1],obj->vertices[indiceOfCurrentVertex].position[2]); //} vector2Copy(tmpTextureCoor[idata[3*i+1]], obj->vertices[indiceOfCurrentVertex].textCoo); //printf("num_indices = %d, indice pointer=%d\n",obj->num_indices,indiceOfCurrentVertex); obj->indices[obj->num_indices++] = indiceOfCurrentVertex ; } LE_SetWhiteCharValue('/', 0); } else if (!strcmp("usemtl",LE_getCurrentToken())) { LE_readToken(); entity->material = MATLIB_Get(LE_getCurrentToken()); if (entity->material == 0) printf("************[OBJ Loader] Could not find material: '%s'\n",LE_getCurrentToken()); } } LE_popLexer(); FS_CloseFile(objFile); //Adjust size of vertices and indices obj->vertices = realloc(obj->vertices,obj->num_vertices * sizeof(obj_vertex_t)); obj->indices = realloc(obj->indices ,obj->num_indices * sizeof(ushort)); //printf("Obj %s has %d vertices.\n",filename,obj->num_vertices); //printf("Obj %s has %d indices.\n",filename,obj->num_indices); OBJ_DestroyVerticeCache(); //Generate lighting infos. OBJ_GenerateLightingInfo(obj); return 1; }
void dEngine_ReadConfig(void) { filehandle_t* config; int currentSceneId=0; config = FS_OpenFile(CONFIG_PATH, "rt"); FS_UploadToRAM(config); if (!config) { Log_Printf("Configuration file: data/config.cfg not found"); exit(0); } //renderer.resolution = 1; LE_pushLexer(); LE_init(config); while (LE_hasMoreData()) { LE_readToken(); if (!strcmp("scenes", LE_getCurrentToken())) { LE_readToken(); // { LE_readToken(); // numScenes while (LE_hasMoreData() && strcmp("}", LE_getCurrentToken())) { if (!strcmp("numScenes", LE_getCurrentToken())) { engine.numScenes = LE_readReal(); } else if (!strcmp("scene", LE_getCurrentToken())) { currentSceneId = LE_readReal(); LE_readToken(); //The name of the scene, here only to help developer to keep track of config.cfg strReplace(LE_getCurrentToken(), '_', ' '); strcpy(engine.scenes[currentSceneId].name, LE_getCurrentToken()); LE_readToken(); strcpy(engine.scenes[currentSceneId].path, LE_getCurrentToken()); Log_Printf("Read scene %d, name %s, path %s\n",currentSceneId,engine.scenes[currentSceneId].name,engine.scenes[currentSceneId].path); } LE_readToken(); } } else if (!strcmp("fx", LE_getCurrentToken())) { LE_readToken(); //{ while (LE_hasMoreData() && strcmp("}", LE_getCurrentToken())) { LE_readToken(); if (!strcmp("impactTextureName", LE_getCurrentToken())) { LE_readToken(); // explosionTexture.path = calloc(strlen(LE_getCurrentToken())+1, sizeof(char)); strcpy(explosionTexture.path, LE_getCurrentToken()); } else if (!strcmp("smokeTextureName", LE_getCurrentToken())) { LE_readToken(); // smokeTexture.path = calloc(strlen(LE_getCurrentToken())+1, sizeof(char)); strcpy(smokeTexture.path, LE_getCurrentToken()); } else if (!strcmp("ghostTextureName", LE_getCurrentToken())) { LE_readToken(); // ghostTexture.path = calloc(strlen(LE_getCurrentToken())+1, sizeof(char)); strcpy(ghostTexture.path, LE_getCurrentToken()); } } } /* else if (!strcmp("video", LE_getCurrentToken())) { LE_readToken(); // { LE_readToken(); while (strcmp("}", LE_getCurrentToken())) { if (!strcmp("record", LE_getCurrentToken())) { engine.recordVideo = LE_readReal(); } LE_readToken(); } } */ else if (!strcmp("players", LE_getCurrentToken())) { LE_readToken(); //{ LE_readToken(); while (LE_hasMoreData() && strcmp("}", LE_getCurrentToken())) { if (!strcmp("model1", LE_getCurrentToken())) { LE_readToken(); strcpy(players[1].modelPath, LE_getCurrentToken()); } else if (!strcmp("model0", LE_getCurrentToken())) { LE_readToken(); strcpy(players[0].modelPath, LE_getCurrentToken()); } else if (!strcmp("bulletTextureName", LE_getCurrentToken())) { LE_readToken(); // bulletConfig.bulletTexture.path = calloc(strlen(LE_getCurrentToken())+1, sizeof(char)); strcpy(bulletConfig.bulletTexture.path, LE_getCurrentToken()); } else if (!strcmp("ttl", LE_getCurrentToken())) { bulletConfig.ttl = LE_readReal(); } else if (!strcmp("heightRatio", LE_getCurrentToken())) { bulletConfig.heightRatio = LE_readReal(); } else if (!strcmp("widthRatio", LE_getCurrentToken())) { bulletConfig.widthRatio = LE_readReal(); } else if (!strcmp("screenSpaceXDeltaRatio", LE_getCurrentToken())) { bulletConfig.screenSpaceXDeltaRatio = LE_readReal(); } else if (!strcmp("screenSpaceYDeltaRatio", LE_getCurrentToken())) { bulletConfig.screenSpaceYDeltaRatio = LE_readReal(); } else if (!strcmp("flashHeightRatio", LE_getCurrentToken())) { bulletConfig.flashHeightRatio = LE_readReal(); } else if (!strcmp("flashWidthRatio", LE_getCurrentToken())) { bulletConfig.flashWidthRatio = LE_readReal(); } else if (!strcmp("flashScreenSpaceXDeltaRatio", LE_getCurrentToken())) { bulletConfig.flashScreenSpaceXDeltaRatio = LE_readReal(); } else if (!strcmp("flashScreenSpaceYDeltaRatio", LE_getCurrentToken())) { bulletConfig.flashScreenSpaceYDeltaRatio = LE_readReal(); } LE_readToken(); } } } LE_popLexer(); FS_CloseFile(config); }
char MD5_LoadMesh(md5_mesh_t* mesh, const char* filename) { filehandle_t* fhandle = 0; vertex_t* currentVertex = 0; ushort verticesCounter=0; int versionNumber = 0; int i; fhandle = FS_OpenFile(filename, "rt"); if (!fhandle) { return 0; } LE_pushLexer(); LE_init(fhandle); mesh->memLocation = MD5_MEMLOC_RAM; //printf("[MD5_LoadEntity] Loading MD5 '%s' .\n",filename); while (LE_hasMoreData()) { LE_readToken(); if(!strcmp("MD5Version", LE_getCurrentToken())) { versionNumber = LE_readReal(); if (versionNumber != 10) { printf ("[MD5_Loader ERROR] : %s has a bad model version (%d)\n",filename,versionNumber); return 0; } } else if (!strcmp("numJoints", LE_getCurrentToken())) { mesh->numBones = LE_readReal(); mesh->bones = (md5_bone_t*)calloc(mesh->numBones,sizeof(md5_bone_t)); //printf("[MD5_LoadEntity] Found numJoints: %d.\n",mesh->numBones); } else if (!strcmp("mesh", LE_getCurrentToken())) { //printf("[MD5_LoadEntity] Found mesh.\n"); MD5_ReadMesh(mesh); } else if (!strcmp("numMeshes", LE_getCurrentToken())) { //printf("[MD5_LoadEntity] Found numMeshes.\n"); if(LE_readReal() > 1) { printf("[MD5_Loader ERROR] %s has more than one mesh: Not supported.\n",filename); return 0; } } else if (!strcmp("joints", LE_getCurrentToken())) { //printf("[MD5_LoadEntity] Found joints.\n"); MD5_ReadJoints(mesh->bones,mesh->numBones); } } mesh->vertexArray = (vertex_t*)calloc(mesh->numVertices, sizeof(vertex_t)); mesh->indices = (ushort*)calloc(mesh->numTriangles * 3,sizeof(ushort)); //Write indices mesh->numIndices=0 ; for (i = 0 ; i < mesh->numTriangles; i++) { mesh->indices[mesh->numIndices++] = mesh->triangles[i].index[0]; mesh->indices[mesh->numIndices++] = mesh->triangles[i].index[1]; mesh->indices[mesh->numIndices++] = mesh->triangles[i].index[2]; } //Write textureCoo //Set all textures coordinate once for all, this will never change currentVertex = mesh->vertexArray; for(verticesCounter=0 ; verticesCounter < mesh->numVertices ; verticesCounter++,currentVertex++) { currentVertex->text[0] = mesh->vertices[verticesCounter].st[0]; currentVertex->text[1] = mesh->vertices[verticesCounter].st[1]; } //printf("[MD5 Loader] %d vertices.\n",mesh->numVertices); //printf("[MD5 Loader] %d triangles.\n",mesh->numTriangles); //printf("[MD5 Loader] %d indices.\n",mesh->numIndices); MD5_GenerateSkin(mesh,mesh->bones); MD5_GenerateLightingInfo(mesh); MD5_GenerateSkin(mesh,mesh->bones); //Bounding box MD5_GenerateModelSpaceBBox(mesh); if (TRACE_BLOCK) { if (!strcmp(filename,"data/models/misc/FRA200L.obj.md5mesh")) { currentVertex = mesh->vertexArray; printf("Listing Vertices.\n"); for (i=0; i < mesh->numVertices ; i++,currentVertex++) { printf("vertex: %d/%hu (norm: %hd , %hd , %hd ) (text: %hd , %hd ) (pos: %f , %f , %f )\n", i, mesh->numVertices-1, currentVertex->normal[0], currentVertex->normal[1], currentVertex->normal[2], currentVertex->text[0], currentVertex->text[1], currentVertex->pos[0], currentVertex->pos[1], currentVertex->pos[2] ); } printf("Listing indices.\n"); for(i=0; i < mesh->numIndices ; i++) { printf("indice: %d/%hu, vertex: %hu (norm: %hd , %hd , %hd ) (text: %hd , %hd ) (pos: %f , %f , %f )\n", i, mesh->numIndices-1, mesh->indices[i], mesh->vertexArray[mesh->indices[i]].normal[0], mesh->vertexArray[mesh->indices[i]].normal[1], mesh->vertexArray[mesh->indices[i]].normal[2], mesh->vertexArray[mesh->indices[i]].text[0], mesh->vertexArray[mesh->indices[i]].text[1], mesh->vertexArray[mesh->indices[i]].pos[0], mesh->vertexArray[mesh->indices[i]].pos[1], mesh->vertexArray[mesh->indices[i]].pos[2] ); } } } FS_CloseFile(fhandle); LE_popLexer(); //Cache mesh to speed up future loading. return 1; }