2020-02-17 02:44:21 +00:00
|
|
|
using System;
|
|
|
|
using System.IO;
|
|
|
|
using System.Text;
|
|
|
|
using OpenTK.Graphics.OpenGL;
|
|
|
|
namespace RecrownedGTK.Graphics.Render.Shader {
|
|
|
|
public class Shader : IDisposable {
|
|
|
|
int handle;
|
|
|
|
public bool IsDisposed {
|
|
|
|
get;
|
|
|
|
private set;
|
|
|
|
}
|
|
|
|
public Shader(string vertexPath, string fragmentPath) {
|
|
|
|
IsDisposed = false;
|
|
|
|
|
|
|
|
int vertShader = 0;
|
|
|
|
int fragShader = 0;
|
|
|
|
|
|
|
|
string vertShaderSource;
|
|
|
|
string fragShaderSource;
|
|
|
|
|
|
|
|
using (StreamReader stream = new StreamReader(vertexPath, Encoding.UTF8)) {
|
|
|
|
vertShaderSource = stream.ReadToEnd();
|
|
|
|
}
|
|
|
|
using (StreamReader stream = new StreamReader(fragmentPath, Encoding.UTF8)) {
|
|
|
|
fragShaderSource = stream.ReadToEnd();
|
|
|
|
}
|
|
|
|
|
|
|
|
vertShader = GL.CreateShader(ShaderType.VertexShader);
|
|
|
|
GL.ShaderSource(vertShader, vertShaderSource);
|
|
|
|
fragShader = GL.CreateShader(ShaderType.FragmentShader);
|
|
|
|
GL.ShaderSource(fragShader, fragShaderSource);
|
|
|
|
|
|
|
|
string log;
|
|
|
|
GL.CompileShader(vertShader);
|
|
|
|
if ((log = GL.GetShaderInfoLog(vertShader)) == "") {
|
|
|
|
throw new ArgumentException("Error while compiling vertex shader: " + log, "vertexPath");
|
|
|
|
}
|
|
|
|
GL.CompileShader(fragShader);
|
|
|
|
if ((log = GL.GetShaderInfoLog(fragShader)) == "") {
|
|
|
|
throw new ArgumentException("Error while compiling fragment shader: " + log, "fragmentPath");
|
|
|
|
}
|
2020-02-17 07:25:13 +00:00
|
|
|
|
|
|
|
//Creates the shader program.
|
2020-02-17 02:44:21 +00:00
|
|
|
handle = GL.CreateProgram();
|
2020-02-17 07:25:13 +00:00
|
|
|
|
|
|
|
//Attaches shader to program and links it.
|
2020-02-17 02:44:21 +00:00
|
|
|
GL.AttachShader(handle, vertShader);
|
|
|
|
GL.AttachShader(handle, fragShader);
|
|
|
|
GL.LinkProgram(handle);
|
2020-02-17 07:25:13 +00:00
|
|
|
|
2020-02-17 02:44:21 +00:00
|
|
|
GL.DetachShader(handle, vertShader);
|
|
|
|
GL.DetachShader(handle, fragShader);
|
|
|
|
GL.DeleteShader(vertShader);
|
|
|
|
GL.DeleteShader(fragShader);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Use() {
|
|
|
|
GL.UseProgram(handle);
|
|
|
|
}
|
|
|
|
|
2020-02-17 07:25:13 +00:00
|
|
|
public int GetAttribLocation(string attributeName) {
|
|
|
|
return GL.GetAttribLocation(handle, attributeName);
|
|
|
|
}
|
|
|
|
|
2020-02-17 02:44:21 +00:00
|
|
|
protected virtual void Dispose(bool disposing) {
|
|
|
|
if (IsDisposed) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (disposing) {
|
|
|
|
}
|
|
|
|
GL.DeleteProgram(handle);
|
|
|
|
IsDisposed = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Dispose() {
|
|
|
|
Dispose(true);
|
|
|
|
GC.SuppressFinalize(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
~Shader() {
|
|
|
|
Dispose(false);
|
|
|
|
}
|
2020-02-17 07:25:13 +00:00
|
|
|
public static Shader CreateBasicShader() {
|
2020-02-17 02:44:21 +00:00
|
|
|
Shader shader = new Shader("Graphics/Render/Shader/default.vert", "Graphics/Render/Shader/default.frag");
|
|
|
|
return shader;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|