Example #1
0
        private void Form1_Load(object sender, EventArgs e)
        {
            findResults = new List <KeyValuePair <Forge.FileEntry, EntryTreeNode> >();

            // hide the progress bar
            toolStripProgressBar.Visible = false;

            // load the games' directories into the tree view
            LoadGamesIntoTreeView();

            // set up the zoom dropdown in the Image viewer
            foreach (double z in zoomLevels)
            {
                ToolStripMenuItem item = new ToolStripMenuItem();
                item.Text   = $"{z * 100}%";
                item.Click += new EventHandler(delegate(object s, EventArgs a)
                {
                    ChangeZoom(z);
                });
                zoomDropDownButton.DropDownItems.Add(item);
            }

            // create a GLControl and a GLViewer
            GLControl glControl = new GLControl(new GraphicsMode(32, 24, 0, 4), 3, 0, GraphicsContextFlags.ForwardCompatible);

            glControl.Dock = DockStyle.Fill;
            tabPage1.Controls.Add(glControl);
            gl = new GLViewer(glControl);
            gl.BackgroundColor = Properties.Settings.Default.threeBG;
            gl.Init();

            // load the Utah teapot
            OBJMesh teapot = OBJMesh.LoadFromFile(Application.ExecutablePath + "\\..\\Shaders\\teapot.obj");

            gl.Meshes.Add(teapot);
            Console.WriteLine(teapot.GetVertices().Length);

            // create a timer to constantly render the 3D Viewer
            Timer t = new Timer();

            t.Interval = (int)(1 / 70f * 1000); // 60 FPS
            t.Tick    += new EventHandler(delegate(object s, EventArgs a) {
                gl.Render();
            });
            t.Start();

            glControl.Focus();

            // load settings
            LoadSettings();
        }
    public static OBJMesh LoadOBJMesh(string fn)
    {
        string meshName = Path.GetFileNameWithoutExtension(fn);

        bool hasNormals = false;
        //OBJ LISTS
        List <Vector3> vertices = new List <Vector3>();
        List <Vector3> normals  = new List <Vector3>();
        List <Vector2> uvs      = new List <Vector2>();
        //MESH CONSTRUCTION
        List <string>            materialNames = new List <string>();
        List <string>            objectNames   = new List <string>();
        Dictionary <string, int> hashtable     = new Dictionary <string, int>();
        List <OBJFace>           faceList      = new List <OBJFace>();
        string cmaterial = "";
        string cmesh     = "default";

        //CACHE
        Material[] materialCache = null;
        //save this info for later
        FileInfo OBJFileInfo = new FileInfo(fn);

        foreach (string ln in File.ReadAllLines(fn))
        {
            if (ln.Length > 0 && ln[0] != '#')
            {
                string   l    = ln.Trim().Replace("  ", " ");
                string[] cmps = l.Split(' ');
                string   data = l.Remove(0, l.IndexOf(' ') + 1);

                if (cmps[0] == "mtllib")
                {
                    //load cache
                    string pth = OBJGetFilePath(data, OBJFileInfo.Directory.FullName + Path.DirectorySeparatorChar, meshName);
                    if (pth != null)
                    {
                        materialCache = LoadMTLFile(pth);
                    }
                }
                else if ((cmps[0] == "g" || cmps[0] == "o") && splitByMaterial == false)
                {
                    cmesh = data;
                    if (!objectNames.Contains(cmesh))
                    {
                        objectNames.Add(cmesh);
                    }
                }
                else if (cmps[0] == "usemtl")
                {
                    cmaterial = data;
                    if (!materialNames.Contains(cmaterial))
                    {
                        materialNames.Add(cmaterial);
                    }

                    if (splitByMaterial)
                    {
                        if (!objectNames.Contains(cmaterial))
                        {
                            objectNames.Add(cmaterial);
                        }
                    }
                }
                else if (cmps[0] == "v")
                {
                    //VERTEX
                    vertices.Add(ParseVectorFromCMPS(cmps));
                }
                else if (cmps[0] == "vn")
                {
                    //VERTEX NORMAL
                    normals.Add(ParseVectorFromCMPS(cmps));
                }
                else if (cmps[0] == "vt")
                {
                    //VERTEX UV
                    uvs.Add(ParseVectorFromCMPS(cmps));
                }
                else if (cmps[0] == "f")
                {
                    int[] indexes = new int[cmps.Length - 1];
                    for (int i = 1; i < cmps.Length; i++)
                    {
                        string felement    = cmps[i];
                        int    vertexIndex = -1;
                        int    normalIndex = -1;
                        int    uvIndex     = -1;
                        if (felement.Contains("//"))
                        {
                            //doubleslash, no UVS.
                            string[] elementComps = felement.Split('/');
                            vertexIndex = int.Parse(elementComps[0]) - 1;
                            normalIndex = int.Parse(elementComps[2]) - 1;
                        }
                        else if (felement.Count(x => x == '/') == 2)
                        {
                            //contains everything
                            string[] elementComps = felement.Split('/');
                            vertexIndex = int.Parse(elementComps[0]) - 1;
                            uvIndex     = int.Parse(elementComps[1]) - 1;
                            normalIndex = int.Parse(elementComps[2]) - 1;
                        }
                        else if (!felement.Contains("/"))
                        {
                            //just vertex inedx
                            vertexIndex = int.Parse(felement) - 1;
                        }
                        else
                        {
                            //vertex and uv
                            string[] elementComps = felement.Split('/');
                            vertexIndex = int.Parse(elementComps[0]) - 1;
                            uvIndex     = int.Parse(elementComps[1]) - 1;
                        }

                        indexes[i - 1] = vertexIndex;
                    }
                    if (indexes.Length < 5 && indexes.Length >= 3)
                    {
                        OBJFace f1 = new OBJFace();
                        f1.materialName = cmaterial;
                        f1.indexes      = new int[] { indexes[0], indexes[1], indexes[2] };
                        f1.meshName     = (splitByMaterial) ? cmaterial : cmesh;
                        faceList.Add(f1);
                        if (indexes.Length > 3)
                        {
                            OBJFace f2 = new OBJFace();
                            f2.materialName = cmaterial;
                            f2.meshName     = (splitByMaterial) ? cmaterial : cmesh;
                            f2.indexes      = new int[] { indexes[2], indexes[3], indexes[0] };
                            faceList.Add(f2);
                        }
                    }
                }
            }
        }

        // return object with verts and faces
        OBJMesh finalMesh = new OBJMesh();

        finalMesh.vertices = vertices;
        finalMesh.faces    = faceList;
        return(finalMesh);
    }