Class: RBGLox::Shader
- Includes:
- Gl
- Defined in:
- lib/rbglox/shader.rb
Overview
GLSL Shader Class
Instance Attribute Summary collapse
-
#filename ⇒ Object
readonly
Returns the value of attribute filename.
-
#fragment ⇒ Object
readonly
Returns the value of attribute fragment.
-
#uniforms ⇒ Object
readonly
Returns the value of attribute uniforms.
-
#vertex ⇒ Object
readonly
Returns the value of attribute vertex.
Attributes inherited from Resource
Instance Method Summary collapse
-
#[]=(name, value) ⇒ Object
Set internal GLSL uniforms.
-
#bind ⇒ Object
Bind the Shader.
-
#free ⇒ Object
Free the Shader.
-
#initialize(filename) ⇒ Shader
constructor
Constructor.
-
#release ⇒ Object
Release the Shader.
-
#reload ⇒ Object
Reload the Shader.
Constructor Details
#initialize(filename) ⇒ Shader
Constructor
8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/rbglox/shader.rb', line 8 def initialize(filename) @filename = filename @id = glCreateProgram @vertex = glCreateShader GL_VERTEX_SHADER glAttachShader id, vertex @fragment = glCreateShader GL_FRAGMENT_SHADER glAttachShader id, fragment @uniforms = [] reload end |
Instance Attribute Details
#filename ⇒ Object (readonly)
Returns the value of attribute filename.
5 6 7 |
# File 'lib/rbglox/shader.rb', line 5 def filename @filename end |
#fragment ⇒ Object (readonly)
Returns the value of attribute fragment.
5 6 7 |
# File 'lib/rbglox/shader.rb', line 5 def fragment @fragment end |
#uniforms ⇒ Object (readonly)
Returns the value of attribute uniforms.
5 6 7 |
# File 'lib/rbglox/shader.rb', line 5 def uniforms @uniforms end |
#vertex ⇒ Object (readonly)
Returns the value of attribute vertex.
5 6 7 |
# File 'lib/rbglox/shader.rb', line 5 def vertex @vertex end |
Instance Method Details
#[]=(name, value) ⇒ Object
Set internal GLSL uniforms
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/rbglox/shader.rb', line 24 def []=(name, value) loc = glGetUniformLocation id, name if value.is_a? Float glUniform1f loc, value elsif value.is_a? Integer glUniform1i loc, value elsif value.is_a? Array l = value.size if l == 2 glUniform2fv loc, value elsif l == 3 glUniform3fv loc, value elsif l == 4 glUniform4fv loc, value end else raise TypeError.new('Uniform type not supported') end end |
#bind ⇒ Object
Bind the Shader
45 46 47 48 49 50 51 |
# File 'lib/rbglox/shader.rb', line 45 def bind glUseProgram id uniforms.each do |k, v| self[k] = v end end |
#free ⇒ Object
Free the Shader
87 88 89 90 91 92 93 94 95 |
# File 'lib/rbglox/shader.rb', line 87 def free glDetachShader id, vertex glDeleteShader vertex glDetachShader id, fragment glDeleteShader fragment glDeleteProgram id end |
#release ⇒ Object
Release the Shader
54 55 56 |
# File 'lib/rbglox/shader.rb', line 54 def release glUseProgram 0 end |
#reload ⇒ Object
Reload the Shader
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/rbglox/shader.rb', line 59 def reload content = File.read filename vert, frag, unis = parse content glShaderSource vertex, vert glCompileShader vertex log = glGetShaderInfoLog vertex puts log return if log =~ /error/i glShaderSource fragment, frag glCompileShader fragment log = glGetShaderInfoLog fragment puts log return if log =~ /error/i glLinkProgram id log = glGetProgramInfoLog id puts log return if log =~ /error/i @uniforms = unis end |