Class: Rugl::Program
- Inherits:
-
Object
- Object
- Rugl::Program
- Defined in:
- lib/rugl/shader.rb
Constant Summary collapse
- GLSL_UNIFORM_TYPE_MAP =
{ "float" => :float, "vec2" => :vec2, "vec3" => :vec3, "vec4" => :vec4, "int" => :int, "bool" => :bool, "mat2" => :mat2, "mat3" => :mat3, "mat4" => :mat4, "sampler2D" => :sampler2D }.freeze
Instance Attribute Summary collapse
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#uniform_types ⇒ Object
readonly
Returns the value of attribute uniform_types.
Class Method Summary collapse
Instance Method Summary collapse
- #attribute_location(name) ⇒ Object
- #destroy ⇒ Object
-
#initialize(gl:, vert_source:, frag_source:, debug: true) ⇒ Program
constructor
A new instance of Program.
- #uniform_location(name) ⇒ Object
- #use ⇒ Object
Constructor Details
#initialize(gl:, vert_source:, frag_source:, debug: true) ⇒ Program
Returns a new instance of Program.
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/rugl/shader.rb', line 61 def initialize(gl:, vert_source:, frag_source:, debug: true) @gl = gl @debug = debug @id = Util.gl_call(@gl, :CreateProgram) @uniform_locations = {} @attribute_locations = {} vertex_shader = Shader.compile(gl: @gl, type: :vertex, source: vert_source, debug: @debug) fragment_shader = Shader.compile(gl: @gl, type: :fragment, source: frag_source, debug: @debug) Util.gl_call(@gl, :AttachShader, @id, vertex_shader) Util.gl_call(@gl, :AttachShader, @id, fragment_shader) Util.gl_call(@gl, :LinkProgram, @id) validate_link! detach_and_delete_shader(vertex_shader) detach_and_delete_shader(fragment_shader) @uniform_types = self.class.extract_uniforms(vert_source.to_s + "\n" + frag_source.to_s) end |
Instance Attribute Details
#id ⇒ Object (readonly)
Returns the value of attribute id.
59 60 61 |
# File 'lib/rugl/shader.rb', line 59 def id @id end |
#uniform_types ⇒ Object (readonly)
Returns the value of attribute uniform_types.
59 60 61 |
# File 'lib/rugl/shader.rb', line 59 def uniform_types @uniform_types end |
Class Method Details
.extract_uniforms(source) ⇒ Object
114 115 116 117 118 119 |
# File 'lib/rugl/shader.rb', line 114 def self.extract_uniforms(source) source.scan(/uniform\s+([A-Za-z0-9_]+)\s+([A-Za-z0-9_]+)\s*;/).each_with_object({}) do |(type_name, var_name), memo| mapped_type = GLSL_UNIFORM_TYPE_MAP.fetch(type_name, type_name.to_sym) memo[var_name.to_sym] = mapped_type end end |
Instance Method Details
#attribute_location(name) ⇒ Object
97 98 99 100 101 102 103 104 |
# File 'lib/rugl/shader.rb', line 97 def attribute_location(name) key = name.to_s @attribute_locations[key] ||= begin Util.gl_call(@gl, :GetAttribLocation, @id, key) rescue StandardError -1 end end |
#destroy ⇒ Object
106 107 108 109 110 111 112 |
# File 'lib/rugl/shader.rb', line 106 def destroy return if @id.nil? Util.gl_call(@gl, :DeleteProgram, @id) @id = nil nil end |