Ejemplo n.º 1
0
        private void PickingLastLineInLineLoop(out uint[] vertexIds, out vec3[] positions)
        {
            const int vertexCount = 2;

            VertexBuffer[] buffers = this.Node.PickingRenderMethod.PositionBuffers;
            int            sum     = (from item in buffers select item.Length).Sum();

            vertexIds = new uint[vertexCount] {
                (uint)(sum - 1), 0
            };
            IEnumerable <IndexesInBuffer> workItems = buffers.GetWorkItems(vertexIds);
            var positionList = new List <vec3>();

            foreach (var item in workItems)
            {
                VertexBuffer buffer  = buffers[item.whichBuffer];
                IntPtr       pointer = buffer.MapBuffer(MapBufferAccess.ReadOnly);
                unsafe
                {
                    var array = (vec3 *)pointer.ToPointer();
                    foreach (var indexInBuffer in item.indexesInBuffer)
                    {
                        positionList.Add(array[indexInBuffer]);
                    }
                }
                buffer.UnmapBuffer();
            }

            if (positionList.Count != vertexCount)
            {
                throw new Exception();
            }

            positions = positionList.ToArray();
        }
        /// <summary>
        /// Move vertexes' position accroding to difference on screen.
        /// <para>根据<paramref name="differenceOnScreen"/>来修改指定索引处的顶点位置。</para>
        /// </summary>
        /// <param name="differenceOnScreen"></param>
        /// <param name="viewMatrix"></param>
        /// <param name="projectionMatrix"></param>
        /// <param name="viewport"></param>
        /// <param name="positionIndexes"></param>
        /// <returns></returns>
        public vec3[] MovePositions(ivec2 differenceOnScreen,
                                    mat4 viewMatrix, mat4 projectionMatrix, vec4 viewport, params uint[] positionIndexes)
        {
            var list = new vec3[positionIndexes.Length];

            VertexBuffer buffer  = this.PickingRenderUnit.PositionBuffer;
            IntPtr       pointer = buffer.MapBuffer(MapBufferAccess.ReadWrite);

            unsafe
            {
                mat4 modelMatrix     = this.GetModelMatrix();
                mat4 modelViewMatrix = viewMatrix * modelMatrix;
                var  array           = (vec3 *)pointer.ToPointer();
                foreach (uint index in positionIndexes)
                {
                    vec3 windowPos    = glm.project(array[index], modelViewMatrix, projectionMatrix, viewport);
                    var  newWindowPos = new vec3(
                        windowPos.x + differenceOnScreen.x,
                        windowPos.y + differenceOnScreen.y,
                        windowPos.z);
                    vec3 result = glm.unProject(newWindowPos, modelViewMatrix, projectionMatrix, viewport);
                    array[index] = result;
                    list[index]  = result;
                }
            }
            buffer.UnmapBuffer();

            return(list);
        }
Ejemplo n.º 3
0
        public CSharpGL.VertexBuffer GetVertexAttributeBuffer(string bufferName, string varNameInShader)
        {
            if ((bufferName == position))
            {
                if ((this.positionBuffer == null))
                {
                    int          length = this.pointCount;
                    VertexBuffer buffer = VertexBuffer.Create(typeof(vec3), length, VBOConfig.Vec3, varNameInShader, BufferUsage.StaticDraw);
                    unsafe
                    {
                        var    random  = new Random();
                        IntPtr pointer = buffer.MapBuffer(MapBufferAccess.WriteOnly);
                        var    array   = (vec3 *)pointer;
                        for (int i = 0; i < this.pointCount; i++)
                        {
                            array[i] = (new vec3((float)random.NextDouble(), (float)random.NextDouble(), (float)random.NextDouble()) - new vec3(0.5f, 0.5f, 0.5f)) * this.Lengths;
                        }
                        buffer.UnmapBuffer();
                    }

                    this.positionBuffer = buffer;
                }
                return(positionBuffer);
            }
            else
            {
                throw new System.ArgumentException("bufferName");
            }
        }
