Class: Bestguigui::GLTFPrimitive

Inherits:
Object
  • Object
show all
Defined in:
lib/bestguigui_lab/gltf_primitive.rb

Overview

Un sous-maillage skinne d'un GLTFModel : geometrie de repos (espace du noeud squelette — position/normale/UV/joints/poids), indices et texture. #skin! recalcule les sommets deformes sur le CPU (aucun shader dans ce kit — meme philosophie que GouraudShading) et les renvoie au VBO ; #draw les affiche.

Constant Summary collapse

FLOATS_PER_VERTEX =

position (3) + texcoord (2) + normale ou teinte Gouraud (3)

8
STRIDE =

4 octets par float

FLOATS_PER_VERTEX * 4

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(geometry, texture, mesh_node, shading: :texture) ⇒ GLTFPrimitive

geometry : normals:, texcoords:, joints:, weights:, indices: (tableaux deja decodes par GLTFModel). mesh_node : indice du noeud qui porte ce maillage (cf. GLTFSkeleton#joint_matrices).



18
19
20
21
22
23
24
25
26
27
# File 'lib/bestguigui_lab/gltf_primitive.rb', line 18

def initialize(geometry, texture, mesh_node, shading: :texture)
    assign_geometry(geometry)
    @texture = texture || GLTexture.blank
    @mesh_node = mesh_node
    @gouraud = GouraudShading.new if shading == :gouraud
    @material_color = [1.0, 1.0, 1.0]
    @vertex_count = @positions.length
    @vbo = GLBuffer.upload(Array.new(@vertex_count * FLOATS_PER_VERTEX, 0.0).pack('F*'))
    @ibo = upload_index_buffer
end

Instance Attribute Details

#mesh_node ⇒ Object (readonly)

Returns the value of attribute mesh_node.



13
14
15
# File 'lib/bestguigui_lab/gltf_primitive.rb', line 13

def mesh_node
  @mesh_node
end

Instance Method Details

#draw ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/bestguigui_lab/gltf_primitive.rb', line 39

def draw
    glEnable(GL_TEXTURE_2D)
    glBindTexture(GL_TEXTURE_2D, @texture.tex_name)
    glBindBuffer(GL_ARRAY_BUFFER, @vbo)
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, @ibo)

    enable_vertex_arrays
    glDrawElements(GL_TRIANGLES, @indices.length, GL_UNSIGNED_INT, 0)
    disable_vertex_arrays

    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0)
    glBindBuffer(GL_ARRAY_BUFFER, 0)
    glDisable(GL_TEXTURE_2D)
end

#skin!(joint_matrices) ⇒ Object

joint_matrices : une matrice par joint (meme ordre que WEIGHTS_0/ JOINTS_0, cf. GLTFSkeleton#joint_matrices). A appeler avant chaque draw ou la pose a change (typiquement une fois par frame, depuis GLTFModel#update).



33
34
35
36
37
# File 'lib/bestguigui_lab/gltf_primitive.rb', line 33

def skin!(joint_matrices)
    data = Array.new(@vertex_count) { |i| skin_vertex(i, joint_matrices) }.flatten.pack('F*')
    glBindBuffer(GL_ARRAY_BUFFER, @vbo)
    glBufferSubData(GL_ARRAY_BUFFER, 0, data.bytesize, data)
end