Module: GL

Extended by:
GL
Included in:
GL
Defined in:
lib/opengl-aux/gl.rb,
lib/opengl-aux/error.rb,
lib/opengl-aux/version.rb

Overview

Extensions to the opengl-core Gl module.

Defined Under Namespace

Classes: Buffer, GLCreateError, GLError, GLProgramBinary, GLStateError, Program, Shader, Texture, VertexArray

Constant Summary collapse

GLObject =
Snow::CStruct.new { gl_uint :name }
OPENGL_AUX_VERSION =
'1.0.0.pre3'

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.__box_types__Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


47
48
49
# File 'lib/opengl-aux/gl.rb', line 47

def __box_types__
  @__box_types__
end

Class Method Details

.__boxed_type__(typename, &block) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a boxed type for the given typename. All boxed types contain a single member, 'name', which is of the boxed type. Boxed types are created on demand and cached.


79
80
81
82
83
84
85
# File 'lib/opengl-aux/gl.rb', line 79

def __boxed_type__(typename, &block)
  box_types = (__box_types__ ||= Hash.new do |hash, type|
    hash[type] = Snow::CStruct.new { __send__(type, :name) }
  end)

  box_types[typename]
end

.__define_gl_delete_object_method__(name, type) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Similar to define_gl_gen_object_method, except for deletion. Takes four possible types of objects to delete: a GLObject or an array of GLObjects, a Fixnum, or an array of Fixnums. Any other types will raise an error, and any type contained by an Array that is not implicitly convertible to a Fixnum will also raise an error.


125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/opengl-aux/gl.rb', line 125

def __define_gl_delete_object_method__(name, type)
  self.module_exec(name, :"#{name}__") do |func_name, raw_name|

    define_method(func_name, -> (objects) do
      case objects
      when GLObject
        send(raw_name, 1, objects.address)
      when GLObject::Array
        send(raw_name, objects.length, objects.address)
      when Fixnum
        GL.__temp_alloc__(GLObject) do |p|
          p.name = objects
          send(raw_name, 1, p.address)
        end
      when Array # Assumes an array of fixnums
        GL.__temp_alloc__(GLObject::Array, objects.length) do |p|
          objects.each_with_index { |e, i| p[i].name = e }
          send(raw_name, p.length, p.address)
        end
      else
        raise ArgumentError,
        "Invalid object passed to #{name} for deletion: #{objects.inspect}"
      end
      self
    end) # define_method

  end # module_exec
end

.__define_gl_gen_object_method__(name, type) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Wraps a previously-defined native glGen* function that returns the given type of object. The glGen function returns one or more GL object names. If returning a single name, the return type is Fixnum, otherwise it's an array of Fixnums.


94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/opengl-aux/gl.rb', line 94

def __define_gl_gen_object_method__(name, type)
  self.module_exec(name, :"#{name}__", GL.__boxed_type__(type)) do
    |func_name, raw_name, box_type|

    define_method(func_name, -> (count) do
      return nil if count <= 0

      if count == 1
        GL.__temp_alloc__(box_type) do |p|
          send(raw_name, count, p.address)
          p.name
        end
      else
        GL.__temp_alloc__(box_type::Array, count) do |p|
          send(raw_name, count, p.address)
          Array.new(count) { |i| p[i].name }
        end
      end
    end) # define_method

  end # module_exec
end

.__define_gl_get_method__(name, type) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Defines a glGet* method that returns a single value of the given type.


158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/opengl-aux/gl.rb', line 158

def __define_gl_get_method__(name, type)
  self.module_exec(name, :"#{name}v__", GL.__boxed_type__(type)) do
    |func_name, raw_name, box_type|

    define_method(func_name, -> (pname) do
      GL.__temp_alloc__(box_type) do |p|
        send(raw_name, pname, p.address)
        p.name
      end
    end) # define_method

  end # module_exec
end

.__temp_alloc__(type, *args, &block) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Temporarily allocates the given type, yields it to a block, and releases the allocated memory. Uses alloca if available. Any additional arguments to this function are forwarded to the type's allocator.


55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/opengl-aux/gl.rb', line 55

def __temp_alloc__(type, *args, &block)
  result = nil

  if type.respond_to?(:alloca)
    result = type.alloca(*args, &block)
  else
    type.new(*args) do |p|
      begin
        result = yield(p)
      ensure
        p.free!
      end
    end
  end

  result
end

.glDeleteBuffers(count, objects) ⇒ Object


191
# File 'lib/opengl-aux/gl.rb', line 191

__define_gl_delete_object_method__ :glDeleteBuffers, :gl_uint

.glDeleteFramebuffers(count, objects) ⇒ Object


209
# File 'lib/opengl-aux/gl.rb', line 209

__define_gl_delete_object_method__ :glDeleteFramebuffers, :gl_uint

