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

Saturday, 26 October 2013

Using shaders in ruby-processing (tutorial example translated)

Anyone interested in using shaders in ruby-processing should read the vanilla processing tutorial here. All the code for the tutorial is available here, including the necessary glsl shader (and image) files used in this sketch, which is translated from one of the included examples.You might also like to read the codeanticode blog (by Andrés Colubri) to keep up to date with latest developments.
# Texture from Jason Liebig's FLICKR collection of vintage labels and wrappers:
# http://www.flickr.com/photos/jasonliebigstuff/3739263136/in/photostream/

CAN_SIZE = 60

attr_reader :label, :can, :cap, :angle, :color_shader, :light_shader, :texlight_shader
attr_reader :sel_shader, :pixlight_shader, :tex_shader, :texlightx_shader, :bw_shader
attr_reader :edges_shader, :use_texture, :use_light, :emboss_shader

def setup
  size(480, 480, P3D)
  @label = load_image("lachoy.jpg")
  @can = create_can(CAN_SIZE, 2 * CAN_SIZE, 32, label)
  @cap = create_cap(CAN_SIZE, 32)
  @angle = 0
  @color_shader = load_shader("colorfrag.glsl", "colorvert.glsl")
  @light_shader = load_shader("lightfrag.glsl", "lightvert.glsl")
  @tex_shader = load_shader("texfrag.glsl", "texvert.glsl")

  @texlight_shader = load_shader("texlightfrag.glsl", "texlightvert.glsl")
  @pixlight_shader = load_shader("pixlightfrag.glsl", "pixlightvert.glsl")
  @texlightx_shader = load_shader("pixlightxfrag.glsl", "pixlightxvert.glsl")

  @bw_shader = load_shader("bwfrag.glsl")
  @edges_shader = load_shader("edgesfrag.glsl")
  @emboss_shader = load_shader("embossfrag.glsl")

  @sel_shader = texlight_shader
  @use_light = true
  @use_texture = true
  puts "Vertex lights, texture shading"
end

def draw
  background(0)

  x = 1.88 * CAN_SIZE
  y = 2 * CAN_SIZE
  3.times do
    3.times do
      draw_can(x, y, angle)
      x += (2 * CAN_SIZE + 8)
    end
    x = 1.88 * CAN_SIZE
    y += (2 * CAN_SIZE + 5)
  end

  @angle += 0.01
end

def draw_can( centerx, centery, rot_angle)
  push_matrix

  if (use_light)
    point_light(255, 255, 255, centerx, centery, 200)
  end
  shader(sel_shader)

  translate(centerx, centery, 65)
  rotate_y(rot_angle)
  if (use_texture)
    can.set_texture(label)
  else
    can.set_texture(nil)
  end
  shape(can)
  no_lights

  reset_shader

  push_matrix
  translate(0, CAN_SIZE - 5, 0)
  shape(cap)
  pop_matrix

  push_matrix
  translate(0, -CAN_SIZE + 5, 0)
  shape(cap)
  pop_matrix

  pop_matrix
end

def create_can(r, h, detail, tex)
  texture_mode(NORMAL)
  sh = create_shape
  sh.begin_shape(QUAD_STRIP)
  sh.no_stroke
  sh.texture(tex)
  (0 .. detail).each do |i|
    angle = TAU / detail
    x = sin(i * angle)
    z = cos(i * angle)
    u = i.to_f / detail
    sh.normal(x, 0, z)
    sh.vertex(x * r, -h/2, z * r, u, 0)
    sh.vertex(x * r, +h/2, z * r, u, 1)
  end
  sh.end_shape
  return sh
end

def create_cap(r, detail)
  sh = create_shape
  sh.begin_shape(TRIANGLE_FAN)
  sh.no_stroke
  sh.fill(128)
  sh.vertex(0, 0, 0)
  (0 ... detail).each do |i|
    angle = TAU / detail
    x = sin(i * angle)
    z = cos(i * angle)
    sh.vertex(x * r, 0, z * r)
  end
  sh.end_shape
  return sh
end

def key_pressed
  case key
  when '1'
    puts "No lights, no texture shading"
    @sel_shader = color_shader
    @use_light = false
    @use_texture = false
  when '2'
    puts "Vertex lights, no texture shading"
    @sel_shader = light_shader
    @use_light = true
    @use_texture = false
  when '3'
    puts "No lights, texture shading"
    @sel_shader = tex_shader
    @use_light = false
    @use_texture = true
  when '4'
    puts "Vertex lights, texture shading"
    @sel_shader = texlight_shader
    @use_light = true
    @use_texture = true
  when '5'
    puts "Pixel lights, no texture shading"
    @sel_shader = pixlight_shader
    @use_light = true
    @use_texture = false
  when '6'
    puts "Pixel lights, texture shading"
    @sel_shader = texlightx_shader
    @use_light = true
    @use_texture = true
  when '7'
    puts "Black&white texture filtering"
    @sel_shader = bw_shader
    @use_light = false
    @use_texture = true
  when '8'
    puts "Edge detection filtering"
    @sel_shader = edges_shader
    @use_light = false
    @use_texture = true
  when '9'
    puts "Emboss filtering"
    @sel_shader = emboss_shader
    @use_light = false
    @use_texture = true
  end
end

Emboss Filtering

No comments:

Post a Comment

Followers

Blog Archive

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