Can opengl work with an object method?

I started with robot.rb in the sample folder of opengl-0.32g

It comes with a display proc:
display = Proc.new {}

which is called by a glut function
GLUT.DisplayFunc(display);

When I place the exact same code in a method
class Node
def traversetree
end
end

upperarm = Node.new
GLUT.DisplayFunc(upperarm.traversetree);

displays the image in a different size, and does not respond to resize
or keyboard keys

Any option?

On Feb 20, 2006, at 7:38 PM, anne001 wrote:

def traversetree

I’m pretty sure you meant to write:
GLUT.DisplayFunc(upperarm.method(:traversetree))

Hi,

From: “anne001” [email protected]

class Node
Any option?
I have a very simplistic Ruby wrapper for GLUT… It’s small so
I’ve attached it to this email.

You can subclass GLUTWindow, and just override the methods you
want to add custom handlers for, for ex. reshape_func, keyboard_func,
display_func, draw_scene. This example just overrides draw_scene…

require ‘opengl’
require ‘glut’
require ‘ftgl’

case RUBY_PLATFORM
when /mswin32/
DEFAULT_FONT = “C:/WINNT/Fonts/arial.ttf”
when /darwin/
DEFAULT_FONT = “./demo/arial.ttf” # ???
else
DEFAULT_FONT = “/usr/share/fonts/truetype/arial.ttf”
end

class MyWindow < GLUTWindow
def initialize(left, top, wid, hgt, title, parent=nil)
super
font_filename = DEFAULT_FONT
@font = FTGL::PixmapFont.new(font_filename)
font_size = 24
@font.SetFaceSize(font_size)
end

def draw_scene
  super  # GLUTWindow#draw_scene will clear the screen for us

  GL.PolygonMode(GL::FRONT_AND_BACK, GL::FILL)
  GL.RasterPos(100, 50)
  @font.Render("hello world")
end

end

GLUT.Init
GLUT.InitDisplayMode(GLUT::DOUBLE | GLUT::RGB)
w = MyWindow.new(100, 100, 800, 600, “hello”)
GLUT.MainLoop

The above example uses the ‘ftgl’ true-type-font extension[1], but
that’s not required to use the GLUTWindow wrapper itself.

Hope this helps,

Bill

[1] http://rubyforge.org/projects/ruby-ftgl/

“anne001” [email protected] writes:

def traversetree
end
end

upperarm = Node.new
GLUT.DisplayFunc(upperarm.traversetree);

displays the image in a different size, and does not respond to resize
or keyboard keys

Any option?

It doesn’t work because upperarm.traversetree' calls #traversetree and returns its value, rather than the method itself. If you want the method itself, you need to do upperarm.method(:traversetree)'.

However, it seems GLUT.DisplayFunc won’t accept Method objects; only
Procs. Do that with Kernel#lambda:

GLUT.DisplayFunc(lambda{upperarm.traversetree})

On 2/21/06, Logan C. [email protected] wrote:

On Feb 20, 2006, at 7:38 PM, anne001 wrote:

GLUT.DisplayFunc(upperarm.traversetree);

I’m pretty sure you meant to write:
GLUT.DisplayFunc(upperarm.method(:traversetree))

Except that GLUT insists on getting a Proc :expressionless:
glut.c:22
if (!rb_obj_is_kind_of(arg1,rb_cProc) && !NIL_P(arg1))
rb_raise(rb_eTypeError, “GLUT.%s:%s”,#_funcname,
rb_class2name(CLASS_OF(arg1))); \

Thank you to the four of you, this list is just amazing to help folks
like me.

George O.'s idea worked!
GLUT.DisplayFunc(lambda{upperarm.traversetree})

Logan C. wrote:

On Feb 20, 2006, at 7:38 PM, anne001 wrote:

def traversetree

I’m pretty sure you meant to write:
GLUT.DisplayFunc(upperarm.method(:traversetree))

I’m pretty sure you meant to write:
GLUT.DisplayFunc(upperarm.method(:traversetree).to_proc)

Note the “to_proc” at the end. Just had to do this myself :slight_smile:

On Feb 20, 2006, at 9:16 PM, Ilmari H. wrote:

Except that GLUT insists on getting a Proc :expressionless:
glut.c:22
if (!rb_obj_is_kind_of(arg1,rb_cProc) && !NIL_P(arg1))
rb_raise(rb_eTypeError, “GLUT.%s:%s”,#_funcname,
rb_class2name(CLASS_OF(arg1))); \

This makes me sad :frowning: