示例#1
0
        /// <summary>
        /// 在三角形图元中拾取指定位置的Line
        /// </summary>
        /// <param name="arg"></param>
        /// <param name="lastVertexId">三角形图元的最后一个顶点</param>
        /// <param name="picker"></param>
        /// <returns></returns>
        internal override uint[] Search(PickingEventArgs arg,
                                        uint lastVertexId, ZeroIndexPicker picker)
        {
            // 创建临时索引
            OneIndexBuffer buffer = GLBuffer.Create(IndexBufferElementType.UInt, 6, DrawMode.Lines, BufferUsage.StaticDraw);

            unsafe
            {
                var array = (uint *)buffer.MapBuffer(MapBufferAccess.WriteOnly);
                array[0] = lastVertexId - 1; array[1] = lastVertexId - 0;
                array[2] = lastVertexId - 2; array[3] = lastVertexId - 1;
                array[4] = lastVertexId - 0; array[5] = lastVertexId - 2;
                buffer.UnmapBuffer();
            }
            // 用临时索引渲染此三角形图元(仅渲染此三角形图元)
            picker.Renderer.Render4InnerPicking(arg, buffer);
            // id是拾取到的Line的Last Vertex Id
            uint id = ColorCodedPicking.ReadStageVertexId(arg.X, arg.Y);

            buffer.Dispose();

            // 对比临时索引,找到那个Line
            if (id + 2 == lastVertexId)
            {
                return(new uint[] { id + 2, id, });
            }
            else
            {
                return(new uint[] { id - 1, id, });
            }
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="arg"></param>
        /// <param name="lastVertexId"></param>
        /// <param name="picker"></param>
        /// <returns></returns>
        internal override uint[] Search(PickingEventArgs arg,
                                        uint lastVertexId, ZeroIndexPicker picker)
        {
            OneIndexBuffer buffer = GLBuffer.Create(IndexBufferElementType.UInt, 8, DrawMode.Lines, BufferUsage.StaticDraw);

            unsafe
            {
                var array = (uint *)buffer.MapBuffer(MapBufferAccess.WriteOnly);
                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;
                buffer.UnmapBuffer();
            }
            picker.Node.Render4InnerPicking(arg, buffer);
            uint id = ColorCodedPicking.ReadStageVertexId(arg.X, arg.Y);

            buffer.Dispose();

            if (id + 3 == lastVertexId)
            {
                return(new uint[] { id + 3, id, });
            }
            else
            {
                return(new uint[] { id - 1, id, });
            }
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="arg"></param>
        /// <param name="x">mouse position(Left Down is (0, 0)).</param>
        /// <param name="y">mouse position(Left Down is (0, 0)).</param>
        /// <param name="lastVertexId"></param>
        /// <param name="modernRenderer"></param>
        /// <returns></returns>
        internal override uint[] Search(RenderEventArgs arg,
                                        int x, int y,
                                        uint lastVertexId, ZeroIndexRenderer modernRenderer)
        {
            OneIndexBuffer buffer = GLBuffer.Create(IndexBufferElementType.UInt, 6, DrawMode.Lines, BufferUsage.StaticDraw);

            unsafe
            {
                var array = (uint *)buffer.MapBuffer(MapBufferAccess.WriteOnly);
                array[0] = lastVertexId - 2; array[1] = lastVertexId - 0;
                array[2] = lastVertexId - 4; array[3] = lastVertexId - 2;
                array[4] = lastVertexId - 0; array[5] = lastVertexId - 4;
                buffer.UnmapBuffer();
            }

            modernRenderer.Render4InnerPicking(arg, buffer);
            uint id = ColorCodedPicking.ReadStageVertexId(x, y);

            buffer.Dispose();

            if (id + 4 == lastVertexId)
            {
                return(new uint[] { id + 4, id, });
            }
            else
            {
                return(new uint[] { id - 2, id, });
            }
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="arg"></param>
        /// <param name="lastVertexId"></param>
        /// <param name="picker"></param>
        /// <returns></returns>
        internal override uint[] Search(PickingEventArgs arg,
                                        uint lastVertexId, DrawArraysPicker picker)
        {
            IndexBuffer buffer = GLBuffer.Create(IndexBufferElementType.UInt, 6, BufferUsage.StaticDraw);

            unsafe
            {
                var array = (uint *)buffer.MapBuffer(MapBufferAccess.WriteOnly);
                array[0] = lastVertexId - 2; array[1] = lastVertexId - 0;
                array[2] = lastVertexId - 4; array[3] = lastVertexId - 2;
                array[4] = lastVertexId - 0; array[5] = lastVertexId - 4;
                buffer.UnmapBuffer();
            }
            var cmd = new DrawElementsCmd(buffer, DrawMode.Lines);

            picker.Node.Render4InnerPicking(arg, ControlMode.ByFrame, cmd);
            uint id = ColorCodedPicking.ReadStageVertexId(arg.X, arg.Y);

            buffer.Dispose();

            if (id + 4 == lastVertexId)
            {
                return(new uint[] { id + 4, id, });
            }
            else
            {
                return(new uint[] { id - 2, id, });
            }
        }
示例#5
0
        public IndexBuffer GetIndexBuffer()
        {
            if (this.indexBuffer == null)
            {
                int            length = this.SideLength * (this.SideLength - 1) * 2;
                OneIndexBuffer buffer = GLBuffer.Create(IndexBufferElementType.UInt, length, DrawMode.TriangleStrip, BufferUsage.StaticDraw);
                unsafe
                {
                    IntPtr pointer = buffer.MapBuffer(MapBufferAccess.WriteOnly);
                    var    array   = (uint *)pointer;
                    for (int k = 0; k < this.SideLength - 1; k++)
                    {
                        for (int i = 0; i < this.SideLength; i++)
                        {
                            if (k % 2 == 0)
                            {
                                array[(i + k * this.SideLength) * 2 + 0] = (uint)(i + (k + 1) * this.SideLength);
                                array[(i + k * this.SideLength) * 2 + 1] = (uint)(i + (k + 0) * this.SideLength);
                            }
                            else
                            {
                                array[(i + k * this.SideLength) * 2 + 0] = (uint)(this.SideLength - 1 - i + (k + 0) * this.SideLength);
                                array[(i + k * this.SideLength) * 2 + 1] = (uint)(this.SideLength - 1 - i + (k + 1) * this.SideLength);
                            }
                        }
                    }
                    buffer.UnmapBuffer();
                }
                this.indexBuffer = buffer;
            }

            return(this.indexBuffer);
        }
示例#6
0
        private void DrawInitVertices()
        {
            //Console.WriteLine(WGL.wglGetCurrentContext());
            _verticesPositionBuffer  = GLBuffer.Create();
            _verticesNormalBuffer    = GLBuffer.Create();
            _verticesTexcoordsBuffer = GLBuffer.Create();
            _verticesColorsBuffer    = GLBuffer.Create();
            _verticesWeightsBuffer   = GLBuffer.Create();
            _shader = new GLShader(
                typeof(OpenglGpuImpl).Assembly.GetManifestResourceStream("CSPspEmu.Core.Gpu.Impl.Opengl.shader.vert")
                .ReadAllContentsAsString(),
                typeof(OpenglGpuImpl).Assembly.GetManifestResourceStream("CSPspEmu.Core.Gpu.Impl.Opengl.shader.frag")
                .ReadAllContentsAsString()
                );
            Console.WriteLine("###################################");
            foreach (var uniform in _shader.Uniforms)
            {
                Console.WriteLine(uniform);
            }
            foreach (var attribute in _shader.Attributes)
            {
                Console.WriteLine(attribute);
            }
            Console.WriteLine("###################################");

            _shader.BindUniformsAndAttributes(ShaderInfo);
        }
        private void Initialize()
        {
            Smaa = new Smaa();

            var prefix = @"
				#ifndef GL_ES
				#version 330
				#endif
			"            ;

            Shader = new GLShader(
                prefix +
                "attribute vec4 position; attribute vec4 texCoords; varying vec2 v_texCoord; void main() { gl_Position = position; v_texCoord = texCoords.xy; }",
                prefix +
                "uniform sampler2D texture; varying vec2 v_texCoord; void main() { gl_FragColor = texture2D(texture, v_texCoord); }"
                );

            VertexBuffer = GLBuffer.Create()
                           .SetData(CSharpPlatform.RectangleF.FromCoords(-1, -1, +1, +1).GetFloat2TriangleStripCoords());

            Shader.BindUniformsAndAttributes(ShaderInfo);

            // TestTexture = GLTexture.Create().SetFormat(TextureFormat.RGBA).SetSize(2, 2).SetData(new uint[] { 0xFF0000FF, 0xFF00FFFF, 0xFFFF00FF, 0xFFFFFFFF });
        }
        //GLTexture TestTexture;

        public void DrawVram(bool EnableSmaa)
        {
            if (Shader == null)
            {
                Initialize();
            }

            //int RectWidth = 512;
            //int RectHeight = 272;
            var Rectangle = IGuiWindowInfo.ClientRectangle;

            GL.glClearColor(0, 0, 0, 1);
            GL.glClear(GL.GL_COLOR_BUFFER_BIT);
            //IGuiWindowInfo.SwapBuffers();
            //return;

            if (
                (PspDisplay.CurrentInfo.Enabled || PspDisplay.CurrentInfo.PlayingVideo)
                )
            {
                GetTex((Tex) =>
                {
                    //Console.Out.WriteLineColored(ConsoleColor.Red, "{0}", Tex.Texture);
                    if (Tex != null && Tex.Color.Texture != 0)
                    {
                        var TexColor = Tex.Color;
                        var TexDepth = Tex.Depth;

                        if (EnableSmaa)
                        {
                            if (TexDepth.Texture != 0)
                            {
                                TexColor = Smaa.Process(TexColor, TexDepth);
                            }
                        }

                        GL.glViewport(Rectangle.X, Rectangle.Y, Rectangle.Width, Rectangle.Height);

                        Shader.Draw(GLGeometry.GL_TRIANGLE_STRIP, 4, () =>
                        {
                            var TextureRect = CSharpPlatform.RectangleF.FromCoords(0, 0,
                                                                                   (float)PspDisplay.CurrentInfo.Width / 512f,
                                                                                   (float)PspDisplay.CurrentInfo.Height / 272f);
                            if (TextureVerticalFlip)
                            {
                                TextureRect = TextureRect.VFlip();
                            }
                            TexCoordsBuffer = GLBuffer.Create().SetData(TextureRect.GetFloat2TriangleStripCoords());

                            ShaderInfo.texture.Set(GLTextureUnit.CreateAtIndex(0).SetFiltering(GLScaleFilter.Nearest)
                                                   .SetWrap(GLWrap.ClampToEdge).SetTexture(TexColor));
                            //ShaderInfo.texture.Set(GLTextureUnit.CreateAtIndex(0).SetFiltering(GLScaleFilter.Nearest).SetWrap(GLWrap.ClampToEdge).SetTexture(TexDepth));
                            ShaderInfo.position.SetData <float>(VertexBuffer, 2);
                            ShaderInfo.texCoords.SetData <float>(TexCoordsBuffer, 2);
                        });
                    }
                });
            }

            IGuiWindowInfo.SwapBuffers();
        }