internal override uint[] Search(RenderEventArg arg,
            int x, int y,
            uint lastVertexId, ZeroIndexRenderer modernRenderer)
        {
            OneIndexBufferPtr indexBufferPtr = null;
            using (var buffer = new OneIndexBuffer<uint>(DrawMode.Lines, BufferUsage.StaticDraw))
            {
                buffer.Alloc(6);
                unsafe
                {
                    var array = (uint*)buffer.Header.ToPointer();
                    array[0] = 0; array[1] = lastVertexId - 1;
                    array[2] = lastVertexId - 1; array[3] = lastVertexId - 0;
                    array[4] = lastVertexId - 0; array[5] = 0;
                }

                indexBufferPtr = buffer.GetBufferPtr() as OneIndexBufferPtr;
            }

            modernRenderer.Render4InnerPicking(arg, indexBufferPtr);
            uint id = ColorCodedPicking.ReadPixel(x, y, arg.CanvasRect.Height);

            indexBufferPtr.Dispose();

            if (id + 1 == lastVertexId)
            { return new uint[] { 0, lastVertexId - 1, }; }
            else if (id == lastVertexId)
            { return new uint[] { lastVertexId - 1, lastVertexId - 0, }; }
            else if (id == 0)
            { return new uint[] { lastVertexId - 0, 0, }; }
            else
            { throw new Exception("This should not happen!"); }
        }
        internal override uint Search(RenderEventArg arg,
            int x, int y,
            RecognizedPrimitiveIndex lastIndexId,
            OneIndexRenderer modernRenderer)
        {
            List<uint> indexList = lastIndexId.IndexIdList;
            if (indexList.Count != 3) { throw new ArgumentException(); }

            OneIndexBufferPtr indexBufferPtr = null;
            using (var buffer = new OneIndexBuffer<uint>(DrawMode.Points, BufferUsage.StaticDraw))
            {
                buffer.Alloc(3);
                unsafe
                {
                    var array = (uint*)buffer.Header.ToPointer();
                    array[0] = indexList[0];
                    array[1] = indexList[1];
                    array[2] = indexList[2];
                }

                indexBufferPtr = buffer.GetBufferPtr() as OneIndexBufferPtr;
            }

            modernRenderer.Render4InnerPicking(arg, indexBufferPtr);
            uint id = ColorCodedPicking.ReadPixel(x, y, arg.CanvasRect.Height);

            indexBufferPtr.Dispose();

            if (id == indexList[0] || id == indexList[1] || id == indexList[2])
            { return id; }
            else
            { throw new Exception("This should not happen!"); }
        }
        internal override uint Search(RenderEventArg arg,
            int x, int y,
            uint lastVertexId, ZeroIndexRenderer modernRenderer)
        {
            OneIndexBufferPtr indexBufferPtr = null;
            using (var buffer = new OneIndexBuffer<uint>(DrawMode.Points, BufferUsage.StaticDraw))
            {
                buffer.Alloc(3);
                unsafe
                {
                    var array = (uint*)buffer.Header.ToPointer();
                    array[0] = lastVertexId - 0;
                    array[1] = lastVertexId - 2;
                    array[2] = lastVertexId - 4;
                }

                indexBufferPtr = buffer.GetBufferPtr() as OneIndexBufferPtr;
            }

            modernRenderer.Render4InnerPicking(arg, indexBufferPtr);
            uint id = ColorCodedPicking.ReadPixel(x, y, arg.CanvasRect.Height);

            indexBufferPtr.Dispose();

            if (lastVertexId - 0 == id || lastVertexId - 2 == id || lastVertexId - 4 == id)
            { return id; }
            else
            { throw new Exception("This should not happen!"); }
        }
        internal override uint[] Search(RenderEventArg arg,
            int x, int y,
            uint lastVertexId, ZeroIndexRenderer modernRenderer)
        {
            OneIndexBufferPtr indexBufferPtr = null;
            using (var buffer = new OneIndexBuffer<uint>(DrawMode.Lines, BufferUsage.StaticDraw))
            {
                buffer.Alloc(8);
                unsafe
                {
                    var array = (uint*)buffer.Header.ToPointer();
                    array[0] = lastVertexId - 1; array[1] = lastVertexId - 0;
                    array[2] = lastVertexId - 2; array[3] = lastVertexId - 1;
                    array[4] = lastVertexId - 3; array[5] = lastVertexId - 2;
                    array[6] = lastVertexId - 0; array[7] = lastVertexId - 3;
                }

                indexBufferPtr = buffer.GetBufferPtr() as OneIndexBufferPtr;
            }

            modernRenderer.Render4InnerPicking(arg, indexBufferPtr);
            uint id = ColorCodedPicking.ReadPixel(x, y, arg.CanvasRect.Height);

            indexBufferPtr.Dispose();

            if (id + 3 == lastVertexId)
            { return new uint[] { id + 3, id, }; }
            else
            { return new uint[] { id - 1, id, }; }
        }
        public override PickedGeometry Pick(RenderEventArg arg, uint stageVertexId,
            int x, int y)
        {
            uint lastVertexId;
            if (!this.GetLastVertexIdOfPickedGeometry(stageVertexId, out lastVertexId))
            { return null; }

            GeometryType geometryType = arg.PickingGeometryType;

            if (geometryType == GeometryType.Point)
            {
                DrawMode mode = this.indexBufferPtr.Mode;
                GeometryType typeOfMode = mode.ToGeometryType();
                if (typeOfMode == GeometryType.Point)
                { return PickWhateverItIs(stageVertexId, lastVertexId, mode, typeOfMode); }
                else if (typeOfMode == GeometryType.Line)
                {
                    if (this.OnPrimitiveTest(lastVertexId, mode))
                    { return PickPoint(stageVertexId, lastVertexId); }
                    else
                    { return null; }
                }
                else
                {
                    ZeroIndexPointSearcher searcher = GetPointSearcher(mode);
                    if (searcher != null)// point is from triangle, quad or polygon
                    { return SearchPoint(arg, stageVertexId, x, y, lastVertexId, searcher); }
                    else
                    { throw new Exception(string.Format("Lack of searcher for [{0}]", mode)); }
                }
            }
            else if (geometryType == GeometryType.Line)
            {
                DrawMode mode = this.indexBufferPtr.Mode;
                GeometryType typeOfMode = mode.ToGeometryType();
                if (geometryType == typeOfMode)
                { return PickWhateverItIs(stageVertexId, lastVertexId, mode, typeOfMode); }
                else
                {
                    ZeroIndexLineSearcher searcher = GetLineSearcher(mode);
                    if (searcher != null)// line is from triangle, quad or polygon
                    { return SearchLine(arg, stageVertexId, x, y, lastVertexId, searcher); }
                    else if (mode == DrawMode.Points)// want a line when rendering GL_POINTS
                    { return null; }
                    else
                    { throw new Exception(string.Format("Lack of searcher for [{0}]", mode)); }
                }
            }
            else
            {
                DrawMode mode = this.indexBufferPtr.Mode;
                GeometryType typeOfMode = mode.ToGeometryType();
                if (typeOfMode == geometryType)// I want what it is
                { return PickWhateverItIs(stageVertexId, lastVertexId, mode, typeOfMode); }
                else
                { return null; }
                //{ throw new Exception(string.Format("Lack of searcher for [{0}]", mode)); }
            }
        }
