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.

In fact, you could say it's Processing-inspired, but that would be an insult to Processing. I don't wrap anything good (yet), so you still need to know how to use the drawing calls wrapped by ruby-opengl, and 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.

Getting GLApp

I've published the source code in a GLApp GitHub repository. If you know how to use git, then clone it already, but if not, just click the "Download" link at the top of the project page there.

Or click here: download GLApp from GitHub.

Using GLApp

It's really easy; all you need are the ruby-opengl gem and the gl_app.rb file. Check out triangles.rb in the examples/ directory of the download for a fairly simple example (the one I used to generate the graphic at the top of this page). Here is what the example looks like right now:

require File.join(File.dirname(__FILE__), "..", "gl_app")

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

class TriangleDemo
  include GLApp::Engine

  def setup
    @triangles = Triangle.boom(10)
  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
end

app = GLApp.new(TriangleDemo.new, 800, 300, "Triangle demo")
app.show

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

What's New

A few days ago a nice stranger came along and cleaned up all of the GLApp code. It's the reason I put the code on GitHub, right? I don't know the person, but I liked most of the changes. I merged them back into my branch with some adjustments and the additional sprite demo.


Comments

Click here to view the comments on this post.