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

Commit

Permalink
Better skybox
Browse files Browse the repository at this point in the history
  • Loading branch information
BitcoderCZ committed Jan 1, 2023
1 parent b30a01d commit 43216b8
Show file tree
Hide file tree
Showing 17 changed files with 365 additions and 37 deletions.
26 changes: 25 additions & 1 deletion BuildPlate_Editor.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,12 @@
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
<None Include="Data\Shaders\skybox.frag">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="Data\Shaders\skybox.vert">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="Data\Shaders\shader2.frag">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
Expand All @@ -128,7 +134,25 @@
<Content Include="Data\Textures\Black.png">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="Data\Textures\skybox.png">
<Content Include="Data\Textures\Skybox_0.png">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="Data\Textures\Skybox_Cold_Sunset\Cold_Sunset+X.png">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="Data\Textures\Skybox_Cold_Sunset\Cold_Sunset+Y.png">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="Data\Textures\Skybox_Cold_Sunset\Cold_Sunset+Z.png">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="Data\Textures\Skybox_Cold_Sunset\Cold_Sunset-X.png">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="Data\Textures\Skybox_Cold_Sunset\Cold_Sunset-Y.png">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="Data\Textures\Skybox_Cold_Sunset\Cold_Sunset-Z.png">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>
Expand Down
12 changes: 12 additions & 0 deletions Data/Shaders/skybox.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#version 450 core

out vec4 FragColor;

in vec3 TexCoords;

layout (binding = 0) uniform samplerCube cubemap;

void main()
{
FragColor = texture(cubemap, TexCoords);
}
14 changes: 14 additions & 0 deletions Data/Shaders/skybox.vert
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#version 450 core
layout (location = 0) in vec3 aPos;

out vec3 TexCoords;

uniform mat4 uTransform;
uniform mat4 uProjection;
uniform mat4 uView;