Ejemplo n.º 4
0
        protected vec3[] FillPickedGeometrysPosition(uint firstIndex, int indexCount)
        {
            VertexBuffer buffer = this.Renderer.PositionBuffer;
            int          offset = (int)(firstIndex * buffer.Config.GetDataSize() * buffer.Config.GetDataTypeByteLength());
            //IntPtr pointer = GL.MapBuffer(BufferTarget.ArrayBuffer, MapBufferAccess.ReadOnly);
            IntPtr pointer = buffer.MapBufferRange(
                offset,
                indexCount * buffer.Config.GetDataSize() * buffer.Config.GetDataTypeByteLength(),
                MapBufferRangeAccess.MapReadBit);
            var positions = new vec3[indexCount];

            if (pointer.ToInt64() != 0)
            {
                unsafe
                {
                    var array = (vec3 *)pointer.ToPointer();
                    for (uint i = 0; i < indexCount; i++)
                    {
                        positions[i] = array[i];
                    }
                }
            }
            else
            {
                ErrorCode error = (ErrorCode)GL.Instance.GetError();
                if (error != ErrorCode.NoError)
                {
                    throw new Exception(string.Format(
                                            "Error:[{0}] glMapBufferRange failed: buffer ID: [{1}]", error, buffer.BufferId.ToString()));
                }
            }
            buffer.UnmapBuffer();

            return(positions);
        }
        /// <summary>
        /// Move vertexes' position accroding to <paramref name="modelSpacePositionDiff"/>.
        /// <para>根据<paramref name="modelSpacePositionDiff"/>来修改指定索引处的顶点位置。</para>
        /// </summary>
        /// <param name="modelSpacePositionDiff"></param>
        /// <param name="positionIndexes"></param>
        /// <returns></returns>
        public vec3[] MovePositions(vec3 modelSpacePositionDiff, IEnumerable <uint> positionIndexes)
        {
            var buffers = this.PickingRenderMethod.PositionBuffers;
            IEnumerable <IndexesInBuffer> workItems = buffers.GetWorkItems(positionIndexes);

            var list = new List <vec3>();

            foreach (var item in workItems)
            {
                VertexBuffer buffer  = buffers[item.whichBuffer];
                IntPtr       pointer = buffer.MapBuffer(MapBufferAccess.ReadWrite);
                unsafe
                {
                    var array = (vec3 *)pointer.ToPointer();
                    foreach (var indexInBuffer in item.indexesInBuffer)
                    {
                        array[indexInBuffer] = array[indexInBuffer] + modelSpacePositionDiff;
                        list.Add(array[indexInBuffer]);
                    }
                }
                buffer.UnmapBuffer();
            }

            return(list.ToArray());
        }
Ejemplo n.º 6
0
        private void PickingLastLineInLineLoop(out uint[] vertexIds, out vec3[] positions)
        {
            const int    vertexCount = 2;
            VertexBuffer buffer      = this.Renderer.PickingRenderUnit.PositionBuffer;
            var          offsets     = new int[vertexCount] {
                (buffer.VertexCount - 1) * buffer.Config.GetDataSize() * buffer.Config.GetDataTypeByteLength(), 0,
            };

            vertexIds = new uint[vertexCount];
            positions = new vec3[vertexCount];
            buffer.Bind();
            for (int i = 0; i < vertexCount; i++)
            {
                IntPtr pointer = buffer.MapBufferRange(
                    offsets[i],
                    1 * buffer.Config.GetDataSize() * buffer.Config.GetDataTypeByteLength(),
                    MapBufferRangeAccess.MapReadBit, false);
                unsafe
                {
                    var array = (vec3 *)pointer.ToPointer();
                    positions[i] = array[0];
                }
                buffer.UnmapBuffer(false);
                vertexIds[i] = (uint)offsets[i] / (uint)(buffer.Config.GetDataSize() * buffer.Config.GetDataTypeByteLength());
            }
            buffer.Unbind();
        }
