Ejemplo n.º 1
0
        public VertexBuffer(VertexContainer verticesVertexContainer)
        {
            Handle = GL.GenBuffer();
            GL.BindBuffer(BufferTarget.ArrayBuffer, Handle);

            VertexContainer = verticesVertexContainer;

            GL.BufferData(BufferTarget.ArrayBuffer, VertexContainer.GetLength() * sizeof(float), VertexContainer.GetElements(), BufferUsageHint.StaticDraw);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Use AssetData.GetAsset instead!
        /// </summary>
        /// <param name="file"></param>
        public Mesh(VertexContainer verticesContainer, uint[] indices)
        {
            VertexBuffer = new VertexBuffer(verticesContainer);

            VertexArray = new VertexArray(VertexBuffer);

            IndexBuffer = new IndexBuffer(indices);

            Bounds = verticesContainer.GetBoundingBox();

            VertexContainer = verticesContainer;
            Indices         = indices;
        }
Ejemplo n.º 3
0
        Mesh ParseObj(string[] lines)
        {
            List <Vector3> vertices  = new List <Vector3>();
            List <Vector3> normals   = new List <Vector3>();
            List <Vector2> uvs       = new List <Vector2>();
            List <uint>    vIndices  = new List <uint>();
            List <uint>    nIndices  = new List <uint>();
            List <uint>    uvIndices = new List <uint>();

            foreach (var line in lines)
            {
                string[] parts = line.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                if (parts.Length > 0)
                {
                    bool success = false;
                    switch (parts[0])
                    {
                    case "v":
                        float x, y, z;
                        success = float.TryParse(parts[1], out x);
                        if (!success)
                        {
                            Debug.Log("Failed to parse X of vertex", DebugLevel.Error);
                            break;
                        }

                        success = float.TryParse(parts[2], out y);
                        if (!success)
                        {
                            Debug.Log("Failed to parse Y of vertex", DebugLevel.Error);
                            break;
                        }

                        success = float.TryParse(parts[3], out z);
                        if (!success)
                        {
                            Debug.Log("Failed to parse Z of vertex", DebugLevel.Error);
                            break;
                        }

                        vertices.Add(new Vector3(x, y, z));
                        break;

                    case "vn":
                        float nx, ny, nz;
                        success = float.TryParse(parts[1], out nx);
                        if (!success)
                        {
                            Debug.Log("Failed to parse X of vertex", DebugLevel.Error);
                            break;
                        }

                        success = float.TryParse(parts[2], out ny);
                        if (!success)
                        {
                            Debug.Log("Failed to parse Y of vertex", DebugLevel.Error);
                            break;
                        }

                        success = float.TryParse(parts[3], out nz);
                        if (!success)
                        {
                            Debug.Log("Failed to parse Z of vertex", DebugLevel.Error);
                            break;
                        }

                        normals.Add(new Vector3(nx, ny, nz));
                        break;

                    case "vt":
                        float u, v;
                        success = float.TryParse(parts[1], out u);
                        if (!success)
                        {
                            Debug.Log("Failed to parse U of vertex", DebugLevel.Error);
                            break;
                        }


                        success = float.TryParse(parts[2], out v);
                        if (!success)
                        {
                            Debug.Log("Failed to parse V of vertex", DebugLevel.Error);
                            break;
                        }

                        uvs.Add(new Vector2(u, v));
                        break;

                    case "f":
                        int vcount = parts.Count() - 1;
                        for (int i = 0; i < vcount; i++)
                        {
                            string[] data = parts[i + 1].Split('/');

                            uint index = 0;
                            success = uint.TryParse(data[0], out index);
                            if (!success)
                            {
                                Debug.Log("Failed to parse index", DebugLevel.Error);
                                break;
                            }
                            vIndices.Add(index - 1);
                            if (data.Length > 1)
                            {
                                success = uint.TryParse(data[1], out index);
                                if (!success)
                                {
                                    Debug.Log("Failed to parse index", DebugLevel.Error);
                                    break;
                                }
                                uvIndices.Add(index - 1);
                            }
                            if (data.Length > 2)
                            {
                                success = uint.TryParse(data[2], out index);
                                if (!success)
                                {
                                    Debug.Log("Failed to parse index", DebugLevel.Error);
                                    break;
                                }
                                nIndices.Add(index - 1);
                            }
                        }
                        break;
                    }
                }
            }

            List <Vector3> Finalvertices = new List <Vector3>();
            List <Vector2> Finaluvs      = new List <Vector2>();
            List <Vector3> Finalnormals  = new List <Vector3>();
            List <uint>    indices       = new List <uint>();

            for (int i = 0; i < vIndices.Count; i++)
            {
                Finalvertices.Add(vertices[(int)vIndices[i]]);

                if (uvs.Count > 0)
                {
                    Finaluvs.Add(uvs[(int)uvIndices[i]]);
                }

                if (normals.Count > 0)
                {
                    Finalnormals.Add(normals[(int)nIndices[i]]);
                }

                indices.Add((uint)i);
            }

            VertexContainer container = null;

            if (Finalnormals.Count > 0)
            {
                container = new VertexNormalContainer(Finalvertices.ToArray(), Finaluvs.ToArray(), Finalnormals.ToArray());
            }
            else
            {
                container = new VertexContainer(Finalvertices.ToArray(), Finaluvs.ToArray());
            }

            return(new Mesh(container, indices.ToArray()));
        }