Class: Roglew::ShaderProgram

Inherits:
Object
  • Object
show all
Defined in:
lib/roglew/extensions/GL_VERSION_2_0/shader_program.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(handle) ⇒ ShaderProgram

Returns a new instance of ShaderProgram.



7
8
9
10
11
12
13
# File 'lib/roglew/extensions/GL_VERSION_2_0/shader_program.rb', line 7

def initialize(handle)
  @handle = handle
  @id = handle.bind { handle.glCreateProgram() }
  @attribs, @uniforms = {}, {}

  ObjectSpace.define_finalizer(self, self.class.finalize(@id, @handle))
end

Instance Attribute Details

#handleObject (readonly)

Returns the value of attribute handle.



3
4
5
# File 'lib/roglew/extensions/GL_VERSION_2_0/shader_program.rb', line 3

def handle
  @handle
end

#idObject (readonly)

Returns the value of attribute id.



3
4
5
# File 'lib/roglew/extensions/GL_VERSION_2_0/shader_program.rb', line 3

def id
  @id
end

#shadersObject (readonly)

Returns the value of attribute shaders.



3
4
5
# File 'lib/roglew/extensions/GL_VERSION_2_0/shader_program.rb', line 3

def shaders
  @shaders
end

Class Method Details

.finalize(id, handle) ⇒ Object



15
16
17
18
19
20
# File 'lib/roglew/extensions/GL_VERSION_2_0/shader_program.rb', line 15

def self.finalize(id, handle)
  proc do
    #puts "releasing program #{id}"
    handle.bind { handle.glDeleteProgram(id) }
  end
end

Instance Method Details

#attach(*shaders) ⇒ Object



22
23
24
25
26
# File 'lib/roglew/extensions/GL_VERSION_2_0/shader_program.rb', line 22

def attach(*shaders)
  @shaders = shaders unless shaders.length == 0
  @handle.bind { @shaders.each { |shader| @handle.glAttachShader(@id, shader.id) } }
  nil
end

#attrib_location(name) ⇒ Object



47
48
49
50
51
52
53
54
# File 'lib/roglew/extensions/GL_VERSION_2_0/shader_program.rb', line 47

def attrib_location(name)
  name = name.to_sym
  loc = @attribs[name]
  return loc if loc
  loc = @handle.bind { @handle.glGetAttribLocation(@id, name.to_s) }
  return nil if loc < 0
  @attribs[name] = loc
end

#attrib_locations(*names) ⇒ Object



56
57
58
# File 'lib/roglew/extensions/GL_VERSION_2_0/shader_program.rb', line 56

def attrib_locations(*names)
  @handle.bind { names.map { |name| attrib_location(name) } }
end

Raises:



28
29
30
31
# File 'lib/roglew/extensions/GL_VERSION_2_0/shader_program.rb', line 28

def link_program
  @handle.bind { @handle.glLinkProgram(@id) }
  raise CompileError, info_log unless link_status
end


33
34
35
# File 'lib/roglew/extensions/GL_VERSION_2_0/shader_program.rb', line 33

def link_status
  @handle.bind { |context| context.get_program(@id, GL::LINK_STATUS) }
end

#uniform_location(name) ⇒ Object



60
61
62
63
64
65
66
67
# File 'lib/roglew/extensions/GL_VERSION_2_0/shader_program.rb', line 60

def uniform_location(name)
  name = name.to_sym
  loc = @uniforms[name]
  return loc if loc
  loc = @handle.bind { @handle.glGetUniformLocation(@id, name.to_s) }
  return nil if loc < 0
  @uniforms[name] = loc
end

#uniform_locations(*names) ⇒ Object



69
70
71
# File 'lib/roglew/extensions/GL_VERSION_2_0/shader_program.rb', line 69

def uniform_locations(*names)
  @handle.bind { names.map { |name| uniform_location(name) } }
end

#use_programObject Also known as: use



37
38
39
40
41
42
43
44
45
# File 'lib/roglew/extensions/GL_VERSION_2_0/shader_program.rb', line 37

def use_program
  @handle.bind do
    @handle.glUseProgram(@id)
    if block_given?
      yield
      @handle.glUseProgram(0)
    end
  end
end