Ejemplo n.º 1
0
        /// <summary>
        /// Create a basic cube with normals and stores it in a VAO.
        /// This cube consists of 12 triangles and 6 faces.
        /// </summary>
        /// <param name="min">The 3 minimum values of the cube (lower left back corner).</param>
        /// <param name="max">The 3 maximum values of the cube (top right front corner).</param>
        /// <returns></returns>
        public static Mesh CreateCube(Vector3f min, Vector3f max)
        {
            Vector3f[] vertices = new Vector3f[] {
                new Vector3f(min.X, min.Y, max.Z),
                new Vector3f(max.X, min.Y, max.Z),
                new Vector3f(min.X, max.Y, max.Z),
                new Vector3f(max.X, max.Y, max.Z),
                new Vector3f(max.X, min.Y, min.Z),
                new Vector3f(max.X, max.Y, min.Z),
                new Vector3f(min.X, max.Y, min.Z),
                new Vector3f(min.X, min.Y, min.Z)
            };

            int[] indices = new int[] {
                0, 1, 2, 1, 3, 2,
                1, 4, 3, 4, 5, 3,
                4, 7, 5, 7, 6, 5,
                7, 0, 6, 0, 2, 6,
                7, 4, 0, 4, 1, 0,
                2, 3, 6, 3, 5, 6
            };

            Vector3f[] normals = CalculateNormals(vertices, indices);


            MeshEntry cube = new MeshEntry();

            cube.BaseIndex  = 0;
            cube.BaseVertex = 0;
            cube.NumIndices = indices.Length;
            cube.Name       = "Cube";

            // Create the VAO
            uint _vao = GL.GenVertexArray();

            GL.BindVertexArray(_vao);

            VBO <Vector3f> vertex  = new VBO <Vector3f>(vertices);
            VBO <Vector3f> normal  = new VBO <Vector3f>(normals);
            VBO <int>      element = new VBO <int>(indices, BufferTarget.ElementArrayBuffer, BufferUsageHint.StaticRead);

            GL.BindBuffer(vertex.BufferTarget, vertex.ID);
            GL.EnableVertexAttribArray(GL.VERTEX_POSITION_LOCATION);
            GL.VertexAttribPointer(GL.VERTEX_POSITION_LOCATION, vertex.Size, vertex.PointerType, false, 0, 0);

            GL.BindBuffer(normal.BufferTarget, normal.ID);
            GL.EnableVertexAttribArray(GL.VERTEX_NORMAL_LOCATION);
            GL.VertexAttribPointer(GL.VERTEX_NORMAL_LOCATION, normal.Size, normal.PointerType, false, 0, 0);

            GL.BindBuffer(element.BufferTarget, element.ID);

            // Make sure this VAO is not modified from the outside
            GL.BindVertexArray(0);

            // Add a dispose event to delete the generated VAO
            Resources.ResourcesManager.AddOnDisposeEvent(() => GL.DeleteVertexArrays(1, _vao));

            return(new Mesh(_vao, cube));
        }
Ejemplo n.º 2
0
        public static Mesh CreatePlane(Vector2f location, Sizef size)
        {
            Vector3f[] vertices = new Vector3f[]
            {
                new Vector3f(location.X, 0, location.Y),
                new Vector3f(location.X + size.Width, 0, location.Y),
                new Vector3f(location.X + size.Width, 0, location.Y + size.Height),
                new Vector3f(location.X, 0, location.Y + size.Height)
            };

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

            int[] indices = new int[] { 2, 1, 0, 0, 3, 2 };

            Vector3f[] normals = CalculateNormals(vertices, indices);

            MeshEntry plane = new MeshEntry();

            plane.BaseIndex  = 0;
            plane.BaseVertex = 0;
            plane.NumIndices = indices.Length;
            plane.Name       = "Plane";

            // Create the VAO
            uint _vao = GL.GenVertexArray();

            GL.BindVertexArray(_vao);

            VBO <Vector3f> vertex  = new VBO <Vector3f>(vertices);
            VBO <Vector2f> texture = new VBO <Vector2f>(uvs);
            VBO <Vector3f> normal  = new VBO <Vector3f>(normals);
            VBO <int>      element = new VBO <int>(indices, BufferTarget.ElementArrayBuffer, BufferUsageHint.StaticRead);

            GL.BindBuffer(vertex.BufferTarget, vertex.ID);
            GL.EnableVertexAttribArray(GL.VERTEX_POSITION_LOCATION);
            GL.VertexAttribPointer(GL.VERTEX_POSITION_LOCATION, vertex.Size, vertex.PointerType, false, 0, 0);

            GL.BindBuffer(texture.BufferTarget, texture.ID);
            GL.EnableVertexAttribArray(GL.VERTEX_NORMAL_LOCATION);
            GL.VertexAttribPointer(GL.VERTEX_NORMAL_LOCATION, texture.Size, texture.PointerType, false, 0, 0);

            GL.BindBuffer(normal.BufferTarget, normal.ID);
            GL.EnableVertexAttribArray(GL.VERTEX_NORMAL_LOCATION);
            GL.VertexAttribPointer(GL.VERTEX_NORMAL_LOCATION, normal.Size, normal.PointerType, false, 0, 0);

            GL.BindBuffer(element.BufferTarget, element.ID);

            // Make sure this VAO is not modified from the outside
            GL.BindVertexArray(0);

            // Add a dispose event to delete the generated VAO
            Resources.ResourcesManager.AddOnDisposeEvent(() => GL.DeleteVertexArrays(1, _vao));

            return(new Mesh(_vao, plane));
        }
Ejemplo n.º 3
0
 public Mesh(uint vao, MeshEntry entry)
 {
     _vao   = vao;
     _entry = entry;
 }