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

Commit 21d1e30

Browse files
committed
Ported to OpenTK, much better compatibility
0 parents  commit 21d1e30

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

66 files changed

+669556
-0
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.vs/
2+
bin/
3+
obj/
4+
Properties/
5+
BuildPlate_Editor.sln

App.config

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<configuration>
3+
<startup>
4+
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8" />
5+
</startup>
6+
</configuration>

BuildPlate.cs

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
using BuildPlate_Editor.Maths;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.IO;
5+
using System.Linq;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
9+
namespace BuildPlate_Editor
10+
{
11+
[Serializable]
12+
public class BuildPlate
13+
{
14+
public List<Entity> entities;
15+
public int format_version;
16+
public List<SubChunk> sub_chunks;
17+
18+
public class SubChunk
19+
{
20+
public List<PaletteBlock> block_palette;
21+
public List<int> blocks;
22+
public PositionInt position;
23+
}
24+
25+
public class PaletteBlock
26+
{
27+
public int data;
28+
public string name;
29+
}
30+
31+
public class Entity
32+
{
33+
public int changeColor;
34+
public int multiplicitiveTintChangeColor;
35+
public string name;
36+
public PositionDouble position;
37+
public PositionDouble rotation;
38+
public PositionDouble shadowPosition;
39+
public double shadowSize;
40+
}
41+
public class PositionDouble
42+
{
43+
public double x;
44+
public double y;
45+
public double z;
46+
}
47+
public class PositionInt
48+
{
49+
public int x;
50+
public int y;
51+
public int z;
52+
53+
public static implicit operator Vector3i(PositionInt p) => new Vector3i(p.x, p.y, p.z);
54+
55+
public override string ToString() => $"X: {x}, Y: {y}, Z: {z}";
56+
}
57+
58+
public static BuildPlate Load(string path)
59+
{
60+
JsonBuildPlate jsonBuildPlate = Util.JsonDeserialize<JsonBuildPlate>(File.ReadAllText(path));
61+
string model = Util.Base64Decode(jsonBuildPlate.model);
62+
Console.WriteLine(model);
63+
File.WriteAllText("E:\\plate.plate", model);
64+
return Util.JsonDeserialize<BuildPlate>(model);
65+
}
66+
67+
public static void Save(BuildPlate bp, string path, string originalPath)
68+
{
69+
JsonBuildPlate jsonBuildPlate = Util.JsonDeserialize<JsonBuildPlate>(File.ReadAllText(originalPath));
70+
jsonBuildPlate.model = Util.Base64Encode(Util.JsonSerialize(bp));
71+
File.WriteAllText(path, Util.JsonSerialize(jsonBuildPlate));
72+
}
73+
}
74+
}

BuildPlateDimension.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace BuildPlate_Editor
8+
{
9+
[Serializable]
10+
public class BuildPlateDimension
11+
{
12+
public int x { get; set; }
13+
public int z { get; set; }
14+
}
15+
}

BuildPlateOffset.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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
9+
{
10+
[Serializable]
11+
public class BuildPlateOffset
12+
{
13+
public double x { get; set; }
14+
public double y { get; set; }
15+
public double z { get; set; }
16+
17+
public static implicit operator Vector3(BuildPlateOffset b) => new Vector3((float)b.x, (float)b.y, (float)b.z);
18+
}
19+
}

