Ejemplo n.º 1
0
        public Area(float x, float y, float width, float height)
        {
            //conver 0 - 1 to (-1) - 1
            x       = 2 * x - 1;
            y       = -2 * y + 1;
            width  *= 2f;
            height *= 2f;

            Vector2[] verts = new Vector2[]
            {
                new Vector2(x, y),
                new Vector2(x, y - height),
                new Vector2(x + width, y),
                new Vector2(x + width, y - height),
            };

            Vector2[] uvs = new Vector2[]
            {
                new Vector2(0, 1),
                new Vector2(0, 0),
                new Vector2(1, 1),
                new Vector2(1, 0)
            };

            Vao = new VAO(verts);
            Vao.addAttributeArray(uvs, 2, 1);
        }
Ejemplo n.º 2
0
        public static VAO LoadAsVAO(string path)
        {
            List <Vector3>    positions = new List <Vector3>();
            List <Vector2>    uvs       = new List <Vector2>();
            List <Vector3>    normals   = new List <Vector3>();
            List <VertexData> verts     = new List <VertexData>();

            hasUvs     = false;
            hasNormals = false;

            if (!File.Exists(path))
            {
                throw new FileNotFoundException();
            }

            string[] lines = File.ReadAllLines(path);

            foreach (string str in lines)
            {
                if (!string.IsNullOrWhiteSpace(str) && str.Length > 2)
                {
                    switch (str.Substring(0, 2))
                    {
                    case "v ":
                        parseVert(str, positions);
                        break;

                    case "vt":
                        parseTexture(str, uvs);
                        break;

                    case "vn":
                        parseNormal(str, normals);
                        break;

                    case "f ":
                        parseFace(str, positions, uvs, normals, verts);
                        break;
                    }
                }
            }

            Console.WriteLine("Loaded file {0}, with {1} with vertices", path, verts.Count);

            List <Vector3> p = new List <Vector3>();
            List <Vector2> u = new List <Vector2>();
            List <Vector3> n = new List <Vector3>();

            uint[] e = new uint[verts.Count];

            Dictionary <VertexData, int> vertices = new Dictionary <VertexData, int>();

            Stopwatch watch = new Stopwatch();

            watch.Start();
            for (int i = 0; i < verts.Count; i++)
            {
                if (!vertices.ContainsKey(verts[i]))
                {
                    p.Add(verts[i].position);
                    u.Add(verts[i].uv);
                    n.Add(verts[i].normal);

                    e[i] = (uint)p.Count - 1;
                    vertices.Add(verts[i], vertices.Count);
                }
                else
                {
                    e[i] = (uint)vertices[verts[i]];
                }
            }
            watch.Stop();

            Console.WriteLine(watch.ElapsedMilliseconds);

            VAO vao = new VAO(p.ToArray());

            if (hasUvs)
            {
                vao.addAttributeArray(u.ToArray(), 2, 1);
            }
            if (hasNormals)
            {
                vao.addAttributeArray(n.ToArray(), 3, 2);
            }
            vao.addElementArray(e);

            return(vao);
        }