示例#6
0
 public void Render(RenderModes renderMode, Rectangle clientRectangle)
 {
     var arg = new RenderEventArg(renderMode, clientRectangle, this.Camera);
     var list = this.ObjectList.ToArray();
     foreach (var item in list)
     {
         item.Render(arg);
     }
     this.UIRoot.Render(arg);
 }
 public PickedGeometry Pick(
     RenderEventArg arg,
     uint stageVertexId,
     int x, int y)
 {
     InnerPickableRenderer renderer = this.innerPickableRenderer;
     if (renderer != null)
     { return this.innerPickableRenderer.Pick(arg, stageVertexId, x, y); }
     else
     { return null; }
 }
        internal override uint[] Search(RenderEventArg arg,
            int x, int y,
            RecognizedPrimitiveIndex lastIndexId,
            OneIndexRenderer modernRenderer)
        {
            List<uint> indexList = lastIndexId.IndexIdList;
            if (indexList.Count < 3) { throw new ArgumentException(); }

            OneIndexBufferPtr indexBufferPtr = null;
            using (var buffer = new OneIndexBuffer<uint>(DrawMode.LineLoop, BufferUsage.StaticDraw))
            {
                buffer.Alloc(indexList.Count);
                unsafe
                {
                    var array = (uint*)buffer.Header.ToPointer();
                    for (int i = 0; i < indexList.Count; i++)
                    {
                        array[i] = indexList[i];
                    }
                }

                indexBufferPtr = buffer.GetBufferPtr() as OneIndexBufferPtr;
            }

            modernRenderer.Render4InnerPicking(arg, indexBufferPtr);
            uint id = ColorCodedPicking.ReadPixel(x, y, arg.CanvasRect.Height);

            indexBufferPtr.Dispose();

            if (id == indexList[0])
            { return new uint[] { indexList[indexList.Count - 1], id, }; }
            else
            {
                uint[] result = null;
                for (int i = 1; i < indexList.Count; i++)
                {
                    if (id == indexList[i])
                    {
                        result = new uint[] { indexList[i - 1], indexList[i], };
                        break;
                    }
                }

                if (result != null)
                { return result; }
                else
                { throw new Exception("This should not happen!"); }
            }
        }
 public override void Render(RenderEventArg arg)
 {
     Renderer renderer = this.Renderer;
     if (renderer != null)
     {
         mat4 projection, view, model;
         if (this.TryGetMatrix(arg, out projection, out view, out model))
         {
             renderer.SetUniform(strprojection, projection);
             renderer.SetUniform(strview, view);
             renderer.SetUniform(strmodel, model);
             renderer.Render(arg);
         }
     }
 }
