鼠标移动绘制矩形- OpenTK。

问题描述 投票:0回答:1

我试图通过鼠标拖动(右键)来绘制一个矩形。当按钮被按下时,我得到了鼠标的初始位置。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) 现在却什么也画不出来。如何用鼠标拖动画出一个矩形?

c# opengl opentk
1个回答
0
投票

我通过绘制两个三角形来解决这个问题--不知道哪里出了问题。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();
    }
© www.soinside.com 2019 - 2024. All rights reserved.