budnhead/org.budnhead/graphics/Shader.cs

65 lines
1.1 KiB
C#
Raw Normal View History

2017-04-20 16:49:43 +02:00
using System;
using System.IO;
using OpenTK.Graphics.OpenGL;
2017-05-09 23:41:50 +02:00
namespace org.budnhead.graphics
2017-04-20 16:49:43 +02:00
{
public class Shader
{
private int id;
private ShaderType shaderType;
private String source;
public Shader(ShaderType shaderType, String source)
{
this.id = GL.CreateShader(shaderType);
this.shaderType = shaderType;
this.source = source;
compile();
}
public int ID
{
get { return this.id; }
}
public Shader(ShaderType shaderType, FileStream file)
{
this.id = GL.CreateShader(shaderType);
this.shaderType = shaderType;
StreamReader sr = new StreamReader(file);
this.source = sr.ReadToEnd();
compile();
}
private void compile()
{
2017-04-25 22:01:18 +02:00
int result;
2017-04-20 16:49:43 +02:00
GL.ShaderSource(this.id, source);
GL.CompileShader(this.id);
string msg = GL.GetShaderInfoLog((int)this.id);
2017-04-25 22:01:18 +02:00
GL.GetShader(this.id, ShaderParameter.CompileStatus, out result);
2017-04-20 16:49:43 +02:00
2017-04-25 22:01:18 +02:00
if (result != 1){
Console.WriteLine("Shader compile failed: {0}", msg);
throw new Exception(String.Format("OpenGL: Shader did not compile. ({0})",msg));
}
2017-04-20 16:49:43 +02:00
}
public ShaderType ShaderType
{
get { return this.shaderType; }
}
}
}