Advertisement
Guest User

Untitled

a guest
Feb 15th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.21 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Text;
  5. using OpenTK;
  6. using OpenTK.Graphics.OpenGL;
  7. using Wicked.Engine;
  8.  
  9. namespace Wicked.Shaders
  10. {
  11.     public class Shader
  12.     {
  13.         private int _handle;
  14.  
  15.         public Shader CompileShader(ShaderObject[] shaders)
  16.         {
  17.             int status = -1;
  18.             string info = "";
  19.  
  20.             List<int> compiled = new List<int>();
  21.  
  22.             foreach (var shader in shaders)
  23.             {
  24.                 var index = GL.CreateShader(shader.ShaderInformation);
  25.                 GL.ShaderSource(index, shader.ShaderCode);
  26.                 GL.CompileShader(index);
  27.  
  28.                 GL.GetShaderInfoLog(index, out info);
  29.                 GL.GetShader(index, ShaderParameter.CompileStatus, out status);
  30.  
  31.                 if (status != 1)
  32.                     throw new ApplicationException(info);
  33.  
  34.                 compiled.Add(index);
  35.             }
  36.  
  37.             _handle = GL.CreateProgram();
  38.  
  39.             foreach (var shader in compiled) { GL.AttachShader(_handle, shader); }
  40.  
  41.             GL.LinkProgram(_handle);
  42.  
  43.             foreach (var shader in compiled)
  44.             {
  45.                 GL.DetachShader(_handle, shader);
  46.                 GL.DeleteShader(shader);
  47.             }
  48.  
  49.             return this;
  50.         }
  51.  
  52.  
  53.         public void Use()
  54.         {
  55.             GL.UseProgram(_handle);
  56.         }
  57.  
  58.  
  59.         public void SetInt(string name, int value)
  60.         {
  61.             int location = GL.GetUniformLocation(_handle, name);
  62.  
  63.             if (location == -1)
  64.             {
  65.                 throw new ArgumentException("uniform name not found");
  66.             }
  67.  
  68.             GL.Uniform1(location, value);
  69.         }
  70.  
  71.  
  72.         public void SetMatrix4(string name, Matrix4 matrix)
  73.         {
  74.             int location = GL.GetUniformLocation(_handle, name);
  75.  
  76.             if (location == -1)
  77.             {
  78.                 throw new ArgumentException("uniform name not found");
  79.             }
  80.  
  81.             GL.UniformMatrix4(location, true, ref matrix);
  82.         }
  83.  
  84.  
  85.         public int GetAttribLocation(string attribName)
  86.         {
  87.             return GL.GetAttribLocation(_handle, attribName);
  88.         }
  89.     }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement