簡體   English   中英

通過鼠標移動繪制矩形 - OpenTK

[英]Drawing rectangle by mouse movement- OpenTK

我正在嘗試通過鼠標拖動移動(右鍵)來繪制一個矩形。 一旦按下按鈕 onMouseDown 事件,我就會得到初始鼠標 position 和onMouseDown事件期間的新鼠標OnMouseMove 我不認為問題出在鼠標事件上,因為我嘗試按照相同的想法畫一條線並且效果很好。

bool mouawDown = false;

public void DrawRectangle(Vector2 oldPos, Vector2 newPos)
{
    float[] rec = new float[] { oldPos.X, oldPos.Y, newPos.X, oldPos.Y, newPos.X, newPos.Y, oldPos.X, newPos.Y};
    int VertexBufferCursor, _vao;
    _vao = GL.GenVertexArray();
    GL.BindVertexArray(_vao);
    VertexBufferCursor = GL.GenBuffer();
    GL.BindBuffer(BufferTarget.ArrayBuffer, VertexBufferCursor);
    GL.BufferData(BufferTarget.ArrayBuffer, rec.Count() * sizeof(float), rec, BufferUsageHint.StaticDraw);
    GL.VertexAttribPointer(0, 2, VertexAttribPointerType.Float, false, 2 * sizeof(float), 0);
    GL.EnableVertexAttribArray(0);
    GL.BindVertexArray(0);
    shader3.Use();
    Matrix4 model = Matrix4.Identity;
    Matrix4 projectionM = Matrix4.CreateScale(new Vector3(1f / this.Width, 1f / this.Height, 1.0f));
    projectionM = Matrix4.CreateOrthographicOffCenter(0.0f, this.Width, this.Height, 0.0f, -1.0f, 1.0f);
    GL.UniformMatrix4(0, false, ref model);
    GL.UniformMatrix4(1, false, ref projectionM);
    shader3.SetFloat("color", new Vector4(0.0f, 1.0f, 1.0f, 1.0f));
    GL.BindVertexArray(_vao);
    GL.DrawArrays(PrimitiveType.Quads, 0, 4);
    GL.BindVertexArray(0);
    shader3.Unbind();
}

鼠標事件:

    protected override void OnMouseMove(MouseMoveEventArgs e)
    {
        MouseState mstate = Mouse.GetCursorState();

        if (e.Mouse[MouseButton.Right])
        {
            mouseDown = true;
            newPos = new Vector2(e.X, e.Y);
        }
        base.OnMouseMove(e);
    }

    protected override void OnMouseDown(MouseButtonEventArgs e)
    {
        if (e.Button == MouseButton.Left)
        {
            if (locked)
            {
                mouseclick = !mouseclick;
            }
        }
        if(e.Button == MouseButton.Right)
        {
            initialPos = new Vector2(e.X, e.Y);

        }
        base.OnMouseDown(e);
    }

    protected override void OnMouseUp(MouseButtonEventArgs e)
    {
        if (e.Button == MouseButton.Right)
        {
            mouseDown = false;
        }
        base.OnMouseUp(e);
    }

渲染事件:

    protected override void OnRenderFrame(FrameEventArgs e)
    {
        if (mouseDown)
        {
            DrawRectangle(initialPos, newPos);
        }
        SwapBuffers();
        base.OnRenderFrame(e);
    }

shader3.Vert

#version 460

layout (location = 0) in vec2 in_pos;

layout (location = 0) uniform mat4 model;
layout (location = 1) uniform mat4 projection;

void main()
{
    gl_Position = projection * model * vec4(in_pos.xy, 0.0, 1.0);
}

當我有GL.DrawArrays(PrimitiveType.Linesloop, 0, 4)它正在畫一條線。 我將其更改為GL.DrawArrays(PrimitiveType.Quads, 0, 4)現在它根本沒有繪制任何東西。 如何通過鼠標拖動移動繪制矩形?

我通過繪制 2 個三角形來修復它 - 不確定Quad有什么問題

 public void DrawRectangle(Vector2 oldPos, Vector2 newPos)
    {
        float[] rec = new float[] { oldPos.X, oldPos.Y, newPos.X, oldPos.Y, newPos.X, newPos.Y, oldPos.X, newPos.Y};
        byte[] indices = new byte[] { 0, 2, 1, 0, 2, 3 };
        int VertexBufferCursor, _vao, VBO;
        VBO = GL.GenBuffer();
        GL.BindBuffer(BufferTarget.ElementArrayBuffer, VBO);
        GL.BufferData(BufferTarget.ElementArrayBuffer, indices.Count() * sizeof(byte), indices, BufferUsageHint.StaticDraw);
        VertexBufferCursor = GL.GenBuffer();
        GL.BindBuffer(BufferTarget.ArrayBuffer, VertexBufferCursor);
        GL.BufferData(BufferTarget.ArrayBuffer, rec.Count() * sizeof(float), rec, BufferUsageHint.StaticDraw);
        _vao = GL.GenVertexArray();
        GL.BindVertexArray(_vao);
        GL.EnableVertexAttribArray(0);   
        GL.VertexAttribPointer(0, 2, VertexAttribPointerType.Float, false, 2 * sizeof(float), 0);
        GL.BindVertexArray(0);
        shader3.Use();
        Matrix4 model = Matrix4.Identity;
        Matrix4 projectionM = Matrix4.CreateScale(new Vector3(1f / this.Width, 1f / this.Height, 1.0f));
        projectionM = Matrix4.CreateOrthographicOffCenter(0.0f, this.Width, this.Height, 0.0f, -1.0f, 1.0f);
        GL.UniformMatrix4(0, false, ref model);
        GL.UniformMatrix4(1, false, ref projectionM);
        shader3.SetFloat("color", new Vector4(0.0f, 1.0f, 1.0f, 1.0f));
        GL.BindVertexArray(_vao);
        GL.DrawElements(PrimitiveType.Triangles, 6, DrawElementsType.UnsignedByte, indices);
        GL.BindVertexArray(0);
        shader3.Unbind();
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM