2019-09-12 22:52:47 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using OpenTK;
|
|
|
|
|
using OpenTK.Graphics.OpenGL;
|
|
|
|
|
|
|
|
|
|
namespace LayoutBXLYT
|
|
|
|
|
{
|
|
|
|
|
public class RenderablePane
|
|
|
|
|
{
|
|
|
|
|
int vbo_position;
|
2019-09-28 21:27:48 +00:00
|
|
|
|
int ibo_position;
|
2019-09-12 22:52:47 +00:00
|
|
|
|
|
|
|
|
|
public struct Vertex
|
|
|
|
|
{
|
2019-09-28 21:27:48 +00:00
|
|
|
|
public Vector2 Position;
|
2019-09-12 22:52:47 +00:00
|
|
|
|
public Vector4 Color;
|
|
|
|
|
public Vector2 TexCoord0;
|
|
|
|
|
public Vector2 TexCoord1;
|
|
|
|
|
public Vector2 TexCoord2;
|
|
|
|
|
|
2019-09-28 21:27:48 +00:00
|
|
|
|
public static int SizeInBytes = 4 * (2 + 4 + 2 + 2 + 2);
|
2019-09-12 22:52:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-09-28 21:27:48 +00:00
|
|
|
|
private void GenerateBuffers(Vector2[] positions, Vector4[] colors, Vector2[] texCoords0)
|
2019-09-12 22:52:47 +00:00
|
|
|
|
{
|
|
|
|
|
GL.GenBuffers(1, out vbo_position);
|
2019-09-28 21:27:48 +00:00
|
|
|
|
GL.GenBuffers(1, out ibo_position);
|
|
|
|
|
|
2019-09-12 22:52:47 +00:00
|
|
|
|
UpdateVertexData(positions, colors, texCoords0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Destroy()
|
|
|
|
|
{
|
2019-09-28 21:27:48 +00:00
|
|
|
|
bool buffersWereInitialized = vbo_position != 0 && ibo_position != 0;
|
2019-09-12 22:52:47 +00:00
|
|
|
|
if (!buffersWereInitialized)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
GL.DeleteBuffer(vbo_position);
|
2019-09-28 21:27:48 +00:00
|
|
|
|
GL.DeleteBuffer(vbo_position);
|
2019-09-12 22:52:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Vertex[] Vertices;
|
2019-09-28 21:27:48 +00:00
|
|
|
|
ushort[] Indices = new ushort[] { 0, 1, 2, 3 };
|
2019-09-12 22:52:47 +00:00
|
|
|
|
|
2019-09-28 21:27:48 +00:00
|
|
|
|
public void Render(BxlytShader shader, Vector2[] positions, Vector4[] colors, Vector2[] texCoords0)
|
2019-09-12 22:52:47 +00:00
|
|
|
|
{
|
2019-09-28 21:27:48 +00:00
|
|
|
|
shader.Enable();
|
|
|
|
|
|
|
|
|
|
bool buffersWereInitialized = vbo_position != 0 && ibo_position != 0;
|
2019-09-12 22:52:47 +00:00
|
|
|
|
if (!buffersWereInitialized)
|
|
|
|
|
GenerateBuffers(positions, colors, texCoords0);
|
|
|
|
|
|
2019-09-28 21:27:48 +00:00
|
|
|
|
shader.EnableVertexAttributes();
|
2019-09-12 22:52:47 +00:00
|
|
|
|
GL.BindBuffer(BufferTarget.ArrayBuffer, vbo_position);
|
2019-09-28 21:27:48 +00:00
|
|
|
|
GL.VertexAttribPointer(shader.GetAttribute("vPosition"), 2, VertexAttribPointerType.Float, false, Vertex.SizeInBytes, 0);
|
|
|
|
|
GL.VertexAttribPointer(shader.GetAttribute("vColor"), 4, VertexAttribPointerType.Float, false, Vertex.SizeInBytes, 8);
|
|
|
|
|
GL.VertexAttribPointer(shader.GetAttribute("vTexCoord0"), 2, VertexAttribPointerType.Float, false, Vertex.SizeInBytes, 24);
|
|
|
|
|
GL.VertexAttribPointer(shader.GetAttribute("vTexCoord1"), 2, VertexAttribPointerType.Float, false, Vertex.SizeInBytes, 32);
|
|
|
|
|
GL.VertexAttribPointer(shader.GetAttribute("vTexCoord2"), 2, VertexAttribPointerType.Float, false, Vertex.SizeInBytes, 40);
|
|
|
|
|
GL.BindBuffer(BufferTarget.ElementArrayBuffer, ibo_position);
|
|
|
|
|
GL.DrawElements(PrimitiveType.Quads, 4, DrawElementsType.UnsignedShort, IntPtr.Zero);
|
|
|
|
|
shader.DisableVertexAttributes();
|
2019-09-12 22:52:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-09-28 21:27:48 +00:00
|
|
|
|
public void UpdateVertexData(Vector2[] positions, Vector4[] colors, Vector2[] texCoords0)
|
2019-09-12 22:52:47 +00:00
|
|
|
|
{
|
|
|
|
|
Vertices = new Vertex[positions.Length];
|
|
|
|
|
for (int v = 0; v < Vertices.Length; v++)
|
|
|
|
|
{
|
|
|
|
|
Vertices[v] = new Vertex();
|
|
|
|
|
Vertices[v].Position = positions[v];
|
|
|
|
|
Vertices[v].Color = colors[v];
|
|
|
|
|
Vertices[v].TexCoord0 = texCoords0[v];
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-28 21:27:48 +00:00
|
|
|
|
GL.BindBuffer(BufferTarget.ElementArrayBuffer, ibo_position);
|
|
|
|
|
GL.BufferData(
|
|
|
|
|
BufferTarget.ElementArrayBuffer,
|
|
|
|
|
(IntPtr)(Indices.Length * sizeof(ushort)),
|
|
|
|
|
Indices,
|
|
|
|
|
BufferUsageHint.StaticDraw);
|
|
|
|
|
|
2019-09-12 22:52:47 +00:00
|
|
|
|
GL.BindBuffer(BufferTarget.ArrayBuffer, vbo_position);
|
|
|
|
|
GL.BufferData<Vertex>(BufferTarget.ArrayBuffer,
|
|
|
|
|
new IntPtr(Vertices.Length * Vertex.SizeInBytes),
|
|
|
|
|
Vertices, BufferUsageHint.StaticDraw);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|