Recently I've been looking at Povray, pyprocessing, and cfdg (version 3.0) as tools for creating digital images. I have branched two separate blogs where I mainly explore jruby + processing and processing.py

Monday, 16 April 2012

ArcBall for pyprocessing

Here is a sketch that demonstrates the use of my processing.py ArcBall class to create intuitive object rotation for pyprocessing.
from util.arcball import ArcBall

"""
test_arcball.py by Martin Prout a pyprocessing sketch
Sketch features the use of ArcBall class, provides intuitive manipulation of sketch object
ArcBall class uses Quaternions class for efficient calculation of rotation, hold down x, y or z
keys to constrain rotation to that plane otherwise drag mouse for smooth rotation
"""

def setup():
    size(600, 600, OPENGL)
    global arcball
    arcball = ArcBall(width/2.0, height/2.0, min(width - 20, height - 20) * 0.5)
    arcball.axis = -1

def draw():
    background(0xff66c0ff)
    translate(width/2.0, height/2.0, -height/4.0)
    defineLights()
    update()
    lights()
    stroke(0)
    cube(arcball.radius)
    
def update(): 
    theta, x, y, z = arcball.update()
    rotate(theta, x, y,  z)

def mousePressed():
    arcball.mousePressed(mouseX, mouseY)
  
def mouseDragged():
    arcball.mouseDragged(mouseX, mouseY) 

def defineLights():
    """
    Light up the cube
    """
    ambientLight(50, 50, 50)
    pointLight(150, 100, 0, 200, -150, 0)
    directionalLight(0, 102, 255, 1, 0, 0)
    spotLight(255, 255, 109, 0, 40, 200, 0, -0.5, -0.5, PI / 2, 2)

def keyPressed():
    """
    Fix axis of rotation by holding down key corresponding to axis
    """
    if (key == 'x'):
        arcball.selectAxis(0)
    if (key == 'y'):
        arcball.selectAxis(1)
    if (key == 'z'):
        arcball.selectAxis(2)

def keyReleased():
    arcball.selectAxis(-1)

def cube(sz):
    sz *= 0.5  
    fill(200,  200,  200,  255) 
    beginShape(QUADS)
    vertex(-sz, -sz, -sz)
    vertex(+sz, -sz, -sz)
    vertex(+sz, +sz, -sz)
    vertex(-sz, +sz, -sz)
    vertex(-sz, -sz, +sz)
    vertex(+sz, -sz, +sz)
    vertex(+sz, +sz, +sz)
    vertex(-sz, +sz, +sz)
    vertex(-sz, -sz, -sz)
    vertex(-sz, -sz, +sz)
    vertex(-sz, +sz, +sz)
    vertex(-sz, +sz, -sz)
    vertex(+sz, -sz, -sz)
    vertex(+sz, -sz, +sz)
    vertex(+sz, +sz, +sz)
    vertex(+sz, +sz, -sz)
    vertex(-sz, -sz, -sz)
    vertex(+sz, -sz, -sz)
    vertex(+sz, -sz, +sz)
    vertex(-sz, -sz, +sz)
    vertex(-sz, +sz, -sz)
    vertex(+sz, +sz, -sz)
    vertex(+sz, +sz, +sz)
    vertex(-sz, +sz, +sz)
    endShape()
For full code including quaternion.py and arcball.py go to github, where you can either clone my pyprocessing-experiments, or download them as a zip archive.

No comments:

Post a Comment

Followers

Blog Archive

About Me

My photo
Pembrokeshire, United Kingdom
I have developed JRubyArt and propane new versions of ruby-processing for JRuby-9.1.5.0 and processing-3.2.2