public static void ConvertObjFileToDMesh(ObjFile obj_file, DMesh dm, bool units_inches, string obj_file_name, Editor editor, TextureManager tex_manager)
        {
            dm.Init(editor);

            // Convert the verts
            foreach (var v in obj_file.Vertices)
            {
                // Flip Z for coordinate system translation
                                #if DMESH_EDITOR
                dm.AddVertexEditor(new Vector3(v.X, v.Y, -v.Z), false);
                                #else
                dm.AddVertex(v.X, v.Y, -v.Z);
                                #endif
            }

            if (units_inches)
            {
                for (int i = 0; i < dm.vertex.Count; i++)
                {
                    dm.vertex[i] *= 0.0254f;
                }
            }

            // Convert the textures
            for (var i = 0; i < obj_file.Materials.Count; i++)
            {
                var    material    = obj_file.Materials[i];
                string tex_name    = material.Diffuse.Texture;
                string tex_id_name = tex_name != null?Path.GetFileNameWithoutExtension(tex_name) : material.Name;

                string decal_tex_name = Path.Combine(editor.m_filepath_decal_textures, tex_id_name + ".png");

                if (tex_name != null)
                {
                    if (!CopyImportTextureToDecalFolder(obj_file_name, tex_name, editor.m_filepath_decal_textures))
                    {
                        Utility.DebugLog("No texture imported/updated: " + tex_name);
                    }
                    else
                    {
                        Utility.DebugLog("Imported/updated a texture: " + tex_name);
                    }
                }
                else if (!File.Exists(decal_tex_name))
                {
                    using (var bmp = new Bitmap(1, 1, PixelFormat.Format32bppArgb)) {
                        bmp.SetPixel(0, 0, material.Diffuse.Color);
                        bmp.Save(decal_tex_name, ImageFormat.Png);
                    }
                }

                if (tex_manager.FindTextureIDByName(tex_id_name) < 0)
                {
                    // Get the texture (if it exists)
                    if (File.Exists(decal_tex_name))
                    {
                        tex_manager.AddTexture(decal_tex_name);
                    }
                    else
                    {
                        // Try to steal it from the level directory instead
                        string level_tex_name = Path.Combine(editor.m_filepath_level_textures, tex_id_name + ".png");
                        if (File.Exists(level_tex_name))
                        {
                            // Copy it to the decal textures directory, then load it
                            if (!File.Exists(decal_tex_name))
                            {
                                File.Copy(level_tex_name, decal_tex_name);

                                tex_manager.AddTexture(decal_tex_name);
                            }
                        }
                        else
                        {
                            editor.AddOutputText("IMPORT WARNING: No PNG file could be found matching the name: " + tex_id_name);
                        }
                    }
                }

                dm.AddTexture(i, tex_id_name);
            }

            // Convert the faces
            var vrt_idx = new int[3];
            var uv      = new Vector2[3];
            var nrml    = new Vector3[3];
            var tris    = new List <ObjFile.FaceVert[]>();
            foreach (var f in obj_file.Faces)
            {
                // Triangulate face
                tris.Clear();
                var fv = f.FaceVerts;
                if (fv.Length == 3)
                {
                    tris.Add(fv);
                }
                else if (fv.Length == 4 &&
                         Vector3.Subtract(obj_file.Vertices[fv[0].VertIdx], obj_file.Vertices[fv[2].VertIdx]).LengthSquared >
                         Vector3.Subtract(obj_file.Vertices[fv[1].VertIdx], obj_file.Vertices[fv[3].VertIdx]).LengthSquared)
                {
                    tris.Add(new [] { fv[0], fv[1], fv[3] });
                    tris.Add(new [] { fv[1], fv[2], fv[3] });
                }
                else                     // assume convex...
                {
                    for (int i = 1; i < fv.Length - 1; i++)
                    {
                        tris.Add(new [] { fv[0], fv[i], fv[i + 1] });
                    }
                }

                foreach (var tri in tris)
                {
                    for (var i = 0; i < 3; i++)
                    {
                        // Flip Z/V and reverse vertex order for coordinate system translation
                        int fi = 2 - i;
                        vrt_idx[i] = tri[fi].VertIdx;
                        nrml[i]    = obj_file.Normals[tri[fi].NormIdx];
                        nrml[i].Z  = -nrml[i].Z;
                        uv[i]      = tri[fi].UVIdx >= 0 ? obj_file.UVs[tri[fi].UVIdx] : new Vector2();
                        uv[i].Y    = 1.0f - uv[i].Y;
                    }
                    dm.AddFace(vrt_idx, nrml, uv, f.MatIdx);
                }
            }
        }
Exemplo n.º 2
0
        private void Editor_Load(object sender, EventArgs e)
        {
            m_dmesh        = new DMesh("default_dmesh");
            tm_decal       = new TextureManager(this);
            texture_list   = new TextureList(this);
            uv_editor      = new UVEditor(this);
            tunnel_builder = new TunnelBuilder(this);
            dmesh_browser  = new DMeshBrowser(this);
            NewDMesh();
            UndoInit();
            KeyPreview = true;

            // Init views and view labels
            Size sz = gl_panel.Size;

            sz.Height -= 5;
            sz.Width  -= 5;

            // Create GL views and labels
            for (int i = 0; i < (int)ViewType.NUM; i++)
            {
                gl_view[i]        = new GLView((ViewType)i, this);
                gl_view[i].Parent = gl_panel;
                switch (i)
                {
                case 0: gl_view[i].Location = new Point(0, 0); break;

                case 1: gl_view[i].Location = new Point(sz.Width / 2, 0); break;

                case 2: gl_view[i].Location = new Point(0, sz.Height / 2); break;

                case 3: gl_view[i].Location = new Point(sz.Width / 2, sz.Height / 2); break;
                }
                gl_view[i].Size = new Size(sz.Width / 2, sz.Height / 2);
                gl_view[i].Show();
            }

            for (int i = 0; i < (int)ViewType.NUM; i++)
            {
                label_viewport[i]           = new Label();
                label_viewport[i].Parent    = gl_panel;
                label_viewport[i].Location  = new Point(gl_view[i].Location.X + 2, gl_view[i].Location.Y + 2);
                label_viewport[i].AutoSize  = true;
                label_viewport[i].Text      = ((ViewType)i).ToString();
                label_viewport[i].BackColor = GLView.C_bg;
                label_viewport[i].ForeColor = Color.White;
                label_viewport[i].Show();
                gl_panel.Controls.Add(label_viewport[i]);
                label_viewport[i].BringToFront();
            }

            // Could make this save/load?
            WindowState = FormWindowState.Maximized;

            ResetViews();

            UpdateDirectories();
            LoadPreferences();
            UpdateOptionLabels();
            RefreshGrid();
            ResetViews();
            InitChecklists();

            ComputerInfo ci           = new ComputerInfo();
            ulong        total_memory = (ulong)ci.TotalPhysicalMemory;
            bool         low_mem      = total_memory < 5000000000; // 5 GB

            if (m_low_res_force || low_mem)
            {
                m_low_res_textures = true;
            }
            tm_decal.LoadTexturesInDir(m_filepath_decal_textures, false, false);
            tm_decal.LoadTexturesInDir(m_filepath_level_textures, false, false);
            texture_list.InitImageLists();
            texture_list.Hide();

            uv_editor.Hide();
            tunnel_builder.Hide();
        }
        public static bool ImportOBJToDMesh(DMesh dm, string obj_file_name, bool units_inches, Editor editor, TextureManager tex_manager)
        {
            if (!File.Exists(obj_file_name))
            {
                return(false);
            }

            ObjFile obj_file = new ObjFile();

            obj_file.Load(obj_file_name);

            ConvertObjFileToDMesh(obj_file, dm, units_inches, obj_file_name, editor, tex_manager);

            return(true);
        }