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

Tuesday 13 July 2010

Exploring java opengl calls in ruby-processing

Here I try out some calls to javax.media.opengl in ruby processing (to achieve back face culling). I do this with a disco version of my ruby-processing menger sponge.


# menger.rb (disco version) by Martin Prout

load_library 'opengl'
include_package 'javax.media.opengl'

# full_screen required for linux with OPENGL (a bug)
full_screen if java.lang.System.get_property('os.name') == "Linux"

ANGLE_STEP = Math::PI/180
DATA = [-1, 0, 1]
MIN_SIZE = 50      


COL_1 = [0.6667, 1, 0.5]
COL_2 = [0.5, 1, 0.5]
COL_3 = [0.83333, 1, 0.5]
COL_4 = [0.16667, 1, 0.5]


def setup
  size(600, 600, OPENGL)
  library_loaded?(:opengl) ? setup_opengl : render_mode(P3D)
  @start_t = Time.now.to_f
  @data = []              # initialize empty array
  cube(0, 0, 0, height * 0.4)   # fill data array
  @angle = 0
  color_mode HSB, 1.0
end

def setup_opengl
  render_mode(OPENGL)
  hint DISABLE_OPENGL_ERROR_REPORT
  hint ENABLE_OPENGL_4X_SMOOTH
end

def configure_gl  # experimenting with some java opengl calls
   pgl = g
   gl = pgl.gl
   pgl.beginGL
   gl.gl_enable(GL::GL_CULL_FACE)
   gl.gl_cull_face(GL::GL_BACK)
   #gl.gl_enable(GL::GL_NORMALIZE)
   pgl.endGL
end

# Fill @data array with cube center coordinate
# and size data (this can be done at setup)

def cube x, y, z, sz
  unless (sz <= MIN_SIZE) then
    u = sz/3.0
    DATA.each do |i|
      DATA.each do |j|
        DATA.each do |k|
          cube(x+(i*u), y+(j*u), z+(k*u), u) unless (i.abs + j.abs + k.abs) <= 1
        end
      end
    end
  end
  @data.push [x, y, z, sz] unless sz > MIN_SIZE
end

# Render the cubes (data from @data) using
# translate and custom box function, uses ruby splat


def draw_cubes
  @data.each do |point|
    x, y, z, sz = *point
    push_matrix
    translate(x, y, z)
    my_box(sz)
    pop_matrix
  end
end

def draw
  configure_gl
  background 0
  ambient_light 0.8, 0.8, 0.8
  directional_light 1.0, 1.0, 1.0, 1, 1, -1
  @angle = (ANGLE_STEP + @angle) % (Math::PI * 2)
  translate(width/2, height/2, 0)
  rotate_x @angle
  rotate_y @angle
  rotate_z @angle
  draw_cubes
end

def my_box sz
  no_stroke
  begin_shape QUADS
  # +Z "front" face
  fill *COL_1
  vertex -sz/2, -sz/2,  sz/2
  fill *COL_2
  vertex  sz/2, -sz/2,  sz/2
  fill *COL_3
  vertex  sz/2,  sz/2,  sz/2
  fill *COL_4
  vertex -sz/2,  sz/2,  sz/2

  # -Z "back" face
  fill *COL_1
  vertex  sz/2, -sz/2, -sz/2
  fill *COL_2
  vertex -sz/2, -sz/2, -sz/2
  fill *COL_3
  vertex -sz/2,  sz/2, -sz/2
  fill *COL_4
  vertex  sz/2,  sz/2, -sz/2

  # +Y "bottom" face
  fill *COL_1
  vertex -sz/2,  sz/2,  sz/2
  fill *COL_2
  vertex  sz/2,  sz/2,  sz/2
  fill *COL_3
  vertex  sz/2,  sz/2, -sz/2
  fill *COL_4
  vertex -sz/2,  sz/2, -sz/2

  # -Y "top" face
  fill *COL_1
  vertex -sz/2, -sz/2, -sz/2
  fill *COL_2
  vertex  sz/2, -sz/2, -sz/2
  fill *COL_3
  vertex  sz/2, -sz/2,  sz/2
  fill *COL_4
  vertex -sz/2, -sz/2,  sz/2

  # +X "right" face
  fill *COL_1
  vertex  sz/2, -sz/2,  sz/2
  fill *COL_2
  vertex  sz/2, -sz/2, -sz/2
  fill *COL_3
  vertex  sz/2,  sz/2, -sz/2
  fill *COL_4
  vertex  sz/2,  sz/2,  sz/2

  # -X "left" face
  fill *COL_1
  vertex -sz/2, -sz/2, -sz/2
  fill *COL_2
  vertex -sz/2, -sz/2,  sz/2
  fill *COL_3
  vertex -sz/2,  sz/2,  sz/2
  fill *COL_4
  vertex -sz/2,  sz/2, -sz/2

  end_shape
end


 

No comments:

Post a Comment

Followers

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