Ejemplo n.º 7
0
        /// <summary>
        /// start from (0, 0) to the right.
        /// </summary>
        /// <param name="text"></param>
        /// <param name="server"></param>
        /// <param name="totalWidth"></param>
        /// <param name="totalHeight"></param>
        unsafe private void PositionPass(string text, GlyphServer server,
                                         out float totalWidth, out float totalHeight)
        {
            int          textureWidth  = server.TextureWidth;
            int          textureHeight = server.TextureHeight;
            VertexBuffer buffer        = this.positionBuffer;
            var          positionArray = (QuadStruct *)buffer.MapBuffer(MapBufferAccess.ReadWrite);
            const float  height        = 2.0f; // let's say height is 2.0f.

            totalWidth  = 0;
            totalHeight = height;
            int index = 0;

            foreach (var c in text)
            {
                GlyphInfo glyphInfo;
                float     wByH = 0;
                if (server.GetGlyphInfo(c, out glyphInfo))
                {
                    float w = (glyphInfo.quad.rightBottom.x - glyphInfo.quad.leftBottom.x) * textureWidth;
                    float h = (glyphInfo.quad.rightBottom.y - glyphInfo.quad.rightTop.y) * textureHeight;
                    wByH = height * w / h;
                }
                else
                {
                    // put an empty glyph(square) here.
                    wByH = height * 1.0f / 1.0f;
                }

                var leftTop     = new vec2(totalWidth, 1f);
                var leftBottom  = new vec2(totalWidth, -1f);
                var rightBottom = new vec2(totalWidth + wByH, -1f);
                var rightTop    = new vec2(totalWidth + wByH, 1f);
                positionArray[index++] = new QuadStruct(leftTop, leftBottom, rightBottom, rightTop);
                totalWidth            += wByH;
            }

            // move to center.
            const float scale = 1f;

            for (int i = 0; i < text.Length; i++)
            {
                QuadStruct quad   = positionArray[i];
                var        newPos = new QuadStruct(
                    // y is already in [-1, 1], so just shrink x to [-1, 1]
                    new vec2(quad.leftTop.x / totalWidth * 2.0f - 1f, quad.leftTop.y) * scale,
                    new vec2(quad.leftBottom.x / totalWidth * 2.0f - 1f, quad.leftBottom.y) * scale,
                    new vec2(quad.rightBottom.x / totalWidth * 2.0f - 1f, quad.rightBottom.y) * scale,
                    new vec2(quad.rightTop.x / totalWidth * 2.0f - 1f, quad.rightTop.y) * scale
                    );

                positionArray[i] = newPos;
            }

            buffer.UnmapBuffer();
        }
Ejemplo n.º 8
0
        private VertexBuffer GetPositionBuffer()
        {
            bool         initialized = false;
            vec3         max         = new vec3();
            vec3         min         = new vec3();
            int          uCount      = GetUCount(interval);
            int          vCount      = GetVCount(interval);
            int          length      = uCount * vCount;
            VertexBuffer buffer      = VertexBuffer.Create(typeof(vec3), length, VBOConfig.Vec3, BufferUsage.StaticDraw);

            unsafe
            {
                IntPtr pointer = buffer.MapBuffer(MapBufferAccess.WriteOnly);
                var    array   = (vec3 *)pointer;
                int    index   = 0;
                for (int uIndex = 0; uIndex < uCount; uIndex++)
                {
                    for (int vIndex = 0; vIndex < vCount; vIndex++)
                    {
                        double u        = Math.PI * uIndex / uCount;
                        double v        = Math.PI * 2 * vIndex / vCount;
                        var    position = GetPosition(u, v);

                        if (!initialized)
                        {
                            max         = position;
                            min         = position;
                            initialized = true;
                        }
                        else
                        {
                            position.UpdateMax(ref max);
                            position.UpdateMin(ref min);
                        }
                        array[index++] = position;
                    }
                }
                //this.Lengths = max - min;
                vec3 worldPosition = max / 2.0f + min / 2.0f;
                for (int i = 0; i < index; i++)
                {
                    array[i] = array[i] - worldPosition;
                }
                buffer.UnmapBuffer();
            }

            return(buffer);
        }
