/// <summary> /// Create a TPL texture file from the specified model. /// </summary> /// <param name="model">The model to create the TPL file from.</param> /// <param name="textureIndexMapping">The correspondence between textures images in the model and the generated TPL texture indices.</param> public Tpl(ObjMtlModel model, out Dictionary <Bitmap, int> textureIndexMapping) { if (model == null) { throw new ArgumentNullException("model"); } // Gather all material definitions in the model IEnumerable <ObjMtlMaterial> allMaterials = model.Objects .SelectMany(o => o.Value.Meshes).Select(m => m.Material); Dictionary <Bitmap, int> textureIndexMappingInt = new Dictionary <Bitmap, int>(); foreach (ObjMtlMaterial mat in allMaterials) { // Create and add texture for diffuse map if (mat.DiffuseTextureMap != null && !textureIndexMappingInt.ContainsKey(mat.DiffuseTextureMap)) { int textureIndex = Count; TplTexture texture = new TplTexture(GcTextureFormat.CMPR, mat.DiffuseTextureMap); Add(texture); textureIndexMappingInt.Add(mat.DiffuseTextureMap, textureIndex); } } // Replace the 'out' variable at the end so it does not get // modified if an exception textureIndexMapping = textureIndexMappingInt; }
private void importObjMtlToolStripMenuItem_Click(object sender, EventArgs e) { if (!CheckSaveUnsavedChanges()) { return; } if (ofdLoadObj.ShowDialog() != DialogResult.OK) { return; } List <string> modelWarningLog; ObjMtlModel model; try { model = new ObjMtlModel(ofdLoadObj.FileName, out modelWarningLog); if (modelWarningLog.Count != 0) { ObjMtlWarningLogDialog warningDlg = new ObjMtlWarningLogDialog(modelWarningLog); if (warningDlg.ShowDialog() != DialogResult.Yes) { return; } } } catch (Exception ex) { MessageBox.Show("Error loading the OBJ file. " + ex.Message, "Error loading the OBJ file.", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } Dictionary <Bitmap, int> textureIndexMapping; Tpl = new Tpl(model, out textureIndexMapping); Gma = new Gma(model, textureIndexMapping); paneStageViewer.SetDrawTextures(true); // Set TPL / GMA as changed HaveUnsavedGmaChanges = true; HaveUnsavedTplChanges = true; // Update model list paneModels.UpdateTree(); paneModels.UpdateModelButtons(); // Update material tab paneMaterials.UpdateTrees(); //UpdateTextureButtons(); // Update stage viewer paneStageViewer.InvalidateGL(); }
/// <summary> /// Create a new Gma model from a loaded OBJ/MTL model. /// </summary> /// <param name="model">The model to load the .GMA file from.</param> /// <param name="textureIndexMapping">Correspondence between the textures defined in the model materials and .TPL texture indices.</param> public Gma(ObjMtlModel model, Dictionary <Bitmap, int> textureIndexMapping) : this() { if (model.Objects.ContainsKey("") && model.Objects[""].Meshes.SelectMany(m => m.Faces).Any()) { throw new InvalidOperationException("Geometry is not allowed outside of named objects."); } foreach (KeyValuePair <string, ObjMtlObject> objectEntry in model.Objects) { Gcmf modelObject = new Gcmf(objectEntry.Value, textureIndexMapping); this.Add(new GmaEntry(objectEntry.Key, modelObject)); } }
public void Load(ObjMtlModel model, GxInterpolationFormat intFormat, int numMipmaps, List <int> textureIds, out Dictionary <Bitmap, int> textureIndexMapping) { if (model == null) { throw new ArgumentNullException("model"); } // Gather all material definitions in the model IEnumerable <ObjMtlMaterial> allMaterials = model.Objects .SelectMany(o => o.Value.Meshes).Select(m => m.Material); Dictionary <Bitmap, int> textureIndexMappingInt = new Dictionary <Bitmap, int>(); int textureCount = 0; foreach (ObjMtlMaterial mat in allMaterials) { // Create and add texture for diffuse map if (mat.DiffuseTextureMap != null && !BitmapComparision.ContainsBitmap(textureIndexMappingInt, mat.DiffuseTextureMap)) { if (textureCount >= textureIds.Count) { throw new InvalidObjMtlFileException("Too many textures to import"); } TplTexture texture = new TplTexture(GxTextureFormat.CMPR, intFormat, numMipmaps, mat.DiffuseTextureMap); this[textureIds[textureCount]] = texture; textureIndexMappingInt.Add(mat.DiffuseTextureMap, textureIds[textureCount]); ++textureCount; } } // Replace the 'out' variable at the end so it does not get // modified if an exception textureIndexMapping = textureIndexMappingInt; }
public void Load(ObjMtlModel model, Dictionary <Bitmap, int> textureIndexMapping, string name, bool preserveFlags) { if (model.Objects.ContainsKey("") && model.Objects[""].Meshes.SelectMany(m => m.Faces).Any()) { throw new InvalidOperationException("Geometry is not allowed outside of named objects."); } if (model.Objects.Count != 1) { throw new InvalidObjMtlFileException("Only replace 1 model with 1 model"); } int entryIndex = GetEntryIndex(name); foreach (KeyValuePair <string, ObjMtlObject> objectEntry in model.Objects) { Gcmf modelObject = new Gcmf(objectEntry.Value, textureIndexMapping); if (preserveFlags) { modelObject.CopyFlags(this[entryIndex].ModelObject); } this[entryIndex] = new GmaEntry(name, modelObject); } }
/// <summary> /// Create a TPL texture file from the specified model. /// </summary> /// <param name="model">The model to create the TPL file from.</param> /// <param name="textureIndexMapping">The correspondence between textures images in the model and the generated TPL texture indices.</param> public Tpl(ObjMtlModel model, GxInterpolationFormat intFormat, int numMipmaps, out Dictionary <Bitmap, int> textureIndexMapping) { if (model == null) { throw new ArgumentNullException("model"); } // Gather all material definitions in the model IEnumerable <ObjMtlMaterial> allMaterials = model.Objects .SelectMany(o => o.Value.Meshes).Select(m => m.Material); Dictionary <Bitmap, int> textureIndexMappingInt = new Dictionary <Bitmap, int>(); foreach (ObjMtlMaterial mat in allMaterials) { // Create and add texture for diffuse map if (mat.DiffuseTextureMap != null && !textureIndexMappingInt.ContainsKey(mat.DiffuseTextureMap)) { int textureIndex = Count; Match materialPreset = Regex.Match(mat.Name, @"(?<=TEX_)[^\]]*"); GxTextureFormat format = GxTextureFormat.CMPR; if (materialPreset.Success) { switch (materialPreset.Value) { case "RGB5A3": format = GxTextureFormat.RGB5A3; break; case "RGB565": format = GxTextureFormat.RGB565; break; case "RGBA8": format = GxTextureFormat.RGBA8; break; case "I4": format = GxTextureFormat.I4; break; case "I8": format = GxTextureFormat.I8; break; case "IA4": format = GxTextureFormat.IA4; break; default: break; } } TplTexture texture = new TplTexture(format, intFormat, numMipmaps, mat.DiffuseTextureMap); Add(texture); textureIndexMappingInt.Add(mat.DiffuseTextureMap, textureIndex); } } // Replace the 'out' variable at the end so it does not get // modified if an exception textureIndexMapping = textureIndexMappingInt; }
private void tsBtnImportObjMtl_Click(object sender, EventArgs e) { if (!CheckSaveUnsavedChanges()) { return; } if (ofdLoadObj.ShowDialog() != DialogResult.OK) { return; } List <string> modelWarningLog; ObjMtlModel model; try { model = new ObjMtlModel(ofdLoadObj.FileName, out modelWarningLog); if (modelWarningLog.Count != 0) { ObjMtlWarningLogDialog warningDlg = new ObjMtlWarningLogDialog(modelWarningLog); if (warningDlg.ShowDialog() != DialogResult.Yes) { return; } } } catch (Exception ex) { MessageBox.Show("Error loading the OBJ file. " + ex.Message, "Error loading the OBJ file.", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } Dictionary <Bitmap, int> textureIndexMapping; tpl = new Tpl(model, out textureIndexMapping); gma = new Gma(model, textureIndexMapping); // Set TPL / GMA as changed haveUnsavedGmaChanges = true; haveUnsavedTplChanges = true; // Update model list UpdateModelTree(); UpdateModelButtons(); UpdateModelDisplay(); // Update material tab UpdateMaterialList(); UpdateMaterialDisplay(); // Update texture list UpdateTextureTree(); UpdateTextureButtons(); UpdateTextureDisplay(); // Update model viewer reloadOnNextRedraw = true; glControlModel.Invalidate(); }