示例#10
0
        protected override void DoRender(RenderEventArg arg)
        {
            ICamera camera = arg.Camera;
            mat4 projection = this.GetOrthoProjection();
            vec3 position = (camera.Position - camera.Target).normalize();
            mat4 view = glm.lookAt(position, new vec3(0, 0, 0), camera.UpVector);
            float length = Math.Max(this.Size.Width, this.Size.Height) / 2;
            mat4 model = glm.scale(mat4.identity(),
                new vec3(length, length, length));
            Renderer renderer = this.Renderer as Renderer;
            renderer.SetUniform("projectionMatrix", projection);
            renderer.SetUniform("viewMatrix", view);
            renderer.SetUniform("modelMatrix", model);

            base.DoRender(arg);
        }
示例#11
0
        protected override void DoRender(RenderEventArg arg)
        {
            mat4 projection = this.GetOrthoProjection();
            //vec3 position = (this.camera.Position - this.camera.Target).normalize();
            mat4 view = glm.lookAt(new vec3(0, 0, 1), new vec3(0, 0, 0), new vec3(0, 1, 0));
            //float length = Math.Max(glText.Size.Width, glText.Size.Height) / 2;
            float length = this.Size.Height / 2;
            mat4 model = glm.scale(mat4.identity(), new vec3(length, length, length));
            //model = mat4.identity();
            Renderer renderer = this.Renderer as Renderer;
            renderer.SetUniform("mvp", projection * view * model);

            blendSwitch.On();

            base.DoRender(arg);

            blendSwitch.Off();
        }
示例#12
0
        //protected override void DoInitialize()
        //{
        //    base.DoInitialize();
        //    int location;
        //    location = this.shaderProgram.GetUniformLocation(projection);
        //    if (location < 0)
        //    { throw new Exception(string.Format("No uniform found for the name [{0}]", projection)); }
        //    else
        //    { this.projectionLocation = (uint)location; }
        //    location = this.shaderProgram.GetUniformLocation(view);
        //    if (location < 0)
        //    { throw new Exception(string.Format("No uniform found for the name [{0}]", view)); }
        //    else
        //    { this.viewLocation = (uint)location; }
        //    location = this.shaderProgram.GetUniformLocation(model);
        //    if (location < 0)
        //    { throw new Exception(string.Format("No uniform found for the name [{0}]", model)); }
        //    else
        //    { this.modelLocation = (uint)location; }
        //}
        protected bool TryGetMatrix(RenderEventArg arg,
            out mat4 projection, out mat4 view, out mat4 model)
        {
            projection = arg.Camera.GetProjectionMat4();
            view = arg.Camera.GetViewMat4();

            SceneObject bindingObject = this.BindingObject;
            if (bindingObject != null)
            {
                model = bindingObject.Transform.GetModelMatrix();

                return true;
            }
            else
            {
                model = new mat4();

                return false;
            }
        }