Ejemplo n.º 9
0
        /// <summary>
        /// start from (0, 0) to the right.
        /// </summary>
        /// <param name="text"></param>
        /// <param name="server"></param>
        /// <param name="totalWidth"></param>
        /// <param name="totalHeight"></param>
        unsafe private void PositionPass(string text, GlyphServer server,
                                         out float totalWidth, out float totalHeight)
        {
            int          textureWidth  = server.TextureWidth;
            int          textureHeight = server.TextureHeight;
            VertexBuffer buffer        = this.positionBuffer;
            var          positionArray = (QuadPositionStruct *)buffer.MapBuffer(MapBufferAccess.ReadWrite);
            const float  height        = 2; // 2 is the height value in clip space.

            totalWidth  = 0;
            totalHeight = height;
            int index = 0;

            foreach (var c in text)
            {
                if (index >= this.textModel.Capacity)
                {
                    break;
                }

                GlyphInfo glyphInfo;
                float     wByH = 0;
                if (server.GetGlyphInfo(c, out glyphInfo))
                {
                    float w = (glyphInfo.quad.rightBottom.x - glyphInfo.quad.leftBottom.x) * textureWidth;
                    float h = (glyphInfo.quad.rightBottom.y - glyphInfo.quad.rightTop.y) * textureHeight;
                    wByH = height * w / h;
                }
                else
                {
                    // put an empty glyph(square) here.
                    wByH = height * 1.0f / 1.0f;
                }

                var leftTop     = new vec2(totalWidth, height);
                var leftBottom  = new vec2(totalWidth, 0);
                var rightBottom = new vec2(totalWidth + wByH, 0);
                var rightTop    = new vec2(totalWidth + wByH, height);
                positionArray[index++] = new QuadPositionStruct(leftTop, leftBottom, rightBottom, rightTop);

                totalWidth += wByH;
            }

            buffer.UnmapBuffer();
        }
Ejemplo n.º 10
0
        /// <summary>
        /// start from (0, 0) to the right.
        /// </summary>
        /// <param name="text"></param>
        /// <param name="server"></param>
        unsafe private void UVPass(string text, GlyphServer server)
        {
            VertexBuffer buffer  = this.uvBuffer;
            var          uvArray = (QuadStruct *)buffer.MapBuffer(MapBufferAccess.WriteOnly);
            int          index   = 0;

            foreach (var c in text)
            {
                GlyphInfo glyphInfo;
                if (server.GetGlyphInfo(c, out glyphInfo))
                {
                    uvArray[index] = glyphInfo.quad;
                }

                index++;
            }

            buffer.UnmapBuffer();
        }
Ejemplo n.º 11
0
        /// <summary>
        /// start from (0, 0) to the right.
        /// </summary>
        /// <param name="text"></param>
        /// <param name="server"></param>
        unsafe private void TextureIndexPass(string text, GlyphServer server)
        {
            VertexBuffer buffer            = this.textureIndexBuffer;
            var          textureIndexArray = (float *)buffer.MapBuffer(MapBufferAccess.WriteOnly);
            int          index             = 0;

            foreach (var c in text)
            {
                GlyphInfo glyphInfo;
                if (server.GetGlyphInfo(c, out glyphInfo))
                {
                    textureIndexArray[index] = glyphInfo.textureIndex;
                }

                index++;
            }

            buffer.UnmapBuffer();
        }
Ejemplo n.º 12
0
        private VertexBuffer GetTexCoordBuffer()
        {
            int          uCount = GetUCount(interval);
            int          length = uCount;
            VertexBuffer buffer = VertexBuffer.Create(typeof(float), length, VBOConfig.Float, BufferUsage.StaticDraw);

            unsafe
            {
                IntPtr pointer = buffer.MapBuffer(MapBufferAccess.WriteOnly);
                int    index   = 0;
                var    array   = (float *)pointer;
                for (int uIndex = 0; uIndex < uCount; uIndex++)
                {
                    array[index++] = (float)uIndex / (float)uCount;
                }
                buffer.UnmapBuffer();
            }

            return(buffer);
        }
