From 543006b3ecff65e920c66ff70ac7ae301bbd8aae Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Wed, 20 Nov 2013 23:54:15 -0500 Subject: [PATCH] obj2d: switch to flat array of indices --- obj2d.rb | 54 +++++++++++++++++++++++++++++++++++------------------- src/logo.d | 10 ++++++---- 2 files changed, 41 insertions(+), 23 deletions(-) diff --git a/obj2d.rb b/obj2d.rb index 27f38aa..6908c01 100644 --- a/obj2d.rb +++ b/obj2d.rb @@ -3,31 +3,38 @@ require "erb" TEMPLATE = < -]; -<% objects.each_pair do |obj_name, obj_array| %> - <% obj_array.each_with_index do |oa, index| %> - -ushort[<%= oa.size * oa.first.size %>] <%= obj_name %>_<%= index %> = [ - <%= oa.map {|indices| indices.join(", ")}.join(",\n ") %> - ]; - <% end %> - -ushort[][] get_<%= obj_name %>() +struct indices_range_t { - return [ - <% obj_array.each_with_index do |oa, index| -%> - <%= obj_name %>_<%= index %>, - <% end -%> - ]; -} -<% end %> + int start; + int length; +}; + +float[<%= vertices.size %>] vertices = [ +<% vertices.each do |coord| -%> + <%= coord %>, +<% end -%> +]; + +ushort[<%= indices.size %>] indices = [ +<% indices.each do |index| -%> + <%= index %>, +<% end -%> +]; + +<% object_ranges.each_pair do |obj_name, obj_ranges| -%> +indices_range_t[] <%= obj_name %> = [ +<% obj_ranges.each do |obj_range| -%> + {<%= obj_range[0] %>, <%= obj_range[1] %>}, +<% end -%> +]; +<% end -%> EOF def main(obj_fname, d_fname) objects = {} + object_ranges = {} vertices = [] + indices = [] cur_obj = nil File.read(obj_fname).each_line do |line| if line =~ /^v\s+(\S+)\s+(\S+)/ @@ -45,6 +52,15 @@ def main(obj_fname, d_fname) cur_obj << [$1, $2].map {|v| v.to_i - 1} end end + vertices.flatten! + objects.each_pair do |obj_name, obj_array| + object_ranges[obj_name] = obj_array.map do |obj_indices| + obj_indices.flatten! + base_index = indices.size + indices += obj_indices + [base_index, obj_indices.size] + end + end File.open(d_fname, "w") do |fh| fh.puts ERB.new(TEMPLATE, nil, "<>-").result(binding) end diff --git a/src/logo.d b/src/logo.d index ec2ac0f..9b0a185 100644 --- a/src/logo.d +++ b/src/logo.d @@ -9,14 +9,14 @@ immutable enum int WIRE = 1; immutable enum int GENTEX = 0; immutable enum int CORPORATION = 1; -ushort[][][2][2] object_indices; +logoobj.indices_range_t [][2][2] object_indices; /* Indexed by: face/wire, word, character */ VAO[][2][2] object_vaos; void init() { - object_indices = [[logoobj.get_fg(), logoobj.get_fc()], - [logoobj.get_lg(), logoobj.get_lc()]]; + object_indices = [[logoobj.fg, logoobj.fc], + [logoobj.lg, logoobj.lc]]; Buffer vertices_buffer = new Buffer(logoobj.vertices); for (int draw_type = FACE; draw_type <= WIRE; draw_type++) { @@ -31,7 +31,9 @@ void init() vao.bind(); vertices_buffer.bind(); - ElementBuffer ibo = new ElementBuffer(object_indices[draw_type][word][char_index]); + int start = object_indices[draw_type][word][char_index].start; + int length = object_indices[draw_type][word][char_index].length; + ElementBuffer ibo = new ElementBuffer(logoobj.indices[start .. (start + length)]); ibo.bind(); object_vaos[draw_type][word][char_index] = vao;