示例#13
0
        /// <summary>
        /// 在OpenGL中创建VAO。
        /// 创建的过程就是执行一次渲染的过程。
        /// </summary>
        /// <param name="arg"></param>
        /// <param name="shaderProgram"></param>
        public void Create(RenderEventArg arg, ShaderProgram shaderProgram)
        {
            if (this.ID != 0)
            { throw new Exception(string.Format("ID[{0}] is already generated!", this.ID)); }

            uint[] buffers = new uint[1];
            glGenVertexArrays(1, buffers);

            this.ID = buffers[0];

            this.Bind();
            BufferPtr[] propertyBufferPtrs = this.propertyBufferPtrs;
            if (propertyBufferPtrs != null)
            {
                foreach (var item in propertyBufferPtrs)
                {
                    item.Render(arg, shaderProgram);
                }
            }
            this.Unbind();
        }
        internal override uint Search(RenderEventArg arg,
            int x, int y,
            uint lastVertexId, ZeroIndexRenderer modernRenderer)
        {
            ZeroIndexBufferPtr zeroIndexBufferPtr = modernRenderer.IndexBufferPtr;
            ZeroIndexBufferPtr indexBufferPtr = null;
            // when the temp index buffer could be long, it's no longer needed.
            // what a great OpenGL API design!
            using (var buffer = new ZeroIndexBuffer(DrawMode.Points,
                zeroIndexBufferPtr.FirstVertex, zeroIndexBufferPtr.VertexCount))
            {
                indexBufferPtr = buffer.GetBufferPtr() as ZeroIndexBufferPtr;
            }
            modernRenderer.Render4InnerPicking(arg, indexBufferPtr);
            uint id = ColorCodedPicking.ReadPixel(x, y, arg.CanvasRect.Height);

            indexBufferPtr.Dispose();

            if (zeroIndexBufferPtr.FirstVertex <= id
                && id < zeroIndexBufferPtr.FirstVertex + zeroIndexBufferPtr.VertexCount)
            { return id; }
            else
            { throw new Exception("This should not happen!"); }
        }
示例#15
0
 /// <summary>
 /// 执行一次渲染的过程。
 /// </summary>
 /// <param name="arg"></param>
 /// <param name="shaderProgram"></param>
 /// <param name="temporaryIndexBufferPtr">render by a temporary index buffer</param>
 public void Render(RenderEventArg arg, ShaderProgram shaderProgram, IndexBufferPtr temporaryIndexBufferPtr = null)
 {
     if (temporaryIndexBufferPtr != null)
     {
         this.Bind();
         temporaryIndexBufferPtr.Render(arg, shaderProgram);
         this.Unbind();
     }
     else
     {
         IndexBufferPtr indexBufferPtr = this.IndexBufferPtr;
         if (indexBufferPtr != null)
         {
             this.Bind();
             indexBufferPtr.Render(arg, shaderProgram);
             this.Unbind();
         }
     }
 }
 internal abstract uint[] Search(RenderEventArg arg,
     int x, int y,
     RecognizedPrimitiveIndex lastIndexId,
     OneIndexRenderer modernRenderer);
 internal abstract uint[] Search(RenderEventArg arg,
     int x, int y,
     uint lastVertexId, ZeroIndexRenderer modernRenderer);
示例#18
0
 public abstract void Render(RenderEventArg arg);
        /// <summary>
        /// 在所有可能的图元(<see cref="lastVertexId"/>匹配)中,
        /// 逐个测试,找到最接近摄像机的那个图元,
        /// 返回此图元的最后一个索引在<see cref="indexBufferPtr"/>中的索引(位置)。
        /// </summary>
        /// <param name="lastIndexIdList"></param>
        /// <returns></returns>
        private RecognizedPrimitiveIndex GetLastIndexId(
            RenderEventArg arg,
            List<RecognizedPrimitiveIndex> lastIndexIdList,
            int x, int y)
        {
            if (lastIndexIdList == null || lastIndexIdList.Count == 0) { return null; }

            int current = 0;
            #if DEBUG
            NoPrimitiveRestartIndex(lastIndexIdList);
            #endif
            for (int i = 1; i < lastIndexIdList.Count; i++)
            {
                OneIndexBufferPtr twoPrimitivesIndexBufferPtr;
                uint lastIndex0, lastIndex1;
                AssembleIndexBuffer(
                    lastIndexIdList[current], lastIndexIdList[i], this.indexBufferPtr.Mode,
                    out twoPrimitivesIndexBufferPtr, out lastIndex0, out lastIndex1);
                uint pickedIndex = Pick(arg, twoPrimitivesIndexBufferPtr,
                    x, y);
                if (pickedIndex == lastIndex1)
                { current = i; }
                else if (pickedIndex == lastIndex0)
                { /* nothing to do */}
                else if (pickedIndex == uint.MaxValue)// 两个候选图元都没有被拾取到
                { /* nothing to do */}
                else
                { throw new Exception("This should not happen!"); }
            }

            return lastIndexIdList[current];
        }
        private PickedGeometry SearchPoint(RenderEventArg arg, uint stageVertexId, int x, int y, uint lastVertexId, ZeroIndexPointSearcher searcher)
        {
            PickedGeometry pickedGeometry = new PickedGeometry();
            pickedGeometry.From = this;
            pickedGeometry.GeometryType = GeometryType.Line;
            pickedGeometry.StageVertexId = stageVertexId;
            pickedGeometry.Indexes = new uint[] { searcher.Search(arg, x, y, lastVertexId, this), };
            pickedGeometry.Positions = FillPickedGeometrysPosition(pickedGeometry.Indexes);

            return pickedGeometry;
        }