Class: Rendering::InstanceRenderer

Inherits:
Object
  • Object
show all
Defined in:
lib/engine/rendering/instance_renderer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mesh, material) ⇒ InstanceRenderer

Returns a new instance of InstanceRenderer.



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/engine/rendering/instance_renderer.rb', line 7

def initialize(mesh, material)
  @mesh = mesh
  @material = material
  @mesh_renderers = []
  @mesh_matrix_data = []

  setup_vertex_attribute_buffer
  setup_vertex_buffer
  setup_index_buffer
  generate_instance_vbo_buf
end

Instance Attribute Details

#materialObject (readonly)

Returns the value of attribute material.



5
6
7
# File 'lib/engine/rendering/instance_renderer.rb', line 5

def material
  @material
end

#meshObject (readonly)

Returns the value of attribute mesh.



5
6
7
# File 'lib/engine/rendering/instance_renderer.rb', line 5

def mesh
  @mesh
end

Instance Method Details

#add_instance(mesh_renderer) ⇒ Object



19
20
21
22
# File 'lib/engine/rendering/instance_renderer.rb', line 19

def add_instance(mesh_renderer)
  @mesh_renderers << mesh_renderer
  @mesh_matrix_data += mesh_renderer.game_object.model_matrix.to_a.flatten
end

#draw_allObject



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/engine/rendering/instance_renderer.rb', line 35

def draw_all
  set_material_per_frame_data

  GL.BindVertexArray(@vao)
  update_vbo_buf
  GL.BindBuffer(GL::ELEMENT_ARRAY_BUFFER, @ebo)
  GL.BindBuffer(GL::ARRAY_BUFFER, @instance_vbo)

  GL.DrawElementsInstanced(GL::TRIANGLES, mesh.index_data.length, GL::UNSIGNED_INT, 0, @mesh_renderers.count)
  GL.BindVertexArray(0)
  GL.BindBuffer(GL::ELEMENT_ARRAY_BUFFER, 0)
end

#remove_instance(mesh_renderer) ⇒ Object



24
25
26
27
28
# File 'lib/engine/rendering/instance_renderer.rb', line 24

def remove_instance(mesh_renderer)
  index = @mesh_renderers.index(mesh_renderer)
  @mesh_renderers.delete_at(index)
  @mesh_matrix_data[index * 16, 16] = []
end

#update_instance(mesh_renderer) ⇒ Object



30
31
32
33
# File 'lib/engine/rendering/instance_renderer.rb', line 30

def update_instance(mesh_renderer)
  index = @mesh_renderers.index(mesh_renderer)
  @mesh_matrix_data[index * 16, 16] = mesh_renderer.game_object.model_matrix.to_a.flatten
end