//-------------------------------------------------------------------- // Función: openFileDialog1_FileOk // Propósito: // Fecha: miércoles, 08 de noviembre de 2006, 20:07:12 //-------------------------------------------------------------------- private void openFileDialog1_FileOk(object sender, CancelEventArgs e) { m_bIsValidFile = true; StreamReader sr = new StreamReader(openFileDialog1.FileName); String currentLine = sr.ReadLine(); try { while (currentLine.CompareTo("*GEOMOBJECT {") != 0) { currentLine = sr.ReadLine(); } } catch (Exception ex) { m_bIsValidFile = false; textBox1.Text = "Archivo no válido"; Xtrace.Log("#!No se pudo encontrar un GEOMOBJECT"); sr.Close(); return; } ReadObject(sr); sr.Close(); if (m_bIsValidFile) { textBox1.Text = openFileDialog1.FileName; } }
//-------------------------------------------------------------------- // Función: SaveObject // Propósito: // Fecha: miércoles, 08 de noviembre de 2006, 22:58:23 //-------------------------------------------------------------------- private void SaveObject(Stream st) { BinaryWriter br = new BinaryWriter(st); Xtrace.Log("#_Grabando el fichero"); if (checkBox1.Checked) { br.Write((uint)(0)); br.Write((uint)(0)); } br.Write((uint)(m_ArrayTriangulos.Length)); br.Write((uint)(m_ArrayTriangulos.Length * 3)); foreach (Triangulo t in m_ArrayTriangulos) { br.Write((ushort)(65535.0f * Clamp(t.tex1.u))); br.Write((ushort)(65535.0f * (1.0f - Clamp(t.tex1.v)))); if (checkBox1.Checked) { br.Write(t.nor1.nx); br.Write(t.nor1.nz); br.Write(-t.nor1.ny); } br.Write(t.vert1.x); br.Write(t.vert1.z); br.Write(-t.vert1.y); br.Write((ushort)(65535.0f * Clamp(t.tex2.u))); br.Write((ushort)(65535.0f * (1.0f - Clamp(t.tex2.v)))); if (checkBox1.Checked) { br.Write(t.nor2.nx); br.Write(t.nor2.nz); br.Write(-t.nor2.ny); } br.Write(t.vert2.x); br.Write(t.vert2.z); br.Write(-t.vert2.y); br.Write((ushort)(65535.0f * Clamp(t.tex3.u))); br.Write((ushort)(65535.0f * (1.0f - Clamp(t.tex3.v)))); if (checkBox1.Checked) { br.Write(t.nor3.nx); br.Write(t.nor3.nz); br.Write(-t.nor3.ny); } br.Write(t.vert3.x); br.Write(t.vert3.z); br.Write(-t.vert3.y); } Xtrace.Log("#_Fichero grabado"); br.Close(); }
private void scan_dirs(string dir) { Xtrace.Log("#*Escaneando {0}", dir); string[] files = Directory.GetFiles(dir, "*.ase"); foreach (string f in files) { m_bIsValidFile = true; StreamReader sr = new StreamReader(f); String currentLine = sr.ReadLine(); Xtrace.Log("#_Iniciando lectura {0}", f); try { while (currentLine.CompareTo("*GEOMOBJECT {") != 0) { currentLine = sr.ReadLine(); } } catch (Exception ex) { m_bIsValidFile = false; //MessageBox.Show("The file is not valid", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning); //textBox1.Text = "Archivo no válido"; Xtrace.Log("#!No se pudo encontrar un GEOMOBJECT"); sr.Close(); continue; } ReadObject(sr); sr.Close(); if (m_bIsValidFile) { string path = Path.ChangeExtension(f, "o3d"); SaveObject(new FileStream(path, FileMode.Create), path); ficheros_convertidos++; //MessageBox.Show("File converted!\n" + path + "\n\nVertex count: " + m_ListaVerticesFinal.Count + "\nFace count: " + (m_ListaIndicesFinal.Count / 3) + "\nNormals: " + ((m_bHasNormals && !checkBoxNormals.Checked) ? "YES" : "NO"), "Success", MessageBoxButtons.OK, MessageBoxIcon.Information); } } if (checkBoxSubdirs.Checked) { string[] dirs = Directory.GetDirectories(dir); foreach (string d in dirs) { scan_dirs(d); } } }
private void button2_Click_1(object sender, EventArgs e) { folderBrowserDialog1.ShowDialog(); if (folderBrowserDialog1.SelectedPath != "") { ficheros_convertidos = 0; scan_dirs(folderBrowserDialog1.SelectedPath); Xtrace.Log("#*Ficheros convertidos: {0}", ficheros_convertidos); MessageBox.Show("Finished!\n\n" + ficheros_convertidos + " files converted.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information); } }
//-------------------------------------------------------------------- // Función: openFileDialog1_FileOk // Propósito: // Fecha: miércoles, 08 de noviembre de 2006, 20:07:12 //-------------------------------------------------------------------- private void openFileDialog1_FileOk(object sender, CancelEventArgs e) { m_bIsValidFile = true; StreamReader sr = new StreamReader(openFileDialog1.FileName); String currentLine = sr.ReadLine(); Xtrace.Log("#_Iniciando lectura {0}", openFileDialog1.FileName); try { while (currentLine.CompareTo("*GEOMOBJECT {") != 0) { currentLine = sr.ReadLine(); } } catch (Exception ex) { m_bIsValidFile = false; MessageBox.Show("The file is not valid", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning); //textBox1.Text = "Archivo no válido"; Xtrace.Log("#!No se pudo encontrar un GEOMOBJECT"); sr.Close(); return; } ReadObject(sr); sr.Close(); if (m_bIsValidFile) { string path = Path.ChangeExtension(openFileDialog1.FileName, "o3d"); SaveObject(new FileStream(path, FileMode.Create), path); MessageBox.Show("File converted!\n" + path + "\n\nVertex count: " + m_ListaVerticesFinal.Count + "\nFace count: " + (m_ListaIndicesFinal.Count / 3) + "\nNormals: " + ((m_bHasNormals && !checkBoxNormals.Checked) ? "YES" : "NO"), "Success", MessageBoxButtons.OK, MessageBoxIcon.Information); } }
//-------------------------------------------------------------------- // Función: ReadObject // Propósito: // Fecha: miércoles, 08 de noviembre de 2006, 21:01:35 //-------------------------------------------------------------------- private void ReadObject(StreamReader sr) { String currentLine = sr.ReadLine(); while (!currentLine.Contains("*MESH_NUMVERTEX")) { currentLine = sr.ReadLine(); } currentLine = FormatString(currentLine); String[] parse = currentLine.Split(' '); m_VerticesArrayTemp = new VerticeTemporal[Int32.Parse(parse[1])]; Xtrace.Log("Vértices: {0}", m_VerticesArrayTemp.Length); while (!currentLine.Contains("*MESH_NUMFACES")) { currentLine = sr.ReadLine(); } currentLine = FormatString(currentLine); parse = currentLine.Split(' '); m_ArrayTriangulos = new Triangulo[Int32.Parse(parse[1])]; Xtrace.Log("Triángulos: {0}", m_ArrayTriangulos.Length); while (!currentLine.Contains("*MESH_VERTEX ")) { currentLine = sr.ReadLine(); } int i = 0; while (currentLine.Contains("*MESH_VERTEX ")) { currentLine = FormatString(currentLine); parse = currentLine.Split(' '); m_VerticesArrayTemp[i].x = float.Parse(parse[2], System.Globalization.NumberStyles.Float, ci); m_VerticesArrayTemp[i].y = float.Parse(parse[3], System.Globalization.NumberStyles.Float, ci); m_VerticesArrayTemp[i].z = float.Parse(parse[4], System.Globalization.NumberStyles.Float, ci); Xtrace.Log("Vert {0}: ({1}, {2}, {3})", i, m_VerticesArrayTemp[i].x, m_VerticesArrayTemp[i].y, m_VerticesArrayTemp[i].z); currentLine = sr.ReadLine(); i++; } while (!currentLine.Contains("*MESH_FACE ")) { currentLine = sr.ReadLine(); } i = 0; while (currentLine.Contains("*MESH_FACE ")) { currentLine = FormatString(currentLine); parse = currentLine.Split(' '); int a = Int32.Parse(parse[3]); int b = Int32.Parse(parse[5]); int c = Int32.Parse(parse[7]); m_ArrayTriangulos[i].vert1 = m_VerticesArrayTemp[a]; m_ArrayTriangulos[i].vert2 = m_VerticesArrayTemp[b]; m_ArrayTriangulos[i].vert3 = m_VerticesArrayTemp[c]; Xtrace.Log("Tri {0}: ({1}, {2}, {3})", i, a, b, c); currentLine = sr.ReadLine(); i++; } while (!currentLine.Contains("*MESH_NUMTVERTEX")) { currentLine = sr.ReadLine(); } currentLine = FormatString(currentLine); parse = currentLine.Split(' '); m_VerticesTexTemp = new TexturaTemporal[Int32.Parse(parse[1])]; while (!currentLine.Contains("*MESH_TVERT ")) { currentLine = sr.ReadLine(); } i = 0; while (currentLine.Contains("*MESH_TVERT ")) { currentLine = FormatString(currentLine); parse = currentLine.Split(' '); m_VerticesTexTemp[i].u = float.Parse(parse[2], System.Globalization.NumberStyles.Float, ci); m_VerticesTexTemp[i].v = float.Parse(parse[3], System.Globalization.NumberStyles.Float, ci); Xtrace.Log("Tex {0}: ({1}, {2})", i, m_VerticesTexTemp[i].u, m_VerticesTexTemp[i].v); currentLine = sr.ReadLine(); i++; } while (!currentLine.Contains("*MESH_TFACE ")) { currentLine = sr.ReadLine(); } i = 0; while (currentLine.Contains("*MESH_TFACE ")) { currentLine = FormatString(currentLine); parse = currentLine.Split(' '); m_ArrayTriangulos[i].tex1 = m_VerticesTexTemp[Int32.Parse(parse[2])]; m_ArrayTriangulos[i].tex2 = m_VerticesTexTemp[Int32.Parse(parse[3])]; m_ArrayTriangulos[i].tex3 = m_VerticesTexTemp[Int32.Parse(parse[4])]; currentLine = sr.ReadLine(); i++; } if (this.checkBox1.Checked) { while (!currentLine.Contains("*MESH_VERTEXNORMAL ")) { currentLine = sr.ReadLine(); } i = 0; int t = 0; while (currentLine.Contains("*MESH_VERTEXNORMAL ") || currentLine.Contains("*MESH_FACENORMAL ")) { if (currentLine.Contains("*MESH_FACENORMAL ")) { currentLine = sr.ReadLine(); continue; } currentLine = FormatString(currentLine); parse = currentLine.Split(' '); NormalTemporal n = new NormalTemporal(); n.nx = float.Parse(parse[2], System.Globalization.NumberStyles.Float, ci); n.ny = float.Parse(parse[3], System.Globalization.NumberStyles.Float, ci); n.nz = float.Parse(parse[4], System.Globalization.NumberStyles.Float, ci); Xtrace.Log("Nor {0}: ({1}, {2}, {3})", (i * 3) + t, n.nx, n.ny, n.nz); switch (t) { case 0: { m_ArrayTriangulos[i].nor1 = n; break; } case 1: { m_ArrayTriangulos[i].nor2 = n; break; } case 2: { m_ArrayTriangulos[i].nor3 = n; break; } } t++; if (t == 3) { i++; t = 0; } currentLine = sr.ReadLine(); } } }
//-------------------------------------------------------------------- // Función: ReadObject // Propósito: // Fecha: miércoles, 08 de noviembre de 2006, 21:01:35 //-------------------------------------------------------------------- private void ReadObject(StreamReader sr) { m_bHasNormals = false; m_ListaVerticesFinal.Clear(); m_ListaIndicesFinal.Clear(); String currentLine = sr.ReadLine(); while (!currentLine.Contains("*MESH_NUMVERTEX")) { currentLine = sr.ReadLine(); } currentLine = FormatString(currentLine); String[] parse = currentLine.Split(' '); m_VerticesArrayTemp = new VerticeTemporal[Int32.Parse(parse[1])]; Xtrace.Log("ASE Vértices: {0}", m_VerticesArrayTemp.Length); while (!currentLine.Contains("*MESH_NUMFACES")) { currentLine = sr.ReadLine(); } currentLine = FormatString(currentLine); parse = currentLine.Split(' '); m_ArrayTriangulos = new Triangulo[Int32.Parse(parse[1])]; for (int h = 0; h < m_ArrayTriangulos.Length; h++) { m_ArrayTriangulos[h] = new Triangulo(0); } Xtrace.Log("ASE Triángulos: {0}", m_ArrayTriangulos.Length); while (!currentLine.Contains("*MESH_VERTEX ")) { currentLine = sr.ReadLine(); } int i = 0; while (currentLine.Contains("*MESH_VERTEX ")) { currentLine = FormatString(currentLine); parse = currentLine.Split(' '); m_VerticesArrayTemp[i].x = float.Parse(parse[2], System.Globalization.NumberStyles.Float, ci); m_VerticesArrayTemp[i].y = float.Parse(parse[3], System.Globalization.NumberStyles.Float, ci); m_VerticesArrayTemp[i].z = float.Parse(parse[4], System.Globalization.NumberStyles.Float, ci); Xtrace.Log("ASE Vert {0}: ({1}, {2}, {3})", i, m_VerticesArrayTemp[i].x, m_VerticesArrayTemp[i].y, m_VerticesArrayTemp[i].z); currentLine = sr.ReadLine(); i++; } while (!currentLine.Contains("*MESH_FACE ")) { currentLine = sr.ReadLine(); } i = 0; while (currentLine.Contains("*MESH_FACE ")) { currentLine = FormatString(currentLine); parse = currentLine.Split(' '); int a = Int32.Parse(parse[3]); int b = Int32.Parse(parse[5]); int c = Int32.Parse(parse[7]); m_ArrayTriangulos[i].vert[0] = m_VerticesArrayTemp[a]; m_ArrayTriangulos[i].vert[1] = m_VerticesArrayTemp[b]; m_ArrayTriangulos[i].vert[2] = m_VerticesArrayTemp[c]; Xtrace.Log("ASE Tri {0}: ({1}, {2}, {3})", i, a, b, c); currentLine = sr.ReadLine(); i++; } while (!currentLine.Contains("*MESH_NUMTVERTEX")) { currentLine = sr.ReadLine(); } currentLine = FormatString(currentLine); parse = currentLine.Split(' '); m_VerticesTexTemp = new TexturaTemporal[Int32.Parse(parse[1])]; while (!currentLine.Contains("*MESH_TVERT ")) { currentLine = sr.ReadLine(); } i = 0; while (currentLine.Contains("*MESH_TVERT ")) { currentLine = FormatString(currentLine); parse = currentLine.Split(' '); m_VerticesTexTemp[i].u = float.Parse(parse[2], System.Globalization.NumberStyles.Float, ci); m_VerticesTexTemp[i].v = float.Parse(parse[3], System.Globalization.NumberStyles.Float, ci); Xtrace.Log("ASE Tex {0}: ({1}, {2})", i, m_VerticesTexTemp[i].u, m_VerticesTexTemp[i].v); currentLine = sr.ReadLine(); i++; } while (!currentLine.Contains("*MESH_TFACE ")) { currentLine = sr.ReadLine(); } i = 0; while (currentLine.Contains("*MESH_TFACE ")) { currentLine = FormatString(currentLine); parse = currentLine.Split(' '); m_ArrayTriangulos[i].tex[0] = m_VerticesTexTemp[Int32.Parse(parse[2])]; m_ArrayTriangulos[i].tex[1] = m_VerticesTexTemp[Int32.Parse(parse[3])]; m_ArrayTriangulos[i].tex[2] = m_VerticesTexTemp[Int32.Parse(parse[4])]; currentLine = sr.ReadLine(); i++; } //if (this.checkBoxSubdirs.Checked) { while (!currentLine.Contains("*MESH_VERTEXNORMAL ")) { currentLine = sr.ReadLine(); if (currentLine == null) { break; } } if (currentLine != null) { i = 0; int t = 0; while (currentLine.Contains("*MESH_VERTEXNORMAL ") || currentLine.Contains("*MESH_FACENORMAL ")) { if (currentLine.Contains("*MESH_FACENORMAL ")) { currentLine = sr.ReadLine(); continue; } currentLine = FormatString(currentLine); parse = currentLine.Split(' '); NormalTemporal n = new NormalTemporal(); n.nx = float.Parse(parse[2], System.Globalization.NumberStyles.Float, ci); n.ny = float.Parse(parse[3], System.Globalization.NumberStyles.Float, ci); n.nz = float.Parse(parse[4], System.Globalization.NumberStyles.Float, ci); Xtrace.Log("ASE Nor {0}: ({1}, {2}, {3})", (i * 3) + t, n.nx, n.ny, n.nz); m_bHasNormals = true; m_ArrayTriangulos[i].nor[t] = n; t++; if (t == 3) { i++; t = 0; } currentLine = sr.ReadLine(); } } } ComputeIndices(); }
//-------------------------------------------------------------------- // Función: SaveObject // Propósito: // Fecha: miércoles, 08 de noviembre de 2006, 22:58:23 //-------------------------------------------------------------------- private void SaveObject(Stream st, string file) { BinaryWriter br = new BinaryWriter(st); Xtrace.Log("#_Grabando el fichero {0}", file); if (m_bHasNormals && !checkBoxNormals.Checked) ///--- normales { br.Write((uint)(0)); br.Write((uint)(0)); } br.Write((uint)(m_ListaVerticesFinal.Count)); br.Write((uint)(m_ListaIndicesFinal.Count / 3)); foreach (VerticeFinal vf in m_ListaVerticesFinal) { br.Write((short)(vf.x * 16.0f * SCALE_FACTOR)); br.Write((short)(vf.z * 16.0f * SCALE_FACTOR)); br.Write((short)((-vf.y) * 16.0f * SCALE_FACTOR)); short v_padding = 0; br.Write(v_padding); if (m_bHasNormals && !checkBoxNormals.Checked) { double length = Math.Sqrt((vf.nx * vf.nx) + (vf.ny * vf.ny) + (vf.nz * vf.nz)); double x = vf.nx / length; double y = vf.nz / length; double z = (-vf.ny) / length; sbyte nx = (sbyte)(x * 127.0f); sbyte ny = (sbyte)(y * 127.0f); sbyte nz = (sbyte)(z * 127.0f); sbyte n_padding = 0; br.Write(nx); br.Write(ny); br.Write(nz); br.Write(n_padding); } br.Write((short)(vf.u * 512.0f)); br.Write((short)(vf.v * 512.0f)); } foreach (ushort ind in m_ListaIndicesFinal) { br.Write(ind); } br.Close(); if (m_ListaIndicesFinal.Count % 3 != 0) { Xtrace.Log("!Error: El número de índices no es múltiplo de 3: {0}", m_ListaIndicesFinal.Count); } if (m_bHasNormals && !checkBoxNormals.Checked) { Xtrace.Log("Las normales se han incluido"); } else { Xtrace.Log("No se han incluido normales"); } Xtrace.Log("Vértices: {0}", m_ListaVerticesFinal.Count); Xtrace.Log("Triángulos: {0}", m_ListaIndicesFinal.Count / 3); Xtrace.Log("#_Fichero grabado"); }
private void ComputeIndices() { foreach (Triangulo t in m_ArrayTriangulos) { for (int i = 0; i < 3; i++) { bool encontrado = false; foreach (VerticeFinal vf in m_ListaVerticesFinal) { if (vf.x == t.vert[i].x && vf.y == t.vert[i].y && vf.z == t.vert[i].z && vf.nx == t.nor[i].nx && vf.ny == t.nor[i].ny && vf.nz == t.nor[i].nz && vf.u == t.tex[i].u && vf.v == t.tex[i].v) { encontrado = true; break; } } if (!encontrado) { VerticeFinal temp = new VerticeFinal(); temp.x = t.vert[i].x; temp.y = t.vert[i].y; temp.z = t.vert[i].z; temp.nx = t.nor[i].nx; temp.ny = t.nor[i].ny; temp.nz = t.nor[i].nz; temp.u = t.tex[i].u; temp.v = t.tex[i].v; m_ListaVerticesFinal.Add(temp); int a = m_ListaVerticesFinal.Count - 1; Xtrace.Log("O3D Ver {0}: ({1}, {2}, {3})", a, temp.x, temp.y, temp.z); Xtrace.Log("O3D Tex {0}: ({1}, {2})", a, temp.u, temp.v); Xtrace.Log("O3D Nor {0}: ({1}, {2}, {3})", a, temp.nx, temp.ny, temp.nz); } } } foreach (Triangulo t in m_ArrayTriangulos) { for (int i = 0; i < 3; i++) { bool encontrado = false; ushort verticeID = 0; foreach (VerticeFinal vf in m_ListaVerticesFinal) { if (vf.x == t.vert[i].x && vf.y == t.vert[i].y && vf.z == t.vert[i].z && vf.nx == t.nor[i].nx && vf.ny == t.nor[i].ny && vf.nz == t.nor[i].nz && vf.u == t.tex[i].u && vf.v == t.tex[i].v) { encontrado = true; break; } verticeID++; } if (encontrado) { m_ListaIndicesFinal.Add(verticeID); } else { ///--- algo va mal Xtrace.Log("!**** Error: no se encontró un vértice al computar índices"); } } Xtrace.Log("O3D Ind {0}: ({1}, {2}, {3})", m_ListaIndicesFinal.Count / 3, m_ListaIndicesFinal[m_ListaIndicesFinal.Count - 3], m_ListaIndicesFinal[m_ListaIndicesFinal.Count - 2], m_ListaIndicesFinal[m_ListaIndicesFinal.Count - 1]); } }