Ejemplo n.º 13
0
        /// <summary>
        /// Move vertexes' position accroding to <paramref name="modelSpacePositionDiff"/>.
        /// <para>根据<paramref name="modelSpacePositionDiff"/>来修改指定索引处的顶点位置。</para>
        /// </summary>
        /// <param name="modelSpacePositionDiff"></param>
        /// <param name="positionIndexes"></param>
        /// <returns></returns>
        public IList <vec3> MovePositions(vec3 modelSpacePositionDiff, IEnumerable <uint> positionIndexes)
        {
            var list = new List <vec3>();

            VertexBuffer buffer  = this.PickingRenderUnit.PositionBuffer;
            IntPtr       pointer = buffer.MapBuffer(MapBufferAccess.ReadWrite);

            unsafe
            {
                var array = (vec3 *)pointer.ToPointer();
                foreach (uint index in positionIndexes)
                {
                    array[index] = array[index] + modelSpacePositionDiff;
                    list.Add(array[index]);
                }
            }
            buffer.UnmapBuffer();

            return(list);
        }
Ejemplo n.º 14
0
        /// <summary>
        /// Move vertexes' position accroding to <paramref name="modelSpacePositionDiff"/>.
        /// <para>根据<paramref name="modelSpacePositionDiff"/>来修改指定索引处的顶点位置。</para>
        /// </summary>
        /// <param name="modelSpacePositionDiff"></param>
        /// <param name="positionIndexes"></param>
        /// <returns></returns>
        public vec3[] MovePositions(vec3 modelSpacePositionDiff, params uint[] positionIndexes)
        {
            var list = new vec3[positionIndexes.Length];

            VertexBuffer buffer  = this.PickingRenderUnit.PositionBuffer;
            IntPtr       pointer = buffer.MapBuffer(MapBufferAccess.ReadWrite);

            unsafe
            {
                var array = (vec3 *)pointer.ToPointer();
                int t     = 0;
                foreach (uint index in positionIndexes)
                {
                    array[index] = array[index] + modelSpacePositionDiff;
                    list[t++]    = array[index];
                }
            }
            buffer.UnmapBuffer();

            return(list);
        }
Ejemplo n.º 15
0
        protected vec3[] FillPickedGeometrysPosition(uint[] indexes)
        {
            var positions = new vec3[indexes.Length];

            VertexBuffer buffer = this.Renderer.PositionBuffer;

            buffer.Bind();
            for (int i = 0; i < indexes.Length; i++)
            {
                int offset = (int)(indexes[i] * buffer.Config.GetDataSize() * buffer.Config.GetDataTypeByteLength());
                //IntPtr pointer = GL.MapBuffer(BufferTarget.ArrayBuffer, MapBufferAccess.ReadOnly);
                IntPtr pointer = buffer.MapBufferRange(
                    offset,
                    1 * buffer.Config.GetDataSize() * buffer.Config.GetDataTypeByteLength(),
                    MapBufferRangeAccess.MapReadBit, false);
                if (pointer.ToInt64() != 0)
                {
                    unsafe
                    {
                        var array = (vec3 *)pointer.ToPointer();
                        positions[i] = array[0];
                    }
                }
                else
                {
                    ErrorCode error = (ErrorCode)GL.Instance.GetError();
                    if (error != ErrorCode.NoError)
                    {
                        Debug.WriteLine(string.Format(
                                            "Error:[{0}] glMapBufferRange failed: buffer ID: [{1}]", error, buffer.BufferId.ToString()));
                    }
                }
                buffer.UnmapBuffer(false);
            }
            buffer.Unbind();

            return(positions);
        }
Ejemplo n.º 16
0
        protected vec3[] FillPickedGeometrysPosition(uint[] positionIndexes)
        {
            VertexBuffer[] buffers = this.Node.PickingRenderMethod.PositionBuffers;
            IEnumerable <IndexesInBuffer> workItems = buffers.GetWorkItems(positionIndexes);
            var positions = new List <vec3>();

            foreach (var item in workItems)
            {
                VertexBuffer buffer  = buffers[item.whichBuffer];
                IntPtr       pointer = buffer.MapBuffer(MapBufferAccess.ReadOnly);
                unsafe
                {
                    var array = (vec3 *)pointer.ToPointer();
                    foreach (var indexInBuffer in item.indexesInBuffer)
                    {
                        positions.Add(array[indexInBuffer]);
                    }
                }
                buffer.UnmapBuffer();
            }

            return(positions.ToArray());
        }
