Class: CyberarmEngine::WavefrontParser
  
  
  
  
    
      Class Method Summary
      collapse
    
    
  
    
      Instance Method Summary
      collapse
    
    
  
  
  
  
  
  
  
  
  
  
  #add_material, #change_object, #current_material, find, inherited, #initialize, #set_material, #set_object, supported_formats
  
    Class Method Details
    
      
  
  
    .handles  ⇒ Object 
  
  
  
  
    | 
3
4
5 | # File 'lib/cyberarm_engine/model/parsers/wavefront_parser.rb', line 3
def self.handles
  [:obj]
end
 | 
 
    
   
  
    Instance Method Details
    
      
  
  
    #add_normal(array)  ⇒ Object 
  
  
  
  
    | 
128
129
130
131
132
133
134
135
136
137
138
139 | # File 'lib/cyberarm_engine/model/parsers/wavefront_parser.rb', line 128
def add_normal(array)
  vert = nil
  if array.size == 5
    vert = Vector.new(Float(array[1]), Float(array[2]), Float(array[3]), Float(array[4]))
  elsif array.size == 4
    vert = Vector.new(Float(array[1]), Float(array[2]), Float(array[3]), 1.0)
  else
    raise
  end
  @model.current_object.normals << vert
  @model.normals << vert
end
 | 
 
    
      
  
  
    #add_texture_coordinate(array)  ⇒ Object 
  
  
  
  
    | 
141
142
143
144
145
146
147
148
149
150
151
152 | # File 'lib/cyberarm_engine/model/parsers/wavefront_parser.rb', line 141
def add_texture_coordinate(array)
  texture = nil
  if array.size == 4
    texture = Vector.new(Float(array[1]), 1 - Float(array[2]), Float(array[3]))
  elsif array.size == 3
    texture = Vector.new(Float(array[1]), 1 - Float(array[2]), 1.0)
  else
    raise
  end
  @model.uvs << texture
  @model.current_object.uvs << texture
end
 | 
 
    
      
  
  
    #add_vertex(array)  ⇒ Object 
  
  
  
  
    | 
114
115
116
117
118
119
120
121
122
123
124
125
126 | # File 'lib/cyberarm_engine/model/parsers/wavefront_parser.rb', line 114
def add_vertex(array)
  @model.vertex_count += 1
  vert = nil
  if array.size == 5
    vert = Vector.new(Float(array[1]), Float(array[2]), Float(array[3]), Float(array[4]))
  elsif array.size == 4
    vert = Vector.new(Float(array[1]), Float(array[2]), Float(array[3]), 1.0)
  else
    raise
  end
  @model.current_object.vertices << vert
  @model.vertices << vert
end
 | 
 
    
      
  
  
    #parse  ⇒ Object 
  
  
  
  
    | 
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77 | # File 'lib/cyberarm_engine/model/parsers/wavefront_parser.rb', line 7
def parse
  lines = 0
  list = File.read(@model.file_path).split("\n")
  list.each do |line|
    lines += 1
    line = line.strip
    array = line.split(" ")
    case array[0]
    when "mtllib"
      @model.material_file = array[1]
      parse_mtllib
    when "usemtl"
      set_material(array[1])
    when "o"
      change_object(nil, array[1])
    when "s"
      set_smoothing(array[1])
    when "v"
      add_vertex(array)
    when "vt"
      add_texture_coordinate(array)
    when "vn"
      add_normal(array)
    when "f"
      verts = []
      uvs   = []
      norms = []
      array[1..3].each do |f|
        verts << f.split("/")[0]
        uvs   << f.split("/")[1]
        norms << f.split("/")[2]
      end
      face = Face.new
      face.vertices = []
      face.uvs      = []
      face.normals  = []
      face.colors   = []
      face.material = current_material
      face.smoothing = @model.smoothing
      mat   = face.material.diffuse
      color = mat
      verts.each_with_index do |v, index|
        if uvs.first != ""
          face.vertices << @model.vertices[Integer(v) - 1]
          face.uvs      << @model.uvs[Integer(uvs[index]) - 1]
          face.normals  << @model.normals[Integer(norms[index]) - 1]
          face.colors   << color
        else
          face.vertices << @model.vertices[Integer(v) - 1]
          face.uvs      << nil
          face.normals  << @model.normals[Integer(norms[index]) - 1]
          face.colors   << color
        end
      end
      @model.current_object.faces << face
      @model.faces << face
    end
  end
  @model.calculate_bounding_box(@model.vertices, @model.bounding_box)
  @model.objects.each do |o|
    @model.calculate_bounding_box(o.vertices, o.bounding_box)
  end
end
 | 
 
    
      
  
  
    #parse_mtllib  ⇒ Object 
  
  
  
  
    | 
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108 | # File 'lib/cyberarm_engine/model/parsers/wavefront_parser.rb', line 79
def parse_mtllib
  file = File.open(@model.file_path.sub(File.basename(@model.file_path), "") + @model.material_file, "r")
  file.readlines.each do |line|
    array = line.strip.split(" ")
    case array.first
    when "newmtl"
      material = Model::Material.new(array.last)
      @model.current_material = array.last
      @model.materials[array.last] = material
    when "Ns" 
    when "Ka" 
      @model.materials[@model.current_material].ambient  = Color.new(Float(array[1]), Float(array[2]),
                                                                     Float(array[3]))
    when "Kd" 
      @model.materials[@model.current_material].diffuse  = Color.new(Float(array[1]), Float(array[2]),
                                                                     Float(array[3]))
    when "Ks" 
      @model.materials[@model.current_material].specular = Color.new(Float(array[1]), Float(array[2]),
                                                                     Float(array[3]))
    when "Ke" 
    when "Ni" 
    when "d"  
    when "illum" 
    when "map_Kd" 
      texture = File.basename(array[1])
      texture_path = "#{File.expand_path('../../', @model.file_path)}/textures/#{texture}"
      @model.materials[@model.current_material].set_texture(texture_path)
    end
  end
end
 | 
 
    
      
  
  
    #set_smoothing(value)  ⇒ Object 
  
  
  
  
    | 
110
111
112 | # File 'lib/cyberarm_engine/model/parsers/wavefront_parser.rb', line 110
def set_smoothing(value)
  @model.smoothing = value == "1"
end
 |