Exemplo n.º 1
0
        public static void LoadOBJ(MainForm _MainForm,
                                   string[] fileLines,
                                   MainForm.EGRP egrp,
                                   StreamWriter streamWriter,
                                   ref List <XVector> _Vertices,
                                   ref List <XFace> _Faces,
                                   ref List <XBrush> _Brushes,
                                   float scale,
                                   char[] separator1,
                                   char[] separator2)
        {
            _MainForm.UpdateProgress("Loading OBJ File...");

            foreach (string Line in fileLines)
            {
                _MainForm.UpdateProgress();

                string TrimmedLine = Line.Trim();

                streamWriter.WriteLine(string.Format("# OBJ Line: {0}", (object)TrimmedLine));
                if (!TrimmedLine.StartsWith("# ") && TrimmedLine.Length != 0)
                {
                    if (egrp == MainForm.EGRP.Undefined && (TrimmedLine.StartsWith("o ") || TrimmedLine.StartsWith("g ")))
                    {
                        egrp = !TrimmedLine.StartsWith("g ") ? MainForm.EGRP.Ungrouped : MainForm.EGRP.Grouped;
                    }

                    if (TrimmedLine.StartsWith("g ") && egrp == MainForm.EGRP.Grouped || TrimmedLine.StartsWith("o ") && egrp == MainForm.EGRP.Ungrouped)
                    {
                        if (_Faces.Count > 0)
                        {
                            XBrush B = new XBrush();
                            _Brushes.Add(B);
                            B.Faces = _Faces;
                        }
                        _Faces = new List <XFace>();
                    }
                    if (TrimmedLine.StartsWith("v "))
                    {
                        string[] strArray = TrimmedLine.Split(separator1, StringSplitOptions.RemoveEmptyEntries);
                        if (strArray.Length == 4)
                        {
                            XVector xvector = new XVector(double.Parse(strArray[1], new CultureInfo("en-US")), double.Parse(strArray[2], new CultureInfo("en-US")), double.Parse(strArray[3], new CultureInfo("en-US")));
                            double  num6    = xvector.y;
                            xvector.y  = -xvector.z;
                            xvector.z  = num6;
                            xvector.x *= (double)scale / 100.0;
                            xvector.y *= (double)scale / 100.0;
                            xvector.z *= (double)scale / 100.0;
                            _Vertices.Add(xvector);
                        }
                    }
                    if (TrimmedLine.StartsWith("f "))
                    {
                        XFace    xface     = new XFace();
                        string[] strArray1 = TrimmedLine.Split(separator1, StringSplitOptions.RemoveEmptyEntries);
                        for (int index = 1; index < strArray1.Length; ++index)
                        {
                            string[] strArray2 = strArray1[index].Split(separator2, StringSplitOptions.RemoveEmptyEntries);
                            xface.VertIdx.Add(int.Parse(strArray2[0]) - 1);
                        }
                        _Faces.Add(xface);
                    }
                }
            }
        }
