Example #1
0
        /// <summary>
        /// Convert <see cref="BeginMode"/> to <see cref="GeometryTypes"/>.
        /// </summary>
        /// <param name="mode"></param>
        /// <returns></returns>
        public static GeometryTypes ToGeometryType(this PrimitiveModes mode)
        {
            GeometryTypes result = GeometryTypes.Point;

            switch (mode)
            {
            case PrimitiveModes.Points:
                result = GeometryTypes.Point;
                break;

            case PrimitiveModes.Lines:
                result = GeometryTypes.Line;
                break;

            case PrimitiveModes.LineLoop:
                result = GeometryTypes.Line;
                break;

            case PrimitiveModes.LineStrip:
                result = GeometryTypes.Line;
                break;

            case PrimitiveModes.Triangles:
                result = GeometryTypes.Triangle;
                break;

            case PrimitiveModes.TriangleStrip:
                result = GeometryTypes.Triangle;
                break;

            case PrimitiveModes.TriangleFan:
                result = GeometryTypes.Triangle;
                break;

            case PrimitiveModes.Quads:
                result = GeometryTypes.Quad;
                break;

            case PrimitiveModes.QuadStrip:
                result = GeometryTypes.Quad;
                break;

            case PrimitiveModes.Polygon:
                result = GeometryTypes.Polygon;
                break;

            default:
                throw new NotImplementedException();
            }

            return(result);
        }
Example #2
0
        /// <summary>
        /// Get the primitive of <paramref name="element"/> according to vertex's id.
        /// <para>Returns <code>null</code> if <paramref name="element"/> is null or <paramref name="stageVertexID"/> does not belong to any of this <paramref name="element"/>'s vertices.</para>
        /// <para>Note: the <paramref name="stageVertexID"/> refers to the last vertex that constructs the primitive. And it's unique in scene's all elements.</para>
        /// </summary>
        /// <typeparam name="T">Subclass of <see cref="IPickedGeometry"/></typeparam>
        /// <param name="element">the scene's element that contains the primitive.</param>
        /// <param name="mode">specifies what type of primitive it is.</param>
        /// <param name="stageVertexID">Refers to the last vertex that constructs the primitive. And it's unique in scene's all elements.</param>
        /// <param name="positions">element's vertices' position array.</param>
        /// <returns></returns>
        public static T TryPick <T>(
            this IColorCodedPicking element, PrimitiveModes mode, uint stageVertexID, float[] positions)
            where T : IPickedGeometry, new()
        {
            if (positions == null)
            {
                throw new ArgumentNullException("positions");
            }

            T pickedGeometry = element.TryPick <T>(mode, stageVertexID);

            // Fill primitive's positions and colors. This maybe changes much more than lines above in second dev.
            if (pickedGeometry != null)
            {
                uint lastVertexID;
                if (element.GetLastVertexIDOfPickedGeometry(stageVertexID, out lastVertexID))
                {
                    int vertexCount = pickedGeometry.GeometryType.GetVertexCount();
                    if (vertexCount == -1)
                    {
                        vertexCount = positions.Length / 3;
                    }
                    float[] geometryPositions = new float[vertexCount * 3];
                    uint    i = lastVertexID * 3 + 2;
                    for (int j = (geometryPositions.Length - 1); j >= 0; i--, j--)
                    {
                        if (i == uint.MaxValue)// This is when mode is GL_LINE_LOOP.
                        {
                            i = (uint)positions.Length - 1;
                        }
                        geometryPositions[j] = positions[i];
                    }

                    var poss = new vec3[vertexCount];
                    for (int t = 0; t < vertexCount; t++)
                    {
                        poss[t] = new vec3(geometryPositions[t * 3 + 0], geometryPositions[t * 3 + 1], geometryPositions[t * 3 + 2]);
                    }
                    pickedGeometry.positions = poss;
                }
            }

            return(pickedGeometry);
        }
