Class: Bestguigui::ObjModel

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

Overview

Charge un modèle .obj/.mtl et le dessine via un VBO texturé. shading: :gouraud ajoute, en plus de la texture (ou d'un blanc si absente), une teinte par sommet calculée une fois au chargement à partir de sa normale et d'une lumière fixe (voir GouraudShading) : simule le rendu bas-poly des perso PSX (FF7 & co), sans lumière OpenGL temps réel.

Constant Summary collapse

FLOATS_PER_VERTEX =

position (3) + texcoord (2) + normale ou teinte Gouraud (3) par sommet.

8
STRIDE =

4 octets par float

FLOATS_PER_VERTEX * 4

Instance Method Summary collapse

Constructor Details

#initialize(obj_path, shading: :texture) ⇒ ObjModel

Returns a new instance of ObjModel.



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/bestguigui_lab/obj_model.rb', line 14

def initialize(obj_path, shading: :texture)
    super()
    @shading = shading
    # rubocop:disable-next Style/ParallelAssignment -- reste sous la limite de Metrics/MethodLength

    @vertices, @tex_coords, @normals, @faces = [], [], [], []
    @texture = nil
    @material_color = [1.0, 1.0, 1.0]
    parse_obj(obj_path)
    @texture ||= GLTexture.blank
    @vertex_count = @faces.size
    @vbo = build_vertex_buffer
end

Instance Method Details

#draw ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/bestguigui_lab/obj_model.rb', line 27

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

    enable_vertex_arrays
    glDrawArrays(GL_TRIANGLES, 0, @vertex_count)
    disable_vertex_arrays

    glBindBuffer(GL_ARRAY_BUFFER, 0)
    glDisable(GL_TEXTURE_2D)
end