Exemplo n.º 2
0
        private void GoButton_Click(object sender, EventArgs e)
        {
            List <bool> SavedCtrlStates = new List <bool>();

            foreach (Control C in Controls)
            {
                SavedCtrlStates.Add(C.Enabled);
                C.Enabled = false;
            }

            ProgressBar.Enabled   = true;
            ProgressLabel.Enabled = true;

            ProgressLabel.Show();
            ProgressBar.Show();
            GOButton.Hide();

            UpdateProgress("Initializing...");

            string OBJFilename = this.OBJFilename.Text;
            string MAPFilename = this.MAPFilename.Text;

            MainForm.EConvOption econvOption = MainForm.EConvOption.Standard;
            if (this.RB_Extrusion.Checked)
            {
                econvOption = MainForm.EConvOption.Extrusion;
            }
            else if (this.RB_Spikes.Checked)
            {
                econvOption = MainForm.EConvOption.Spikes;
            }
            double Depth              = double.Parse(this.DepthTextBox.Text);
            bool   bAxisAligned       = this.AxisAlignedCheckBox.Checked;
            int    NumDecimals        = Math.Max(int.Parse(this.DecimalsTextBox.Text), 0);
            float  ScaleFactor        = float.Parse(this.ScaleTextBox.Text);
            string VisibleTextureName = VisibleTextureTextBox.Text.Length > 0 ? VisibleTextureTextBox.Text : "DEFAULT";
            string HiddenTextureName  = HiddenTextureTextBox.Text.Length > 0 ? HiddenTextureTextBox.Text : "SKIP";
            bool   bCopyToClipboard   = this.CopyToClipboardCheck.Checked;
            string Classname          = this.ClassTextBox.Text;

            StreamWriter LogFile = new StreamWriter("OBJ2MAP.log");

            // Error checking

            if (!File.Exists(OBJFilename))
            {
                int num3 = (int)MessageBox.Show("OBJ file doesn't exist.");
            }
            else if ((double)ScaleFactor <= 0.0)
            {
                int num4 = (int)MessageBox.Show("Scale needs to be above 0%.");
            }
            else if (econvOption != MainForm.EConvOption.Standard && Depth < 1.0)
            {
                int num5 = (int)MessageBox.Show("Depth must be greater than 0.");
            }
            else
            {
                // Input looks good, let's go...

                LogFile.AutoFlush = true;
                LogFile.WriteLine(">>> OBJ-2-MAP v1.1 starting up. <<<");
                LogFile.WriteLine(string.Format("{0}", (object)DateTime.Now));
                LogFile.WriteLine(string.Format("OBJ Filename : {0}", (object)OBJFilename));
                StreamWriter streamWriter2 = (StreamWriter)null;
                if (MAPFilename.Length > 0)
                {
                    streamWriter2 = File.CreateText(MAPFilename);
                    LogFile.WriteLine(string.Format("MAP Filename : {0}", (object)MAPFilename));
                }
                MainForm.EGRP  egrp       = MainForm.EGRP.Undefined;
                List <XVector> _Vertices  = new List <XVector>();
                List <XFace>   list1      = new List <XFace>();
                List <XBrush>  list2      = new List <XBrush>();
                char[]         separator1 = new char[1] {
                    ' '
                };
                char[] separator2 = new char[1] {
                    '/'
                };

                string format = string.Format("F{0}", (object)NumDecimals);
                LogFile.WriteLine("");
                LogFile.WriteLine(string.Format("Method : {0}", (object)econvOption.ToString()));
                LogFile.WriteLine(string.Format("Copy To Clipboard : {0}", (object)bCopyToClipboard.ToString()));
                LogFile.WriteLine(string.Format("Depth: {0}", (object)Depth));
                LogFile.WriteLine(string.Format("Scale: {0}", (object)ScaleFactor));
                LogFile.WriteLine(string.Format("Decimal Places: {0}", (object)NumDecimals));
                LogFile.WriteLine(string.Format("Class: {0}", Classname.Length > 0 ? (object)Classname : (object)"worldspawn"));
                LogFile.WriteLine(string.Format("Visible Texture: {0}", VisibleTextureName.Length > 0 ? (object)VisibleTextureName : (object)"DEFAULT"));
                LogFile.WriteLine(string.Format("Hidden Texture: {0}", HiddenTextureName.Length > 0 ? (object)HiddenTextureName : (object)"SKIP"));
                LogFile.WriteLine("");
                LogFile.WriteLine("! Reading OBJ file into memory");

                string[] fileLines = File.ReadAllLines(OBJFilename);
                MAPCreation.LoadOBJ(this, fileLines, egrp, LogFile, ref _Vertices, ref list1, ref list2, ScaleFactor, separator1, separator2);

                if (list1.Count > 0)
                {
                    list2.Add(new XBrush()
                    {
                        Faces = list1
                    });
                }
                LogFile.WriteLine("");
                LogFile.WriteLine("Summary:");
                LogFile.WriteLine(string.Format("Vertices: {0}", (object)_Vertices.Count));
                LogFile.WriteLine(string.Format("Faces: {0}", (object)list1.Count));
                LogFile.WriteLine(string.Format("Brushes: {0}", (object)list2.Count));
                LogFile.WriteLine("");
                LogFile.WriteLine("! Computing face normals.");
                foreach (XFace xface in list1)
                {
                    UpdateProgress("Processing Faces...");
                    xface.ComputeNormal(ref _Vertices);
                    if (bAxisAligned)
                    {
                        XVector[] AxisArray = new XVector[6]
                        {
                            new XVector(1.0, 0.0, 0.0),
                            new XVector(-1.0, 0.0, 0.0),
                            new XVector(0.0, 1.0, 0.0),
                            new XVector(0.0, -1.0, 0.0),
                            new XVector(0.0, 0.0, 1.0),
                            new XVector(0.0, 0.0, -1.0)
                        };
                        int    index1 = -1;
                        double num6   = -999.0;
                        for (int index2 = 0; index2 < 6; ++index2)
                        {
                            double num7 = XVector.Dot(AxisArray[index2], xface.Normal);
                            if (num7 > num6)
                            {
                                num6   = num7;
                                index1 = index2;
                            }
                        }
                        xface.Normal = AxisArray[index1];
                    }
                }
                LogFile.WriteLine("! Beginning MAP construction.");
                string str5 = "" + "{\n" + string.Format("\"classname\" \"{0}\"\n", Classname.Length > 0 ? (object)Classname : (object)"worldspawn");

                // HACK: Fix for crash.
                foreach (var brush in list2)
                {
                    foreach (var face in brush.Faces)
                    {
                        if (face.Normal == null)
                        {
                            face.ComputeNormal(ref _Vertices);
                        }
                    }
                }

                MAPCreation.AddBrushesToMAP(this, econvOption, _Vertices, list1, list2, format, ref str5, VisibleTextureName, HiddenTextureName, Depth);

                string text4 = str5 + "}\n";
                if (streamWriter2 != null)
                {
                    streamWriter2.Write(text4);
                    streamWriter2.Close();
                }
                if (bCopyToClipboard)
                {
                    Clipboard.Clear();
                    Clipboard.SetText(text4);
                }

                SceneSettings.SettingsSave(this.MAPFilename.Text, econvOption, bCopyToClipboard, Depth, ScaleFactor, NumDecimals, Classname, VisibleTextureName, HiddenTextureName, OBJFilename, this.AxisAlignedCheckBox.Checked);
            }

            int SCSIdx = 0;

            foreach (Control C in Controls)
            {
                C.Enabled = SavedCtrlStates[SCSIdx++];
            }

            UpdateProgress(" ");
            ProgressLabel.Hide();
            ProgressBar.Hide();
            GOButton.Show();

            string str6 = "Done!" + "\n\n" + string.Format("\"{0}\" converted successfully.\n", (object)OBJFilename) + "\n";

            if (MAPFilename.Length > 0)
            {
                str6 += string.Format("Written to \"{0}\"", (object)MAPFilename);
            }
            string FinishMsg = !bCopyToClipboard ? str6 + "." : (MAPFilename.Length <= 0 ? str6 + "MAP text copied to the clipboard." : str6 + "and MAP text copied to the clipboard.");

            LogFile.WriteLine(FinishMsg);
            MessageBox.Show(FinishMsg);
            LogFile.Close();
        }