|
| 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 | +} |
0 commit comments