Method: Wavefront::Object#export

Defined in:
lib/wavefront/wavefront_object.rb

#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