Forum: Ruby can opengl work with an object method?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
anne001 (Guest)
on 2006-02-21 02:38
(Received via mailing list)
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?
Bill K. (Guest)
on 2006-02-21 03:09
(Received via mailing list)
Hi,

From: "anne001" <removed_email_address@domain.invalid>
> 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/
Logan C. (Guest)
on 2006-02-21 03:33
(Received via mailing list)
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))
Ilmari H. (Guest)
on 2006-02-21 04:16
(Received via mailing list)
On 2/21/06, Logan C. <removed_email_address@domain.invalid> 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 :|
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))); \
George O. (Guest)
on 2006-02-21 04:54
(Received via mailing list)
"anne001" <removed_email_address@domain.invalid> 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})
Xavier (Guest)
on 2006-02-21 11:04
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 :)
anne001 (Guest)
on 2006-02-21 13:24
(Received via mailing list)
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. (Guest)
on 2006-02-21 19:14
(Received via mailing list)
On Feb 20, 2006, at 9:16 PM, Ilmari H. wrote:

> Except that GLUT insists on getting a Proc :|
> 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 :(
This topic is locked and can not be replied to.