Class: Bestguigui::Render3D::Mesh

Inherits:
Object
  • Object
show all
Includes:
OpenGL
Defined in:
lib/bestguigui/render_3d/mesh.rb

Overview

Géométrie envoyée au GPU une fois (VBO/VAO), dessinée en un seul appel — remplace glBegin/glEnd (un aller-retour CPU->GPU par sommet, très lent et retiré du coeur d'OpenGL moderne).

# positions + UV entrelacés : x, y, z, u, v par sommet
mesh = Bestguigui::Render3D::Mesh.new([
-0.5, 0.0, 0.0,  0.0, 0.0,
 0.5, 0.0, 0.0,  1.0, 0.0,
 0.0, 1.0, 0.0,  0.5, 1.0
])
mesh.draw

layout: :pos_uv_normal pour ajouter une normale par sommet (x, y, z, u, v, nx, ny, nz — 8 floats/sommet) — nécessaire pour Shader.gouraud (voir ObjModel.new(..., lighting: true)).

Constant Summary collapse

VERTEX_SIZE =
{ pos_uv: 5, pos_uv_normal: 8 }.freeze

Instance Method Summary collapse

Constructor Details

#initialize(vertices, layout: :pos_uv) ⇒ Mesh

Returns a new instance of Mesh.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/bestguigui/render_3d/mesh.rb', line 23

def initialize(vertices, layout: :pos_uv)
  vertex_size = VERTEX_SIZE.fetch(layout)
  @count = vertices.size / vertex_size

  vao_buf = " " * 4
  glGenVertexArrays(1, vao_buf)
  @vao = vao_buf.unpack1("L")
  glBindVertexArray(@vao)

  vbo_buf = " " * 4
  glGenBuffers(1, vbo_buf)
  @vbo = vbo_buf.unpack1("L")

  data = vertices.pack("F*")
  glBindBuffer(GL_ARRAY_BUFFER, @vbo)
  glBufferData(GL_ARRAY_BUFFER, data.bytesize, data, GL_STATIC_DRAW)

  stride = vertex_size * 4
  glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, stride, 0)
  glEnableVertexAttribArray(0)
  glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, stride, 3 * 4)
  glEnableVertexAttribArray(1)

  if layout == :pos_uv_normal
    glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, stride, 5 * 4)
    glEnableVertexAttribArray(2)
  end
end

Instance Method Details

#drawObject



52
53
54
55
# File 'lib/bestguigui/render_3d/mesh.rb', line 52

def draw
  glBindVertexArray(@vao)
  glDrawArrays(GL_TRIANGLES, 0, @count)
end