Skip to content

Commit

Permalink
Adding some sketches from the Processing download
Browse files Browse the repository at this point in the history
  • Loading branch information
jashkenas committed Jan 18, 2009
1 parent 9b3f1ed commit d42ff11
Show file tree
Hide file tree
Showing 9 changed files with 361 additions and 100 deletions.
100 changes: 0 additions & 100 deletions samples/kinetic_type.rb

This file was deleted.

46 changes: 46 additions & 0 deletions samples/processing_app/3D/camera/ortho_vs_perspective.rb
@@ -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
44 changes: 44 additions & 0 deletions samples/processing_app/3D/camera/perspective.rb
@@ -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
94 changes: 94 additions & 0 deletions samples/processing_app/3D/form/brick_tower.rb
@@ -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.
79 changes: 79 additions & 0 deletions samples/processing_app/3D/typography/kinetic_type.rb
@@ -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"
3 changes: 3 additions & 0 deletions samples/processing_app/ABOUT
@@ -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.

0 comments on commit d42ff11

Please sign in to comment.