Skip to content
This repository was archived by the owner on Aug 3, 2023. It is now read-only.

Commit 2cbbc52

Browse files
committed
Editor Update
+You can now edit, no saving yet
1 parent a60d0b9 commit 2cbbc52

18 files changed

+813
-76
lines changed

BlockOutline.cs

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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+
}

BlockToPlace.cs

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Diagnostics;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading;
7+
using System.Threading.Tasks;
8+
9+
namespace BuildPlate_Editor
10+
{
11+
public static class BlockToPlace
12+
{
13+
public static bool ShouldBeTakingInput = false;
14+
private static string inputed;
15+
16+
public static Thread loopThread;
17+
18+
private static int startTop;
19+
private static int lastNumbLines;
20+
21+
public static void Init()
22+
{
23+
loopThread = new Thread(() => Loop());
24+
loopThread.Start();
25+
}
26+
27+
private static void Loop()
28+
{
29+
while (true) {
30+
start:
31+
if (!ShouldBeTakingInput)
32+
Thread.Sleep(1);
33+
34+
// get input
35+
ConsoleKeyInfo info = Console.ReadKey(true);
36+
ConsoleKey key = info.Key;
37+
38+
if (key == ConsoleKey.Enter) {
39+
ShouldBeTakingInput = false;
40+
goto start;
41+
}
42+
else if (key == ConsoleKey.Backspace && inputed.Length > 0)
43+
inputed = inputed.Substring(0, inputed.Length - 1);
44+
else
45+
inputed += info.KeyChar;
46+
47+
// clear last render
48+
Console.CursorTop = startTop;
49+
Console.CursorLeft = 0;
50+
for (int i = 0; i < lastNumbLines; i++) {
51+
Console.CursorLeft = 0;
52+
Console.WriteLine(new string(' ', Console.WindowWidth));
53+
}
54+
55+
// render
56+
Console.CursorTop = startTop;
57+
Console.CursorLeft = 0;
58+
Console.WriteLine(inputed);
59+
60+
lastNumbLines = 1;
61+
}
62+
}
63+
64+
public static string TakeInput()
65+
{
66+
inputed = string.Empty;
67+
startTop = Console.CursorTop;
68+
lastNumbLines = 0;
69+
Util.SetConsoleForeground();
70+
ShouldBeTakingInput = true;
71+
while (ShouldBeTakingInput) Thread.Sleep(1);
72+
Util.SetOpenTKForeground();
73+
return inputed;
74+
}
75+
}
76+
}

BuildPlate_Editor.csproj

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,18 @@
7979
<Reference Include="WindowsFormsIntegration" />
8080
</ItemGroup>
8181
<ItemGroup>
82+
<Compile Include="BlockOutline.cs" />
83+
<Compile Include="BlockToPlace.cs" />
8284
<Compile Include="BuildPlate.cs" />
8385
<Compile Include="BuildPlateDimension.cs" />
8486
<Compile Include="BuildPlateOffset.cs" />
8587
<Compile Include="Camera.cs" />
88+
<Compile Include="ColVertex.cs" />
8689
<Compile Include="EXITCODE.cs" />
8790
<Compile Include="Font.cs" />
8891
<Compile Include="GLGetVersion.cs" />
92+
<Compile Include="Graphics\IRenderObject.cs" />
93+
<Compile Include="Graphics\RenderObject.cs" />
8994
<Compile Include="JsonBuildPlate.cs" />
9095
<Compile Include="Maths\Vector2i.cs" />
9196
<Compile Include="Maths\Vector3i.cs" />
@@ -97,6 +102,7 @@
97102
<Compile Include="SubChunk.cs" />
98103
<Compile Include="TargaImage.cs" />
99104
<Compile Include="Texture.cs" />
105+
<Compile Include="TexVertex.cs" />
100106
<Compile Include="UI\GUI.cs" />
101107
<Compile Include="UI\GUIElement.cs" />
102108
<Compile Include="UI\IGUIElement.cs" />
@@ -123,6 +129,12 @@
123129
<None Include="Data\Fonts\Minecraft.ifh">
124130
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
125131
</None>
132+
<None Include="Data\Shaders\colShader.frag">
133+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
134+
</None>
135+
<None Include="Data\Shaders\colShader.vert">
136+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
137+
</None>
126138
<None Include="Data\Shaders\skybox.frag">
127139
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
128140
</None>

