This ruby processing sketch shows how to get the best of both worlds; the smooth easily setup PeasyCam, that you can use the scroll-wheel to zoom, and mouse to drag, and the possible fine grain control of a control panel. In this example I have initially disabled mouse control; use the button to toggle mouse control on or off. Use the control panel slider to set off the rotation or the freeze! button (to stop the rotation). If you only want fine grain rotation (cf. continuous rotation with the slider) then you will need get the camera to remember state (I believe Quark has done this in vanilla processing). If you are really keen you could also implement a slider to control the zoom of the peasy cam (in place of the mouse-wheel).
load_libraries 'PeasyCam', 'control_panel'
import 'peasy'
attr_reader :cam, :x_rotate, :y_rotate, :z_rotate, :controlled
def setup()
size(200, 200, P3D)
configure_panel()
@controlled = true
configure_camera()
end
def configure_camera()
@cam = PeasyCam.new(self, 100)
cam.set_minimum_distance(50)
cam.set_maximum_distance(500)
mouse_control()
end
def configure_panel()
control_panel do |c|
c.slider(:x_rotate, -1.0..1.0, 0.0)
c.slider(:y_rotate, -1.0..1.0, 0.0)
c.slider(:z_rotate, -1.0..1.0, 0.0)
c.button(:freeze!)
c.button(:mouse_control)
end
end
def freeze!()
@x_rotate = 0.0
@y_rotate = 0.0
@z_rotate = 0.0
end
def mouse_control() # toggle mouse controlled camera
cam.set_mouse_controlled(!controlled)
@controlled = !controlled
end
def draw()
cam.rotate_x(x_rotate/100)
cam.rotate_y(y_rotate/100)
cam.rotate_z(z_rotate/100)
rotate_x(-0.5)
rotate_y(-0.5)
background(0)
fill(255, 0, 0)
box(30)
push_matrix()
translate(0, 0, 20)
fill(0, 0, 255)
box(5)
pop_matrix()
end
Experiments with ruby-processing (processing-2.2.1) and JRubyArt for processing-3.0
Showing posts with label PeasyCam library. Show all posts
Showing posts with label PeasyCam library. Show all posts
Tuesday, 2 February 2010
Sunday, 31 January 2010
Hello Peasy in ruby processing (a simple PeasyCam example)
load_libraries 'PeasyCam'
import 'peasy'
attr_reader :cam
def setup()
size(200,200,P3D)
configure_camera()
end
def configure_camera()
@cam = PeasyCam.new(self, 100)
cam.set_minimum_distance(50)
cam.set_maximum_distance(500)
end
def draw()
rotate_x(-0.5)
rotate_y(-0.5)
background(0)
fill(255, 0, 0)
box(30)
push_matrix()
translate(0, 0, 20)
fill(0, 0, 255)
box(5)
pop_matrix()
end
import 'peasy'
attr_reader :cam
def setup()
size(200,200,P3D)
configure_camera()
end
def configure_camera()
@cam = PeasyCam.new(self, 100)
cam.set_minimum_distance(50)
cam.set_maximum_distance(500)
end
def draw()
rotate_x(-0.5)
rotate_y(-0.5)
background(0)
fill(255, 0, 0)
box(30)
push_matrix()
translate(0, 0, 20)
fill(0, 0, 255)
box(5)
pop_matrix()
end
Labels:
PeasyCam library,
ruby processing
Saturday, 30 January 2010
Using the PeasyCam library in ruby-processing
Here is good example of how nice it is to have libraries, the PeasyCam library makes configuring the camera a cinch (it also works pretty smoothly). The lsystem grammar library means you can forget about how the production string is generated. What was the question?
########################################################
# A 3D Hilbert fractal implemented using a
# Lindenmayer System in ruby-processing by Martin Prout
# Best if you've got opengl
########################################################
require 'hilbert'
class Hilbert_Test < Processing::App
full_screen # NB: All distances are relative to screen height
load_libraries 'grammar', 'PeasyCam', 'opengl'
import 'peasy'
import "processing.opengl" if library_loaded? "opengl"
attr_reader :hilbert, :cam
def setup()
library_loaded?(:opengl) ? configure_opengl : render_mode(P3D)
configure_peasycam
@hilbert = Hilbert.new(height)
hilbert.create_grammar(3)
no_stroke()
end
def configure_peasycam
cam = PeasyCam.new(self, height/10)
cam.set_minimum_distance(height/10)
cam.set_maximum_distance(height/2)
end
def configure_opengl
render_mode OPENGL
hint ENABLE_OPENGL_4X_SMOOTH # optional
hint DISABLE_OPENGL_ERROR_REPORT # optional
end
def draw()
background(0)
lights()
hilbert.render()
end
end
############################
# hilbert.rb
###########################
class Hilbert
include Processing::Proxy
attr_reader :grammar, :axiom, :production, :premis, :rule,
:theta, :scale_factor, :distance, :phi, :len
def initialize(len)
@axiom = "X"
@grammar = Grammar.new(axiom)
@production = axiom
@premis = "X"
@rule = "^<XF^<XFX-F^>>XFX&F+>>XFX-F>X->"
@len = len
@distance = len/12 # distance value relative to screen height
@theta = Math::PI/180 * 90
@phi = Math::PI/180 * 90
grammar.add_rule(premis, rule)
no_stroke()
end
def render()
translate(-len/42, len/42, -len/42) # use the "answer?" to center the Hilbert
fill(0, 75, 152)
light_specular(204, 204, 204)
specular(255, 255, 255)
shininess(1.0)
production.scan(/./) do |ch|
case(ch)
when "F"
translate(0, distance/-2, 0)
box(distance/9 , distance, distance/9)
translate(0, distance/-2, 0)
when "+"
rotateX(-theta)
when "-"
rotateX(theta)
when ">"
rotateY(theta)
when "<"
rotateY(-theta)
when "&"
rotateZ(-phi)
when "^"
rotateZ(phi)
when "X"
else
puts("character '#{ch}' not in grammar")
end
end
end
##############################
# create grammar from axiom and
# rules (adjust scale)
##############################
def create_grammar(gen)
@distance *= 0.5**gen
@production = @grammar.generate gen
end
end
########################################################
# A 3D Hilbert fractal implemented using a
# Lindenmayer System in ruby-processing by Martin Prout
# Best if you've got opengl
########################################################
require 'hilbert'
class Hilbert_Test < Processing::App
full_screen # NB: All distances are relative to screen height
load_libraries 'grammar', 'PeasyCam', 'opengl'
import 'peasy'
import "processing.opengl" if library_loaded? "opengl"
attr_reader :hilbert, :cam
def setup()
library_loaded?(:opengl) ? configure_opengl : render_mode(P3D)
configure_peasycam
@hilbert = Hilbert.new(height)
hilbert.create_grammar(3)
no_stroke()
end
def configure_peasycam
cam = PeasyCam.new(self, height/10)
cam.set_minimum_distance(height/10)
cam.set_maximum_distance(height/2)
end
def configure_opengl
render_mode OPENGL
hint ENABLE_OPENGL_4X_SMOOTH # optional
hint DISABLE_OPENGL_ERROR_REPORT # optional
end
def draw()
background(0)
lights()
hilbert.render()
end
end
############################
# hilbert.rb
###########################
class Hilbert
include Processing::Proxy
attr_reader :grammar, :axiom, :production, :premis, :rule,
:theta, :scale_factor, :distance, :phi, :len
def initialize(len)
@axiom = "X"
@grammar = Grammar.new(axiom)
@production = axiom
@premis = "X"
@rule = "^<XF^<XFX-F^>>XFX&F+>>XFX-F>X->"
@len = len
@distance = len/12 # distance value relative to screen height
@theta = Math::PI/180 * 90
@phi = Math::PI/180 * 90
grammar.add_rule(premis, rule)
no_stroke()
end
def render()
translate(-len/42, len/42, -len/42) # use the "answer?" to center the Hilbert
fill(0, 75, 152)
light_specular(204, 204, 204)
specular(255, 255, 255)
shininess(1.0)
production.scan(/./) do |ch|
case(ch)
when "F"
translate(0, distance/-2, 0)
box(distance/9 , distance, distance/9)
translate(0, distance/-2, 0)
when "+"
rotateX(-theta)
when "-"
rotateX(theta)
when ">"
rotateY(theta)
when "<"
rotateY(-theta)
when "&"
rotateZ(-phi)
when "^"
rotateZ(phi)
when "X"
else
puts("character '#{ch}' not in grammar")
end
end
end
##############################
# create grammar from axiom and
# rules (adjust scale)
##############################
def create_grammar(gen)
@distance *= 0.5**gen
@production = @grammar.generate gen
end
end
Labels:
PeasyCam library,
ruby processing
Subscribe to:
Comments (Atom)
Followers
About Me
- monkstone
- I have developed JRubyArt and propane new versions of ruby-processing for JRuby-9.1.5.0 and processing-3.2.2

