/// <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> /// Set a buffer object which specify the element arrays. /// </summary> /// <param name="mode"> /// A <see cref="PrimitiveType"/> that specify how arrays elements are interpreted. /// </param> /// <param name="bufferObject"> /// A <see cref="ElementBufferObject"/> that specify a sequence of indices that defines the /// array element sequence. /// </param> /// <param name="offset"> /// A <see cref="UInt32"/> that specify the offset applied to the drawn elements indices. /// </param> /// <param name="count"> /// A <see cref="UInt32"/> that specify the number of element indices drawn. /// </param> public void SetElementArray(PrimitiveType mode, ElementBufferObject bufferObject, uint offset, uint count) { if (bufferObject == null) { throw new ArgumentNullException("bufferObject"); } // Store element array _Elements.Add(new IndexedElement(this, mode, bufferObject, offset, count)); }
/// <summary> /// Specify which elements shall be drawn by indexing them, specifying an offset and the number of element indices. /// </summary> /// <param name="vao"> /// The <see cref="VertexArrayObject"/> to which this element belongs to. /// </param> /// <param name="mode"> /// A <see cref="PrimitiveType"/> that indicates how array elements are interpreted. /// </param> /// <param name="indices"> /// A <see cref="ElementBufferObject"/> containing the indices of the drawn vertices. /// </param> /// <param name="offset"> /// A <see cref="UInt32"/> that specify the offset applied to the drawn elements indices. /// </param> /// <param name="count"> /// A <see cref="UInt32"/> that specify the number of element indices drawn. /// </param> /// <exception cref="ArgumentNullException"> /// Exception thrown if <paramref name="indices"/> is null. /// </exception> public IndexedElement(VertexArrayObject vao, PrimitiveType mode, ElementBufferObject indices, uint offset, uint count) : base(vao, mode, offset, count) { if (indices == null) { throw new ArgumentNullException("indices"); } ArrayIndices = indices; ArrayIndices.IncRef(); }
/// <summary> /// Set a buffer object which specify the element arrays. /// </summary> /// <param name="mode"> /// A <see cref="Primitive"/> that specify how arrays elements are interpreted. /// </param> /// <param name="bufferObject"> /// A <see cref="ElementBufferObject"/> that specify a sequence of indices that defines the /// array element sequence. /// </param> public int SetElementArray(PrimitiveType mode, ElementBufferObject bufferObject) { if (bufferObject == null) { throw new ArgumentNullException("bufferObject"); } // Store element array _Elements.Add(new IndexedElement(this, mode, bufferObject)); return(_Elements.Count - 1); }
/// <summary> /// Specify which elements shall be drawn by indexing them, specifying an offset and the number of element indices. /// </summary> /// <param name="mode"> /// A <see cref="PrimitiveType"/> that indicates how array elements are interpreted. /// </param> /// <param name="indices"> /// A <see cref="ElementBufferObject"/> containing the indices of the drawn vertices. /// </param> /// <exception cref="ArgumentNullException"> /// Exception thrown if <paramref name="indices"/> is null. /// </exception> /// <remarks> /// The element indices count is implictly defined by <paramref name="indices"/> at <see cref="Draw(GraphicsContext)"/> /// execution time. /// </remarks> public IndexedElement(VertexArrayObject vao, PrimitiveType mode, ElementBufferObject indices) : this(vao, mode, indices, 0, 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); }