-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSkybox.cs
90 lines (75 loc) · 3.19 KB
/
Skybox.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
using Intro3DFramework.Rendering;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using OpenTK.Graphics.OpenGL;
using OpenTK;
namespace Sample
{
class Skybox
{
private int vertexBuffer;
private int indexBuffer;
private TextureCube texture;
private Shader shader;
public Skybox(TextureCube.LoadDescription cubemapDesc, Shader.LoadDescription shaderDesc)
{
Vector3[] vertices = new Vector3[]
{
new Vector3(-1.0f, 1.0f, 1.0f),
new Vector3(1.0f, 1.0f, 1.0f),
new Vector3(1.0f, 1.0f, -1.0f),
new Vector3(-1.0f, 1.0f, -1.0f),
new Vector3(-1.0f, -1.0f, 1.0f),
new Vector3(1.0f, -1.0f, 1.0f),
new Vector3(1.0f, -1.0f, -1.0f),
new Vector3(-1.0f, -1.0f, -1.0f)
};
vertexBuffer = GL.GenBuffer();
GL.BindBuffer(BufferTarget.ArrayBuffer, vertexBuffer);
GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)(Marshal.SizeOf(typeof(Vector3)) * vertices.Length), vertices, BufferUsageHint.StaticDraw);
short[] indices = new short[]
{7, 3, 0, 4, 7, 0, // front
5, 1, 2, 6, 5, 2, // back
4, 0, 1, 5, 4, 1, // left
6, 2, 3, 7, 6, 3, // right
2, 1, 0, 3, 2, 0, // top
4, 5, 6, 7, 4, 6}; // down
indexBuffer = GL.GenBuffer();
GL.BindBuffer(BufferTarget.ElementArrayBuffer, indexBuffer);
GL.BufferData(BufferTarget.ElementArrayBuffer, (IntPtr)(sizeof(short) * indices.Length), indices, BufferUsageHint.StaticDraw);
texture = TextureCube.GetResource(cubemapDesc);
shader = Shader.GetResource(shaderDesc);
// On some drivers its needed to activate this flag, otherwise there may be artifacts across different cubemaps faces.
GL.Enable(EnableCap.TextureCubeMapSeamless);
}
public void Draw()
{
// Assert the object exists and is valid.
System.Diagnostics.Debug.Assert(vertexBuffer > 0);
GL.BindBuffer(BufferTarget.ArrayBuffer, vertexBuffer);
GL.BindBuffer(BufferTarget.ElementArrayBuffer, indexBuffer);
// Set vertex type
GL.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, false, sizeof(float)*3, 0);
GL.EnableVertexAttribArray(0);
// Setting the texture.
if (texture != null)
GL.BindTexture(TextureTarget.TextureCubeMap, texture.Texture);
else
GL.BindTexture(TextureTarget.TextureCubeMap, 0);
// Disable culling and depth write.
GL.Disable(EnableCap.CullFace);
GL.DepthMask(false);
// Setup shader
GL.UseProgram(shader.Program);
// Finally draw.
GL.DrawElements(PrimitiveType.Triangles, 2 * 3 * 6, DrawElementsType.UnsignedShort, 0);
// Reenable culling and depth write.
GL.Enable(EnableCap.CullFace);
GL.DepthMask(true);
}
}
}