Experiments with ruby-processing (processing-2.2.1) and JRubyArt for processing-3.0

Friday, 14 December 2012

MeshToVBO conversion ruby-processing

Since processing-2.0 there is the possibility of converting 3D meshes to vertical buffer objects (vbo) as instances of PShape. Here I explore a toxiclibs example sketch that instead of using toxis gfx package to render the shapes converts the shapes to vbo using a ruby library (albeit with a Processing::Proxy mixin. Here is the library:-
############################################
# mesh_to_vbo.rb
# a ruby library to convert toxi.mesh object
# to vbo (PShape) written by Martin Prout
# make use of Processing::Proxy mixin
############################################
class MeshToVBO
    include Processing::Proxy

    attr_reader :parent

    def initialize(parent)
        @parent = parent
    end

    def meshToVBO(mesh, smth)
        retained = parent.createShape(PShape::TRIANGLES)
        retained.enableStyle()
        retained.fill(222, 222, 222)
        retained.ambient(50)
        retained.shininess(10)
        retained.specular(50)
        if (smth)
            mesh.computeVertexNormals()
            mesh.getFaces.each do |f|
                retained.normal(f.a.normal.x, f.a.normal.y, f.a.normal.z)
                retained.vertex(f.a.x, f.a.y, f.a.z)
                retained.normal(f.b.normal.x, f.b.normal.y, f.b.normal.z)
                retained.vertex(f.b.x, f.b.y, f.b.z)
                retained.normal(f.c.normal.x, f.c.normal.y, f.c.normal.z)
                retained.vertex(f.c.x, f.c.y, f.c.z)
            end
        else
            mesh.getFaces.each do |f|
                retained.normal(f.normal.x, f.normal.y, f.normal.z)
                retained.vertex(f.a.x, f.a.y, f.a.z)
                retained.vertex(f.b.x, f.b.y, f.b.z)
                retained.vertex(f.c.x, f.c.y, f.c.z)
            end
        end
        retained.end()
        return retained
    end

    # variant
    # input array of meshes, output an array of shapes
    def meshToRetained(mesh, smth)
        rshapes = []
        (0 ... mesh.length).each do |i|
            rshapes.push(meshToVBO(mesh[i], smth))
        end
        return rshapes
    end
end



Here is the example sketch:-
# This example demonstrates how to rotate a number of meshes
# so that each points towards a common & user controlled focal point.
#
# Requires toxiclibs-0020 or newer
#
# (c) 2012 Karsten Schmidt / LGPL2 licensed
#
load_libraries 'toxiclibscore', 'vbo'

include_package 'toxi.geom'
include_package 'toxi.geom.mesh'

# container for mesh positions

attr_reader :positions, :vbo

def setup
  size(640,480,P3D)
  @vbo = MeshToVBO.new(self)
  @positions = []
  # compute mesh positions on circle in XZ plane
  (Circle.new(200).toPolygon2D(8)).each do |p|
    positions.push(p.to3DXZ)
  end
end

def draw
  background(51)
  lights
  no_stroke
  translate(width/2,height/2,0)
  rotate_x(-PI/6)
  # create manual focal point in XY plane
  focus = Vec3D.new((mouse_x - width/2), (mouse_y - height/2), 0)
  # create mesh prototype to draw at all generated positions
  # the mesh is a simple box placed at the world origin
  m = AABB.new(25).to_mesh
  # draw focus
  shape(vbo.meshToVBO(AABB.new(focus, 5).to_mesh, false))
  positions.each do |p|
    # align the positive z-axis of mesh to point at focus
    # mesh needs to be located at world origin for it to work correctly
    # only once rotated, move it to actual position
    shape(vbo.meshToVBO(m.copy.pointTowards(focus.sub(p), Vec3D::Z_AXIS).translate(p), false))
  end
  # draw connections from mesh centers to focal point
  stroke(0,255,255)
  positions.each do |p|
    line(p.x, p.y, p.z, focus.x, focus.y, focus.z)
  end
end

No comments:

Post a Comment

Followers

Blog Archive

About Me

My photo
I have developed JRubyArt and propane new versions of ruby-processing for JRuby-9.1.5.0 and processing-3.2.2