|
| 1 | +using BuildPlate_Editor.Graphics; |
| 2 | +using BuildPlate_Editor.Maths; |
| 3 | +using OpenTK; |
| 4 | +using OpenTK.Graphics.OpenGL4; |
| 5 | +using System; |
| 6 | +using System.Collections.Generic; |
| 7 | +using System.Drawing; |
| 8 | +using System.Linq; |
| 9 | +using System.Text; |
| 10 | +using System.Threading.Tasks; |
| 11 | + |
| 12 | +namespace BuildPlate_Editor |
| 13 | +{ |
| 14 | + public class BlockOutline |
| 15 | + { |
| 16 | + public Vector3 Position { get; set; } |
| 17 | + |
| 18 | + public ColVertex[] vertices; |
| 19 | + public uint[] triangles; |
| 20 | + public bool Active { get; set; } |
| 21 | + |
| 22 | + |
| 23 | + protected int vao; |
| 24 | + protected int vbo; |
| 25 | + protected int ebo; |
| 26 | + |
| 27 | + public float lineWidth; |
| 28 | + public float blocksFromCam; |
| 29 | + |
| 30 | + public BlockOutline(float size, float _lineWidth, float _blocksFromCam, Color _c) |
| 31 | + { |
| 32 | + Vector4 c = new Vector4( |
| 33 | + (float)_c.R / 255f, |
| 34 | + (float)_c.G/ 255f, |
| 35 | + (float)_c.B / 255f, |
| 36 | + (float)_c.A / 255f |
| 37 | + ); |
| 38 | + Vector3 half = Vector3.One / 2f; |
| 39 | + |
| 40 | + vertices = new ColVertex[VoxelData.voxelVerts.Length]; |
| 41 | + for (int i = 0; i < vertices.Length; i++) |
| 42 | + vertices[i] = new ColVertex((VoxelData.voxelVerts[i] - half) * size, c); |
| 43 | + |
| 44 | + triangles = VoxelData.voxelLines; |
| 45 | + |
| 46 | + lineWidth = _lineWidth; |
| 47 | + blocksFromCam = _blocksFromCam; |
| 48 | + |
| 49 | + InitMesh(); |
| 50 | + |
| 51 | + Active = true; |
| 52 | + } |
| 53 | + |
| 54 | + private void InitMesh() |
| 55 | + { |
| 56 | + GL.CreateVertexArrays(1, out vao); |
| 57 | + GL.BindVertexArray(vao); |
| 58 | + GL.CreateBuffers(1, out ebo); |
| 59 | + GL.CreateBuffers(1, out vbo); |
| 60 | + UploadMesh(); |
| 61 | + } |
| 62 | + |
| 63 | + public void UploadMesh() |
| 64 | + { |
| 65 | + GL.NamedBufferData(ebo, triangles.Length * sizeof(uint), triangles, BufferUsageHint.StaticDraw); |
| 66 | + GL.VertexArrayElementBuffer(vao, ebo); |
| 67 | + |
| 68 | + int vertexBindingPoint = 0; |
| 69 | + GL.NamedBufferData(vbo, vertices.Length * ColVertex.Size, vertices, BufferUsageHint.StaticDraw); |
| 70 | + GL.VertexArrayVertexBuffer(vao, vertexBindingPoint, vbo, IntPtr.Zero, ColVertex.Size); |
| 71 | + |
| 72 | + // pos |
| 73 | + GL.VertexArrayAttribFormat(vao, 0, 3, VertexAttribType.Float, false, 0); |
| 74 | + GL.VertexArrayAttribBinding(vao, 0, vertexBindingPoint); |
| 75 | + GL.EnableVertexArrayAttrib(vao, 0); |
| 76 | + // color |
| 77 | + GL.VertexArrayAttribFormat(vao, 1, 4, VertexAttribType.Float, false, 3 * sizeof(float)); |
| 78 | + GL.VertexArrayAttribBinding(vao, 1, vertexBindingPoint); |
| 79 | + GL.EnableVertexArrayAttrib(vao, 1); |
| 80 | + } |
| 81 | + |
| 82 | + public void Update() |
| 83 | + { |
| 84 | + Vector3 dir = Camera.target - Camera.position; // is normalized |
| 85 | + Position = (Vector3i)((Camera.position + dir * blocksFromCam) + Vector3.One / 2f); |
| 86 | + } |
| 87 | + |
| 88 | + public void Render(Shader s) |
| 89 | + { |
| 90 | + if (!Active) |
| 91 | + return; |
| 92 | + |
| 93 | + Matrix4 transform = Matrix4.CreateTranslation(Position); |
| 94 | + s.UploadMat4("uTransform", ref transform); |
| 95 | + |
| 96 | + GL.BindVertexArray(vao); |
| 97 | + GL.LineWidth(lineWidth); |
| 98 | + GL.DrawElements(BeginMode.Lines, triangles.Length, DrawElementsType.UnsignedInt, 0); |
| 99 | + } |
| 100 | + } |
| 101 | +} |
0 commit comments