Example #3
0
        /// <summary>
        /// Get the primitive of <paramref name="element"/> according to vertex's id.
        /// <para>Returns <code>null</code> if <paramref name="element"/> is null or <paramref name="stageVertexID"/> is not in the range of this <paramref name="element"/>.</para>
        /// <para>Note: the <paramref name="stageVertexID"/> Refers to the last vertex that constructs the primitive. And it's unique in scene's all elements.</para>
        /// <para>Note: The result's positions property is not set up as there will be different kinds of storage mode for positions(float[], IntPtr, etc). You have to initialize the positions property and fill correct position information afterwards.</para>
        /// </summary>
        /// <typeparam name="T">Sub type of <see cref="IPickedGeometry"/></typeparam>
        /// <param name="element">the scene's element that contains the primitive.</param>
        /// <param name="mode">specifies what type of primitive it is.</param>
        /// <param name="stageVertexID">Refers to the last vertex that constructs the primitive. And it's unique in scene's all elements.</param>
        /// <returns></returns>
        public static T TryPick <T>(
            this IColorCodedPicking element, PrimitiveModes mode, uint stageVertexID)
            where T : IPickedGeometry, new()
        {
            T pickedGeometry = default(T);

            if (element != null)
            {
                uint lastVertexID;
                if (element.GetLastVertexIDOfPickedGeometry(stageVertexID, out lastVertexID))
                {
                    pickedGeometry = new T();

                    pickedGeometry.GeometryType  = mode.ToGeometryType();
                    pickedGeometry.StageVertexID = stageVertexID;
                    pickedGeometry.From          = element;
                }
            }

            return(pickedGeometry);
        }
Example #4
0
        private void InitVAO()
        {
            primitiveMode = PrimitiveModes.Points;

            GL.GenVertexArrays(1, vao);
            GL.BindVertexArray(vao[0]);

            //  Create a vertex buffer for the vertex data.
            {
                UnmanagedArray <vec3> in_Position = new UnmanagedArray <vec3>(1);
                in_Position[0] = this.position;

                uint[] ids = new uint[1];
                GL.GenBuffers(1, ids);
                GL.BindBuffer(BufferTarget.ArrayBuffer, ids[0]);
                GL.BufferData(BufferTarget.ArrayBuffer, in_Position, BufferUsage.StaticDraw);
                GL.VertexAttribPointer(attributeIndexPosition, 3, GL.GL_FLOAT, false, 0, IntPtr.Zero);
                GL.EnableVertexAttribArray(attributeIndexPosition);
            }

            //  Unbind the vertex array, we've finished specifying data for it.
            GL.BindVertexArray(0);
        }
Example #5
0
 public static void Begin(PrimitiveModes primitiveMode)
 {
     GL.Begin((uint)primitiveMode);
 }
Example #6
0
        /// <summary>
        /// Get geometry's count according to specified <paramref name="mode"/>.
        /// <para>Returns false if the <paramref name="element"/> is null.</para>
        /// </summary>
        /// <param name="element"></param>
        /// <param name="mode"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public static bool GetGeometryCount(this IColorCodedPicking element, PrimitiveModes mode, out uint count)
        {
            bool result = false;

            count = uint.MaxValue;

            if (element != null)
            {
                uint vertexCount = element.GetVertexCount();

                switch (mode)
                {
                case PrimitiveModes.Points:
                    count = vertexCount;
                    break;

                case PrimitiveModes.Lines:
                    count = vertexCount / 2;
                    break;

                case PrimitiveModes.LineLoop:
                    count = vertexCount;
                    break;

                case PrimitiveModes.LineStrip:
                    count = vertexCount - 1;
                    break;

                case PrimitiveModes.Triangles:
                    count = vertexCount / 3;
                    break;

                case PrimitiveModes.TriangleStrip:
                    count = vertexCount - 2;
                    break;

                case PrimitiveModes.TriangleFan:
                    count = vertexCount - 2;
                    break;

                case PrimitiveModes.Quads:
                    count = vertexCount / 4;
                    break;

                case PrimitiveModes.QuadStrip:
                    count = vertexCount / 2 - 1;
                    break;

                case PrimitiveModes.Polygon:
                    count = 1;
                    break;

                default:
                    throw new NotImplementedException();
                }

                result = true;
            }

            return(result);
        }
