Parent

RubyGL::VertexArray

Public Class Methods

new(vertex_array) click to toggle source

Allocates a GPU buffer for vertex_array and transfers the data as a vertex attribute array. This allows you to access the given data from within any shader as long as it is bound beforehand.

# File lib/rubygl/memory.rb, line 9
def initialize(vertex_array)
    buff_ptr = FFI::MemoryPointer.new(:uint)
    Native.glGenBuffers(1, buff_ptr)
    
    @buffer_id = buff_ptr.get_int(0)
    @buffer_elements = vertex_array.size
    @buffer_valid = true
    
    if @buffer_id <= 0 then
        raise "Could Not Allocate A GPU Buffer For The VertexArray Object"
    end
    
    Native.glBindBuffer(Native::GL_ARRAY_BUFFER, @buffer_id)
    
    vertex_data = FFI::MemoryPointer.new(:float, vertex_array.size)
    vertex_data.write_array_of_float(vertex_array)
    
    Native.glBufferData(Native::GL_ARRAY_BUFFER, vertex_data.size, vertex_data, Native::GL_STATIC_DRAW)
    
    Native.glBindBuffer(Native::GL_ARRAY_BUFFER, 0)
end

Public Instance Methods

draw(components) click to toggle source
# File lib/rubygl/memory.rb, line 31
def draw(components)
    raise "Call To VertexArray#draw On Invalid Object" unless @buffer_valid

    Native.glDrawArrays(Native::GL_TRIANGLES, 0, @buffer_elements / components)
end
draw_instanced(components, iterations) click to toggle source
# File lib/rubygl/memory.rb, line 37
def draw_instanced(components, iterations)
    raise "Call To VertexArray#draw_instanced On Invalid Object" unless @buffer_valid
    
    Native.glDrawArraysInstanced(Native::GL_TRIANGLES, 0, @buffer_elements / components, iterations)
end
release() click to toggle source

This frees the currently allocated GPU buffer for this VertexArray and invalidates this VertexArray object. Any calls to this object after calling this method will throw a runtime error.

# File lib/rubygl/memory.rb, line 66
def release()
    raise "Call To VertexArray#release On Invalid Object" unless @buffer_valid

    buff_ptr = FFI::MemoryPointer.new(:uint)
    buff_ptr.write_uint(@buffer_id)
    
    Native.glDeleteBuffers(1, buff_ptr)
    
    @buffer_valid = false
end
vertex_attrib_div(location, instances_per_change) click to toggle source
# File lib/rubygl/memory.rb, line 43
def vertex_attrib_div(location, instances_per_change)
    raise "Call To VertexArray#vertex_attrib_div On Invalid Object" unless @buffer_valid
    
    Native.glVertexAttribDivisor(location, instances_per_change)
end
vertex_attrib_ptr(location, components) click to toggle source

Binds the VertexArray to the attribute at location within a shader.

# File lib/rubygl/memory.rb, line 50
def vertex_attrib_ptr(location, components)
    raise "Call To VertexArray#vertex_attrib_ptr On Invalid Object" unless @buffer_valid

    # TODO: Move This To Constructor In The Future
    Native.glEnableVertexAttribArray(location)
    
    Native.glBindBuffer(Native::GL_ARRAY_BUFFER, @buffer_id)
    
    Native.glVertexAttribPointer(location, components, Native::GL_FLOAT, Native::GL_FALSE, 0, FFI::MemoryPointer::NULL)

    Native.glBindBuffer(Native::GL_ARRAY_BUFFER, 0)
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.