00001
00008 #include "scene.h"
00009 #include "../extern/lib3ds/lib3ds.h"
00010
00011
00017 void TScene::LoadScene(const char* file, bool load_materials, bool load_lights)
00018 {
00019 int i;
00020
00021 cout<<"\nLoading scene "<<file<<":";
00022 Lib3dsFile *scene;
00023 scene = lib3ds_file_open(file);
00024 if(!scene)
00025 {
00026 ShowMessage("Cannot open 3DS file with scene!\n",false);
00027 throw ERR;
00028 }
00029
00030
00031 UpdateLoadList(scene->nmeshes + scene->nmaterials);
00032
00033
00034 if(scene->cameras != NULL)
00035 {
00036 Lib3dsCamera *cam = scene->cameras[0];
00037 MoveCamera(-cam->position[0], -cam->position[2], cam->position[1]);
00038
00039 }
00040
00041
00042 if(load_lights)
00043 {
00044 cout<<scene->nlights<<" lights, ";
00045 for(i = 0; i < scene->nlights; i++)
00046 {
00047 Lib3dsLight *l = scene->lights[i];
00048 TVector l_color = TVector(l->color[0], l->color[1], l->color[2]);
00049 AddLight(i, black, l_color, l_color, TVector(l->position[0], l->position[2], l->position[1]));
00050 }
00051 }
00052
00053
00054 vector<string> mats;
00055 if(load_materials)
00056 {
00057 cout<<scene->nmaterials<<" materials. Loading textures:\n";
00058 for(i = 0; i < scene->nmaterials; i++)
00059 {
00060
00061 Lib3dsMaterial *m = scene->materials[i];
00062
00063
00064 string m_name = m->name;
00065 for(unsigned i=0; i<m_name.length(); i++)
00066 {
00067 if(m_name[i] < 0 || (!isalpha(m_name[i]) && !isdigit(m_name[i])) || m_name[i] > 128)
00068 m_name.replace(i,1,"");
00069 }
00070 cout<<"Adding material "<<m_name<<endl;
00071
00072 AddMaterial(
00073 m_name.c_str(),
00074 grey,
00075 TVector(m->diffuse[0], m->diffuse[1], m->diffuse[2]),
00076 TVector(m->specular[0], m->specular[1], m->specular[2]),
00077 256.0f - 256.0f*m->shininess
00078 );
00079 mats.push_back(m_name);
00080
00081
00082 string path;
00083
00084 if(strlen(m->texture1_map.name) > 0)
00085 {
00086 path = "data/tex/";
00087 path += m->texture1_map.name;
00088 path.replace(path.find_last_of('.') + 1,3,"tga");
00089 AddTexture(m_name.c_str(), path.c_str(), BASE, MODULATE, m->texture1_map.percent, m->texture1_map.scale[0], m->texture1_map.scale[1]);
00090 }
00091
00092 if(strlen(m->texture2_map.name) > 0)
00093 {
00094 path = "data/tex/";
00095 path += m->texture2_map.name;
00096 path.replace(path.find_last_of('.') + 1,3,"tga");
00097 AddTexture(m_name.c_str(), path.c_str(), BASE, MODULATE, m->texture2_map.percent, m->texture2_map.scale[0], m->texture2_map.scale[1]);
00098 }
00099
00100 if(strlen(m->bump_map.name) > 0)
00101 {
00102 path = "data/tex/normal/";
00103 path += m->bump_map.name;
00104 path.replace(path.find_last_of('.') + 1,3,"tga");
00105 AddTexture(m_name.c_str(), path.c_str(), BUMP, MODULATE, 1.0, m->bump_map.scale[0], m->bump_map.scale[1]);
00106 }
00107
00108 if(strlen(m->reflection_map.name) > 0)
00109 {
00110 path = "data/tex/";
00111 path += m->reflection_map.name;
00112 path.replace(path.find_last_of('.') + 1,3,"tga");
00113 AddTexture(m_name.c_str(), path.c_str(), ENV, ADD, m->reflection_map.percent, m->reflection_map.scale[0], m->reflection_map.scale[1]);
00114 }
00115 }
00116 }
00117
00118 cout<<"Adding "<<scene->nmeshes<<" objects, ";
00119
00120 int polygons = 0;
00121 for(i = 0; i < scene->nmeshes; i++)
00122 {
00123
00124 if(scene->meshes[i]->nfaces > 0)
00125 {
00126 polygons += scene->meshes[i]->nfaces;
00127 Lib3dsMesh *m = scene->meshes[i];
00128
00129 obj_cache[m->name] = objects[m->name].Create(scene->meshes[i]);
00130
00131 if(load_materials)
00132 {
00133 if(m->faces[0].material >= 0)
00134 SetMaterial(m->name, mats[m->faces[0].material].c_str());
00135 }
00136 }
00137 LoadScreen();
00138 }
00139 cout<<polygons<<" polygons.\nScene loaded.\n\n";
00140 }