/// <summary> /// Loads model data for conversion. /// </summary> /// <param name="id">Model ID.</param> /// <param name="scale">Amount to scale model.</param> private void LoadModelData(uint id, float scale) { // Open ARCH3D.BSA Arch3dFile arch3dFile = new Arch3dFile( Path.Combine(arena2Path, "ARCH3D.BSA"), FileUsage.UseDisk, true); // Get DFMesh int record = arch3dFile.GetRecordIndex(id); DFMesh dfMesh = arch3dFile.GetMesh(record); // Scale DFMesh if (scale != 1.0f) { foreach (var mesh in dfMesh.SubMeshes) { foreach (var plane in mesh.Planes) { for (int point = 0; point < plane.Points.Length; point++) { plane.Points[point].X *= scale; plane.Points[point].Y *= scale; plane.Points[point].Z *= scale; } } } } // Load model data model = new ModelData(); model.DFMesh = dfMesh; LoadVertices(ref model); LoadIndices(ref model); }
private void ShowMeshTextures() { // Clear texture flow panel TexturesPanel.Controls.Clear(); // Get the mesh data DFMesh mesh = arch3dFile.GetMesh(MeshIndexTrackBar.Value); // Loop through all submeshes foreach (DFMesh.DFSubMesh sm in mesh.SubMeshes) { // Load texture file string FileName = TextureFile.IndexToFileName(sm.TextureArchive); MyImageFileReader.LoadFile(FileName); // Get texture file DFImageFile textureFile = MyImageFileReader.ImageFile; // Get managed bitmap Bitmap bm = textureFile.GetManagedBitmap(sm.TextureRecord, 0, true, false); // Create a new picture box PictureBox pb = new PictureBox(); pb.Width = bm.Width; pb.Height = bm.Height; pb.Image = bm; // Add picture box to flow panel TexturesPanel.Controls.Add(pb); } }
/// <summary> /// Loads model data from DFMesh. /// </summary> /// <param name="id">Key of source mesh.</param> /// <param name="modelData">ModelData out.</param> /// <returns>True if successful.</returns> private bool LoadModelData(uint id, out ModelData modelData) { // Return from cache if present if (cacheModelData && modelDataDict.ContainsKey(id)) { modelData = modelDataDict[id]; return(true); } // New model object modelData = new ModelData(); // Find mesh index int index = arch3dFile.GetRecordIndex(id); if (index == -1) { return(false); } // Get DFMesh DFMesh dfMesh = arch3dFile.GetMesh(index); if (dfMesh.TotalVertices == 0) { return(false); } // Load mesh data modelData.DFMesh = dfMesh; LoadVertices(ref modelData); LoadIndices(ref modelData); AddModelTangents(ref modelData); CreateModelBuffers(graphicsDevice, ref modelData); // Load materials for (int i = 0; i < modelData.SubMeshes.Length; i++) { // Create material BaseMaterialEffect material = CreateModelMaterial( modelData.DFMesh.SubMeshes[i].TextureArchive, modelData.DFMesh.SubMeshes[i].TextureRecord); // Save key in submesh modelData.SubMeshes[i].MaterialKey = material.ID; } // Add to cache if (cacheModelData) { modelDataDict.Add(id, modelData); } return(true); }
/// <summary> /// Loads model data. /// </summary> /// <param name="id">Key of source mesh.</param> /// <param name="modelData">ModelData out.</param> /// <param name="scale">Scale of model.</param> /// <returns>True if successful.</returns> public bool GetModelData(uint id, out ModelData modelData) { // New model object modelData = new ModelData(); // Ready check if (!IsReady) { return(false); } // Return from cache if present if (modelDict.ContainsKey((int)id)) { modelData = modelDict[(int)id]; return(true); } // Find mesh index int index = arch3dFile.GetRecordIndex(id); if (index == -1) { return(false); } // Get DFMesh DFMesh dfMesh = arch3dFile.GetMesh(index); if (dfMesh.TotalVertices == 0) { return(false); } // Load mesh data modelData.DFMesh = dfMesh; LoadVertices(ref modelData, GlobalScale); LoadIndices(ref modelData); // Add to cache modelDict.Add((int)id, modelData); return(true); }
/// <summary> /// Loads model data from DFMesh. /// </summary> /// <param name="id">Key of source mesh.</param> /// <param name="model">ModelData out.</param> /// <returns>True if successful.</returns> private bool LoadModelData(uint id, out ModelData model) { // Return from cache if present if (cacheModelData && modelDataDict.ContainsKey(id)) { model = modelDataDict[id]; return(true); } // New model object model = new ModelData(); // Find mesh index int index = arch3dFile.GetRecordIndex(id); if (index == -1) { return(false); } // Get DFMesh DFMesh dfMesh = arch3dFile.GetMesh(index); if (dfMesh.TotalVertices == 0) { return(false); } // Load mesh data model.DFMesh = dfMesh; LoadVertices(ref model); LoadIndices(ref model); AddModelTangents(ref model); CreateModelBuffers(ref model); // Add to cache if (cacheModelData) { modelDataDict.Add(id, model); } return(true); }
static void Main(string[] args) { // Specify Arena2 path of local Daggerfall installation string MyArena2Path = "C:\\dosgames\\DAGGER\\ARENA2"; // Path to BSA file string FilePath = Path.Combine(MyArena2Path, "ARCH3D.BSA"); // Open file Arch3dFile arch3dFile = new Arch3dFile( FilePath, FileUsage.UseDisk, true); // Output some information about the file Console.WriteLine("{0} has {1} records", Path.GetFileName(FilePath), arch3dFile.Count); // Get a mesh record int record = 5557; DFMesh mesh = arch3dFile.GetMesh(record); // Output some information about this record Console.WriteLine("Record {0} has {1} total vertices and ObjectID {2}", record, mesh.TotalVertices, mesh.ObjectId); // Output the texture archives used for this mesh Console.WriteLine("The following textures are used:"); foreach (DFMesh.DFSubMesh sm in mesh.SubMeshes) { Console.WriteLine("TEXTURE.{0:000} (Record {1})", sm.TextureArchive, sm.TextureRecord); } }
/// <summary> /// Exports a DFMesh object to Collada format. /// </summary> /// <param name="id">ID of DFMesh to export.</param> /// <param name="scale">Scale model by value.</param> /// <returns>True if successful.</returns> public bool DFMeshToCollada(uint id, float scale) { // Get source mesh int index = arch3dFile.GetRecordIndex(id); DFMesh dfMesh = arch3dFile.GetMesh(index); if (dfMesh.SubMeshes == null) { string error = string.Format("Model '{0}' not found", id); Console.WriteLine(error); throw new Exception(error); } // Test model output path if (!Directory.Exists(modelOutputPath)) { string error = string.Format("Model output path '{0}' does not exist.", modelOutputPath); Console.WriteLine(error); throw new Exception(error); } // Test or create image output path string fullImageOutputPath = Path.Combine(modelOutputPath, imageOutputRelativePath); if (!Directory.Exists(fullImageOutputPath)) { Directory.CreateDirectory(fullImageOutputPath); } // Scale model if (scale != 1.0f) { foreach (var mesh in dfMesh.SubMeshes) { foreach (var plane in mesh.Planes) { for (int point = 0; point < plane.Points.Length; point++) { plane.Points[point].X *= scale; plane.Points[point].Y *= scale; plane.Points[point].Z *= scale; } } } } // Create dae root string filePath = Path.Combine(modelOutputPath, dfMesh.ObjectId.ToString() + ".dae"); _daeElement root = dae.add(filePath); // Build dae file AddAsset(ref root); AddGeometry(ref root, ref dfMesh); AddImages(ref root, ref dfMesh); AddEffects(ref root, ref dfMesh); AddMaterials(ref root, ref dfMesh); AddVisualScene(ref root, ref dfMesh); // Write dae file dae.writeAll(); return(true); }