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

Commit b30a01d

Browse files
committed
Added skybox
1 parent 3b64e46 commit b30a01d

File tree

13 files changed

+420
-25
lines changed

13 files changed

+420
-25
lines changed

BuildPlate_Editor.csproj

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@
9191
<Compile Include="Program.cs" />
9292
<Compile Include="Properties\AssemblyInfo.cs" />
9393
<Compile Include="Shader.cs" />
94+
<Compile Include="SkyBox.cs" />
9495
<Compile Include="SubChunk.cs" />
9596
<Compile Include="Texture.cs" />
9697
<Compile Include="Util.cs" />
@@ -101,6 +102,12 @@
101102
</ItemGroup>
102103
<ItemGroup>
103104
<None Include="App.config" />
105+
<None Include="Data\Shaders\shader2.frag">
106+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
107+
</None>
108+
<None Include="Data\Shaders\shader2.vert">
109+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
110+
</None>
104111
<None Include="Data\Shaders\shader.frag">
105112
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
106113
</None>
@@ -121,6 +128,9 @@
121128
<Content Include="Data\Textures\Black.png">
122129
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
123130
</Content>
131+
<Content Include="Data\Textures\skybox.png">
132+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
133+
</Content>
124134
</ItemGroup>
125135
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
126136
</Project>

Camera.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,13 @@ public static void UpdateView(float width, float height)
5656
float bottom = -projHeight / 2f;
5757
float top = projHeight / 2f;
5858
float near = 0.001f;
59-
float far = 10000f;
59+
float far = 100000f;
6060

6161
projMatrix = Matrix4.CreateOrthographicOffCenter(left, right, bottom, top, near, far);
6262
} else {
6363
float fov = 45f;
6464
float near = 0.001f;
65-
float far = 10000f;
65+
float far = 100000f;
6666
float aspect = width / height;
6767
projMatrix = Matrix4.CreatePerspectiveFieldOfView(DegToRad(fov), aspect, near, far);
6868
}

Data/Shaders/shader2.frag

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

Data/Shaders/shader2.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 vec2 aUv;
4+
5+
out vec2 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/Textures/skybox.png

1.46 MB
Loading

Program.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ static void Main(string[] args)
3737
baseDir = Path.GetDirectoryName(myExecutable) + "\\";
3838
Console.WriteLine($"Base directory: {baseDir}");
3939

40+
#if DEBUG == false
4041
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
42+
#endif
4143

4244
if (File.Exists(baseDir + "askedForDefault") || args != null && args.Length > 0)
4345
goto skipAks;
@@ -90,7 +92,7 @@ static void Main(string[] args)
9092
World.textureBasePath = texturesPath;
9193
#if DEBUG
9294
//World.targetFilePath = @"C:\Users\Tomas\Desktop\Project Earth\Api\data\buildplates\7cd6d53b-1715-4b22-9a99-d6d43edd61df.json";
93-
World.targetFilePath = @"C:\Users\Tomas\Desktop\Project Earth\Api\data\buildplates\00d1fa99-7acf-449d-bb4f-8d11127bd6e3.json";
95+
World.targetFilePath = @"C:\Users\Tomas\Desktop\Project Earth\Api\data\buildplates\2cb8bda2-c49b-4ead-887f-593d37bc2784.json";
9496
#else
9597
Console.Write("Build plate to edit (.json): ");
9698
// json - api, plate - data only, plate64 - base64 encoded data

SkyBox.cs

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
using OpenTK;
2+
using OpenTK.Graphics.OpenGL4;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.IO;
6+
using System.Linq;
7+
using System.Text;
8+
using System.Threading.Tasks;
9+
10+
namespace BuildPlate_Editor
11+
{
12+
public static class SkyBox
13+
{
14+
private static List<Vertex> verts = new List<Vertex>();
15+
private static List<uint> tris = new List<uint>();
16+
17+
public static Vector3 pos;
18+
19+
private static int vao;
20+
private static int vbo;
21+
private static int ebo;
22+
23+
public static int texId { get; private set; }
24+
25+
public static void Init(string texturePath, Vector3 cameraPos, float size)
26+
{
27+
if (File.Exists(texturePath)) {
28+
Texture tex = new Texture(texturePath);
29+
texId = tex.id;
30+
}
31+
else
32+
texId = -1;
33+
pos = cameraPos;
34+
Util.SkyboxTex(texId, Vector3.One * size, ref verts, ref tris);
35+
InitMesh();
36+
}
37+
38+
static void InitMesh()
39+
{
40+
GL.CreateVertexArrays(1, out vao);
41+
GL.BindVertexArray(vao);
42+
GL.CreateBuffers(1, out ebo);
43+
GL.CreateBuffers(1, out vbo);
44+
CreateMesh();
45+
}
46+
47+
static void CreateMesh()
48+
{
49+
GL.NamedBufferData(ebo, tris.Count * sizeof(uint), tris.ToArray(), BufferUsageHint.DynamicDraw);
50+
GL.VertexArrayElementBuffer(vao, ebo);
51+
52+
int vertexBindingPoint = 0;
53+
GL.NamedBufferData(vbo, verts.Count * Vertex.Size, verts.ToArray(), BufferUsageHint.DynamicDraw);
54+
GL.VertexArrayVertexBuffer(vao, vertexBindingPoint, vbo, IntPtr.Zero, Vertex.Size);
55+
56+
// pos
57+
GL.VertexArrayAttribFormat(vao, 0, 3, VertexAttribType.Float, false, 0);
58+
GL.VertexArrayAttribBinding(vao, 0, vertexBindingPoint);
59+
GL.EnableVertexArrayAttrib(vao, 0);
60+
// uv
61+
GL.VertexArrayAttribFormat(vao, 1, 3, VertexAttribType.Float, false, 3 * sizeof(float));
62+
GL.VertexArrayAttribBinding(vao, 1, vertexBindingPoint);
63+
GL.EnableVertexArrayAttrib(vao, 1);
64+
}
65+
66+
public static void Render(Shader s)
67+
{
68+
if (texId == -1)
69+
return;
70+
71+
Matrix4 transform = Matrix4.CreateTranslation(pos);
72+
73+
s.Bind();
74+
s.UploadMat4("uTransform", ref transform);
75+
GL.ActiveTexture(TextureUnit.Texture0);
76+
GL.BindTexture(TextureTarget.Texture2D, texId);
77+
s.UploadInt("text", 0);
78+
79+
GL.BindVertexArray(vao);
80+
GL.DrawElements(BeginMode.Triangles, tris.Count, DrawElementsType.UnsignedInt, 0);
81+
}
82+
}
83+
}

SubChunk.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public void CreateMeshData()
7171

7272
int renderer = renderers[currentBlock];
7373
if (renderer > -1 && blocks[currentBlock] < palette.Length) // don't render air
74-
World.blockRenderers[renderer](new Vector3(x, y, z), pos * 16, palette[blocks[currentBlock]].textures, palette[blocks[currentBlock]].data,
74+
World.blockRenderers[renderer](new Vector3(x, y, z), pos * 16, palette[blocks[currentBlock]].textures, palette[blocks[currentBlock]].data,
7575
ref vertices, ref triangles);
7676

7777
z = origz;

Texture.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ public Texture(string path)
8181
Width = db.Width;
8282
Height = db.Height;
8383

84+
GL.ActiveTexture(TextureUnit.Texture0);
8485
GL.GenTextures(1, out id);
8586
GL.BindTexture(TextureTarget.Texture2D, id);
8687

Util.cs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public static string JsonSerialize<T>(T value)
4646

4747
public static int Normalized(this int i)
4848
{
49-
int value = System.Math.Min(System.Math.Max(i, -1), 1);
49+
int value = Math.Min(Math.Max(i, -1), 1);
5050
if (value == 0)
5151
value = 1;
5252
return value;
@@ -116,6 +116,24 @@ public static void CubeTex(int _tex, Vector3 pos, Vector3 size, ref List<Vertex>
116116
}
117117
}
118118

119+
public static void SkyboxTex(int _tex, Vector3 size, ref List<Vertex> verts, ref List<uint> tris)
120+
{
121+
uint tex = (uint)_tex;
122+
for (int p = 0; p < 6; p++) {
123+
uint firstVertIndex = (uint)verts.Count;
124+
verts.Add(new Vertex(VoxelData.voxelVerts[VoxelData.SkyBox.tris[p, 3]] * size - size / 2f, VoxelData.SkyBox.voxelUvs[p][0], tex));
125+
verts.Add(new Vertex(VoxelData.voxelVerts[VoxelData.SkyBox.tris[p, 2]] * size - size / 2f, VoxelData.SkyBox.voxelUvs[p][1], tex));
126+
verts.Add(new Vertex(VoxelData.voxelVerts[VoxelData.SkyBox.tris[p, 1]] * size - size / 2f, VoxelData.SkyBox.voxelUvs[p][2], tex));
127+
verts.Add(new Vertex(VoxelData.voxelVerts[VoxelData.SkyBox.tris[p, 0]] * size - size / 2f, VoxelData.SkyBox.voxelUvs[p][3], tex));
128+
tris.Add(firstVertIndex);
129+
tris.Add(firstVertIndex + 1);
130+
tris.Add(firstVertIndex + 2);
131+
tris.Add(firstVertIndex + 2);
132+
tris.Add(firstVertIndex + 1);
133+
tris.Add(firstVertIndex + 3);
134+
}
135+
}
136+
119137
public static T2[] ForArray<T1, T2>(this IEnumerator<T1> e, Func<T1, T2> func)
120138
{
121139
List<T2> list = new List<T2>();

0 commit comments

Comments
 (0)