////////////////////////////////////////////////////////////////////// // Set Functions ////////////////////////////////////////////////////////////////////// bool GLC_WorldTo3ds::exportToFile(const QString& fileName, bool useAbsolutePosition) { m_ReferenceToMesh.clear(); m_NameToMaterial.clear(); m_pRootLib3dsNode= NULL; m_CurrentNodeId= 0; m_OccIdToNodeId.clear(); m_CurrentMeshIndex= 0; m_UseAbsolutePosition= useAbsolutePosition; m_TextureToFileName.clear(); m_FileName= fileName; bool subject= false; { QFile exportFile(m_FileName); subject= exportFile.open(QIODevice::WriteOnly); exportFile.close(); } if (subject) { m_pLib3dsFile= lib3ds_file_new(); saveWorld(); subject= lib3ds_file_save(m_pLib3dsFile, fileName.toLocal8Bit().data()); } return subject; }
static Lib3dsFile *qgl3ds_lib3ds_file_load(QIODevice *iod) { Lib3dsFile *file; Lib3dsIo *io; Q_ASSERT(iod->isOpen() && iod->isReadable()); file = lib3ds_file_new(); if (!file) { iod->close(); return(0); } IODevice3ds io3d; io3d.dev = iod; io3d.errorState = false; io = lib3ds_io_new( &io3d, qgl3ds_fileio_error_func, qgl3ds_fileio_seek_func, qgl3ds_fileio_tell_func, qgl3ds_fileio_read_func, qgl3ds_fileio_write_func ); if (!io) { lib3ds_file_free(file); iod->close(); return(0); } if (!lib3ds_file_read(file, io)) { lib3ds_file_free(file); iod->close(); return(0); } lib3ds_io_free(io); iod->close(); return(file); }
/*! * Loads a .3DS file from disk into memory. * * \param filename The filename of the .3DS file * * \return A pointer to the Lib3dsFile structure containing the * data of the .3DS file. * If the .3DS file can not be loaded NULL is returned. * * \note To free the returned structure use lib3ds_free. * * \see lib3ds_file_save, * lib3ds_file_new, * lib3ds_file_free */ Lib3dsFile* lib3ds_file_open(const char *filename) { FILE *f; Lib3dsFile *file; Lib3dsIo io; f = fopen(filename, "rb"); if (!f) { return NULL; } file = lib3ds_file_new(); if (!file) { fclose(f); return NULL; } memset(&io, 0, sizeof(io)); io.self = f; io.seek_func = fileio_seek_func; io.tell_func = fileio_tell_func; io.read_func = fileio_read_func; io.write_func = fileio_write_func; io.log_func = NULL; if (!lib3ds_file_read(file, &io)) { fclose(f); free(file); return NULL; } fclose(f); return file; }
//*********************************************************************************** int charge_scene3ds(char * fichier3ds, Lib3dsFile** out_scene3ds) { FILE * file; Lib3dsIo io; *out_scene3ds = lib3ds_file_new(); printf("LOAD 3DS SCENE FILE : %s\n", fichier3ds); if(!fichier3ds) { printf("[ERROR] File name NULL.\n"); return -1; } if(!(file = fopen(fichier3ds, "rb"))) { printf("[ERROR] File not found : %s\n", fichier3ds); return -1; } io.self = file; io.seek_func = fileio_seek_func; io.tell_func = fileio_tell_func; io.read_func = fileio_read_func; io.write_func = fileio_write_func; io.log_func = fileio_log_func; lib3ds_file_read(*out_scene3ds, &io); printf("LOAD 3DS SCENE FILE - END\n"); return 0; }
/*! * Loads a .3DS file from disk into memory. * * \param filename The filename of the .3DS file * * \return A pointer to the Lib3dsFile structure containing the * data of the .3DS file. * If the .3DS file can not be loaded NULL is returned. * * \note To free the returned structure use lib3ds_free. * * \see lib3ds_file_save * \see lib3ds_file_new * \see lib3ds_file_free * * \ingroup file */ Lib3dsFile* lib3ds_file_load(const char *filename) { FILE *f; Lib3dsFile *file; Lib3dsIo *io; f = fopen(filename, "rb"); if (!f) { return(0); } file = lib3ds_file_new(); if (!file) { fclose(f); return(0); } io = lib3ds_io_new( f, fileio_error_func, fileio_seek_func, fileio_tell_func, fileio_read_func, fileio_write_func ); if (!io) { lib3ds_file_free(file); fclose(f); return(0); } if (!lib3ds_file_read(file, io)) { free(file); lib3ds_io_free(io); fclose(f); return(0); } lib3ds_io_free(io); fclose(f); return(file); }
int main(int argc, char **argv) { Lib3dsFile *f = NULL; FILE *p; parse_args(argc, argv); f = lib3ds_file_new(); p = fopen(input, "r"); if (!p) { fprintf(stderr, "Cannot open \"%s\" for reading: %s", output, strerror(errno)); exit(1); } dump_to_3ds(f, p); fclose(p); lib3ds_file_free(f); return 0; }
/*! * Loads a .3DS file from disk into memory. * * \param filename The filename of the .3DS file * * \return A pointer to the Lib3dsFile structure containing the * data of the .3DS file. * If the .3DS file can not be loaded NULL is returned. * * \note To free the returned pointer use lib3ds_free. * * \see lib3ds_file_save * \see lib3ds_file_new * \see lib3ds_file_free * * \ingroup file */ Lib3dsFile* lib3ds_file_load(const char *filename) { FILE *f; Lib3dsFile *file; f=osgDB::fopen(filename, "rb"); if (!f) { return(0); } file=lib3ds_file_new(); if (!file) { fclose(f); return(0); } if (!lib3ds_file_read(file, f)) { free(file); fclose(f); return(0); } fclose(f); return(file); }
int LD3dsExporter::doExport(LDLModel *pTopModel) { int retVal = 1; TCFloat matrix[16]; TCVector::initIdentityMatrix(matrix); matrix[5] = 0.0; matrix[6] = -1.0; matrix[9] = 1.0; matrix[10] = 0.0; m_topModel = pTopModel; m_file = lib3ds_file_new(); m_names.clear(); m_meshes.clear(); m_meshCount = 0; doExport(pTopModel, NULL, matrix, 7, false, true, false); //if (m_includeCamera) //{ // Lib3dsCamera *pCamera = lib3ds_camera_new("Default"); // Lib3dsCameraNode *pCameraNode; // TCVector cameraLoc(m_camera.getPosition().transformPoint(matrix)); // pCamera->position[0] = cameraLoc[0]; // pCamera->position[1] = cameraLoc[1]; // pCamera->position[2] = cameraLoc[2]; // pCamera->fov = m_fov; // pCameraNode = lib3ds_node_new_camera(pCamera); // lib3ds_file_append_node(m_file, (Lib3dsNode *)pCameraNode, NULL); //} if (!lib3ds_file_save(m_file, m_filename.c_str())) { retVal = 0; } lib3ds_file_free(m_file); return retVal; }
int main(int argc, char **argv) { FILE *file; Lib3dsFile *f = 0; Lib3dsIo io; int result; int i; parse_args(argc, argv); file = fopen(filename, "rb"); if (!file) { fprintf(stderr, "***ERROR***\nFile not found: %s\n", filename); exit(1); } f = lib3ds_file_new(); memset(&io, 0, sizeof(io)); io.self = file; io.seek_func = fileio_seek_func; io.tell_func = fileio_tell_func; io.read_func = fileio_read_func; io.write_func = fileio_write_func; io.log_func = fileio_log_func; result = lib3ds_file_read(f, &io); fclose(file); if (!result) { fprintf(stderr, "***ERROR***\nLoading file failed: %s\n", filename); exit(1); } if (flags & LIB3DSDUMP_MATERIALS) { printf("Dumping materials:\n"); for (i = 0; i < f->nmaterials; ++i) material_dump(f->materials[i]); printf("\n"); } if (flags & LIB3DSDUMP_TRIMESHES) { printf("Dumping meshes:\n"); for (i = 0; i < f->nmeshes; ++i) mesh_dump(f->meshes[i]); printf("\n"); } if (flags & LIB3DSDUMP_INSTANCES) { Lib3dsNode *p; printf("Dumping instances:\n"); for (p = f->nodes; p != 0; p = p->next) { dump_instances(p, ""); } printf("\n"); } if (flags & LIB3DSDUMP_CAMERAS) { printf("Dumping cameras:\n"); for (i = 0; i < f->ncameras; ++i) camera_dump(f->cameras[i]); printf("\n"); } if (flags & LIB3DSDUMP_LIGHTS) { printf("Dumping lights:\n"); for (i = 0; i < f->nlights; ++i) light_dump(f->lights[i]); printf("\n"); } if (flags & LIB3DSDUMP_NODES) { Lib3dsNode *p; printf("Dumping node hierarchy:\n"); for (p = f->nodes; p != 0; p = p->next) { node_dump(p, 1); } printf("\n"); } if (output) { if (!lib3ds_file_save(f, output)) { printf("***ERROR**** Writing %s\n", output); } } lib3ds_file_free(f); return 0; }
int main(int argc, char **argv) { Lib3dsFile *file = lib3ds_file_new(); file->frames = 360; { Lib3dsMaterial *mat = lib3ds_material_new("c_tex"); lib3ds_file_insert_material(file, mat, -1); strcpy(mat->texture1_map.name, "cube.tga"); mat->texture1_map.percent = 1.0; mat = lib3ds_material_new("c_red"); lib3ds_file_insert_material(file, mat, -1); mat->diffuse[0] = 1.0; mat->diffuse[1] = 0.0; mat->diffuse[2] = 0.0; mat = lib3ds_material_new("c_blue"); lib3ds_file_insert_material(file, mat, -1); mat->diffuse[0] = 0.0; mat->diffuse[1] = 0.0; mat->diffuse[2] = 1.0; } { int i, j; Lib3dsMesh *mesh = lib3ds_mesh_new("cube"); Lib3dsMeshInstanceNode *inst; lib3ds_file_insert_mesh(file, mesh, -1); lib3ds_mesh_resize_vertices(mesh, 8, 1, 0); for (i = 0; i < 8; ++i) { lib3ds_vector_copy(mesh->vertices[i], g_vertices[i]); mesh->texcos[i][0] = g_texcoords[i][0]; mesh->texcos[i][1] = g_texcoords[i][1]; } lib3ds_mesh_resize_faces(mesh, 12); for (i = 0; i < 12; ++i) { for (j = 0; j < 3; ++j) { mesh->faces[i].index[j] = g_indices[i][j]; } } for (i = 0; i < 8; ++i) { mesh->faces[i].material = 0; } for (i = 0; i < 2; ++i) { mesh->faces[8+i].material = 1; } for (i = 0; i < 2; ++i) { mesh->faces[10+i].material = 2; } inst = lib3ds_node_new_mesh_instance(mesh, "01", NULL, NULL, NULL); lib3ds_file_append_node(file, (Lib3dsNode*)inst, NULL); } { Lib3dsCamera *camera; Lib3dsCameraNode *n; Lib3dsTargetNode *t; int i; camera = lib3ds_camera_new("camera01"); lib3ds_file_insert_camera(file, camera, -1); lib3ds_vector_make(camera->position, 0.0, -100, 0.0); lib3ds_vector_make(camera->target, 0.0, 0.0, 0.0); n = lib3ds_node_new_camera(camera); t = lib3ds_node_new_camera_target(camera); lib3ds_file_append_node(file, (Lib3dsNode*)n, NULL); lib3ds_file_append_node(file, (Lib3dsNode*)t, NULL); lib3ds_track_resize(&n->pos_track, 37); for (i = 0; i <= 36; i++) { n->pos_track.keys[i].frame = 10 * i; lib3ds_vector_make(n->pos_track.keys[i].value, (float)(100.0 * cos(2 * M_PI * i / 36.0)), (float)(100.0 * sin(2 * M_PI * i / 36.0)), 50.0 ); } } if (!lib3ds_file_save(file, "cube.3ds")) { fprintf(stderr, "ERROR: Saving 3ds file failed!\n"); } lib3ds_file_free(file); }
// TODO: Build own exporter class void objectExporter::on_buttonBox_accepted() { QString fileName = QFileDialog::getSaveFileName(gloParent, "Save 3ds Object", ".", "3D Object (*.3ds)", 0, 0); QList<trackHandler*> trackList = gloParent->getTrackList(); trackHandler* curTrack = trackList[ui->trackBox->currentIndex()]; trackMesh* mesh = curTrack->mMesh; QVector<float> *vertices = new QVector<float>(); QVector<unsigned int> *indices = new QVector<unsigned int>(); QVector<unsigned int> *borders = new QVector<unsigned int>(); Lib3dsFile *file = lib3ds_file_new(); file->frames = 360; { Lib3dsMaterial* mat = lib3ds_material_new("coaster"); lib3ds_file_insert_material(file, mat, -1); mat->diffuse[0] = curTrack->trackColors[0].red()/255.f; mat->diffuse[1] = curTrack->trackColors[0].green()/255.f; mat->diffuse[2] = curTrack->trackColors[0].blue()/255.f; } { for(int section = 0; section < curTrack->trackData->lSections.size(); ++section) { vertices->clear(); indices->clear(); borders->clear(); mesh->build3ds(section, vertices, indices, borders); float* fvertices = new float[vertices->size()]; for(int i = 0; i < vertices->size()/3; ++i) { fvertices[3*i+0] = vertices->at(3*i+0); fvertices[3*i+1] = -vertices->at(3*i+2); fvertices[3*i+2] = vertices->at(3*i+1); //exportScreen->doFastExport(); } for(int subIndex = 0; subIndex < borders->size()-2; subIndex+= 2) { int fromVIndex = borders->at(subIndex)/3; int toVIndex = borders->at(subIndex+2)/3; int fromIIndex = borders->at(subIndex+1)/3; int toIIndex = borders->at(subIndex+3)/3; int i, j; QString name = QString::number(section).append(QString("_").append(QString::number(subIndex/2))); Lib3dsMesh *mesh = lib3ds_mesh_new(name.toLocal8Bit().data()); Lib3dsMeshInstanceNode *inst; lib3ds_file_insert_mesh(file, mesh, -1); lib3ds_mesh_resize_vertices(mesh, toVIndex-fromVIndex, 1, 0); for (i = 0; i < toVIndex-fromVIndex; ++i) { lib3ds_vector_copy(mesh->vertices[i], &fvertices[(i+fromVIndex)*3]); mesh->texcos[i][0] = 0.f; mesh->texcos[i][1] = 0.f; } lib3ds_mesh_resize_faces(mesh, toIIndex-fromIIndex); for (i = 0; i < toIIndex-fromIIndex; ++i) { for (j = 0; j < 3; ++j) { mesh->faces[i].index[j] = indices->at(3*(i+fromIIndex)+j)-fromVIndex; mesh->faces[i].material = 0; } } inst = lib3ds_node_new_mesh_instance(mesh, name.toLocal8Bit().data(), NULL, NULL, NULL); lib3ds_file_append_node(file, (Lib3dsNode*)inst, NULL); } delete[] fvertices; } } { Lib3dsCamera *camera; Lib3dsCameraNode *n; Lib3dsTargetNode *t; int i; camera = lib3ds_camera_new("camera01"); lib3ds_file_insert_camera(file, camera, -1); lib3ds_vector_make(camera->position, 0.0, -100, 0.0); lib3ds_vector_make(camera->target, 0.0, 0.0, 0.0); n = lib3ds_node_new_camera(camera); t = lib3ds_node_new_camera_target(camera); lib3ds_file_append_node(file, (Lib3dsNode*)n, NULL); lib3ds_file_append_node(file, (Lib3dsNode*)t, NULL); lib3ds_track_resize(&n->pos_track, 37); for (i = 0; i <= 36; i++) { n->pos_track.keys[i].frame = 10 * i; lib3ds_vector_make(n->pos_track.keys[i].value, (float)(100.0 * cos(2 * F_PI * i / 36.0)), (float)(100.0 * sin(2 * F_PI * i / 36.0)), 50.0 ); } } if (!lib3ds_file_save(file, fileName.toLocal8Bit().data())) { qDebug("ERROR: Saving 3ds file failed!\n"); } lib3ds_file_free(file); delete indices; delete vertices; }
CL_Lib3dsFile::CL_Lib3dsFile() : file(0) { file = lib3ds_file_new(); }