int main(int argc, char **argv) { Lib3dsFile *f=0; FILE *o; parse_args(argc, argv); f=lib3ds_file_load(filename); if (!f) { fprintf(stderr, "***ERROR***\nLoading file %s failed\n", filename); exit(1); } if (output) { o=fopen(output, "w+"); if (!o) { fprintf(stderr, "***ERROR***\nCan't open %s for writing\n", output); exit(1); } } else { o=stdout; } fprintf(o, "Option \"searchpath\" \"shader\" [\".:../shaders:&\"]\n"); if (flags&LIB3DS2RIB_ALL) { int i; for (i=f->segment_from; i<=f->segment_to; ++i) { lib3ds_file_eval(f,1.0f*i); create_rib(f,o,i); } } else if (downcuts) { int i; int delta=f->segment_to-f->segment_from; for (i=0; i<downcuts; ++i) { float frame=f->segment_from+1.0f*i*delta/(downcuts-1); lib3ds_file_eval(f, frame); create_rib(f,o,i); } } else { lib3ds_file_eval(f,frame); create_rib(f,o, (int)frame); } if (o!=stdout) { fclose(o); } lib3ds_file_free(f); return(0); }
// load the model, and if the texture has textures, then apply them on the geometric primitives void Model::loadFile(const char *name) { filename = name; // load file file = lib3ds_file_load(filename); if(!file) // if we were not able to load the file { // give some errors std::string event = "Error loading: "; std::string online = "On line 61 in file "; online.append(__FILE__); event.append(filename); std::cout << event << std::endl; std::cout << online << std::endl; std::cerr << "3DS Error" << std::endl; exit(1); } lib3ds_file_eval(file, 0); // set current frame to 0 // apply texture to all meshes that have texels Lib3dsMesh *mesh; for(mesh = file->meshes;mesh != 0;mesh = mesh->next) { if(mesh->texels) //if there's texels for the mesh ApplyTexture(mesh); //then apply texture to it } if(file->lights) //if we have lights in our model CreateLightList(); }
// this function actually renders the model at place (x, y, z) and then rotated around the y axis by 'angle' degrees void Model::RenderModel() { //glEnable(GL_CULL_FACE); //glCullFace(GL_BACK); glShadeModel(GL_SMOOTH); if(file->lights) //if we have lights in the model { EnableLights(); //enable all lights glCallList(lightListIndex); //set lights. } ///cout << "B"; Lib3dsNode *nodes; for(nodes = file->nodes;nodes != 0;nodes = nodes->next)// Render all nodes { renderNode(nodes); //cout << "R"; } if(file->lights) DisableLights(); // disable lighting, we don't want it have it enabled longer than necessary curFrame++; if(curFrame > file->frames) //if the next frame doesn't exist, go to frame 0 curFrame = 0; lib3ds_file_eval(file, curFrame); // set current frame //glDisable(GL_CULL_FACE); glShadeModel(GL_FLAT); }
Scene *load_scene(const char *fname) { Lib3dsFile *file; if(!(file = lib3ds_file_load(fname))) { error("%s: could not load %s", __func__, fname); return 0; } lib3ds_file_eval(file, 0); Scene *scene = new Scene; load_objects(file, scene); load_lights(file, scene); load_cameras(file, scene); construct_hierarchy(file, scene); /* std::list<Object*> *obj_list = scene->get_objects_list(); std::list<Object*>::iterator iter = obj_list->begin(); while(iter != obj_list->end()) { fix_hierarchy(*iter++); } */ lib3ds_file_free(file); return scene; }
x3ds_definition::x3ds_definition(player* player, const char* url) : character_def(player), m_file(NULL) { m_file = lib3ds_file_load(url); if (m_file == NULL) { static int n = 0; if (n < 1) { n++; log_error("can't load '%s'\n", url); } return; } // load textures for (Lib3dsMaterial* p = m_file->materials; p != 0; p = p->next) { load_texture(p->texture1_map.name); load_texture(p->bump_map.name); } ensure_camera(); ensure_lights(); ensure_nodes(); lib3ds_file_eval(m_file, 0.); }
QGLAbstractScene *QGL3dsSceneHandler::read() { Lib3dsFile *file = qgl3ds_lib3ds_file_load(device()); Q_CHECK_PTR(file); // wtf? if (!file->nodes) { Lib3dsMesh *mesh; Lib3dsNode *node; for (mesh = file->meshes; mesh; mesh = mesh->next) { if (mesh->faces && mesh->points) { node = lib3ds_node_new_object(); qstrcpy(node->name, mesh->name); node->parent_id = LIB3DS_NO_PARENT; lib3ds_file_insert_node(file, node); } } } lib3ds_file_eval(file, 0.0f); QGL3dsScene *scene = new QGL3dsScene(file, this); return scene; }
void x3ds_instance::advance(float delta_time) { on_event(event_id::ENTER_FRAME); if (m_play_state == PLAY) { m_current_frame = fmod(m_current_frame + 1.0f, (float) m_def->m_file->frames); lib3ds_file_eval(m_def->m_file, m_current_frame); } }
/*! * See \ref Loader3ds::loadFromFile */ dcollide::Proxy* Loader3dsInternal::loadFromFile(dcollide::World* world, const char* fileName, dcollide::ProxyTypes proxyType) { mData->mProxy2Transformation.clear(); mData->mProxy2TextureInformation.clear(); if (!fileName) { // TODO: exception? return 0; } mFile = lib3ds_file_load(fileName); if (!mFile) { std::cout << dc_funcinfo << "unable to load " << fileName << std::endl; return 0; } // AB: some files don't store nodes and just want exactly one node per mesh. // atm we don't support that. if (!mFile->nodes) { std::cout << dc_funcinfo << "File " << fileName << " does not contain any nodes. mesh-only files not supported currently." << std::endl; lib3ds_file_free(mFile); mFile = 0; return 0; } // 3ds stores several frames, we want the first lib3ds_file_eval(mFile, 0); if (!checkUniqueMeshMaterial()) { std::cerr << dc_funcinfo << "the file " << fileName << " contains at least one mesh with different materials. this is not supported by this modelloader, only the first material will be used!" << std::endl; } dcollide::Proxy* topObject = world->createProxy(proxyType); mData->mProxy2Transformation.insert(std::make_pair(topObject, Loader3ds::TransformationNode())); Lib3dsMatrix identityMatrix; lib3ds_matrix_identity(identityMatrix); Lib3dsNode* node = mFile->nodes; while (node) { // node->type can be object, light, camera, ... // -> only object nodes are relevant to us if (node->type == LIB3DS_OBJECT_NODE) { if (!loadNode(world, topObject, node, &identityMatrix)) { std::cerr << dc_funcinfo << "Loading node from " << fileName << " failed" << std::endl; // TODO: is MyObjectNode::mProxy being deleted? delete topObject; lib3ds_file_free(mFile); mFile = 0; return 0; } } node = node->next; } lib3ds_file_free(mFile); mFile = 0; return topObject; }
Model3DS::Model3DS(const char *name) { file=lib3ds_file_load(name); if (!file) { puts("3dsplayer: Error: Loading 3DS file failed.\n"); exit(1); } lib3ds_file_eval(file,0); }
/*! * */ static void display(void) { Lib3dsNode *c,*t; Lib3dsMatrix M; glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); if (!file) { return; } c=lib3ds_file_node_by_name(file, camera, LIB3DS_CAMERA_NODE); t=lib3ds_file_node_by_name(file, camera, LIB3DS_TARGET_NODE); if (!c || !t) { return; } glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective( c->data.camera.fov, 1.0*gl_width/gl_height, 100.0, 20000.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glRotatef(-90, 1.0,0,0); { GLfloat lightPos[] = {0.0f, -1.0f, 0.0f, 0.0f}; glLightfv(GL_LIGHT0, GL_POSITION, lightPos); glPushMatrix(); glTranslatef(0,-10,0); glutSolidTeapot(10.0); glPopMatrix(); } lib3ds_matrix_camera(M, c->data.camera.pos, t->data.target.pos, c->data.camera.roll); glMultMatrixf(&M[0][0]); { Lib3dsNode *p; for (p=file->nodes; p!=0; p=p->next) { render_node(p); } } if (!halt) { current_frame+=1.0; if (current_frame>file->frames) { current_frame=0; } lib3ds_file_eval(file, current_frame); glutSwapBuffers(); glutPostRedisplay(); } }
/*! * Time function, called every frame */ void GLUTCALL timer_cb(int value) { if (!halt) { view_rotz += anim_rotz; current_frame+= 1; if (current_frame > file->frames) current_frame = 0; lib3ds_file_eval(file, current_frame); glutTimerFunc(1000 / FRAMES_PER_SECOND, timer_cb, 0); } glutPostRedisplay(); }
Object_3DS::Object_3DS(const char* filename):file(NULL) { #ifdef HAS_LIB3DS file=lib3ds_file_load(filename); if(!file) { MSG_ERROR("Can't load 3ds file %s",filename); } else lib3ds_file_eval(file,0); #else MSG_ERROR("Object_3DS can't be used. Please ensure lib3ds is linked correctly!"); #endif }
/*! * Time function, called every frame */ static void timer_cb(int value) { glutPostRedisplay(); if (!halt) { view_rotz += anim_rotz; current_frame+=1.0; if (current_frame>file->frames) { current_frame=0; } lib3ds_file_eval(file, current_frame); glutTimerFunc(10, timer_cb, 0); } }
bool m3ds::load(std::string& filename) { m_filename = filename; m_file = lib3ds_file_load(filename.c_str()); if (!m_file) return false; lib3ds_file_eval(m_file, 0.0f); std::map<std::string, texture*>::iterator ite; ite = texlib::instance()->find("default.tga"); if (ite == texlib::instance()->end()) { tga_tex* tex = new tga_tex("models\\3ds\\default.tga"); texlib::instance()->insert( texlib::value_type(std::string("default.tga"), tex)); } return true; }
int main(int argc, char **argv) { Lib3dsFile *f; parse_args(argc, argv); f = lib3ds_file_open(input); if (!f) { fprintf(stderr, "***ERROR***\nLoading file failed: %s\n", input); exit(1); } if (mtl_file) { FILE *mtl = fopen(mtl_file, "wt"); if (!mtl) { fprintf(stderr, "***ERROR***\nCreating output file failed: %s\n", mtl_file); exit(1); } write_mtl(mtl, f); fclose(mtl); } { FILE *obj = fopen(obj_file, "wt"); if (!obj) { fprintf(stderr, "***ERROR***\nCreating output file failed: %s\n", obj_file); exit(1); } if (!f->nodes) lib3ds_file_create_nodes_for_meshes(f); lib3ds_file_eval(f, 0); fprintf(obj, "# Wavefront OBJ file\n"); fprintf(obj, "# Converted by 3ds2obj\n"); fprintf(obj, "# http://www.lib3ds.org\n\n"); if (mtl_file) { fprintf(obj, "mtllib %s\n", mtl_file); } write_nodes(obj, f, f->nodes); fclose(obj); } lib3ds_file_free(f); return 0; }
/*! * */ static void init(void) { glClearColor(0.5, 0.5, 0.5, 1.0); glShadeModel(GL_SMOOTH); glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glDepthFunc(GL_LEQUAL); glEnable(GL_DEPTH_TEST); glEnable(GL_CULL_FACE); glCullFace(GL_BACK); //glDisable(GL_NORMALIZE); //glPolygonOffset(1.0, 2); file=lib3ds_file_load(filename); if (!file) { puts("***ERROR*** Loading 3DS file failed."); exit(1); } if (!file->cameras) { puts("***ERROR*** No Camera found."); lib3ds_file_free(file); file=0; exit(1); } if (!camera) { camera=file->cameras->name; } camera_menu_id=glutCreateMenu(camera_menu); { Lib3dsCamera *c; int i; for (c=file->cameras,i=0; c; c=c->next,++i) { glutAddMenuEntry(c->name, i); } } glutAttachMenu(0); lib3ds_file_eval(file,0); }
/*! * */ void GLUTCALL keyboard(unsigned char key, int x, int y) { switch (key) { case 27: exit(0); break; case 'h': set_halt(!halt); break; case 'a': anim_rotz += .05; break; case 'A': anim_rotz -= .05; break; case 'r': view_rotx = view_roty = view_rotz = anim_rotz = 0.; break; case 'z': view_roty += 5.; break; case 'Z': view_roty -= 5.; break; case 'b': show_bounds = !show_bounds; break; case 'o': show_object = !show_object; break; case '\001': anti_alias = !anti_alias; break; } lib3ds_file_eval(file, current_frame); glutPostRedisplay(); }
void Object_3DS::init() { #ifdef HAS_LIB3DS if(!file) return ; lib3ds_file_eval(file,0); // Lights GLfloat amb[] = {0.0, 0.0, 0.0, 1.0}; GLfloat dif[] = {1.0, 1.0, 1.0, 1.0}; GLfloat spe[] = {1.0, 1.0, 1.0, 1.0}; GLfloat pos[] = {0.0, 0.0, 0.0, 1.0}; int li=GL_LIGHT0; for (Lib3dsLight* l=file->lights; l; l=l->next) { glEnable(li); glLightfv(li, GL_AMBIENT, amb); glLightfv(li, GL_DIFFUSE, dif); glLightfv(li, GL_SPECULAR, spe); pos[0] = l->position[0]; pos[1] = l->position[1]; pos[2] = l->position[2]; glLightfv(li, GL_POSITION, pos); if (!l->spot_light) continue; pos[0] = l->spot[0] - l->position[0]; pos[1] = l->spot[1] - l->position[1]; pos[2] = l->spot[2] - l->position[2]; glLightfv(li, GL_SPOT_DIRECTION, pos); ++li; } #endif }
std::vector<CL_Lib3dsMesh> CL_Lib3dsFile::export_meshes(Lib3dsNode *node) { if (node == 0) { lib3ds_file_eval(file, 0.0f); node = file->nodes; } std::vector<CL_Lib3dsMesh> meshes; for (; node; node = node->next) { if (node->type == LIB3DS_NODE_MESH_INSTANCE) { Lib3dsMeshInstanceNode *instance_node = (Lib3dsMeshInstanceNode *) node; Lib3dsMesh *mesh = lib3ds_file_mesh_for_node(file, (Lib3dsNode*)node); if (mesh && mesh->vertices) { float inv_matrix[4][4], M[4][4]; lib3ds_matrix_copy(M, instance_node->base.matrix); lib3ds_matrix_translate(M, -instance_node->pivot[0], -instance_node->pivot[1], -instance_node->pivot[2]); lib3ds_matrix_copy(inv_matrix, mesh->matrix); lib3ds_matrix_inv(inv_matrix); lib3ds_matrix_mult(M, M, inv_matrix); std::vector<CL_Vec3f> positions; for (int i = 0; i < mesh->nvertices; ++i) { float position[3]; lib3ds_vector_transform(position, M, mesh->vertices[i]); positions.push_back(CL_Vec3f(position[0], position[1], position[2])); } std::vector<CL_Vec2f> texcoords; if (mesh->texcos) { for (int i = 0; i < mesh->nvertices; ++i) { float tx = mesh->texcos[i][0]; float ty = mesh->texcos[i][1]; texcoords.push_back(CL_Vec2f(tx, ty)); } } std::vector<CL_Vec3f> normals; if (mesh->faces && mesh->nfaces > 0) { float (*normals_array)[3] = (float(*)[3])malloc(sizeof(float) * 9 * mesh->nfaces); lib3ds_mesh_calculate_vertex_normals(mesh, normals_array); for (int i = 0; i < 3 * mesh->nfaces; ++i) { normals.push_back(CL_Vec3f(normals_array[i][0], normals_array[i][1], normals_array[i][2])); } free(normals_array); } CL_Lib3dsMesh mesh_object; for (int i = 0; i < mesh->nfaces; ++i) { int material_index = mesh->faces[i].material; mesh_object.face_materials.push_back(material_index); for (int j = 0; j < 3; ++j) { int vertex_index = mesh->faces[i].index[j]; mesh_object.positions.push_back(positions[vertex_index]); if (!texcoords.empty()) mesh_object.texcooords.push_back(texcoords[vertex_index]); if (!normals.empty()) mesh_object.normals.push_back(normals[i*3+j]); } } meshes.push_back(mesh_object); } if (node->childs) { std::vector<CL_Lib3dsMesh> child_meshes = export_meshes(node->childs); meshes.insert(meshes.end(), child_meshes.begin(), child_meshes.end()); } } } return meshes; }
/*! * See \ref Loader3ds::loadFromFileToOneMesh */ dcollide::Mesh* Loader3dsInternal::loadFromFileToOneMesh(const char* fileName, TextureInformation* textureInformation) { // AB: // we cannot load texture information here, because we are merging all // meshes into a single one. // -> adjusting the texels may still be possible, however if the model // uses > 1 texture, we can't change that. mData->mProxy2Transformation.clear(); mData->mProxy2TextureInformation.clear(); if (!fileName) { throw dcollide::NullPointerException("fileName"); } mFile = lib3ds_file_load(fileName); if (!mFile) { std::cout << dc_funcinfo << "unable to load " << fileName << std::endl; return 0; } // AB: some files don't store nodes and just want exactly one node per mesh. // atm we don't support that. if (!mFile->nodes) { std::cout << dc_funcinfo << "File " << fileName << " does not contain any nodes. mesh-only files not supported currently." << std::endl; lib3ds_file_free(mFile); mFile = 0; return 0; } // 3ds stores several frames, we want the first lib3ds_file_eval(mFile, 0); std::list<Lib3dsNode*> nodes; Lib3dsNode* node = mFile->nodes; while (node) { nodes.push_back(node); node = node->next; } bool texelLoadError = false; std::vector<dcollide::Vector3> texels; std::vector<dcollide::Vertex*> vertices; std::vector<dcollide::Triangle*> triangles; std::vector<int> indices; while (!nodes.empty()) { Lib3dsNode* node = nodes.front(); nodes.pop_front(); for (Lib3dsNode* n = node->childs; n; n = n->next) { nodes.push_back(n); } // node->type can be object, light, camera, ... // -> only object nodes are relevant to us if (node->type != LIB3DS_OBJECT_NODE) { continue; } if (strcmp(node->name, "$$$DUMMY") == 0) { // AB: nodes with this name are only transformation nodes, i.e. they // don't have a mesh, only a matrix. continue; } unsigned int pointOffset = vertices.size(); Lib3dsMesh* mesh = lib3ds_file_mesh_by_name(mFile, node->name); Lib3dsMatrix origMeshMatrix; lib3ds_matrix_copy(origMeshMatrix, mesh->matrix); lib3ds_matrix_inv(origMeshMatrix); // 3ds stores inverted mesh matrix dcollide::Matrix meshMatrix(&origMeshMatrix[0][0]); dcollide::Matrix nodeMatrix(&node->matrix[0][0]); Lib3dsObjectData* data = &node->data.object; nodeMatrix.translate(dcollide::Vector3(-data->pivot[0], -data->pivot[1], -data->pivot[2])); dcollide::Matrix matrix = nodeMatrix; matrix.multiply(&meshMatrix); for (unsigned int i = 0; i < mesh->points; i++) { Lib3dsPoint* p = &mesh->pointL[i]; dcollide::Vector3 pos; matrix.transform(&pos, dcollide::Vector3( p->pos[0], p->pos[1], p->pos[2] )); vertices.push_back(new dcollide::Vertex(pos)); } for (unsigned int i = 0; i < mesh->faces; i++) { Lib3dsFace* f = &mesh->faceL[i]; indices.push_back(f->points[0] + pointOffset); indices.push_back(f->points[1] + pointOffset); indices.push_back(f->points[2] + pointOffset); } if (textureInformation) { TextureInformation t = loadTextureInformation(node); const std::vector<dcollide::Vector3>& nodeTexels = t.getTexels(); if (mesh->texelL && nodeTexels.size() != mesh->texels) { dcollide::warning() << "texturing problem in " << fileName; dcollide::warning() << "invalid texel count loaded: have=" << nodeTexels.size() << " expected=" << mesh->texels; dcollide::warning() << "adding dummy texels..."; for (unsigned int i = 0; i < mesh->texels; i++) { texels.push_back(dcollide::Vector3(0.0, 0.0, 0.0)); } // texelLoadError = true; } else { for (unsigned int i = 0; i < nodeTexels.size(); i++) { texels.push_back(nodeTexels[i]); } } } } if (textureInformation) { if (!texelLoadError) { textureInformation->setTextured(true); textureInformation->setTextureFileName("deformable.tga"); textureInformation->setTexels(texels); } else { textureInformation->setTextured(false); } } lib3ds_file_free(mFile); mFile = 0; dcollide::Mesh* mesh = new dcollide::Mesh(vertices, indices); return mesh; }
// Create an GLC_World from an input 3DS File GLC_World* GLC_3dsToWorld::CreateWorldFrom3ds(QFile &file) { clear(); m_FileName= file.fileName(); ////////////////////////////////////////////////////////////////// // Test if the file exist and can be opened ////////////////////////////////////////////////////////////////// if (!file.open(QIODevice::ReadOnly)) { QString message(QString("GLC_3dsToWorld::CreateWorldFrom3ds File ") + m_FileName + QString(" doesn't exist")); GLC_FileFormatException fileFormatException(message, m_FileName, GLC_FileFormatException::FileNotFound); throw(fileFormatException); } // Close the file before open it with lib3ds file.close(); ////////////////////////////////////////////////////////////////// // Init member ////////////////////////////////////////////////////////////////// m_pWorld= new GLC_World; //Load 3ds File m_pLib3dsFile=lib3ds_file_load(m_FileName.toLocal8Bit().data()); if (!m_pLib3dsFile) { QString message= "GLC_3dsToWorld::CreateWorldFrom3ds : Loading Failed"; GLC_FileFormatException fileFormatException(message, m_FileName, GLC_FileFormatException::FileNotSupported); clear(); throw(fileFormatException); } // Evaluate Nodes Matrix for the first frame (Needed by instances) lib3ds_file_eval(m_pLib3dsFile, 0.0); m_CurrentQuantumValue= m_InitQuantumValue; m_PreviousQuantumValue= m_CurrentQuantumValue; emit currentQuantum(m_CurrentQuantumValue); // Count the number of meshes for(Lib3dsMesh *pMesh= m_pLib3dsFile->meshes; pMesh != NULL; pMesh = pMesh->next) { ++m_NumberOfMeshes; } // Check if there is some meshes in the 3ds file if (0 == m_NumberOfMeshes) { QString message= "GLC_3dsToWorld::CreateWorldFrom3ds : No mesh found !"; GLC_FileFormatException fileFormatException(message, m_FileName, GLC_FileFormatException::NoMeshFound); clear(); throw(fileFormatException); } // Create GLC_3DViewInstance with Node for (Lib3dsNode *pNode=m_pLib3dsFile->nodes; pNode!=0; pNode=pNode->next) { createMeshes(m_pWorld->rootOccurence(), pNode); } // Load unloaded mesh name for(Lib3dsMesh *pMesh= m_pLib3dsFile->meshes; pMesh != NULL; pMesh = pMesh->next) { if (!m_LoadedMeshes.contains(QString(pMesh->name))) { //qDebug() << "Mesh without parent found" << QString(pMesh->name); Lib3dsNode *pNode= lib3ds_node_new_object(); strcpy(pNode->name, pMesh->name); pNode->parent_id= LIB3DS_NO_PARENT; lib3ds_file_insert_node(m_pLib3dsFile, pNode); createMeshes(m_pWorld->rootOccurence(), pNode); } } // Free Lib3dsFile and all its ressources lib3ds_file_free(m_pLib3dsFile); m_pLib3dsFile= NULL; emit currentQuantum(100); // Create the world bounding box m_pWorld->collection()->boundingBox(); return m_pWorld; }
// load the model, and if the texture has textures, then apply them on the geometric primitives void Model::LoadFile(const char *name) { if(glIsEnabled(GL_LIGHTING)) lightEnabled = true; else lightEnabled = false; filename = name; // load file file = lib3ds_file_load(filename); if(!file) // if we were not able to load the file { // give some errors string event = "Error loading: "; event.append(filename); cout << event << endl; exit(1); } //Node: /* No nodes? Fabricate nodes to display all the meshes. */ if( !file->nodes ) { cout << "No node found"<< endl; Lib3dsMesh *mesh; Lib3dsNode *node; for(mesh = file->meshes; mesh != NULL; mesh = mesh->next) { node = lib3ds_node_new_object(); strcpy(node->name, mesh->name); node->parent_id = LIB3DS_NO_PARENT; lib3ds_file_insert_node(file, node); } //goto Node; } static Lib3dsVector bmin, bmax; static float sx, sy, sz, size; /* bounding box dimensions */ static float cx, cy, cz; /* bounding box center */ lib3ds_file_bounding_box_of_nodes(file, LIB3DS_TRUE, LIB3DS_FALSE, LIB3DS_FALSE, bmin, bmax); sx = bmax[0] - bmin[0]; sy = bmax[1] - bmin[1]; sz = bmax[2] - bmin[2]; size = MAX(sx, sy); size = MAX(size, sz); cx = (bmin[0] + bmax[0])/2; cy = (bmin[1] + bmax[1])/2; cz = (bmin[2] + bmax[2])/2; lib3ds_file_eval(file, 0); // set current frame to 0 // apply texture to all meshes that have texels if (file->lights == NULL) { Lib3dsLight *light; light = lib3ds_light_new("light0"); light->spot_light = 0; light->see_cone = 0; light->color[0] = light->color[1] = light->color[2] = .6; light->position[0] = cx + size * .75; light->position[1] = cy - size * 1.; light->position[2] = cz + size * 1.5; light->position[3] = 0.; light->outer_range = 100; light->inner_range = 10; light->multiplier = 1; lib3ds_file_insert_light(file, light); light = lib3ds_light_new("light1"); light->spot_light = 0; light->see_cone = 0; light->color[0] = light->color[1] = light->color[2] = .3; light->position[0] = cx - size; light->position[1] = cy - size; light->position[2] = cz + size * .75; light->position[3] = 0.; light->outer_range = 100; light->inner_range = 10; light->multiplier = 1; lib3ds_file_insert_light(file, light); light = lib3ds_light_new("light2"); light->spot_light = 0; light->see_cone = 0; light->color[0] = light->color[1] = light->color[2] = .3; light->position[0] = cx; light->position[1] = cy + size; light->position[2] = cz + size; light->position[3] = 0.; light->outer_range = 100; light->inner_range = 10; light->multiplier = 1; lib3ds_file_insert_light(file, light); } Lib3dsMesh *mesh; for(mesh = file->meshes;mesh != 0;mesh = mesh->next) { if(mesh->texels) //if there's texels for the mesh ApplyTexture(mesh); //then apply texture to it } if(file->lights) //if we have lights in our model CreateLightList(); }
/*! * Load the model from .3ds file. */ static void load_model(void) { file=lib3ds_file_load(filepath); if (!file) { puts("3dsplayer: Error: Loading 3DS file failed.\n"); exit(1); } /* No nodes? Fabricate nodes to display all the meshes. */ if( !file->nodes ) { Lib3dsMesh *mesh; Lib3dsNode *node; for(mesh = file->meshes; mesh != NULL; mesh = mesh->next) { node = lib3ds_node_new_object(); strcpy(node->name, mesh->name); node->parent_id = LIB3DS_NO_PARENT; lib3ds_file_insert_node(file, node); } } lib3ds_file_eval(file, 1.0f); lib3ds_file_bounding_box_of_nodes(file, LIB3DS_TRUE, LIB3DS_FALSE, LIB3DS_FALSE, bmin, bmax); sx = bmax[0] - bmin[0]; sy = bmax[1] - bmin[1]; sz = bmax[2] - bmin[2]; size = MAX(sx, sy); size = MAX(size, sz); cx = (bmin[0] + bmax[0])/2; cy = (bmin[1] + bmax[1])/2; cz = (bmin[2] + bmax[2])/2; /* No cameras in the file? Add four */ if( !file->cameras ) { /* Add some cameras that encompass the bounding box */ Lib3dsCamera *camera = lib3ds_camera_new("Camera_X"); camera->target[0] = cx; camera->target[1] = cy; camera->target[2] = cz; memcpy(camera->position, camera->target, sizeof(camera->position)); camera->position[0] = bmax[0] + 1.5 * MAX(sy,sz); camera->near_range = ( camera->position[0] - bmax[0] ) * .5; camera->far_range = ( camera->position[0] - bmin[0] ) * 2; lib3ds_file_insert_camera(file, camera); /* Since lib3ds considers +Y to be into the screen, we'll put * this camera on the -Y axis, looking in the +Y direction. */ camera = lib3ds_camera_new("Camera_Y"); camera->target[0] = cx; camera->target[1] = cy; camera->target[2] = cz; memcpy(camera->position, camera->target, sizeof(camera->position)); camera->position[1] = bmin[1] - 1.5 * MAX(sx,sz); camera->near_range = ( bmin[1] - camera->position[1] ) * .5; camera->far_range = ( bmax[1] - camera->position[1] ) * 2; lib3ds_file_insert_camera(file, camera); camera = lib3ds_camera_new("Camera_Z"); camera->target[0] = cx; camera->target[1] = cy; camera->target[2] = cz; memcpy(camera->position, camera->target, sizeof(camera->position)); camera->position[2] = bmax[2] + 1.5 * MAX(sx,sy); camera->near_range = ( camera->position[2] - bmax[2] ) * .5; camera->far_range = ( camera->position[2] - bmin[2] ) * 2; lib3ds_file_insert_camera(file, camera); camera = lib3ds_camera_new("Camera_ISO"); camera->target[0] = cx; camera->target[1] = cy; camera->target[2] = cz; memcpy(camera->position, camera->target, sizeof(camera->position)); camera->position[0] = bmax[0] + .75 * size; camera->position[1] = bmin[1] - .75 * size; camera->position[2] = bmax[2] + .75 * size; camera->near_range = ( camera->position[0] - bmax[0] ) * .5; camera->far_range = ( camera->position[0] - bmin[0] ) * 3; lib3ds_file_insert_camera(file, camera); } /* No lights in the file? Add some. */ if (file->lights == NULL) { Lib3dsLight *light; light = lib3ds_light_new("light0"); light->spot_light = 0; light->see_cone = 0; light->color[0] = light->color[1] = light->color[2] = .6; light->position[0] = cx + size * .75; light->position[1] = cy - size * 1.; light->position[2] = cz + size * 1.5; light->position[3] = 0.; light->outer_range = 100; light->inner_range = 10; light->multiplier = 1; lib3ds_file_insert_light(file, light); light = lib3ds_light_new("light1"); light->spot_light = 0; light->see_cone = 0; light->color[0] = light->color[1] = light->color[2] = .3; light->position[0] = cx - size; light->position[1] = cy - size; light->position[2] = cz + size * .75; light->position[3] = 0.; light->outer_range = 100; light->inner_range = 10; light->multiplier = 1; lib3ds_file_insert_light(file, light); light = lib3ds_light_new("light2"); light->spot_light = 0; light->see_cone = 0; light->color[0] = light->color[1] = light->color[2] = .3; light->position[0] = cx; light->position[1] = cy + size; light->position[2] = cz + size; light->position[3] = 0.; light->outer_range = 100; light->inner_range = 10; light->multiplier = 1; lib3ds_file_insert_light(file, light); } if (!file->cameras) { fputs("3dsplayer: Error: No Camera found.\n", stderr); lib3ds_file_free(file); file=0; exit(1); } if (!camera) { camera=file->cameras->name; } lib3ds_file_eval(file,0.); }
/*! * Load the model from .3ds file. */ static void load_model(void) { file = lib3ds_file_open(filepath); if (!file) { puts("3dsplayer: Error: Loading 3DS file failed.\n"); exit(1); } /* No nodes? Fabricate nodes to display all the meshes. */ if (!file->nodes) { Lib3dsNode *node; int i; for (i = 0; i < file->nmeshes; ++i) { Lib3dsMesh *mesh = file->meshes[i]; node = lib3ds_node_new(LIB3DS_NODE_MESH_INSTANCE); strcpy(node->name, mesh->name); lib3ds_file_insert_node(file, node, NULL); } } lib3ds_file_eval(file, 0.0f); lib3ds_file_bounding_box_of_nodes(file, 1, 0, 0, bmin, bmax, NULL); sx = bmax[0] - bmin[0]; sy = bmax[1] - bmin[1]; sz = bmax[2] - bmin[2]; size = MAX(sx, sy); size = MAX(size, sz); cx = (bmin[0] + bmax[0]) / 2; cy = (bmin[1] + bmax[1]) / 2; cz = (bmin[2] + bmax[2]) / 2; /* No cameras in the file? Add four */ if (!file->ncameras) { /* Add some cameras that encompass the bounding box */ Lib3dsCamera *camera = lib3ds_camera_new("Camera_X"); camera->target[0] = cx; camera->target[1] = cy; camera->target[2] = cz; memcpy(camera->position, camera->target, sizeof(camera->position)); camera->position[0] = bmax[0] + 1.5 * MAX(sy, sz); camera->near_range = (camera->position[0] - bmax[0]) * .5; camera->far_range = (camera->position[0] - bmin[0]) * 2; lib3ds_file_insert_camera(file, camera, -1); /* Since lib3ds considers +Y to be into the screen, we'll put * this camera on the -Y axis, looking in the +Y direction. */ camera = lib3ds_camera_new("Camera_Y"); camera->target[0] = cx; camera->target[1] = cy; camera->target[2] = cz; memcpy(camera->position, camera->target, sizeof(camera->position)); camera->position[1] = bmin[1] - 1.5 * MAX(sx, sz); camera->near_range = (bmin[1] - camera->position[1]) * .5; camera->far_range = (bmax[1] - camera->position[1]) * 2; lib3ds_file_insert_camera(file, camera, -1); camera = lib3ds_camera_new("Camera_Z"); camera->target[0] = cx; camera->target[1] = cy; camera->target[2] = cz; memcpy(camera->position, camera->target, sizeof(camera->position)); camera->position[2] = bmax[2] + 1.5 * MAX(sx, sy); camera->near_range = (camera->position[2] - bmax[2]) * .5; camera->far_range = (camera->position[2] - bmin[2]) * 2; lib3ds_file_insert_camera(file, camera, -1); camera = lib3ds_camera_new("Camera_ISO"); camera->target[0] = cx; camera->target[1] = cy; camera->target[2] = cz; memcpy(camera->position, camera->target, sizeof(camera->position)); camera->position[0] = bmax[0] + .75 * size; camera->position[1] = bmin[1] - .75 * size; camera->position[2] = bmax[2] + .75 * size; camera->near_range = (camera->position[0] - bmax[0]) * .5; camera->far_range = (camera->position[0] - bmin[0]) * 3; lib3ds_file_insert_camera(file, camera, -1); } /* No lights in the file? Add some. */ if (!file->nlights) { Lib3dsLight *light; light = lib3ds_light_new("light0"); light->spot_light = 0; light->see_cone = 0; light->color[0] = light->color[1] = light->color[2] = .6; light->position[0] = cx + size * .75; light->position[1] = cy - size * 1.; light->position[2] = cz + size * 1.5; light->position[3] = 0.; light->outer_range = 100; light->inner_range = 10; light->multiplier = 1; lib3ds_file_insert_light(file, light, -1); light = lib3ds_light_new("light1"); light->spot_light = 0; light->see_cone = 0; light->color[0] = light->color[1] = light->color[2] = .3; light->position[0] = cx - size; light->position[1] = cy - size; light->position[2] = cz + size * .75; light->position[3] = 0.; light->outer_range = 100; light->inner_range = 10; light->multiplier = 1; lib3ds_file_insert_light(file, light, -1); light = lib3ds_light_new("light2"); light->spot_light = 0; light->see_cone = 0; light->color[0] = light->color[1] = light->color[2] = .3; light->position[0] = cx; light->position[1] = cy + size; light->position[2] = cz + size; light->position[3] = 0.; light->outer_range = 100; light->inner_range = 10; light->multiplier = 1; lib3ds_file_insert_light(file, light, -1); } camera = file->cameras[0]->name; lib3ds_file_eval(file, 0); }