Permalink
Browse files

Adding some sketches from the Processing download

  • Loading branch information...
1 parent 9b3f1ed commit d42ff1174c5150e03f578279dbc8677a8cece78c @jashkenas jashkenas committed Jan 18, 2009
View
@@ -1,100 +0,0 @@
-# From the Processing Examples
-# by Zach Lieberman
-# Ruby version thanks to Nick Sieger
-
-require 'ruby-processing'
-
-class KineticType < Processing::App
- load_library :opengl
-
- WORDS = ["sometimes it's like", "the lines of text", "are so happy", "that they want to dance",
- "or leave the page or jump", "can you blame them?", "living on the page like that",
- "waiting to be read..."]
-
- def setup
- library_loaded?(:opengl) ? render_mode(OPENGL) : render_mode(P3D)
-
- frame_rate 30
-
- # Array of line objects
- @lns = [];
-
- # Try to create the font from scratch.
- hint(ENABLE_NATIVE_FONTS)
- f = create_font("Monospaced", 66)
- text_font f, 1.0
-
- # White type, black background
- fill 255
-
- # Creating the line objects
- i = -1
- @lines = WORDS.map do |ln|
- i += 1
- Line.new self, ln, 0, i * 70, f
- end
- end
-
- def draw
- background 0
-
- translate (width / 2.0) - 350, (height / 2.0) - 240, -450
- rotateY 0.3
-
- # Now animate every line object & draw it...
- @lines.each_with_index do |line,i|
- f1 = sin((i + 1.0) * (millis() / 10000.0) * TWO_PI)
- f2 = sin((8.0 - i) * (millis() / 10000.0) * TWO_PI)
- push_matrix
- translate 0.0, line.ypos, 0.0
- 0.upto(line.letters.length - 1) do |j|
- if j != 0
- translate(text_width(line.letters[j - 1].char)*75, 0.0, 0.0)
- end
- rotate_y(f1 * 0.035 * f2)
- push_matrix
- scale(75.0, 75.0, 75.0)
- text(line.letters[j].char, 0.0, 0.0)
- pop_matrix
- end
- pop_matrix
- end
- rescue => e
- puts e.to_s, *e.backtrace
- raise e
- end
-
- # Any class inheriting from this app object
- # can use all the plain Processing calls.
- class AppObject
- attr_accessor :app
- def method_missing(meth, *args, &block)
- app.send(meth, *args, &block)
- end
- end
-
- class Line < AppObject
- attr_accessor :string, :xpos, :ypos, :highlight_num,
- :font, :speed, :curl_in_x, :letters
-
- def initialize(app, s, i, j, bagelfont)
- @app, @string, @xpos, @ypos, @font = app, s, i, j, bagelfont
- @letters = []
- f1 = 0.0
- s.each_byte do |c|
- f1 += text_width c
- @letters << Letter.new(c, f1, 0.0)
- end
- @curl_in_x = 0.1
- end
- end
-
- class Letter
- attr_accessor :char, :x, :y
- def initialize(c, x, y)
- @char, @x, @y = c, x, y
- end
- end
-end
-
-KineticType.new :width => 200, :height => 200, :title => "Kinetic Type"
@@ -0,0 +1,46 @@
+require 'ruby-processing'
+
+# Click to see the difference between orthographic projection
+# and perspective projection as applied to a simple box.
+# The ortho function sets an orthographic projection and
+# defines a parallel clipping volume. All objects with the
+# same dimension appear the same size, regardless of whether
+# they are near or far from the camera. The parameters to this
+# function specify the clipping volume where left and right
+# are the minimum and maximum x values, top and bottom are the
+# minimum and maximum y values, and near and far are the minimum
+# and maximum z values.
+
+class OrthoVsPerspective < Processing::App
+
+ def setup
+ render_mode P3D
+ no_stroke
+ fill 204
+ end
+
+ def draw
+ background 0
+ lights
+
+ mouse_pressed? ? show_perspective : show_orthographic
+
+ translate width/2, height/2, 0
+ rotate_x -PI/6
+ rotate_y PI/3
+ box 160
+ end
+
+ def show_perspective
+ fov = PI/3.0
+ camera_z = (height/2.0) / tan(PI * fov / 360.0)
+ perspective fov, width.to_f/height.to_f, camera_z/2.0, camera_z*2.0
+ end
+
+ def show_orthographic
+ ortho -width/2, width/2, -height/2, height/2, -10, 10
+ end
+
+end
+
+OrthoVsPerspective.new :title => "Ortho Vs Perspective", :width => 640, :height => 360
@@ -0,0 +1,44 @@
+require 'ruby-processing'
+
+# Move the mouse left and right to change the field of view (fov).
+# Click to modify the aspect ratio. The perspective method
+# sets a perspective projection applying foreshortening, making
+# distant objects appear smaller than closer ones. The parameters
+# define a viewing volume with the shape of truncated pyramid.
+# Objects near to the front of the volume appear their actual size,
+# while farther objects appear smaller. This projection simulates
+# the perspective of the world more accurately than orthographic projection.
+# The version of perspective without parameters sets the default
+# perspective and the version with four parameters allows the programmer
+# to set the area precisely.
+
+class Perspective < Processing::App
+
+ def setup
+ render_mode P3D
+ no_stroke
+ end
+
+ def draw
+ lights
+ background 204
+ camera_y = height/2.0
+ fov = mouse_x/width.to_f * PI/2.0
+ camera_z = camera_y / tan(fov / 2.0)
+ aspect = width.to_f / height.to_f
+
+ aspect /= 2.0 if mouse_pressed?
+
+ perspective(fov, aspect, camera_z/10.0, camera_z*10.0)
+
+ translate width/2.0+30, height/2.0, 0
+ rotate_x -PI/6
+ rotate_y PI/3 + mouse_y/height.to_f * PI
+ box 45
+ translate 0, 0, -50
+ box 30
+ end
+
+end
+
+Perspective.new :title => "Perspective", :width => 640, :height => 360
@@ -0,0 +1,94 @@
+require 'ruby-processing'
+
+# Original by Ira Greenberg
+
+# 3D castle tower constructed out of individual bricks.
+# Uses the PVecor and Cube classes.
+
+class BrickTower < Processing::App
+
+ def setup
+ @bricks_per_layer = 16
+ @brick_layers = 18
+ @brick_width, @brick_height, @brick_depth = 60, 25, 25
+ @radius = 175.0
+ @angle = 0
+ render_mode P3D
+ @brick = Cubeish.new(@brick_width, @brick_height, @brick_depth)
+ end
+
+ def draw
+ background 0
+ @temp_x, @temp_y, @temp_z = 0, 0, 0
+ fill 182, 62, 29
+ no_stroke
+ lights
+ translate(width/2.0, height*1.2, -380) # move viewpoint into position
+ rotate_x(radians(-45)) # tip tower to see inside
+ rotate_y(frame_count * PI/600) # slowly rotate tower
+ @brick_layers.times {|i| draw_layer(i) }
+ end
+
+ def draw_layer(layer_num)
+ @layer_num = layer_num
+ @temp_y -= @brick_height # increment rows
+ @angle = 360.0 / @bricks_per_layer * @layer_num / 2.0 # alternate brick seams
+ @bricks_per_layer.times {|i| draw_bricks(i) }
+ end
+
+ def draw_bricks(brick_num)
+ @brick_num = brick_num
+ @temp_z = cos(radians(@angle)) * @radius
+ @temp_x = sin(radians(@angle)) * @radius
+ push_matrix
+ translate @temp_x, @temp_y, @temp_z
+ rotate_y(radians(@angle))
+ top_layer = @layer_num == @brick_layers - 1
+ even_brick = @brick_num % 2 == 0
+ @brick.create unless top_layer # main tower
+ @brick.create if top_layer && even_brick # add crenelation
+ pop_matrix
+ @angle += 360.0 / @bricks_per_layer
+ end
+end
+
+
+# The Cubeish class works a little different than the cube in the
+# Processing example. SIDES tells you where the negative numbers go.
+# We dynamically create each of the PVectors by passing in the
+# appropriate signs.
+class Cubeish
+ SIDES = {:front => ['-- ', ' - ', ' ', '- '],
+ :left => ['-- ', '---', '- -', '- '],
+ :right => [' - ', ' --', ' -', ' '],
+ :back => ['---', ' --', ' -', '- -'],
+ :top => ['-- ', '---', ' --', ' - '],
+ :bottom => ['- ', '- -', ' -', ' ']}
+
+ SIGNS = {'-' => -1,
+ ' ' => 1}
+
+ def initialize(width, height, depth)
+ @vertices = {}
+ @w, @h, @d = width, height, depth
+
+ SIDES.each do |side, signs|
+ @vertices[side] = signs.map do |s|
+ s = s.split('').map {|el| SIGNS[el] }
+ Processing::PVector.new(s[0]*@w/2, s[1]*@h/2, s[2]*@d/2)
+ end
+ end
+ end
+
+ def create
+ @vertices.each do |name, vectors|
+ $app.begin_shape BrickTower::QUADS
+ vectors.each {|v| $app.vertex(v.x, v.y, v.z) }
+ $app.end_shape
+ end
+ end
+
+end
+
+
+BrickTower.new :title => "Brick Tower", :width => 640, :height => 360
Binary file not shown.
@@ -0,0 +1,79 @@
+# From the Processing Examples
+# by Zach Lieberman
+# Ruby version thanks to Nick Sieger
+
+require 'ruby-processing'
+
+class KineticType < Processing::App
+ load_library :opengl
+
+ WORDS = ["sometimes it's like", "the lines of text", "are so happy", "that they want to dance",
+ "or leave the page or jump", "can you blame them?", "living on the page like that",
+ "waiting to be read..."]
+
+ def setup
+ library_loaded?(:opengl) ? render_mode(OPENGL) : render_mode(P3D)
+ frame_rate 30
+ # Load the font from the sketch's data directory.
+ text_font load_font("Univers66.vlw.gz"), 1.0
+ fill 255
+
+ # Creating the line objects
+ @lines = Array.new(WORDS.length) do |i|
+ Line.new(WORDS[i], 0, i*70)
+ end
+ end
+
+ def draw
+ background 0
+ translate -240, -120, -450
+ rotate_y 0.3
+ # Now animate every line object & draw it...
+ @lines.each_with_index do |line, i|
+ push_matrix
+ translate 0.0, line.ypos, 0.0
+ line.draw(i)
+ pop_matrix
+ end
+ end
+end
+
+class Line
+ include Math
+ attr_accessor :string, :xpos, :ypos, :letters
+
+ def initialize(string, x, y)
+ @string, @xpos, @ypos = string, x, y
+ spacing = 0.0
+ @letters = @string.split('').map do |c|
+ spacing += $app.text_width(c)
+ Letter.new(c, spacing, 0.0)
+ end
+ end
+
+ def compute_curve(line_num)
+ base = $app.millis / 10000.0 * PI * 2
+ sin((line_num + 1.0) * base) * sin((8.0 - line_num) * base)
+ end
+
+ def draw(line_num)
+ curve = compute_curve(line_num)
+ @letters.each_with_index do |letter, i|
+ $app.translate($app.text_width(@letters[i-1].char)*75, 0.0, 0.0) if i > 0
+ $app.rotate_y(curve * 0.035)
+ $app.push_matrix
+ $app.scale(75.0, 75.0, 75.0)
+ $app.text(letter.char, 0.0, 0.0)
+ $app.pop_matrix
+ end
+ end
+end
+
+class Letter
+ attr_accessor :char, :x, :y
+ def initialize(c, x, y)
+ @char, @x, @y = c, x, y
+ end
+end
+
+KineticType.new :width => 200, :height => 200, :title => "Kinetic Type"
@@ -0,0 +1,3 @@
+Here lie a scattering of examples that come with the Processing download.
+They are categorized by kind, and some of the code has been converted
+into more idiomatic Ruby.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.

0 comments on commit d42ff11

Please sign in to comment.