Class: RubyGL::VertexArray

Inherits:
Object
  • Object
show all
Defined in:
lib/rubygl/memory.rb

Instance Method Summary collapse

Constructor Details

#initialize(vertex_array) ⇒ VertexArray

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.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# 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

Instance Method Details

#draw(components) ⇒ Object



31
32
33
34
35
# 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) ⇒ Object



37
38
39
40
41
# 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

#releaseObject

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.



66
67
68
69
70
71
72
73
74
75
# 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) ⇒ Object



43
44
45
46
47
# 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) ⇒ Object

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



50
51
52
53
54
55
56
57
58
59
60
61
# 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