Camera.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public static class Camera
1212
public static Matrix4 viewMatrix;
1313
public static Matrix4 projMatrix;
1414
public static Vector3 position = new Vector3(5f, VoxelData.ChunkHeight + 5f, 5f);
15-
public static Vector3 center = new Vector3(0f, 0f, 0f);
15+
public static Vector3 target = new Vector3(0f, 0f, 0f);
1616
public static Vector3 up = new Vector3(0f, 1f, 0f);
1717
public static bool ortho = false;
1818
public static Vector3 Rotation;
@@ -42,9 +42,9 @@ public static void UpdateView(float width, float height)
4242
float y = (Rotation.Y * Util.PI) / 180f;
4343
Vector3 offset = new Vector3(0f, 0f, 1f);
4444
Matrix3 mat = Matrix3.CreateRotationX(x) * Matrix3.CreateRotationY(y);
45-
center = position + (offset * mat);
45+
target = position + (offset * mat);
4646

47-
viewMatrix = Matrix4.LookAt(position, center, up);
47+
viewMatrix = Matrix4.LookAt(position, target, up);
4848

4949
if (ortho) {
5050
float projWidth = width;

ColVertex.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using OpenTK;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Runtime.InteropServices;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
9+
namespace BuildPlate_Editor
10+
{
11+
[StructLayout(LayoutKind.Sequential)]
12+
public struct ColVertex
13+
{
14+
public const int Size = 7 * sizeof(float);
15+
16+
public Vector3 position;
17+
public Vector4 color;
18+
19+
public ColVertex(Vector3 _pos, Vector4 _color)
20+
{
21+
position = _pos;
22+
color = _color;
23+
}
24+
}
25+
}

Data/Shaders/colShader.frag

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#version 450 core
2+
3+
out vec4 FragColor;
4+
5+
in vec4 fCol;
6+
7+
void main()
8+
{
9+
if(fCol.a == 0.0)
10+
{
11+
discard;
12+
}
13+
14+
FragColor = fCol;
15+
}

Data/Shaders/colShader.vert

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#version 450 core
2+
layout (location = 0) in vec3 aPosition;
3+
layout (location = 1) in vec4 aCol;
4+
5+
out vec4 fCol;
6+
7+
uniform mat4 uTransform;
8+
uniform mat4 uProjection;
9+
uniform mat4 uView;
10+
11+
void main()
12+
{
13+
fCol = aCol;
14+
gl_Position = uProjection * uView * (uTransform * vec4(aPosition, 1.0));
15+
}

Data/Shaders/shader.frag

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ void main()
1010
{
1111
vec4 col = texture(textures, fUv, 0);
1212

13-
if(col.a == 0.0)
13+
if(col.a < 0.5)
1414
{
1515
discard;
1616
}

EXITCODE.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ public enum EXITCODE : int
1010
{
1111
Normal = 0,
1212
OpenGL_LowVersion = 1,
13-
World_Load_TextureArray = 2,
14-
World_Render_Block = 3,
15-
World_Unknown = 4,
13+
World_Load_TextureArray = 4,
14+
World_ReLoad_TextureArray = 5,
15+
World_Render_Block = 10,
16+
World_Unknown = 20,
1617
}
1718
}

Graphics/IRenderObject.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using OpenTK;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
8+
namespace BuildPlate_Editor.Graphics
9+
{
10+
public interface IRenderObject
11+
{
12+
Vector3 Position { get; }
13+
bool Active { get; set; }
14+
15+
void Render(Shader s);
16+
}
17+
}

0 commit comments

Comments
 (0)