Ejemplo n.º 17
0
        /// <summary>
        /// start from (0, 0) to the right.
        /// </summary>
        /// <param name="text"></param>
        /// <param name="server"></param>
        unsafe private void UVPass(string text, GlyphServer server)
        {
            VertexBuffer buffer            = this.stringBuffer;
            var          textureIndexArray = (QuadSTRStruct *)buffer.MapBuffer(MapBufferAccess.WriteOnly);
            int          index             = 0;

            foreach (var c in text)
            {
                if (index >= this.labelModel.Capacity)
                {
                    break;
                }

                GlyphInfo glyphInfo;
                if (server.GetGlyphInfo(c, out glyphInfo))
                {
                    textureIndexArray[index] = glyphInfo.quad;
                }

                index++;
            }

            buffer.UnmapBuffer();
        }
Ejemplo n.º 18
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="bufferName"></param>
 /// <param name="varNameInShader"></param>
 /// <returns></returns>
 public VertexBuffer GetVertexAttributeBuffer(string bufferName, string varNameInShader)
 {
     if (bufferName == strPosition)
     {
         if (this.positionBuffer == null)
         {
             int          length = 1;
             VertexBuffer buffer = VertexBuffer.Create(typeof(CubeModel.CubePosition), length, VBOConfig.Vec3, varNameInShader, BufferUsage.StaticDraw);
             unsafe
             {
                 IntPtr pointer = buffer.MapBuffer(MapBufferAccess.ReadWrite);
                 {
                     var array = (CubeModel.CubePosition *)pointer;
                     array[0] = CubeModel.position;
                 }
                 {
                     var array = (vec3 *)pointer;
                     for (int i = 0; i < 24; i++)
                     {
                         array[i] = array[i] / 2 * Lengths;
                     }
                 }
                 buffer.UnmapBuffer();
             }
             this.positionBuffer = buffer;
         }
         return(this.positionBuffer);
     }
     else if (bufferName == strColor)
     {
         if (this.colorBuffer == null)
         {
             //int length = 1;
             //VertexBuffer buffer = VertexBuffer.Create(typeof(CubeModel.CubeColor), length, VBOConfig.Vec3, varNameInShader, BufferUsage.StaticDraw);
             //unsafe
             //{
             //    IntPtr pointer = buffer.MapBuffer(MapBufferAccess.WriteOnly);
             //    var array = (CubeModel.CubeColor*)pointer;
             //    array[0] = CubeModel.color;
             //    buffer.UnmapBuffer();
             //}
             //this.colorBuffer = buffer;
             // another way to do this:
             this.colorBuffer = CubeModel.color.GenVertexBuffer(VBOConfig.Vec3, varNameInShader, BufferUsage.StaticDraw);
         }
         return(this.colorBuffer);
     }
     else if (bufferName == strNormal)
     {
         if (this.normalBuffer == null)
         {
             //int length = 1;
             //VertexBuffer buffer = VertexBuffer.Create(typeof(CubeModel.CubeNormal), length, VBOConfig.Vec3, varNameInShader, BufferUsage.StaticDraw);
             //unsafe
             //{
             //    IntPtr pointer = buffer.MapBuffer(MapBufferAccess.WriteOnly);
             //    var array = (CubeModel.CubeNormal*)pointer;
             //    array[0] = CubeModel.normal;
             //    buffer.UnmapBuffer();
             //}
             //this.normalBuffer = buffer;
             // another way to do this:
             this.normalBuffer = CubeModel.normal.GenVertexBuffer(VBOConfig.Vec3, varNameInShader, BufferUsage.StaticDraw);
         }
         return(this.normalBuffer);
     }
     else
     {
         return(null);
     }
 }