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

Friday 18 September 2009

Another of Lazydogs Little Gems translated to ruby-processing


require 'ruby-processing'
load_library 'opengl'
########################################################
# Original code
# http://lazydog-bookfragments.blogspot.com/2009/05/mirror-of-confusion.html
# Original version written for java processing author 'lazydog'
# aka Ben Notorianni translated to ruby-processing by 'monkstone'
# aka Martin Prout
########################################################

#
# Dimensions of screen.
#
K_WIDTH = 800
K_HEIGHT = 600

#
# Dimensions of texture.
# For fast PCs reduce or remove the denominator.
#
K_TEXTURE_WIDTH = K_WIDTH/2
K_TEXTURE_HEIGHT = K_HEIGHT/2


#
# following currently required by linux? Otherwise use 'lazydogs' dimensions
#
if java.lang.System.get_property('os.name') == "Linux" then
  full_screen
end  


def setup
  size(K_WIDTH, K_HEIGHT, OPENGL )
  #
  # P3D doesn't work well in this sketch!
  #
  library_loaded?(:opengl) ? render_mode(OPENGL) : render_mode(P3D)
  @start_t = Time.now.to_f
  color_mode RGB, 255
  smooth
 
  #
  # @history used to store the current frame buffer as a texture.
  #

  @history = create_image( K_TEXTURE_WIDTH, K_TEXTURE_HEIGHT, RGB )

  texture_mode( NORMALIZED )
end

def millis
  (@start_t - Time.now.to_f) * 10**6
end

def draw
  background( 255 )

  translate( width/2, height/2 )

  #
  # Draw the ground - use a simple grid for detail.
  #
  # All numbers are fudged to make the grid and ground look about right.
  #
  hint(DISABLE_DEPTH_TEST)

  no_stroke
  fill( 240, 189, 180 )
  rect( -width/2, 24, width/2, 200 )

  fill( 200, 220, 255 )
  rect( 0, 24, width, 200 )

  stroke( 0 )
  stroke_weight( 1.0 )

  10.times do |i|
     z = -2000 + i * 200
    line( -width*10, 200, z, width* 10, 200, z )

     x = i* 200

    line( -x, 200, -2000, -x, 200, 200 )
    line( x, 200, -2000, x, 200, 200 )
  end

  hint(ENABLE_DEPTH_TEST)

  #
  # Draw the mirror.
  #
  # The mirror is animated by rotating around the y-axis.
  # The mirror is drawn using the last frame buffer as the reflection in the mirror.
  # The view in the mirror is dimmed slightly by drawing an alpha rectangle over it.
  # Also, the mirror is oultined to make it stand out a bit.
  #
  push_matrix()

#  rotate_x( 0.03 * Math.sin( millis * 0.0023)  )
  rotate_y( 0.3 * Math.sin( millis * 0.001) )
  translate( -width/2, -height/2, -200 )

  no_stroke
  begin_shape
  texture( @history )
  vertex( 0.0, 0.0, 0, 0 )
  vertex( width, 0, 1, 0 )
  vertex( width, height, 1, 1 )
  vertex( 0, height, 0, 1 )
  end_shape

  fill( 0, 0, 0, 30 )
  begin_shape
  vertex( 0.0, 0.0 )
  vertex( width, 0 )
  vertex( width, height )
  vertex( 0, height )
  end_shape

  stroke( 200, 200, 200 )
  stroke_weight( 2 )
  no_fill
  begin_shape()
  vertex( 0, 0 )
  vertex( width, 0 )
  vertex( width, height )
  vertex( 0, height )
  end_shape(CLOSE)

  pop_matrix

  #
  # Draw the foreground scene.
  #
  translate( 200 * Math.sin( millis * 0.001 ), 200 * Math.sin( millis* 0.0013 ) )

  lights
  no_stroke
  fill( 238, 80, 70 )
  sphere( 100 )

  #
  # Use the current frame as the texture for the
  # mirror in the next frame.
  #
  # if ( frame_count % 4 == 0 )
  @history.copy( get, 0, 0, width, height, 0, 0, K_TEXTURE_WIDTH, K_TEXTURE_HEIGHT )
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