BuildPlate_Editor.csproj

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
4+
<PropertyGroup>
5+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
6+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
7+
<ProjectGuid>{01B97841-61B3-4A81-89EA-0611EF013BE4}</ProjectGuid>
8+
<OutputType>Exe</OutputType>
9+
<RootNamespace>BuildPlate_Editor</RootNamespace>
10+
<AssemblyName>BuildPlate_Editor</AssemblyName>
11+
<TargetFrameworkVersion>v4.8</TargetFrameworkVersion>
12+
<FileAlignment>512</FileAlignment>
13+
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
14+
<Deterministic>true</Deterministic>
15+
</PropertyGroup>
16+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
17+
<PlatformTarget>AnyCPU</PlatformTarget>
18+
<DebugSymbols>true</DebugSymbols>
19+
<DebugType>full</DebugType>
20+
<Optimize>false</Optimize>
21+
<OutputPath>bin\Debug\</OutputPath>
22+
<DefineConstants>DEBUG;TRACE</DefineConstants>
23+
<ErrorReport>prompt</ErrorReport>
24+
<WarningLevel>4</WarningLevel>
25+
</PropertyGroup>
26+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
27+
<PlatformTarget>AnyCPU</PlatformTarget>
28+
<DebugType>pdbonly</DebugType>
29+
<Optimize>true</Optimize>
30+
<OutputPath>bin\Release\</OutputPath>
31+
<DefineConstants>TRACE</DefineConstants>
32+
<ErrorReport>prompt</ErrorReport>
33+
<WarningLevel>4</WarningLevel>
34+
</PropertyGroup>
35+
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'">
36+
<DebugSymbols>true</DebugSymbols>
37+
<OutputPath>bin\x64\Debug\</OutputPath>
38+
<DefineConstants>DEBUG;TRACE</DefineConstants>
39+
<DebugType>full</DebugType>
40+
<PlatformTarget>x64</PlatformTarget>
41+
<LangVersion>7.3</LangVersion>
42+
<ErrorReport>prompt</ErrorReport>
43+
<Prefer32Bit>true</Prefer32Bit>
44+
</PropertyGroup>
45+
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'">
46+
<OutputPath>bin\x64\Release\</OutputPath>
47+
<DefineConstants>TRACE</DefineConstants>
48+
<Optimize>true</Optimize>
49+
<DebugType>pdbonly</DebugType>
50+
<PlatformTarget>x64</PlatformTarget>
51+
<LangVersion>7.3</LangVersion>
52+
<ErrorReport>prompt</ErrorReport>
53+
<Prefer32Bit>true</Prefer32Bit>
54+
</PropertyGroup>
55+
<ItemGroup>
56+
<Reference Include="Newtonsoft.Json, Version=13.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
57+
<HintPath>packages\Newtonsoft.Json.13.0.2\lib\net45\Newtonsoft.Json.dll</HintPath>
58+
</Reference>
59+
<Reference Include="OpenTK, Version=3.3.3.0, Culture=neutral, PublicKeyToken=bad199fe84eb3df4, processorArchitecture=MSIL">
60+
<HintPath>packages\OpenTK.3.3.3\lib\net20\OpenTK.dll</HintPath>
61+
</Reference>
62+
<Reference Include="System" />
63+
<Reference Include="System.Core" />
64+
<Reference Include="System.Drawing" />
65+
<Reference Include="System.Windows.Forms" />
66+
<Reference Include="System.Xml.Linq" />
67+
<Reference Include="System.Data.DataSetExtensions" />
68+
<Reference Include="Microsoft.CSharp" />
69+
<Reference Include="System.Data" />
70+
<Reference Include="System.Net.Http" />
71+
<Reference Include="System.Xml" />
72+
<Reference Include="SystemPlus+">
73+
<HintPath>..\SystemPlus\bin\Release\net48\SystemPlus+.dll</HintPath>
74+
</Reference>
75+
<Reference Include="WindowsFormsIntegration" />
76+
</ItemGroup>
77+
<ItemGroup>
78+
<Compile Include="BuildPlate.cs" />
79+
<Compile Include="BuildPlateDimension.cs" />
80+
<Compile Include="BuildPlateOffset.cs" />
81+
<Compile Include="Camera.cs" />
82+
<Compile Include="GLGetVersion.cs" />
83+
<Compile Include="JsonBuildPlate.cs" />
84+
<Compile Include="Maths\Vector2i.cs" />
85+
<Compile Include="Maths\Vector3i.cs" />
86+
<Compile Include="Palette.cs" />
87+
<Compile Include="Program.cs" />
88+
<Compile Include="Properties\AssemblyInfo.cs" />
89+
<Compile Include="Shader.cs" />
90+
<Compile Include="SubChunk.cs" />
91+
<Compile Include="Texture.cs" />
92+
<Compile Include="Util.cs" />
93+
<Compile Include="Vertex.cs" />
94+
<Compile Include="VoxelData.cs" />
95+
<Compile Include="Window.cs" />
96+
<Compile Include="World.cs" />
97+
</ItemGroup>
98+
<ItemGroup>
99+
<None Include="App.config" />
100+
<None Include="Data\Shaders\shader.frag">
101+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
102+
</None>
103+
<None Include="Data\Shaders\shader.vert">
104+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
105+
</None>
106+
<None Include="Data\Shaders\ui.frag">
107+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
108+
</None>
109+
<None Include="Data\Shaders\ui.vert">
110+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
111+
</None>
112+
<None Include="OpenTK.dll.config" />
113+
<None Include="packages.config" />
114+
</ItemGroup>
115+
<ItemGroup>
116+
<Content Include="Data\Textures\Black.png">
117+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
118+
</Content>
119+
</ItemGroup>
120+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
121+
</Project>

