Search code examples
c#openglopentk

Drawing rectangle by mouse movement- OpenTK


I am trying to draw a rectangle by mouse drag movement(right button). I get the initial mouse position once the button is pressed onMouseDown event and the new mouse position during OnMouseMove event. I don't think the issue is with the mouse events as I tried drawing a line following the same idea and it worked well.

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();
}

Mouse events:

    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);
    }

Render event:

    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);
}

When I had GL.DrawArrays(PrimitiveType.Linesloop, 0, 4) it was drawing one line. I changed it to GL.DrawArrays(PrimitiveType.Quads, 0, 4) and now it is not drawing anything at all. How can I draw a rectangle by mouse drag movement?


Solution

  • I fixed it by drawing 2 triangles- not sure what's wrong with 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();
        }