Class: Wavefront::Object

Inherits:
Object
  • Object
show all
Defined in:
lib/wavefront/wavefront_object.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, file) ⇒ Object

Returns a new instance of Object.



5
6
7
8
9
10
# File 'lib/wavefront/wavefront_object.rb', line 5

def initialize name, file
  @name = name
  @file = file
  @vertices, @texture_coordinates, @normals, @faces, @groups = [], [], [], [], []
  parse!
end

Instance Attribute Details

#facesObject (readonly)

Returns the value of attribute faces.



3
4
5
# File 'lib/wavefront/wavefront_object.rb', line 3

def faces
  @faces
end

#fileObject (readonly)

Returns the value of attribute file.



3
4
5
# File 'lib/wavefront/wavefront_object.rb', line 3

def file
  @file
end

#groupsObject (readonly)

Returns the value of attribute groups.



3
4
5
# File 'lib/wavefront/wavefront_object.rb', line 3

def groups
  @groups
end

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/wavefront/wavefront_object.rb', line 3

def name
  @name
end

#normalsObject (readonly)

Returns the value of attribute normals.



3
4
5
# File 'lib/wavefront/wavefront_object.rb', line 3

def normals
  @normals
end

#texture_coordinatesObject (readonly)

Returns the value of attribute texture_coordinates.



3
4
5
# File 'lib/wavefront/wavefront_object.rb', line 3

def texture_coordinates
  @texture_coordinates
end

#verticesObject (readonly)

Returns the value of attribute vertices.



3
4
5
# File 'lib/wavefront/wavefront_object.rb', line 3

def vertices
  @vertices
end

Instance Method Details

#compute_vertex_and_index_bufferObject



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/wavefront/wavefront_object.rb', line 102

def compute_vertex_and_index_buffer
  vertex_buffer, index_buffer, composite_indices, current_index = [], [], {}, -1

  groups.map { |g| (g.triangles + g.smoothing_groups.map(&:triangles).flatten) }.flatten.each do |triangle|
    triangle.vertices.each do |v|
      i = composite_indices[v.composite_index]
      if i.nil?
        current_index += 1
        vertex_buffer << v
        i = current_index
        composite_indices[v.composite_index] = i
      end
      index_buffer << i
    end
  end

  {:vertex_buffer => vertex_buffer, :index_buffer => index_buffer}
end

#compute_vertex_bufferObject



91
92
93
94
95
96
97
98
99
100
# File 'lib/wavefront/wavefront_object.rb', line 91

def compute_vertex_buffer
  vertex_buffer = []
  groups.each do |group|
    group.triangles.each { |t| vertex_buffer << t.vertices }
    group.smoothing_groups.each do |smoothing_group|
      smoothing_group.triangles.each { |t| vertex_buffer << t.vertices }
    end
  end
  vertex_buffer.flatten
end

#export(file_name) ⇒ Object



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
# File 'lib/wavefront/wavefront_object.rb', line 34

def export file_name
  unless /\.obj$/.match file_name
    file_name += ".obj"
  end

  ::File.delete file_name if ::File.exist? file_name
  open file_name, 'a' do |f|
    f.puts "# Exported from Wavefront Ruby Gem Version #{Wavefront::VERSION}"
    f.puts "o #{name}"
    f.puts "##{vertices.size} vertices, #{num_faces} faces"
    vertices.each { |v| f.puts "v #{v}" }
    texture_coordinates.each { |t| f.puts "vt #{t}" }
    normals.each { |n| f.puts "vn #{n}" }
    groups.each do |group|
      f.puts "g ##{group.name}"
      group.triangles.each do |t|
        f.puts 'f ' + t.vertices.map { |v| [v.position_index, v.texture_index, v.normal_index].join '/' }.join(' ')
      end
      group.smoothing_groups.each do |smoothing_group|
        f.puts "s #{smoothing_group.name}"
        smoothing_group.triangles.each do |t|
          f.puts 'f ' + t.vertices.map { |v| [v.position_index, v.texture_index, v.normal_index].join '/' }.join(' ')
        end
      end
    end
  end
end

#export_simple(file_name, export_index_buffer = false) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/wavefront/wavefront_object.rb', line 62

def export_simple file_name, export_index_buffer = false
  file_name += ".simple" unless /\.simple$/.match file_name

  if export_index_buffer
    vi = compute_vertex_and_index_buffer
    vertex_buffer = vi[:vertex_buffer]
    index_buffer = vi[:index_buffer]
  else
    vertex_buffer = compute_vertex_buffer
  end

  ::File.delete file_name if ::File.exist? file_name
  open file_name, 'a' do |f|
    f.puts "# Exported in Simple Format from Wavefront Ruby Gem Version #{Wavefront::VERSION}"
    f.puts "#vertices"
    vertex_buffer.each do |v|
      vertex_str = "p,#{v.position.to_a.join ','}"
      vertex_str += ",n,#{v.normal.to_a.join ','}" if v.normal
      vertex_str += ",t,#{v.tex.to_a.join ','}" if v.tex
      f.puts vertex_str
    end
    if export_index_buffer
      f.puts "\n\n\n#indices"
      f.puts index_buffer.join ','
    end
  end
end

#num_facesObject



26
27
28
29
30
31
32
# File 'lib/wavefront/wavefront_object.rb', line 26

def num_faces
  @num_faces = 0
  groups.each do |g|
    @num_faces += g.num_faces
  end
  @num_faces
end

#to_sObject



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/wavefront/wavefront_object.rb', line 12

def to_s
  s = "Object\n\tName: #{name}\n\tNum Vertices: #{vertices.size}\n\tNum Faces: #{num_faces}"
  unless groups.size.zero?
    s += "\n"
    groups.each do |group|
      s += "\tGroup #{group.name}\n\t\tNum Vertices: #{group.num_vertices}\n\t\tNum Faces: #{group.num_faces}"
      group.smoothing_groups.each do |smoothing_group|
        s += "\n\t\t\tSmoothing Group #{smoothing_group.name}\n\t\t\t\tNum Vertices: #{smoothing_group.num_vertices}\n\t\t\t\tNum Faces: #{smoothing_group.num_faces}"
      end
    end
  end
  s
end