Class: GL::Program

Inherits:
Object
  • Object
show all
Defined in:
lib/opengl-aux/program.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name = nil) {|_self| ... } ⇒ Program

Returns a new instance of Program

Yields:

  • (_self)

Yield Parameters:

  • _self (GL::Program)

    the object that the method was called on


40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/opengl-aux/program.rb', line 40

def initialize(name = nil)
  if name
    @name = name
  else
    @name = GL.glCreateProgram()
    raise GL::GLCreateError, "Unable to allocate shader" if @name == 0
  end

  @uniform_locations = {}

  yield self if block_given?
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name


38
39
40
# File 'lib/opengl-aux/program.rb', line 38

def name
  @name
end

Class Method Details

.current_programObject


21
22
23
# File 'lib/opengl-aux/program.rb', line 21

def current_program
  GL::glGetInteger(GL::GL_CURRENT_PROGRAM)
end

.preserve_bindingObject

Raises:

  • (ArgumentError)

25
26
27
28
29
30
31
32
33
# File 'lib/opengl-aux/program.rb', line 25

def preserve_binding
  raise ArgumentError, "No block given" unless block_given?
  current = current_program
  begin
    yield
  ensure
    GL.glUseProgram(current)
  end
end

Instance Method Details

#__reload_uniforms__Object


75
76
77
78
79
80
# File 'lib/opengl-aux/program.rb', line 75

def __reload_uniforms__
  locs = @uniform_locations
  locs.each_key do |key|
    locs[key] = GL.glGetUniformLocation(@name, key.to_s)
  end
end

#attach_shader(shader) ⇒ Object


114
115
116
117
118
119
120
# File 'lib/opengl-aux/program.rb', line 114

def attach_shader(shader)
  case shader
  when ::GL::Shader then GL.glAttachShader(@name, shader.name)
  else GL.glAttachShader(@name, shader)
  end
  self
end

#binaryObject


71
72
73
# File 'lib/opengl-aux/program.rb', line 71

def binary
  GL.glGetProgramBinary(@name)
end

#bind_attrib_location(attrib_index, attrib_name) ⇒ Object


151
152
153
154
# File 'lib/opengl-aux/program.rb', line 151

def bind_attrib_location(attrib_index, attrib_name)
  GL.glBindAttribLocation(@name, attrib_index, attrib_name.to_s)
  self
end

#bind_frag_data_location(color_number, frag_data_name) ⇒ Object


156
157
158
159
# File 'lib/opengl-aux/program.rb', line 156

def bind_frag_data_location(color_number, frag_data_name)
  GL.glBindFragDataLocation(@name, color_number, frag_data_name.to_s)
  self
end

#clear_uniform_location_cacheObject


143
144
145
# File 'lib/opengl-aux/program.rb', line 143

def clear_uniform_location_cache
  @uniform_locations.clear
end

#deleteObject


53
54
55
56
57
58
59
# File 'lib/opengl-aux/program.rb', line 53

def delete
  if @name != 0
    GL.glDeleteProgram(@name)
    @name = 0
  end
  self
end

#detach_shader(shader) ⇒ Object


122
123
124
125
126
127
128
# File 'lib/opengl-aux/program.rb', line 122

def detach_shader(shader)
  case shader
  when ::GL::Shader then GL.glDetachShader(@name, shader.name)
  else GL.glDetachShader(@name, shader)
  end
  self
end

#hint_uniform(name) ⇒ Object


130
131
132
133
134
# File 'lib/opengl-aux/program.rb', line 130

def hint_uniform(name)
  name = name.to_sym
  @uniform_locations[name] ||= nil
  self
end

#info_logObject


98
99
100
# File 'lib/opengl-aux/program.rb', line 98

def info_log
  GL.glGetProgramInfoLog(@name)
end

#is_program?Boolean


61
62
63
# File 'lib/opengl-aux/program.rb', line 61

def is_program?
  GL::glIsProgram(@name) != GL::GL_FALSE
end

82
83
84
85
86
87
# File 'lib/opengl-aux/program.rb', line 82

def link
  GL.glLinkProgram(@name)
  @link_status = nil
  __reload_uniforms__ if (link_successful = linked?)
  link_successful
end

#linked?Boolean


89
90
91
# File 'lib/opengl-aux/program.rb', line 89

def linked?
  GL.glGetProgram(@name, GL::GL_LINK_STATUS) != GL::GL_FALSE
end

#load_binary(binary_format, binary_string) ⇒ Object


65
66
67
68
69
# File 'lib/opengl-aux/program.rb', line 65

def load_binary(binary_format, binary_string)
  GL.glProgramBinary(@name, binary_format, binary_string, binary_string.bytesize)
  __reload_uniforms__ if (link_successful = linked?)
  link_successful
end

#subroutine_uniform_location(shader_kind, uniform_name) ⇒ Object


147
148
149
# File 'lib/opengl-aux/program.rb', line 147

def subroutine_uniform_location(shader_kind, uniform_name)
  GL.glGetSubroutineUniformLocation(@name, shader_kind, uniform_name)
end

#uniform_location(name) ⇒ Object Also known as: []


136
137
138
139
140
# File 'lib/opengl-aux/program.rb', line 136

def uniform_location(name)
  uniform_sym = name.to_sym
  locations = @uniform_locations
  locations[uniform_sym] ||= GL.glGetUniformLocation(@name, name.to_s)
end

#useObject


102
103
104
105
106
107
108
109
110
111
112
# File 'lib/opengl-aux/program.rb', line 102

def use
  if block_given?
    self.class.preserve_binding do
      use
      yield self
    end
  else
    GL.glUseProgram(@name)
    self
  end
end

#valid?Boolean


93
94
95
96
# File 'lib/opengl-aux/program.rb', line 93

def valid?
  GL.glValidateProgram(@name)
  GL.glGetProgram(@name, GL::GL_VALIDATE_STATUS) != GL::GL_FALSE
end