forked from arookas/Demolisher
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGLProgram.cs
More file actions
113 lines (99 loc) · 2.41 KB
/
GLProgram.cs
File metadata and controls
113 lines (99 loc) · 2.41 KB
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
using OpenTK.Graphics.OpenGL;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace Arookas.Demolisher
{
public class GLProgram : IEnumerable<GLShader>, IDisposable
{
List<GLShader> attachedShaders = new List<GLShader>(5);
bool isDisposed;
public int ID { get; private set; }
public string InfoLog { get { return GL.GetProgramInfoLog(ID); } }
public GLShader this[int index] { get { return attachedShaders[index]; } }
public GLShader this[ShaderType shaderType]
{
get
{
if (!shaderType.IsDefined())
{
throw new ArgumentOutOfRangeException("shaderType", shaderType, "The specified shader type was not a defined OpenTK.Graphics.OpenGL.ShaderType value.");
}
return attachedShaders.FirstOrDefault(shader => shader.Type == shaderType);
}
}
public int this[ProgramParameter parameter]
{
get
{
if (!parameter.IsDefined())
{
throw new ArgumentOutOfRangeException("parameter", parameter, "The specified program parameter was not a defined OpenTK.Graphics.OpenGL.ProgramParameter value.");
}
int value;
GL.GetProgram(ID, parameter, out value);
return value;
}
}
GLProgram(int id)
{
ID = id;
}
public static GLProgram Create()
{
return new GLProgram(GL.CreateProgram());
}
public void Attach(GLShader shader)
{
if (shader == null)
{
throw new ArgumentNullException("shader");
}
GL.AttachShader(ID, shader);
attachedShaders.Add(shader);
}
public void Link()
{
GL.LinkProgram(ID);
if (this[ProgramParameter.LinkStatus] != 1)
{
throw new InvalidOperationException(String.Format("The GLProgram failed to be linked. The info log is:\n{0}", InfoLog));
}
}
public void Use()
{
GL.UseProgram(ID);
}
public static void Unuse()
{
GL.UseProgram(0);
}
public void Dispose()
{
if (!isDisposed)
{
int deleteStatus;
GL.DeleteProgram(ID);
GL.GetProgram(ID, ProgramParameter.DeleteStatus, out deleteStatus);
if (deleteStatus != 1)
{
throw new InvalidOperationException("The GLProgram failed to be deleted.");
}
isDisposed = true;
}
}
public IEnumerator<GLShader> GetEnumerator()
{
return attachedShaders.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public static implicit operator int(GLProgram program)
{
return program.ID;
}
}
}