Class: RBGL::Engine::VertexBuffer

Inherits:
Object
  • Object
show all
Defined in:
lib/rbgl/engine/buffer.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(layout) ⇒ VertexBuffer

Returns a new instance of VertexBuffer.



145
146
147
148
149
# File 'lib/rbgl/engine/buffer.rb', line 145

def initialize(layout)
  @layout = layout
  @data = []
  @vertex_count = 0
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



143
144
145
# File 'lib/rbgl/engine/buffer.rb', line 143

def data
  @data
end

#layoutObject (readonly)

Returns the value of attribute layout.



143
144
145
# File 'lib/rbgl/engine/buffer.rb', line 143

def layout
  @layout
end

#vertex_countObject (readonly)

Returns the value of attribute vertex_count.



143
144
145
# File 'lib/rbgl/engine/buffer.rb', line 143

def vertex_count
  @vertex_count
end

Class Method Details

.from_array(layout, data) ⇒ Object



185
186
187
188
189
# File 'lib/rbgl/engine/buffer.rb', line 185

def self.from_array(layout, data)
  buffer = new(layout)
  data.each { |vertex| buffer.add_vertex(**vertex) }
  buffer
end

Instance Method Details

#add_vertex(**attributes) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/rbgl/engine/buffer.rb', line 151

def add_vertex(**attributes)
  validate_attribute_keys!(attributes)

  vertex_data = []
  @layout.attributes.each do |name, attr|
    raise ArgumentError, "Missing attribute: #{name}" unless attributes.key?(name)

    vertex_data.concat(attr.serialize(attributes[name]))
  end
  @data.concat(vertex_data)
  @vertex_count += 1
  self
end

#add_vertices(*vertices) ⇒ Object



165
166
167
168
# File 'lib/rbgl/engine/buffer.rb', line 165

def add_vertices(*vertices)
  vertices.each { |v| add_vertex(**v) }
  self
end

#get_vertex(index) ⇒ Object



170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/rbgl/engine/buffer.rb', line 170

def get_vertex(index)
  return nil if index < 0 || index >= @vertex_count

  start = index * @layout.stride
  vertex = {}

  @layout.attributes.each do |name, attr|
    offset = start + attr.offset
    values = @data[offset, attr.size]
    vertex[name] = attr.deserialize(values)
  end

  vertex
end