.glDeleteQueries(count, objects) ⇒ Object


197
# File 'lib/opengl-aux/gl.rb', line 197

__define_gl_delete_object_method__ :glDeleteQueries, :gl_uint

.glDeleteRenderbuffers(count, objects) ⇒ Object


215
# File 'lib/opengl-aux/gl.rb', line 215

__define_gl_delete_object_method__ :glDeleteRenderbuffers, :gl_uint

.glDeleteRenderbuffersProgramPipelines(count, objects) ⇒ Object


221
# File 'lib/opengl-aux/gl.rb', line 221

__define_gl_delete_object_method__ :glDeleteProgramPipelines, :gl_uint

.glDeleteRenderbuffersTrasnformFeedbacks(count, objects) ⇒ Object


227
# File 'lib/opengl-aux/gl.rb', line 227

__define_gl_delete_object_method__ :glDeleteTransformFeedbacks, :gl_uint

.glDeleteSamplers(count, objects) ⇒ Object


203
# File 'lib/opengl-aux/gl.rb', line 203

__define_gl_delete_object_method__ :glDeleteSamplers, :gl_uint

.glDeleteTextures(count, objects) ⇒ Object


179
# File 'lib/opengl-aux/gl.rb', line 179

__define_gl_delete_object_method__ :glDeleteTextures, :gl_uint

.glDeleteVertexArrays(count, objects) ⇒ Object


185
# File 'lib/opengl-aux/gl.rb', line 185

__define_gl_delete_object_method__ :glDeleteVertexArrays, :gl_uint

.glGenBuffers(count) ⇒ Object

Returns an array of generated buffer object names.


189
# File 'lib/opengl-aux/gl.rb', line 189

__define_gl_gen_object_method__ :glGenBuffers, :gl_uint

.glGenFramebuffers(count) ⇒ Object

Returns an array of generated framebuffer object names.


207
# File 'lib/opengl-aux/gl.rb', line 207

__define_gl_gen_object_method__ :glGenFramebuffers, :gl_uint

.glGenQueries(count) ⇒ Object

Returns an array of generated query object names.


195
# File 'lib/opengl-aux/gl.rb', line 195

__define_gl_gen_object_method__ :glGenQueries, :gl_uint

.glGenRenderbuffers(count) ⇒ Object

Returns an array of generated renderbuffer object names.


213
# File 'lib/opengl-aux/gl.rb', line 213

__define_gl_gen_object_method__ :glGenRenderbuffers, :gl_uint

.glGenRenderbuffersProgramPipelines(count) ⇒ Object

Returns an array of generated program pipeline object names.


219
# File 'lib/opengl-aux/gl.rb', line 219

__define_gl_gen_object_method__ :glGenProgramPipelines, :gl_uint

.glGenRenderbuffersTrasnformFeedbacks(count) ⇒ Object

Returns an array of generated transform feedback objects


225
# File 'lib/opengl-aux/gl.rb', line 225

__define_gl_gen_object_method__ :glGenTransformFeedbacks, :gl_uint

.glGenSamplers(count) ⇒ Object

Returns an array of generated sampler object names.


201
# File 'lib/opengl-aux/gl.rb', line 201

__define_gl_gen_object_method__ :glGenSamplers, :gl_uint

.glGenTextures(count) ⇒ Object

Returns an array of generated texture names.


177
# File 'lib/opengl-aux/gl.rb', line 177

__define_gl_gen_object_method__ :glGenTextures, :gl_uint

.glGenVertexArrays(count) ⇒ Object

Returns an array of generated vertex array object names.


183
# File 'lib/opengl-aux/gl.rb', line 183

__define_gl_gen_object_method__ :glGenVertexArrays, :gl_uint

Instance Method Details

#assert_no_gl_error(msg = nil) ⇒ Object


36
37
38
39
40
41
42
# File 'lib/opengl-aux/gl.rb', line 36

def assert_no_gl_error(msg = nil)
  error = glGetError

  if error != GL_NO_ERROR
    raise GLStateError.new(error), msg
  end
end

#gl_extensionsObject

Gets an array of GL extensions. This calls glGetIntegerv and glGetStringi, so be aware that you should probably cache the results.


304
305
306
307
308
# File 'lib/opengl-aux/gl.rb', line 304

def gl_extensions
  Array.new(glGetInteger(GL_NUM_EXTENSIONS)) do |index|
    glGetStringi(GL_EXTENSIONS, index)
  end
end

#gl_rendererObject

Returns the renderer. Calls glGetString.


293
294
295
# File 'lib/opengl-aux/gl.rb', line 293

def gl_renderer
  glGetString(GL_RENDERER)
end

#gl_shading_language_versionObject

Returns the shading language version. Calls glGetString.


