通过鼠标移动绘制矩形-OpenTK



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

阴影3.垂直

#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),现在它什么都没画。如何通过鼠标拖动来绘制矩形?

我通过画两个三角形修复了它-不确定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();
}

最新更新