Class: Wavefront::Object
- Inherits:
-
Object
- Object
- Wavefront::Object
- Defined in:
- lib/wavefront/wavefront_object.rb
Instance Attribute Summary collapse
-
#faces ⇒ Object
readonly
Returns the value of attribute faces.
-
#file ⇒ Object
readonly
Returns the value of attribute file.
-
#groups ⇒ Object
readonly
Returns the value of attribute groups.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#normals ⇒ Object
readonly
Returns the value of attribute normals.
-
#texture_coordinates ⇒ Object
readonly
Returns the value of attribute texture_coordinates.
-
#vertices ⇒ Object
readonly
Returns the value of attribute vertices.
Instance Method Summary collapse
- #compute_vertex_and_index_buffer ⇒ Object
- #compute_vertex_buffer ⇒ Object
- #export(file_name) ⇒ Object
- #export_simple(file_name, export_index_buffer = false) ⇒ Object
-
#initialize(name, file) ⇒ Object
constructor
A new instance of Object.
- #num_faces ⇒ Object
- #to_s ⇒ Object
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
#faces ⇒ Object (readonly)
Returns the value of attribute faces.
3 4 5 |
# File 'lib/wavefront/wavefront_object.rb', line 3 def faces @faces end |
#file ⇒ Object (readonly)
Returns the value of attribute file.
3 4 5 |
# File 'lib/wavefront/wavefront_object.rb', line 3 def file @file end |
#groups ⇒ Object (readonly)
Returns the value of attribute groups.
3 4 5 |
# File 'lib/wavefront/wavefront_object.rb', line 3 def groups @groups end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
3 4 5 |
# File 'lib/wavefront/wavefront_object.rb', line 3 def name @name end |
#normals ⇒ Object (readonly)
Returns the value of attribute normals.
3 4 5 |
# File 'lib/wavefront/wavefront_object.rb', line 3 def normals @normals end |
#texture_coordinates ⇒ Object (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 |
#vertices ⇒ Object (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_buffer ⇒ Object
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_buffer ⇒ Object
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_faces ⇒ Object
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_s ⇒ Object
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 |