Previous page: Daily Crap 2009-06-08
Next page: Cyclone


GLApp

multi-colored triangles in a circle

GLApp is a Ruby mini-framework for OpenGL/GLUT applications. It's a raw, somewhat unconfigurable toy, but it's small enough to understand in one sitting so that if you already know how to use OpenGL, you can start making cool stuff as quickly as if it were Processing. If for whatever reason you're not hot on using ruby-processing (now a gem and everything!), you're not all out of luck.

I don't wrap anything complicated (yet), so you still need to know how to use the drawing calls wrapped by ruby-opengl, deal with popping pushed matrices, and all the OpenGL and GLUT stuff (which you can learn at NeHe's OpenGL tutorial pages like everyone else does).

Why use this? Open a Ruby file, import GLApp, and you can start coding some graphics. Tiny brain-to-screen time for the Ruby fans. ruby-opengl's not too slow once it's running either.

Getting GLApp

GLApp is a gem on GitHub, which means that you can browse/download its source from the web or gem install alltom-glapp (after you've configured your computer to use GitHub gems).

Using GLApp

It's really easy; all you need are the ruby-opengl and alltom-glapp gems. Check out triangles.rb in the examples/ directory for a fairly simple example (the one I used to generate the graphic at the top of this page). Below is a copy of the triangles2.rb example that's exactly the same, but uses less modular, Processing-like syntax for the sketch:

require "rubygems"
require "glapp"

include GLApp

class Triangle
  attr_accessor :angle

  def initialize(angle)
    @angle = angle
  end

  def self.boom(num)
    slice = (2.0 * Math::PI) / num.to_f
    (1..num).map { |i| Triangle.new(slice * i) }
  end

  def draw
    glPushMatrix
      glTranslate(0, 0.5, -5)
      glRotate(110, 1, 0, 0)
      glTranslate(3.0 * Math::sin(@angle), 3.0 * Math::cos(@angle), 0)
      glRotate(@angle * 90, 1, 1, 1)
      glBegin(GL_TRIANGLES)
        glColor(1, 0, 0)
        glVertex(0, 1, 0)

        glColor(0, 1, 0)
        glVertex(-1, -1, 0)

        glColor(0, 0, 1)
        glVertex(1, -1, 0)
      glEnd
    glPopMatrix
  end
end

def update(seconds)
  @triangles.each { |tri| tri.angle += seconds }
end

def draw
  @triangles.each { |tri| tri.draw }
end

def keyboard_up(key, modifiers)
  exit if key == 27 # escape
end

@triangles = Triangle.boom(10)

show 800, 300, "triangle demo"

sprite.rb reflects my newfound knowledge of how to draw 2D sprites from an image file. The code is terrible, but easily generalized.

Also

pbosetti forked a really old version of the library and added some code for letting you rotate a scene with the mouse.


Comments

Click here to view the comments on this post.