【问题标题】:Drawing rectangle by mouse movement- OpenTK通过鼠标移动绘制矩形 - OpenTK
【发布时间】:2020-04-28 15:00:18
【问题描述】:

我正在尝试通过鼠标拖动移动(右键)来绘制一个矩形。一旦按下按钮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】:

    我通过绘制 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();
        }
    

    【讨论】:

      猜你喜欢
      • 2019-12-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多