298
299
300
# File 'lib/opengl-aux/gl.rb', line 298

def gl_shading_language_version
  glGetString(GL_SHADING_LANGUAGE_VERSION)
end

#gl_vendorObject

Returns the implementation vendor. Calls glGetString.


288
289
290
# File 'lib/opengl-aux/gl.rb', line 288

def gl_vendor
  glGetString(GL_VENDOR)
end

#gl_versionObject

Returns the version or release number. Calls glGetString.


283
284
285
# File 'lib/opengl-aux/gl.rb', line 283

def gl_version
  glGetString(GL_VERSION)
end

#glGetBoolean(pname) ⇒ Boolean


235
236
237
238
239
240
241
# File 'lib/opengl-aux/gl.rb', line 235

def glGetBoolean(pname)
  GL.__temp_alloc__(GL.__boxed_type__(:gl_boolean)) do |p|
    p.name = 0
    glGetBooleanv(pname, p.address)
    p.name != GL_FALSE
  end
end

#glGetProgram(program, pname) ⇒ Object


333
334
335
336
337
338
# File 'lib/opengl-aux/gl.rb', line 333

def glGetProgram(program, pname)
  GL.__temp_alloc__(GL.__boxed_type__(:gl_int)) do |p|
    glGetProgramiv__(program, pname, p.address)
    p.name
  end
end

#glGetProgramBinary(program) ⇒ Object


350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
# File 'lib/opengl-aux/gl.rb', line 350

def glGetProgramBinary(program)
  binary_length = glGetProgram(program, GL_PROGRAM_BINARY_LENGTH)

  return nil if binary_length <= 0

  GL.__temp_alloc__(GL.__boxed_type__(:gl_enum)) do |format_buffer; binary_buffer|
    binary_buffer = ' ' * binary_length

    glGetProgramBinary(
      program,
      binary_buffer.bytesize,
      0,
      format_buffer.address,
      binary_buffer.address
      )

    GLProgramBinary[format_buffer.name, binary_buffer]
  end
end

#glGetProgramInfoLog(program) ⇒ Object


340
341
342
343
344
345
346
# File 'lib/opengl-aux/gl.rb', line 340

def glGetProgramInfoLog(program)
  length = glGetProgram(program, GL_INFO_LOG_LENGTH)
  return '' if length == 0
  output = ' ' * length
  glGetProgramInfoLog__(program, output.bytesize, 0, output)
  output
end

#glGetShader(shader, pname) ⇒ Object


310
311
312
313
314
315
# File 'lib/opengl-aux/gl.rb', line 310

def glGetShader(shader, pname)
  GL.__temp_alloc__(GL.__boxed_type__(:gl_int)) do |p|
    glGetShaderiv__(shader, pname, p.address)
    p.name
  end
end

#glGetShaderInfoLog(shader) ⇒ Object


317
318
319
320
321
322
323
# File 'lib/opengl-aux/gl.rb', line 317

def glGetShaderInfoLog(shader)
  length = glGetShader(shader, GL_INFO_LOG_LENGTH)
  return '' if length == 0
  output = ' ' * length
  glGetShaderInfoLog__(shader, output.bytesize, 0, output)
  output
end

#glGetShaderSource(shader) ⇒ Object


325
326
327
328
329
330
331
# File 'lib/opengl-aux/gl.rb', line 325

def glGetShaderSource(shader)
  length = glGetShader(shader, GL_SHADER_SOURCE_LENGTH)
  return '' if length == 0
  output = ' ' * length
  glGetShaderInfoLog__(shader, output.bytesize, 0, output)
  output
end

#glGetString(name) ⇒ String


244
245
246
# File 'lib/opengl-aux/gl.rb', line 244

def glGetString(name)
  glGetString__(name).to_s
end

#glGetStringi(name, index) ⇒ String


249
250
251
# File 'lib/opengl-aux/gl.rb', line 249

def glGetStringi(name, index)
  glGetStringi__(name, index).to_s
end

#glShaderSource(shader, sources) ⇒ Object

Assumes sources is a


254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# File 'lib/opengl-aux/gl.rb', line 254

def glShaderSource(shader, sources)
  ary_len = (source_array = sources.kind_of?(Array)) ? sources.length : 1

  lengths = GL.__boxed_type__(:gl_sizei)[ary_len]
  pointers = GL.__boxed_type__(:intptr_t)[ary_len]

  begin
    assign_block = -> (src, index) do
      lengths[index].name = src.bytesize
      pointers[index].name = Fiddle::Pointer[src].to_i
    end

    if source_array
      sources.each_with_index(&assign_block)
    else
      assign_block[sources, 0]
    end

    glShaderSource__(shader, ary_len, pointers.address, lengths.address)

  ensure
    lengths.free!
    pointers.free!
  end

  self
end