void GLC_WorldTo3ds::saveMeshes() { // Retrieve the list of mesh and save them into the 3ds QList<GLC_StructReference*> refList= m_World.references(); const int refCount= refList.count(); for (int i= 0; i < refCount; ++i) { GLC_StructReference* pRef= refList.at(i); if (pRef->hasRepresentation()) { GLC_3DRep* pRep= dynamic_cast<GLC_3DRep*>(pRef->representationHandle()); if (NULL != pRep) { // This reference has a mesh const QString meshName= pRef->name() + '_' + QString::number(++m_CurrentMeshIndex); QList<Lib3dsMesh*> meshes= createMeshsFrom3DRep(pRep, meshName); { const int count= meshes.count(); for (int i= 0; i < count; ++i) { lib3ds_file_insert_mesh(m_pLib3dsFile, meshes.at(i)); m_ReferenceToMesh.insertMulti(pRef, meshes.at(i)); } } } } } }
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 named_object_read(Lib3dsFile *file, Lib3dsIo *io) { Lib3dsChunk c; char name[64]; Lib3dsWord chunk; Lib3dsMesh *mesh = NULL; Lib3dsCamera *camera = NULL; Lib3dsLight *light = NULL; Lib3dsDword object_flags; if (!lib3ds_chunk_read_start(&c, LIB3DS_NAMED_OBJECT, io)) { return(LIB3DS_FALSE); } if (!lib3ds_io_read_string(io, name, 64)) { return(LIB3DS_FALSE); } lib3ds_chunk_dump_info(" NAME=%s", name); lib3ds_chunk_read_tell(&c, io); object_flags = 0; while ((chunk=lib3ds_chunk_read_next(&c, io))!=0) { switch (chunk) { case LIB3DS_N_TRI_OBJECT: { mesh=lib3ds_mesh_new(name); if (!mesh) { return(LIB3DS_FALSE); } lib3ds_chunk_read_reset(&c, io); if (!lib3ds_mesh_read(mesh, io)) { return(LIB3DS_FALSE); } lib3ds_file_insert_mesh(file, mesh); } break; case LIB3DS_N_CAMERA: { camera=lib3ds_camera_new(name); if (!camera) { return(LIB3DS_FALSE); } lib3ds_chunk_read_reset(&c, io); if (!lib3ds_camera_read(camera, io)) { return(LIB3DS_FALSE); } lib3ds_file_insert_camera(file, camera); } break; case LIB3DS_N_DIRECT_LIGHT: { light=lib3ds_light_new(name); if (!light) { return(LIB3DS_FALSE); } lib3ds_chunk_read_reset(&c, io); if (!lib3ds_light_read(light, io)) { return(LIB3DS_FALSE); } lib3ds_file_insert_light(file, light); } break; case LIB3DS_OBJ_HIDDEN: object_flags |= LIB3DS_OBJECT_HIDDEN; break; case LIB3DS_OBJ_DOESNT_CAST: object_flags |= LIB3DS_OBJECT_DOESNT_CAST; break; case LIB3DS_OBJ_VIS_LOFTER: object_flags |= LIB3DS_OBJECT_VIS_LOFTER; break; case LIB3DS_OBJ_MATTE: object_flags |= LIB3DS_OBJECT_MATTE; break; case LIB3DS_OBJ_DONT_RCVSHADOW: object_flags |= LIB3DS_OBJECT_DONT_RCVSHADOW; break; case LIB3DS_OBJ_FAST: object_flags |= LIB3DS_OBJECT_FAST; break; case LIB3DS_OBJ_FROZEN: object_flags |= LIB3DS_OBJECT_FROZEN; break; default: lib3ds_chunk_unknown(chunk); } } if (mesh) mesh->object_flags = object_flags; if (camera) camera->object_flags = object_flags; if (light) light->object_flags = object_flags; lib3ds_chunk_read_end(&c, io); return(LIB3DS_TRUE); }
static Lib3dsBool named_object_read(Lib3dsFile *file, FILE *f) { Lib3dsChunk c; char name[64]; Lib3dsWord chunk; if (!lib3ds_chunk_read_start(&c, LIB3DS_NAMED_OBJECT, f)) { return(LIB3DS_FALSE); } if (!lib3ds_string_read(name, 64, f)) { return(LIB3DS_FALSE); } lib3ds_chunk_dump_info(" NAME=%s", name); lib3ds_chunk_read_tell(&c, f); while ((chunk=lib3ds_chunk_read_next(&c, f))!=0) { switch (chunk) { case LIB3DS_N_TRI_OBJECT: { Lib3dsMesh *mesh; mesh=lib3ds_mesh_new(name); if (!mesh) { return(LIB3DS_FALSE); } lib3ds_chunk_read_reset(&c, f); if (!lib3ds_mesh_read(mesh, f)) { return(LIB3DS_FALSE); } lib3ds_file_insert_mesh(file, mesh); } break; case LIB3DS_N_CAMERA: { Lib3dsCamera *camera; camera=lib3ds_camera_new(name); if (!camera) { return(LIB3DS_FALSE); } lib3ds_chunk_read_reset(&c, f); if (!lib3ds_camera_read(camera, f)) { return(LIB3DS_FALSE); } lib3ds_file_insert_camera(file, camera); } break; case LIB3DS_N_DIRECT_LIGHT: { Lib3dsLight *light; light=lib3ds_light_new(name); if (!light) { return(LIB3DS_FALSE); } lib3ds_chunk_read_reset(&c, f); if (!lib3ds_light_read(light, f)) { return(LIB3DS_FALSE); } lib3ds_file_insert_light(file, light); } break; default: lib3ds_chunk_unknown(chunk); } } lib3ds_chunk_read_end(&c, f); return(LIB3DS_TRUE); }
static void named_object_read(Lib3dsFile *file, Lib3dsIo *io) { Lib3dsChunk c; char name[64]; uint16_t chunk; Lib3dsMesh *mesh = NULL; Lib3dsCamera *camera = NULL; Lib3dsLight *light = NULL; uint32_t object_flags; lib3ds_chunk_read_start(&c, CHK_NAMED_OBJECT, io); lib3ds_io_read_string(io, name, 64); lib3ds_io_log(io, LIB3DS_LOG_INFO, " NAME=%s", name); lib3ds_chunk_read_tell(&c, io); object_flags = 0; while ((chunk = lib3ds_chunk_read_next(&c, io)) != 0) { switch (chunk) { case CHK_N_TRI_OBJECT: { mesh = lib3ds_mesh_new(name); lib3ds_file_insert_mesh(file, mesh, -1); lib3ds_chunk_read_reset(&c, io); lib3ds_mesh_read(file, mesh, io); break; } case CHK_N_CAMERA: { camera = lib3ds_camera_new(name); lib3ds_file_insert_camera(file, camera, -1); lib3ds_chunk_read_reset(&c, io); lib3ds_camera_read(camera, io); break; } case CHK_N_DIRECT_LIGHT: { light = lib3ds_light_new(name); lib3ds_file_insert_light(file, light, -1); lib3ds_chunk_read_reset(&c, io); lib3ds_light_read(light, io); break; } case CHK_OBJ_HIDDEN: object_flags |= LIB3DS_OBJECT_HIDDEN; break; case CHK_OBJ_DOESNT_CAST: object_flags |= LIB3DS_OBJECT_DOESNT_CAST; break; case CHK_OBJ_VIS_LOFTER: object_flags |= LIB3DS_OBJECT_VIS_LOFTER; break; case CHK_OBJ_MATTE: object_flags |= LIB3DS_OBJECT_MATTE; break; case CHK_OBJ_DONT_RCVSHADOW: object_flags |= LIB3DS_OBJECT_DONT_RCVSHADOW; break; case CHK_OBJ_FAST: object_flags |= LIB3DS_OBJECT_FAST; break; case CHK_OBJ_FROZEN: object_flags |= LIB3DS_OBJECT_FROZEN; break; default: lib3ds_chunk_unknown(chunk, io); } } if (mesh) mesh->object_flags = object_flags; if (camera) camera->object_flags = object_flags; if (light) light->object_flags = object_flags; lib3ds_chunk_read_end(&c, io); }
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; }
void WriterNodeVisitor::buildMesh(osg::Geode & geo, const osg::Matrix & mat, MapIndices & index_vert, bool texcoords, Lib3dsMesh * mesh) { OSG_DEBUG << "Building Mesh" << std::endl; assert(mesh); // Write points assert(index_vert.size() <= MAX_VERTICES); lib3ds_mesh_resize_vertices(mesh, index_vert.size(), texcoords ? 1 : 0, 0); for(MapIndices::iterator it = index_vert.begin(); it != index_vert.end();++it) { osg::Geometry *g = geo.getDrawable( it->first.second )->asGeometry(); const osg::Array * basevecs = g->getVertexArray(); assert(basevecs); if (!basevecs || basevecs->getNumElements()==0) continue; if (basevecs->getType() == osg::Array::Vec3ArrayType) { const osg::Vec3Array & vecs= *static_cast<const osg::Vec3Array *>(basevecs); copyOsgVectorToLib3dsVector(mesh->vertices[it->second], vecs[it->first.first]*mat); } else if (basevecs->getType() == osg::Array::Vec3dArrayType) { // Handle double presision vertices by converting them to float with a warning OSG_NOTICE << "3DS format only supports single precision vertices. Converting double precision to single." << std::endl; const osg::Vec3dArray & vecs= *static_cast<const osg::Vec3dArray *>(basevecs); copyOsgVectorToLib3dsVector(mesh->vertices[it->second], vecs[it->first.first]*mat); } else { OSG_NOTIFY(osg::FATAL) << "Vertex array is not Vec3 or Vec3d. Not implemented" << std::endl; _succeeded = false; return; } } // Write texture coords (Texture 0 only) if (texcoords) { for(MapIndices::iterator it = index_vert.begin(); it != index_vert.end(); ++it) { osg::Geometry *g = geo.getDrawable( it->first.second )->asGeometry(); const osg::Array * texarray = g->getNumTexCoordArrays()>=1 ? g->getTexCoordArray(0) : NULL; if (!texarray || texarray->getNumElements()==0) continue; if (g->getTexCoordArray(0)->getType() != osg::Array::Vec2ArrayType) { OSG_NOTIFY(osg::FATAL) << "Texture coords array is not Vec2. Not implemented" << std::endl; _succeeded = false; return; } const osg::Vec2Array & vecs= *static_cast<const osg::Vec2Array *>(texarray); mesh->texcos[it->second][0] = vecs[it->first.first][0]; mesh->texcos[it->second][1] = vecs[it->first.first][1]; } } lib3ds_file_insert_mesh(_file3ds, mesh, _lastMeshIndex); ++_lastMeshIndex; Lib3dsMeshInstanceNode * node3ds = lib3ds_node_new_mesh_instance(mesh, mesh->name, NULL, NULL, NULL); lib3ds_file_append_node(_file3ds, reinterpret_cast<Lib3dsNode*>(node3ds), reinterpret_cast<Lib3dsNode*>(_cur3dsNode)); }
void LD3dsExporter::doExport( LDLModel *pModel, Lib3dsNode *pParentNode, const TCFloat *matrix, int colorNumber, bool inPart, bool bfc, bool invert) { LDLFileLineArray *pFileLines = pModel->getFileLines(); if (pFileLines != NULL) { BFCState newBfcState = pModel->getBFCState(); int count = pModel->getActiveLineCount(); std::string meshName; Lib3dsMesh *pMesh = NULL; Lib3dsNode *pChildNode = NULL; // Lib3dsMeshInstanceNode *pInst; bool linesInvert = invert; bool isNew = false; if (TCVector::determinant(matrix) < 0.0f) { linesInvert = !linesInvert; } bfc = (bfc && newBfcState == BFCOnState) || newBfcState == BFCForcedOnState; meshName.resize(128); sprintf(&meshName[0], "m_%06d", ++m_meshCount); // meshName = getMeshName(pModel, pMesh); if (pMesh == NULL) { pMesh = lib3ds_mesh_new(meshName.c_str()); // memcpy(pMesh->matrix, matrix, sizeof(pMesh->matrix)); m_meshes[meshName] = pMesh; lib3ds_file_insert_mesh(m_file, pMesh, -1); isNew = true; } // pInst = lib3ds_node_new_mesh_instance(pMesh, // NULL/*(meshName + "n").c_str()*/, NULL, NULL, NULL); // pChildNode = (Lib3dsNode *)pInst; // memcpy(pChildNode->matrix, matrix, sizeof(float) * 16); // lib3ds_file_append_node(m_file, pChildNode, pParentNode); VertexVector vecVertices; FaceVector vecFaces; for (int i = 0; i < count; i++) { LDLFileLine *pFileLine = (*pFileLines)[i]; if (!pFileLine->isValid()) { continue; } switch (pFileLine->getLineType()) { case LDLLineTypeTriangle: case LDLLineTypeQuad: writeShapeLine(vecVertices, vecFaces, (LDLShapeLine *)pFileLine, matrix, colorNumber, bfc, linesInvert); break; case LDLLineTypeModel: { LDLModelLine *pModelLine = (LDLModelLine *)pFileLine; LDLModel *pOtherModel = pModelLine->getModel(true); if (pOtherModel != NULL) { TCFloat newMatrix[16]; int otherColorNumber = pModelLine->getColorNumber(); bool otherInPart = inPart; bool otherInvert = invert; if (pModelLine->getBFCInvert()) { otherInvert = !otherInvert; } if (otherColorNumber == 16) { otherColorNumber = colorNumber; } TCVector::multMatrix(matrix, pModelLine->getMatrix(), newMatrix); if (!inPart && pOtherModel->isPart() && m_seams) { TCVector min, max; TCFloat seamMatrix[16]; TCFloat tempMatrix[16]; pOtherModel->getBoundingBox(min, max); TCVector::calcScaleMatrix(m_seamWidth, seamMatrix, min, max); TCVector::multMatrix(newMatrix, seamMatrix, tempMatrix); memcpy(newMatrix, tempMatrix, sizeof(newMatrix)); otherInPart = true; } doExport(pOtherModel, pChildNode, newMatrix, otherColorNumber, otherInPart, bfc, otherInvert); } } break; default: // Get rid of warning break; } } if (isNew && vecVertices.size() > 0) { lib3ds_mesh_resize_vertices(pMesh, (int)vecVertices.size(), 0, 0); memcpy(pMesh->vertices, &vecVertices[0], sizeof(vecVertices[0]) * vecVertices.size()); lib3ds_mesh_resize_faces(pMesh, (int)vecFaces.size()); memcpy(pMesh->faces, &vecFaces[0], sizeof(vecFaces[0]) * vecFaces.size()); } else { --m_meshCount; } } }