wavefront 0.0.5 → 0.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/vec3.rb CHANGED
@@ -9,7 +9,11 @@ module Wavefront
9
9
  end
10
10
 
11
11
  def to_s
12
- [x, y, z].compact.join ' '
12
+ to_a.compact.join ' '
13
+ end
14
+
15
+ def to_a
16
+ [x, y, z]
13
17
  end
14
18
  end
15
19
  end
data/lib/vertex.rb CHANGED
@@ -3,8 +3,14 @@ module Wavefront
3
3
  attr_reader :position, :tex, :normal, :position_index, :texture_index, :normal_index
4
4
 
5
5
  def initialize p, uv, n, p_index, t_index, n_index
6
+ raise "cannot initialize vertex without a position!" if p.nil?
6
7
  @position, @uv, @normal = p, uv, n
7
8
  @position_index, @texture_index, @normal_index = p_index, t_index, n_index
8
9
  end
10
+
11
+ def composite_index
12
+ "p_#{position_index}_n_#{normal_index}_t#{texture_index}"
13
+ end
14
+
9
15
  end
10
16
  end
@@ -1,3 +1,3 @@
1
1
  module Wavefront
2
- VERSION = "0.0.5"
2
+ VERSION = "0.0.6"
3
3
  end
@@ -31,13 +31,22 @@ module Wavefront
31
31
 
32
32
  def export out_path
33
33
  raise "no objects to export!" if objects.size.zero?
34
- objects.first.export out_path
34
+ object.export out_path
35
+ end
36
+
37
+ def export_simple out_path, export_index_buffer = false
38
+ raise "no objects to export!" if objects.size.zero?
39
+ object.export_simple out_path, export_index_buffer
35
40
  end
36
41
 
37
42
  def compute_vertex_buffer
38
43
  object.compute_vertex_buffer
39
44
  end
40
45
 
46
+ def compute_vertex_and_index_buffer
47
+ object.compute_vertex_and_index_buffer
48
+ end
49
+
41
50
  def object
42
51
  objects.first
43
52
  end
@@ -31,9 +31,6 @@ module Wavefront
31
31
  @num_faces
32
32
  end
33
33
 
34
-
35
-
36
-
37
34
  def export file_name
38
35
  unless /\.obj$/.match file_name
39
36
  file_name += ".obj"
@@ -62,6 +59,35 @@ module Wavefront
62
59
  end
63
60
  end
64
61
 
62
+ def export_simple file_name, export_index_buffer = false
63
+ file_name += ".simple" unless /\.simple$/.match file_name
64
+
65
+ if export_index_buffer
66
+ vi = compute_vertex_and_index_buffer
67
+ vertex_buffer = vi[:vertex_buffer]
68
+ index_buffer = vi[:index_buffer]
69
+ else
70
+ vertex_buffer = compute_vertex_buffer
71
+ end
72
+
73
+ ::File.delete file_name if ::File.exist? file_name
74
+ open file_name, 'a' do |f|
75
+ f.puts "# Exported in Simple Format from Wavefront Ruby Gem Version #{Wavefront::VERSION}"
76
+ f.puts "#vertices"
77
+ vertex_buffer.each do |v|
78
+ vertex_str = "p,#{v.position.to_a.join ','}"
79
+ vertex_str += ",n,#{v.normal.to_a.join ','}" if v.normal
80
+ vertex_str += ",t,#{v.tex.to_a.join ','}" if v.tex
81
+ f.puts vertex_str
82
+ end
83
+ if export_index_buffer
84
+ f.puts "\n\n\n#indices"
85
+ f.puts index_buffer.join ','
86
+ end
87
+ end
88
+ end
89
+
90
+
65
91
  def compute_vertex_buffer
66
92
  vertex_buffer = []
67
93
  groups.each do |group|
@@ -73,15 +99,26 @@ module Wavefront
73
99
  vertex_buffer.flatten
74
100
  end
75
101
 
76
- def compute_vertex_buffer_and_index_buffer
77
- raise "not yet implemented in version #{Wavefront::VERSION}! will be coming in future versions!"
78
- vertex_buffer, index_buffer = [], []
79
-
102
+ def compute_vertex_and_index_buffer
103
+ vertex_buffer, index_buffer, composite_indices, current_index = [], [], {}, -1
104
+
105
+ groups.map { |g| (g.triangles + g.smoothing_groups.map(&:triangles).flatten) }.flatten.each do |triangle|
106
+ triangle.vertices.each do |v|
107
+ i = composite_indices[v.composite_index]
108
+ if i.nil?
109
+ current_index += 1
110
+ vertex_buffer << v
111
+ i = current_index
112
+ composite_indices[v.composite_index] = i
113
+ end
114
+ index_buffer << i
115
+ end
116
+ end
80
117
 
81
118
  {:vertex_buffer => vertex_buffer, :index_buffer => index_buffer}
82
119
  end
83
120
 
84
- private
121
+ private
85
122
  def parse!
86
123
  while line = file.gets
87
124
  components = line.split
@@ -118,8 +155,8 @@ private
118
155
  @current_group.set_smoothing_group components.first
119
156
  when 'o'
120
157
  raise "Wavefront Version #{Wavefront::VERSION} does not support obj files with more than one object. If you encounter such an obj that fails to load, please attach and email to mishaAconway@gmail.com so that I can update the gem to support the file."
121
- #file.seek -line.size, IO::SEEK_CUR
122
- #return
158
+ #file.seek -line.size, IO::SEEK_CUR
159
+ #return
123
160
  end
124
161
  end
125
162
  end
@@ -133,9 +170,9 @@ private
133
170
 
134
171
  normal_index = vertex_str_components[2]
135
172
 
136
- position = vertices[position_index]
137
- tex_coordinate = tex_index ? texture_coordinates[tex_index] : nil
138
- normal = normals[normal_index]
173
+ position = vertices[position_index-1]
174
+ tex_coordinate = tex_index ? texture_coordinates[tex_index-1] : nil
175
+ normal = normals[normal_index-1]
139
176
 
140
177
  triangle_vertices << Vertex.new(position, tex_coordinate, normal, position_index, tex_index, normal_index)
141
178
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wavefront
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-03-07 00:00:00.000000000 Z
12
+ date: 2013-03-27 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: Wavefront parser and exporter
15
15
  email: