104 lines
2.0 KiB
C#
104 lines
2.0 KiB
C#
|
using System;
|
|||
|
using System.IO;
|
|||
|
using OpenTK.Graphics.OpenGL;
|
|||
|
using System.Collections.Generic;
|
|||
|
|
|||
|
namespace nhengine {
|
|||
|
|
|||
|
public class ShaderManager {
|
|||
|
private static ShaderManager _instance;
|
|||
|
|
|||
|
private List<Shader> shaders;
|
|||
|
|
|||
|
public ShaderManager(){
|
|||
|
if (_instance == null)
|
|||
|
{
|
|||
|
_instance = this;
|
|||
|
}
|
|||
|
|
|||
|
this.shaders = new List<Shader>();
|
|||
|
}
|
|||
|
|
|||
|
void registerShader(Shader shader){
|
|||
|
this.shaders.Add(shader);
|
|||
|
}
|
|||
|
|
|||
|
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;
|
|||
|
|
|||
|
_instance.registerShader(this);
|
|||
|
|
|||
|
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();
|
|||
|
|
|||
|
_instance.registerShader(this);
|
|||
|
|
|||
|
compile();
|
|||
|
}
|
|||
|
|
|||
|
private void compile(){
|
|||
|
GL.ShaderSource(this.id, source);
|
|||
|
GL.CompileShader(this.id);
|
|||
|
|
|||
|
string msg = GL.GetShaderInfoLog((int)this.id);
|
|||
|
|
|||
|
Console.WriteLine("Supported Shader Version: {0}", GL.GetString(StringName.ShadingLanguageVersion));
|
|||
|
Console.WriteLine("Shader Compiled: {0}", msg);
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
public ShaderType ShaderType {
|
|||
|
get { return this.shaderType; }
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public class ShaderProgram {
|
|||
|
|
|||
|
Shader vertexShader;
|
|||
|
Shader fragmentShader;
|
|||
|
|
|||
|
int id;
|
|||
|
|
|||
|
public ShaderProgram(Shader vertexShader, Shader fragmentShader){
|
|||
|
|
|||
|
this.vertexShader = vertexShader;
|
|||
|
this.fragmentShader = fragmentShader;
|
|||
|
|
|||
|
this.id = GL.CreateProgram();
|
|||
|
|
|||
|
GL.AttachShader(this.id, vertexShader.ID);
|
|||
|
GL.AttachShader(this.id, fragmentShader.ID);
|
|||
|
GL.LinkProgram(this.id);
|
|||
|
Console.WriteLine("Shader Program Result:D {0}", GL.GetProgramInfoLog(this.id));
|
|||
|
}
|
|||
|
|
|||
|
public int ID {
|
|||
|
get { return this.id; }
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
}
|