Camera.cs

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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
9+
{
10+
public static class Camera
11+
{
12+
public static Matrix4 viewMatrix;
13+
public static Matrix4 projMatrix;
14+
public static Vector3 position = new Vector3(5f, VoxelData.ChunkHeight + 5f, 5f);
15+
public static Vector3 center = new Vector3(0f, 0f, 0f);
16+
public static Vector3 up = new Vector3(0f, 1f, 0f);
17+
public static bool ortho = false;
18+
public static Vector3 Rotation;
19+
20+
private static float mod(float x, float m)
21+
{
22+
float r = x % m;
23+
return r < 0 ? r + m : r;
24+
}
25+
26+
public static void SetRotation(Vector3 rot)
27+
{
28+
Rotation = rot;
29+
}
30+
31+
public static void Move(float offset, float speed)
32+
{
33+
Vector3 move = new Vector3(0f, 0f, 1f) * speed;
34+
Matrix3 mat = Matrix3.CreateRotationY(((Rotation.Y + offset) * Util.PI) / 180f);
35+
position += move * mat;
36+
}
37+
38+
public static void UpdateView(float width, float height)
39+
{
40+
Rotation.Y = mod(Rotation.Y, 360);
41+
float x = (Rotation.X * Util.PI) / 180f;
42+
float y = (Rotation.Y * Util.PI) / 180f;
43+
Vector3 offset = new Vector3(0f, 0f, 1f);
44+
Matrix3 mat = Matrix3.CreateRotationX(x) * Matrix3.CreateRotationY(y);
45+
center = position + (offset * mat);
46+
47+
viewMatrix = Matrix4.LookAt(position, center, up);
48+
49+
if (ortho) {
50+
float projWidth = width;
51+
float aspect = width / height;
52+
float projHeight = width / aspect;
53+
54+
float left = -projWidth / 2f;
55+
float right = projWidth / 2f;
56+
float bottom = -projHeight / 2f;
57+
float top = projHeight / 2f;
58+
float near = 0.001f;
59+
float far = 10000f;
60+
61+
projMatrix = Matrix4.CreateOrthographicOffCenter(left, right, bottom, top, near, far);
62+
} else {
63+
float fov = 45f;
64+
float near = 0.001f;
65+
float far = 10000f;
66+
float aspect = width / height;
67+
projMatrix = Matrix4.CreatePerspectiveFieldOfView(DegToRad(fov), aspect, near, far);
68+
}
69+
}
70+
71+
const float PI = (float)System.Math.PI;
72+
const float PIDiv = PI / 180f;
73+
74+
static float DegToRad(float degrees)
75+
{
76+
float radians = PIDiv * degrees;
77+
return (radians);
78+
}
79+
}
80+
}

Data/Shaders/shader.frag

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#version 460 core
2+
3+
out vec4 FragColor;
4+
5+
in vec3 fUv;
6+
7+
layout (location = 5, binding = 1) uniform sampler2DArray textures;
8+
9+
void main()
10+
{
11+
vec4 col = texture(textures, fUv, 0);
12+
13+
if(col.a == 0.0)
14+
{
15+
discard;
16+
}
17+
18+
FragColor = col;
19+
}

Data/Shaders/shader.vert

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

Data/Shaders/ui.frag

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#version 460 core
2+
3+
out vec4 FragColor;
4+
5+
void main() {
6+
FragColor = vec4(0.0, 0.0, 0.0, 0.0);
7+
}

0 commit comments

Comments
 (0)