Example #1
0
        /// <summary>
        /// Create a sphere.
        /// </summary>
        /// <param name="radius">
        /// A <see cref="Single"/> that specifies the radius of the sphere.
        /// </param>
        /// <param name="slices">
        /// A <see cref="Int32"/> that specifies the number of horizontal subdivisions of the sphere.
        /// </param>
        /// <param name="stacks">
        /// A <see cref="Int32"/> that specifies the number of vertical subdivisions of the sphere.
        /// </param>
        /// <returns>
        /// It returns a <see cref="VertexArrayObject"/> defining the following semantics:
        /// - Positions
        /// - Normals
        /// </returns>
        public static VertexArrayObject CreateSphere(float radius, int slices, int stacks)
        {
            VertexArrayObject vertexArray = new VertexArrayObject();

            // Vertex generation
            Vertex3f[] position, normal;
            ushort[]   indices;
            int        vertexCount;

            GenerateSphere(radius, slices, stacks, out position, out normal, out indices, out vertexCount);

            // Buffer definition
            ArrayBufferObject <Vertex3f> positionBuffer = new ArrayBufferObject <Vertex3f>(BufferObjectHint.StaticCpuDraw);

            positionBuffer.Create(position);
            vertexArray.SetArray(positionBuffer, VertexArraySemantic.Position);

            ArrayBufferObject <Vertex3f> normalBuffer = new ArrayBufferObject <Vertex3f>(BufferObjectHint.StaticCpuDraw);

            normalBuffer.Create(normal);
            vertexArray.SetArray(normalBuffer, VertexArraySemantic.Normal);

            ElementBufferObject <ushort> elementBuffer = new ElementBufferObject <ushort>(BufferObjectHint.StaticCpuDraw);

            elementBuffer.Create(indices);
            vertexArray.SetElementArray(PrimitiveType.TriangleStrip, elementBuffer);

            return(vertexArray);
        }
            /// <summary>
            /// Ensure that all required resources are created.
            /// </summary>
            /// <param name="ctx">
            /// The <see cref="GraphicsContext"/> used for allocating resources.
            /// </param>
            public override void Create(GraphicsContext ctx)
            {
                CheckCurrentContext(ctx);

                if (ArrayIndices.Exists(ctx) == false)
                {
                    ArrayIndices.Create(ctx);
                }
            }
Example #3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="dx"></param>
        /// <param name="dy"></param>
        /// <returns></returns>
        public static VertexArrayObject CreatePlane(float x, float y, float z, uint dx, uint dy)
        {
            VertexArrayObject vertexArray = new VertexArrayObject();

            // Vertex generation
            Vertex3f[] position = new Vertex3f[(dx + 1) * (dy + 1)];
            float      x2 = x / 2.0f, y2 = y / 2.0f;
            float      vdx = x / dx, vdy = y / dy;
            int        vidx = 0;

            for (float vy = -y2; vy <= y2; vy += vdy)
            {
                for (float vx = -x2; vx <= x2; vx += vdx)
                {
                    Debug.Assert(vidx < position.Length);
                    position[vidx++] = new Vertex3f(vx, vy, z);
                }
            }

            // Elements generation
            List <uint> indices = new List <uint>();
            uint        vstride = dx + 1;

            for (uint i = 0; i < dy; i++)
            {
                uint yoffset = i * vstride;

                // Triangle strip start
                indices.Add(yoffset + vstride);

                for (uint ix = 0; ix < dx; ix++)
                {
                    uint xoffset = yoffset + ix;

                    indices.Add(xoffset);
                    indices.Add(xoffset + vstride + 1);
                }

                indices.Add(yoffset + vstride - 1);

                if (Gl.CurrentExtensions.PrimitiveRestart == false)
                {
                    throw new NotImplementedException();
                }
                else
                {
                    indices.Add(uint.MaxValue);
                }
            }

            // Buffer definition
            ArrayBufferObject <Vertex3f> positionBuffer = new ArrayBufferObject <Vertex3f>(BufferObjectHint.StaticCpuDraw);

            positionBuffer.Create(position);
            vertexArray.SetArray(positionBuffer, VertexArraySemantic.Position);

            ElementBufferObject <uint> elementBuffer = new ElementBufferObject <uint>(BufferObjectHint.StaticCpuDraw);

            elementBuffer.Create(indices.ToArray());
            elementBuffer.RestartIndexEnabled = Gl.CurrentExtensions.PrimitiveRestart;
            vertexArray.SetElementArray(PrimitiveType.TriangleStrip, elementBuffer);

            return(vertexArray);
        }