2020-02-17 02:44:21 +00:00
|
|
|
using System;
|
|
|
|
using System.IO;
|
2020-02-21 07:04:33 +00:00
|
|
|
using System.Reflection;
|
2020-02-17 02:44:21 +00:00
|
|
|
using OpenTK.Graphics.OpenGL;
|
2020-02-21 07:04:33 +00:00
|
|
|
|
2020-02-20 23:07:17 +00:00
|
|
|
namespace RecrownedGTK.Graphics.Render.Shaders {
|
2020-02-17 02:44:21 +00:00
|
|
|
public class Shader : IDisposable {
|
|
|
|
int handle;
|
|
|
|
public bool IsDisposed {
|
|
|
|
get;
|
|
|
|
private set;
|
|
|
|
}
|
2020-02-21 07:04:33 +00:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Creates a shader program using the given paths.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="pathToVertexFile">The relative path to the vertex shader file.</param>
|
|
|
|
/// <param name="pathToFragmentFile">The relative path to the fragment shader file.</param>
|
|
|
|
/// <returns></returns>
|
|
|
|
public Shader(string pathToVertexFile, string pathToFragmentFile) : this(new StreamReader(pathToVertexFile), new StreamReader(pathToFragmentFile)) {
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Creates a shader program using the given streams.
|
|
|
|
/// Automatically disposes of streams immediately after use.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="vertexFileStream">The vertex shader file stream.</param>
|
|
|
|
/// <param name="fragmentFileStream">The fragment shader file stream.</param>
|
|
|
|
public Shader(StreamReader vertexFileStream, StreamReader fragmentFileStream) {
|
2020-02-17 02:44:21 +00:00
|
|
|
int vertShader = 0;
|
|
|
|
int fragShader = 0;
|
|
|
|
|
2020-02-21 07:04:33 +00:00
|
|
|
string vertShaderSource = vertexFileStream.ReadToEnd();
|
|
|
|
string fragShaderSource = fragmentFileStream.ReadToEnd();
|
|
|
|
fragmentFileStream.Dispose();
|
|
|
|
vertexFileStream.Dispose();
|
2020-02-17 02:44:21 +00:00
|
|
|
|
|
|
|
vertShader = GL.CreateShader(ShaderType.VertexShader);
|
|
|
|
GL.ShaderSource(vertShader, vertShaderSource);
|
|
|
|
fragShader = GL.CreateShader(ShaderType.FragmentShader);
|
|
|
|
GL.ShaderSource(fragShader, fragShaderSource);
|
|
|
|
|
|
|
|
string log;
|
|
|
|
GL.CompileShader(vertShader);
|
2020-02-21 07:04:33 +00:00
|
|
|
if ((log = GL.GetShaderInfoLog(vertShader)) != "") {
|
2020-02-17 02:44:21 +00:00
|
|
|
throw new ArgumentException("Error while compiling vertex shader: " + log, "vertexPath");
|
|
|
|
}
|
|
|
|
GL.CompileShader(fragShader);
|
2020-02-21 07:04:33 +00:00
|
|
|
if ((log = GL.GetShaderInfoLog(fragShader)) != "") {
|
2020-02-17 02:44:21 +00:00
|
|
|
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-21 07:04:33 +00:00
|
|
|
string resourceLoc = "RecrownedGTK.Graphics.Render.Shaders.{0}";
|
2020-02-22 05:00:59 +00:00
|
|
|
Shader shader = new Shader(new StreamReader(Utilities.ReadEmbeddedFile(String.Format(resourceLoc, "default.vert"))),
|
|
|
|
new StreamReader(Utilities.ReadEmbeddedFile(String.Format(resourceLoc, "default.frag"))));
|
2020-02-17 02:44:21 +00:00
|
|
|
return shader;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|