diff --git a/lib/renderer/bounding_box_renderer.rb b/lib/renderer/bounding_box_renderer.rb index 78921fa..11c70f7 100644 --- a/lib/renderer/bounding_box_renderer.rb +++ b/lib/renderer/bounding_box_renderer.rb @@ -18,24 +18,55 @@ class IMICFPS end def create_bounding_box(object, box, color = nil, mesh_object_id) + color ||= object.debug_color if @bounding_boxes[mesh_object_id] if @bounding_boxes[mesh_object_id][:color] != color - update_mesh_colors(mesh_object_id, color) + @bounding_boxes[mesh_object_id][:colors] = mesh_colors(color).pack("f*") + @bounding_boxes[mesh_object_id][:color] = color return else return end end @bounding_boxes[mesh_object_id] = {} - @bounding_boxes[mesh_object_id] = {object: object, box: box, color: color} + @bounding_boxes[mesh_object_id] = {object: object, box: box, color: color, objects: []} box = object.normalize_bounding_box(box) - update_mesh_colors(mesh_object_id, color) + normals = mesh_normals + colors = mesh_colors(color) + vertices = mesh_vertices(box) - normals = [ + @vertex_count+=vertices.size + + @bounding_boxes[mesh_object_id][:vertices_size] = vertices.size + @bounding_boxes[mesh_object_id][:vertices] = vertices.pack("f*") + @bounding_boxes[mesh_object_id][:normals] = normals.pack("f*") + @bounding_boxes[mesh_object_id][:colors] = colors.pack("f*") + + object.model.objects.each do |mesh| + data = {} + box = object.normalize_bounding_box(mesh.bounding_box) + + normals = mesh_normals + colors = mesh_colors(mesh.debug_color) + vertices = mesh_vertices(box) + + @vertex_count+=vertices.size + + data[:vertices_size] = vertices.size + data[:vertices] = vertices.pack("f*") + data[:normals] = normals.pack("f*") + data[:colors] = colors.pack("f*") + + @bounding_boxes[mesh_object_id][:objects] << data + end + end + + def mesh_normals + [ 0,1,0, 0,1,0, 0,1,0, @@ -79,7 +110,51 @@ class IMICFPS -1,0,0, -1,0,0 ] - vertices = [ + end + + def mesh_colors(color) + [ + color.red, color.green, color.blue, + color.red, color.green, color.blue, + color.red, color.green, color.blue, + color.red, color.green, color.blue, + color.red, color.green, color.blue, + color.red, color.green, color.blue, + color.red, color.green, color.blue, + color.red, color.green, color.blue, + color.red, color.green, color.blue, + color.red, color.green, color.blue, + color.red, color.green, color.blue, + color.red, color.green, color.blue, + color.red, color.green, color.blue, + color.red, color.green, color.blue, + color.red, color.green, color.blue, + color.red, color.green, color.blue, + color.red, color.green, color.blue, + color.red, color.green, color.blue, + color.red, color.green, color.blue, + color.red, color.green, color.blue, + color.red, color.green, color.blue, + color.red, color.green, color.blue, + color.red, color.green, color.blue, + color.red, color.green, color.blue, + color.red, color.green, color.blue, + color.red, color.green, color.blue, + color.red, color.green, color.blue, + color.red, color.green, color.blue, + color.red, color.green, color.blue, + color.red, color.green, color.blue, + color.red, color.green, color.blue, + color.red, color.green, color.blue, + color.red, color.green, color.blue, + color.red, color.green, color.blue, + color.red, color.green, color.blue, + color.red, color.green, color.blue + ] + end + + def mesh_vertices(box) + [ box.min_x, box.max_y, box.max_z, box.min_x, box.max_y, box.min_z, box.max_x, box.max_y, box.min_z, @@ -128,55 +203,6 @@ class IMICFPS box.min_x, box.max_y, box.min_z, box.max_x, box.max_y, box.min_z ] - - @vertex_count+=vertices.size - - @bounding_boxes[mesh_object_id][:vertices_size] = vertices.size - @bounding_boxes[mesh_object_id][:vertices] = vertices.pack("f*") - @bounding_boxes[mesh_object_id][:normals] = normals.pack("f*") - end - - def update_mesh_colors(mesh_object_id, color) - @bounding_boxes[mesh_object_id][:colors] = [ - color.red, color.green, color.blue, - color.red, color.green, color.blue, - color.red, color.green, color.blue, - color.red, color.green, color.blue, - color.red, color.green, color.blue, - color.red, color.green, color.blue, - color.red, color.green, color.blue, - color.red, color.green, color.blue, - color.red, color.green, color.blue, - color.red, color.green, color.blue, - color.red, color.green, color.blue, - color.red, color.green, color.blue, - color.red, color.green, color.blue, - color.red, color.green, color.blue, - color.red, color.green, color.blue, - color.red, color.green, color.blue, - color.red, color.green, color.blue, - color.red, color.green, color.blue, - color.red, color.green, color.blue, - color.red, color.green, color.blue, - color.red, color.green, color.blue, - color.red, color.green, color.blue, - color.red, color.green, color.blue, - color.red, color.green, color.blue, - color.red, color.green, color.blue, - color.red, color.green, color.blue, - color.red, color.green, color.blue, - color.red, color.green, color.blue, - color.red, color.green, color.blue, - color.red, color.green, color.blue, - color.red, color.green, color.blue, - color.red, color.green, color.blue, - color.red, color.green, color.blue, - color.red, color.green, color.blue, - color.red, color.green, color.blue, - color.red, color.green, color.blue - ].pack("f*") - - @bounding_boxes[mesh_object_id][:color] = color end def draw_bounding_boxes @@ -185,12 +211,15 @@ class IMICFPS glTranslatef(bounding_box[:object].x, bounding_box[:object].y, bounding_box[:object].z) draw_bounding_box(bounding_box) + @bounding_boxes[key][:objects].each {|o| draw_bounding_box(o)} glPopMatrix found = ObjectManager.objects.detect { |o| o == bounding_box[:object] } + unless found @vertex_count -= @bounding_boxes[key][:vertices_size] + @bounding_boxes[key][:objects].each {|o| @vertex_count -= [:vertex_count]} @bounding_boxes.delete(key) end end diff --git a/lib/renderer/renderer.rb b/lib/renderer/renderer.rb index 9ad480b..86d5206 100644 --- a/lib/renderer/renderer.rb +++ b/lib/renderer/renderer.rb @@ -15,7 +15,6 @@ class IMICFPS if object.visible && object.renderable # Render bounding boxes before transformation is applied @bounding_box_renderer.create_bounding_box(object, object.model.bounding_box, object.debug_color, object.object_id) if $debug - object.model.objects.each {|o| @bounding_box_renderer.create_bounding_box(object, o.bounding_box, o.debug_color, o.object_id)} if $debug @opengl_renderer.draw_object(object) end