void main()
{
TexCoords = aPos;
gl_Position = uProjection * uView * (uTransform * vec4(aPos, 1.0));
}
Binary file added Data/Textures/Skybox_0.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed Data/Textures/skybox.png
Binary file not shown.
2 changes: 2 additions & 0 deletions Maths/Vector2i.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace BuildPlate_Editor.Maths
{
[StructLayout(LayoutKind.Sequential)]
public struct Vector2i
{
public static readonly Vector2i Zero = new Vector2i(0, 0);
Expand Down
130 changes: 120 additions & 10 deletions SkyBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,122 @@
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SystemPlus.Utils;

namespace BuildPlate_Editor
{
public static class SkyBox
{
private static List<Vertex> verts = new List<Vertex>();
private static List<uint> tris = new List<uint>();
private static Vector3[] verts = new Vector3[0];
private static uint[] tris = new uint[0];

public static Vector3 pos;

private static float size;

private static int vao;
private static int vbo;
private static int ebo;

public static int texId { get; private set; }

private static string[] MultiImageLookUp = new string[]
{
"+X", "-X", "+Y", "-Y", "+Z", "-Z"
};

public static void Init(string skyboxName, Vector3 cameraPos, float _size)
{
string fileName = "Skybox_" + skyboxName;
string baseTexPath = Program.baseDir + "Data/Textures/";
if (File.Exists(baseTexPath + fileName + ".png")) {
DirectBitmap db = DirectBitmap.Load(baseTexPath + fileName + ".png");
texId = Texture.GenerateCubeMap(db, Texture.CreateOffsets(db.Width, db.Height));
}
else if (Directory.Exists(baseTexPath + fileName)) {
string dir = baseTexPath + fileName + "/";
string[] paths = new string[MultiImageLookUp.Length];
for (int i = 0; i < MultiImageLookUp.Length; i++) {
string path = dir + skyboxName + MultiImageLookUp[i] + ".png";
if (!File.Exists(path)) {
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"Couldn't load part of skybox \"{skyboxName}\": {path}");
Console.ResetColor();
texId = -1;
goto cont;
}
else
paths[i] = path;
}
texId = Texture.GenerateCubeMap(paths);
}
else {
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"Couldn't load skybox \"{skyboxName}\": {baseTexPath + fileName + ".png"}");
Console.ResetColor();
texId = -1;
}

cont: // continue
pos = cameraPos;
size = _size;

verts = VoxelData.SkyBox2.verts;
tris = VoxelData.SkyBox2.tris;
InitMesh();
}

static void InitMesh()
{
GL.CreateVertexArrays(1, out vao);
GL.BindVertexArray(vao);
GL.CreateBuffers(1, out ebo);
GL.CreateBuffers(1, out vbo);
CreateMesh();
}

static void CreateMesh()
{
GL.NamedBufferData(ebo, tris.Length * sizeof(uint), tris, BufferUsageHint.DynamicDraw);
GL.VertexArrayElementBuffer(vao, ebo);

int vertexBindingPoint = 0;
GL.NamedBufferData(vbo, verts.Length * sizeof(float) * 3, verts, BufferUsageHint.DynamicDraw);
GL.VertexArrayVertexBuffer(vao, vertexBindingPoint, vbo, IntPtr.Zero, sizeof(float) * 3);

// pos
GL.VertexArrayAttribFormat(vao, 0, 3, VertexAttribType.Float, false, 0);
GL.VertexArrayAttribBinding(vao, 0, vertexBindingPoint);
GL.EnableVertexArrayAttrib(vao, 0);
}

public static void Render(Shader s)
{
if (texId == -1)
return;

Matrix4 transform = Matrix4.CreateScale(size);
transform *= Matrix4.CreateTranslation(pos);

s.Bind();
s.UploadMat4("uTransform", ref transform);
GL.ActiveTexture(TextureUnit.Texture0);
GL.BindTexture(TextureTarget.TextureCubeMap, texId);
s.UploadInt("tex", 0);

GL.DepthMask(false);

GL.BindVertexArray(vao);
GL.DrawElements(BeginMode.Triangles, tris.Length, DrawElementsType.UnsignedInt, 0);

GL.DepthMask(true);
}
}

/*public static class SkyBox
{
private static Vertex2[] verts = new Vertex2[0];
private static uint[] tris = new uint[0];
public static Vector3 pos;
Expand All @@ -31,7 +140,8 @@ public static void Init(string texturePath, Vector3 cameraPos, float size)
else
texId = -1;
pos = cameraPos;
Util.SkyboxTex(texId, Vector3.One * size, ref verts, ref tris);
//Util.SkyboxTex(Vector3.One * size, ref verts, ref tris);
SphereMesh.Create(5, size, ref tris, ref verts);
InitMesh();
}
Expand All @@ -46,19 +156,19 @@ static void InitMesh()
static void CreateMesh()
{
GL.NamedBufferData(ebo, tris.Count * sizeof(uint), tris.ToArray(), BufferUsageHint.DynamicDraw);
GL.NamedBufferData(ebo, tris.Length * sizeof(uint), tris, BufferUsageHint.DynamicDraw);
GL.VertexArrayElementBuffer(vao, ebo);
int vertexBindingPoint = 0;
GL.NamedBufferData(vbo, verts.Count * Vertex.Size, verts.ToArray(), BufferUsageHint.DynamicDraw);
GL.VertexArrayVertexBuffer(vao, vertexBindingPoint, vbo, IntPtr.Zero, Vertex.Size);
GL.NamedBufferData(vbo, verts.Length * Vertex2.Size, verts, BufferUsageHint.DynamicDraw);
GL.VertexArrayVertexBuffer(vao, vertexBindingPoint, vbo, IntPtr.Zero, Vertex2.Size);
// pos
GL.VertexArrayAttribFormat(vao, 0, 3, VertexAttribType.Float, false, 0);
GL.VertexArrayAttribBinding(vao, 0, vertexBindingPoint);
GL.EnableVertexArrayAttrib(vao, 0);
// uv
GL.VertexArrayAttribFormat(vao, 1, 3, VertexAttribType.Float, false, 3 * sizeof(float));
GL.VertexArrayAttribFormat(vao, 1, 2, VertexAttribType.Float, false, 3 * sizeof(float));
GL.VertexArrayAttribBinding(vao, 1, vertexBindingPoint);
GL.EnableVertexArrayAttrib(vao, 1);
}
Expand All @@ -74,10 +184,10 @@ public static void Render(Shader s)
s.UploadMat4("uTransform", ref transform);
GL.ActiveTexture(TextureUnit.Texture0);
GL.BindTexture(TextureTarget.Texture2D, texId);
s.UploadInt("text", 0);
s.UploadInt("tex", 0);
GL.BindVertexArray(vao);
GL.DrawElements(BeginMode.Triangles, tris.Count, DrawElementsType.UnsignedInt, 0);
GL.DrawElements(BeginMode.Triangles, tris.Length, DrawElementsType.UnsignedInt, 0);
}
}
}*/
}
96 changes: 95 additions & 1 deletion Texture.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using OpenTK;
using BuildPlate_Editor.Maths;
using OpenTK;
using OpenTK.Graphics.OpenGL4;
using System;
using System.Collections.Generic;
Expand All @@ -20,6 +21,99 @@ public class Texture
public int Width;
public int Height;

