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

Thursday 24 September 2009

A Simple Rotating Cube using PVector and splat


require 'ruby-processing'
class RotatingCube < Processing::App

  def setup
    render_mode P3D
    @data = [
    PVector.new( -1, -1,  1),  # 0  twice unit cube vertices
    PVector.new(  1, -1,  1),  # 1  use halves for unit cube
    PVector.new(  1,  1,  1),  # 2
    PVector.new( -1,  1,  1),  # 3
    PVector.new(  1, -1, -1),  # 4
    PVector.new( -1, -1, -1),  # 5
    PVector.new( -1,  1, -1),  # 6
    PVector.new(  1,  1, -1),  # 7
    PVector.new(  1, -1,  1)   # 8
    ]
    frame_rate 60
    @theta = 0
  end

  def draw
    background 0
    @theta += 0.005
    ambient_light 255, 255, 255
    directional_light 255, 255, 255, 1, -1, 0   
    translate width/2, height/2, 0
    rotate_x @theta
    rotate_y @theta
    rotate_z @theta
    pts = scale_vectors width/4
    draw_cube pts
  end

  def draw_cube pts
    stroke 0
    begin_shape QUADS
    fill 255, 0, 0   
    # +Z "front" face
    vertex *pts[0].array # 0
    vertex *pts[1].array # 1
    vertex *pts[2].array # 2
    vertex *pts[3].array # 3

    # -Z "back" face
    vertex *pts[4].array # 4
    vertex *pts[5].array # 5
    vertex *pts[6].array # 6
    vertex *pts[7].array # 7
    fill 0, 255, 0
    # +Y "bottom" face
    vertex *pts[3].array # 3
    vertex *pts[2].array # 2
    vertex *pts[7].array # 7
    vertex *pts[6].array # 6

    # -Y "top" face
    vertex *pts[5].array # 5
    vertex *pts[4].array # 4
    vertex *pts[8].array # 8
    vertex *pts[0].array # 0
    fill 0, 0, 255
    # +X "right" face
    vertex *pts[8].array # 8
    vertex *pts[4].array # 4
    vertex *pts[7].array # 7
    vertex *pts[2].array # 2

    # -X "left" face
    vertex *pts[5].array # 5
    vertex *pts[0].array # 0
    vertex *pts[3].array # 3
    vertex *pts[6].array # 6
    
    end_shape
  end
 
  def scale_vectors sz
    points = Array.new
    @data.each do |point
      points.push(PVector.mult(point, sz))
    end
    return points
  end
end

RotatingCube.new :title => "Rotating Cube",  :width => 300,  :height => 300

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