Example #7
0
        /// <summary>
        /// Get geometry's index(start from 0) according to <paramref name="lastVertexID"/> and <paramref name="mode"/>.
        /// <para>Returns false if failed.</para>
        /// </summary>
        /// <param name="element"></param>
        /// <param name="mode"></param>
        /// <param name="lastVertexID">Refers to the last vertex that constructs the primitive.
        /// <para>Ranges from 0 to (<paramref name="element"/>'s vertices' count - 1).</para></param>
        /// <param name="index"></param>
        /// <returns></returns>
        public static bool GetGeometryIndex(this IColorCodedPicking element, PrimitiveModes mode, uint lastVertexID, out uint index)
        {
            index = uint.MaxValue;
            if (element == null)
            {
                return(false);
            }

            uint vertexCount = element.GetVertexCount();

            if (lastVertexID < vertexCount)
            {
                switch (mode)
                {
                case PrimitiveModes.Points:
                    // vertexID should range from 0 to vertexCount - 1.
                    index = lastVertexID;
                    break;

                case PrimitiveModes.Lines:
                    // vertexID should range from 0 to vertexCount - 1.
                    index = lastVertexID / 2;
                    break;

                case PrimitiveModes.LineLoop:
                    // vertexID should range from 0 to vertexCount.
                    if (lastVertexID == 0)     // This is the last primitive.
                    {
                        index = vertexCount - 1;
                    }
                    else
                    {
                        index = lastVertexID - 1;
                    }
                    break;

                case PrimitiveModes.LineStrip:
                    index = lastVertexID - 1;    // If lastVertexID is 0, this returns -1.
                    break;

                case PrimitiveModes.Triangles:
                    index = lastVertexID / 3;
                    break;

                case PrimitiveModes.TriangleStrip:
                    index = lastVertexID - 2;    // if lastVertexID is 0 or 1, this returns -2 or -1.
                    break;

                case PrimitiveModes.TriangleFan:
                    index = lastVertexID - 2;    // if lastVertexID is 0 or 1, this returns -2 or -1.
                    break;

                case PrimitiveModes.Quads:
                    index = lastVertexID / 4;
                    break;

                case PrimitiveModes.QuadStrip:
                    index = lastVertexID / 2 - 1;    // If lastVertexID is 0 or 1, this returns -1.
                    break;

                case PrimitiveModes.Polygon:
                    index = 0;
                    break;

                default:
                    throw new NotImplementedException();
                }
            }

            return(true);
        }
        private void InitVAO()
        {
            primitiveMode = PrimitiveModes.Points;

            GL.GenVertexArrays(1, vao);
            GL.BindVertexArray(vao[0]);

            //  Create a vertex buffer for the vertex data.
            {
                UnmanagedArray<vec3> in_Position = new UnmanagedArray<vec3>(1);
                in_Position[0] = this.position;

                uint[] ids = new uint[1];
                GL.GenBuffers(1, ids);
                GL.BindBuffer(BufferTarget.ArrayBuffer, ids[0]);
                GL.BufferData(BufferTarget.ArrayBuffer, in_Position, BufferUsage.StaticDraw);
                GL.VertexAttribPointer(attributeIndexPosition, 3, GL.GL_FLOAT, false, 0, IntPtr.Zero);
                GL.EnableVertexAttribArray(attributeIndexPosition);
            }

            //  Unbind the vertex array, we've finished specifying data for it.
            GL.BindVertexArray(0);
        }
Example #9
0
 public static void Begin(PrimitiveModes primitiveMode)
 {
     GL.Begin((uint)primitiveMode);
 }