public static (Vector2i offset, Vector2i size)[] CreateOffsets(int width, int height) // +X, -X, +Y, -Y, +Z, -Z
{
int w = (int)((float)width / 4f);
int h = (int)((float)height / 3f);

if (w > h)
w = h;
else if (h > w)
h = w;

return new (Vector2i offset, Vector2i size)[]
{
(new Vector2i(w * 2, h), new Vector2i(w, h)),
(new Vector2i(0, h), new Vector2i(w, h)),
(new Vector2i(w, 0), new Vector2i(w, h)),
(new Vector2i(w, h * 2), new Vector2i(w, h)),
(new Vector2i(w, h), new Vector2i(w, h)),
(new Vector2i(w * 3, h), new Vector2i(w, h)),
};
}

public static int GenerateCubeMap(DirectBitmap db, (Vector2i offset, Vector2i size)[] offsets)
{
GL.ActiveTexture(TextureUnit.Texture0);
GL.GenTextures(1, out int id);
GL.BindTexture(TextureTarget.TextureCubeMap, id);

int peaceWidth = (int)((float)db.Width / 4f);
int peaceHeight = (int)((float)db.Height / 3f);

// Texture must be 1:1
if (peaceWidth > peaceHeight)
peaceWidth = peaceHeight;
else if (peaceHeight > peaceWidth)
peaceHeight = peaceWidth;

for (int i = 0; i < 6; i++) {

DirectBitmap peace = new DirectBitmap(peaceWidth, peaceHeight);
for (int j = 0; j < peaceHeight; j++)
peace.Write(j * peaceWidth, db.Data, (offsets[i].offset.Y + j) * db.Width + offsets[i].offset.X, peaceWidth);


BitmapData data = peace.Bitmap.LockBits(new Rectangle(0, 0, peaceWidth, peaceHeight), ImageLockMode.ReadOnly,
System.Drawing.Imaging.PixelFormat.Format32bppArgb);

GL.TexImage2D(TextureTarget.TextureCubeMapPositiveX + i, 0, PixelInternalFormat.Rgb, peaceWidth, peaceHeight, 0,
PixelFormat.Bgra, PixelType.UnsignedByte, data.Scan0);

peace.Bitmap.UnlockBits(data);
peace.Dispose();
}

GL.TexParameter(TextureTarget.TextureCubeMap, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);
GL.TexParameter(TextureTarget.TextureCubeMap, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear);
GL.TexParameter(TextureTarget.TextureCubeMap, TextureParameterName.TextureWrapS, (int)TextureWrapMode.ClampToEdge);
GL.TexParameter(TextureTarget.TextureCubeMap, TextureParameterName.TextureWrapT, (int)TextureWrapMode.ClampToEdge);
GL.TexParameter(TextureTarget.TextureCubeMap, TextureParameterName.TextureWrapR, (int)TextureWrapMode.ClampToEdge);

return id;
}
public static int GenerateCubeMap(string[] paths)
{
GL.ActiveTexture(TextureUnit.Texture0);
GL.GenTextures(1, out int id);
GL.BindTexture(TextureTarget.TextureCubeMap, id);

if (paths.Length != 6)
throw new Exception($"Skybox must be 6 images not {paths.Length}");

for (int i = 0; i < paths.Length; i++) {

DirectBitmap db = DirectBitmap.Load(paths[i]);

BitmapData data = db.Bitmap.LockBits(new Rectangle(0, 0, db.Width, db.Height), ImageLockMode.ReadOnly,
System.Drawing.Imaging.PixelFormat.Format32bppArgb);

GL.TexImage2D(TextureTarget.TextureCubeMapPositiveX + i, 0, PixelInternalFormat.Rgb, db.Width, db.Height, 0,
PixelFormat.Bgra, PixelType.UnsignedByte, data.Scan0);

db.Bitmap.UnlockBits(data);
db.Dispose();
}

GL.TexParameter(TextureTarget.TextureCubeMap, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);
GL.TexParameter(TextureTarget.TextureCubeMap, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear);
GL.TexParameter(TextureTarget.TextureCubeMap, TextureParameterName.TextureWrapS, (int)TextureWrapMode.ClampToEdge);
GL.TexParameter(TextureTarget.TextureCubeMap, TextureParameterName.TextureWrapT, (int)TextureWrapMode.ClampToEdge);
GL.TexParameter(TextureTarget.TextureCubeMap, TextureParameterName.TextureWrapR, (int)TextureWrapMode.ClampToEdge);

return id;
}

public static int CreateTextureArray(string[] texturesToLoad, TexFlip flip)
{
int taid;
Expand Down
Loading

0 comments on commit 43216b8

Please sign in to comment.