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_definition::ensure_nodes() { // No nodes? Fabricate nodes to display all the meshes. if (m_file->nodes == NULL) { Lib3dsMesh *mesh; Lib3dsNode *node; for (mesh = m_file->meshes; mesh != NULL; mesh = mesh->next) { node = lib3ds_node_new_object(); strcpy(node->name, mesh->name); node->parent_id = LIB3DS_NO_PARENT; node->data.object.scl_track.keyL = lib3ds_lin3_key_new(); node->data.object.scl_track.keyL->value[0] = 1.; node->data.object.scl_track.keyL->value[1] = 1.; node->data.object.scl_track.keyL->value[2] = 1.; lib3ds_file_insert_node(m_file, node); } } }
void GLC_WorldTo3ds::createNodeFromOccurrence(GLC_StructOccurence* pOcc) { Lib3dsNode* p3dsNode = lib3ds_node_new_object(); p3dsNode->node_id= m_CurrentNodeId; m_OccIdToNodeId.insert(pOcc->id(), m_CurrentNodeId++); if (pOcc->parent() == m_World.rootOccurence()) { p3dsNode->parent_id= LIB3DS_NO_PARENT; } else { Q_ASSERT(m_OccIdToNodeId.contains(pOcc->parent()->id())); p3dsNode->parent_id= m_OccIdToNodeId.value(pOcc->parent()->id()); } lib3ds_file_insert_node(m_pLib3dsFile, p3dsNode); GLC_StructReference* pRef= pOcc->structReference(); if (m_UseAbsolutePosition) { if (pOcc->structReference()->hasRepresentation()) { GLC_3DRep* pRep= dynamic_cast<GLC_3DRep*>(pOcc->structReference()->representationHandle()); if (NULL != pRep) { // This reference has a mesh const GLC_Matrix4x4 matrix= pOcc->absoluteMatrix(); const QString meshName= pRef->name() + '_' + QString::number(++m_CurrentMeshIndex); QList<Lib3dsMesh*> meshes= createMeshsFrom3DRep(pRep, meshName, matrix); const int meshCount= meshes.count(); for (int i= 0; i < meshCount; ++i) { lib3ds_file_insert_mesh(m_pLib3dsFile, meshes.at(i)); } if (meshCount > 1) { for (int i= 0; i < meshCount; ++i) { Lib3dsNode* pCurrent3dsNode = lib3ds_node_new_object(); pCurrent3dsNode->node_id= m_CurrentNodeId++; pCurrent3dsNode->parent_id= p3dsNode->node_id; strcpy(pCurrent3dsNode->name, meshes.at(i)->name); lib3ds_file_insert_node(m_pLib3dsFile, pCurrent3dsNode); } } else if (!meshes.isEmpty()) { strcpy(p3dsNode->name, meshes.first()->name); } } } } else { // Node matrix const GLC_Matrix4x4 matrix= pOcc->structInstance()->relativeMatrix(); setNodePosition(p3dsNode, matrix); // Set mesh name if necessary if (m_ReferenceToMesh.contains(pRef)) { QList<Lib3dsMesh*> meshes= m_ReferenceToMesh.values(pRef); const int meshCount= meshes.count(); if (meshCount > 1) { for (int i= 0; i < meshCount; ++i) { Lib3dsNode* pCurrent3dsNode = lib3ds_node_new_object(); pCurrent3dsNode->node_id= m_CurrentNodeId++; pCurrent3dsNode->parent_id= p3dsNode->node_id; strcpy(pCurrent3dsNode->name, meshes.at(i)->name); lib3ds_file_insert_node(m_pLib3dsFile, pCurrent3dsNode); } } else { strcpy(p3dsNode->name, m_ReferenceToMesh.value(pRef)->name); } } } }
static Lib3dsBool kfdata_read(Lib3dsFile *file, FILE *f) { Lib3dsChunk c; Lib3dsWord chunk; if (!lib3ds_chunk_read_start(&c, LIB3DS_KFDATA, f)) { return(LIB3DS_FALSE); } while ((chunk=lib3ds_chunk_read_next(&c, f))!=0) { switch (chunk) { case LIB3DS_KFHDR: { file->keyf_revision=lib3ds_word_read(f); if (!lib3ds_string_read(file->name, 12+1, f)) { return(LIB3DS_FALSE); } file->frames=lib3ds_intd_read(f); } break; case LIB3DS_KFSEG: { file->segment_from=lib3ds_intd_read(f); file->segment_to=lib3ds_intd_read(f); } break; case LIB3DS_KFCURTIME: { file->current_frame=lib3ds_intd_read(f); } break; case LIB3DS_VIEWPORT_LAYOUT: case LIB3DS_DEFAULT_VIEW: { lib3ds_chunk_read_reset(&c, f); if (!lib3ds_viewport_read(&file->viewport_keyf, f)) { return(LIB3DS_FALSE); } } break; case LIB3DS_AMBIENT_NODE_TAG: { Lib3dsNode *node; node=lib3ds_node_new_ambient(); if (!node) { return(LIB3DS_FALSE); } lib3ds_chunk_read_reset(&c, f); if (!lib3ds_node_read(node, file, f)) { return(LIB3DS_FALSE); } lib3ds_file_insert_node(file, node); } break; case LIB3DS_OBJECT_NODE_TAG: { Lib3dsNode *node; node=lib3ds_node_new_object(); if (!node) { return(LIB3DS_FALSE); } lib3ds_chunk_read_reset(&c, f); if (!lib3ds_node_read(node, file, f)) { return(LIB3DS_FALSE); } lib3ds_file_insert_node(file, node); } break; case LIB3DS_CAMERA_NODE_TAG: { Lib3dsNode *node; node=lib3ds_node_new_camera(); if (!node) { return(LIB3DS_FALSE); } lib3ds_chunk_read_reset(&c, f); if (!lib3ds_node_read(node, file, f)) { return(LIB3DS_FALSE); } lib3ds_file_insert_node(file, node); } break; case LIB3DS_TARGET_NODE_TAG: { Lib3dsNode *node; node=lib3ds_node_new_target(); if (!node) { return(LIB3DS_FALSE); } lib3ds_chunk_read_reset(&c, f); if (!lib3ds_node_read(node, file, f)) { return(LIB3DS_FALSE); } lib3ds_file_insert_node(file, node); } break; case LIB3DS_LIGHT_NODE_TAG: case LIB3DS_SPOTLIGHT_NODE_TAG: { Lib3dsNode *node; node=lib3ds_node_new_light(); if (!node) { return(LIB3DS_FALSE); } lib3ds_chunk_read_reset(&c, f); if (!lib3ds_node_read(node, file, f)) { return(LIB3DS_FALSE); } lib3ds_file_insert_node(file, node); } break; case LIB3DS_L_TARGET_NODE_TAG: { Lib3dsNode *node; node=lib3ds_node_new_spot(); if (!node) { return(LIB3DS_FALSE); } lib3ds_chunk_read_reset(&c, f); if (!lib3ds_node_read(node, file, f)) { return(LIB3DS_FALSE); } lib3ds_file_insert_node(file, node); } break; default: lib3ds_chunk_unknown(chunk); } } lib3ds_chunk_read_end(&c, f); return(LIB3DS_TRUE); }
// 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.); }
// 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; }