# Can opengl work with an object method?

**URL:** <https://www.ruby-forum.com/t/can-opengl-work-with-an-object-method/71123>\
**Category:** Ruby\
**Created:** [February 21, 2006, 1:38am UTC](https://www.ruby-forum.com/t/can-opengl-work-with-an-object-method/71123 "2006-02-21T01:38:56Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![anne001](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/34f0e0/32.png) [@anne001](https://www.ruby-forum.com/u/anne001)\
**Post date:** [February 21, 2006, 1:38am UTC](https://www.ruby-forum.com/t/can-opengl-work-with-an-object-method/71123/1 "2006-02-21T01:38:56Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![anne001](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/34f0e0/32.png) [@anne001](https://www.ruby-forum.com/u/anne001)\
**Post date:** [February 21, 2006, 2:33am UTC](https://www.ruby-forum.com/t/can-opengl-work-with-an-object-method/71123/2 "2006-02-21T02:33:45Z")

</div>

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))

---

<div class="post-metadata">

**Author:** ![anne001](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/34f0e0/32.png) [@anne001](https://www.ruby-forum.com/u/anne001)\
**Post date:** [February 21, 2006, 2:09am UTC](https://www.ruby-forum.com/t/can-opengl-work-with-an-object-method/71123/3 "2006-02-21T02:09:24Z")

</div>

Hi,

From: “anne001” [removed\_email\_address@domain.invalid](mailto: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/](http://rubyforge.org/projects/ruby-ftgl/)

---

<div class="post-metadata">

**Author:** ![anne001](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/34f0e0/32.png) [@anne001](https://www.ruby-forum.com/u/anne001)\
**Post date:** [February 21, 2006, 3:54am UTC](https://www.ruby-forum.com/t/can-opengl-work-with-an-object-method/71123/4 "2006-02-21T03:54:02Z")

</div>

“anne001” [removed\_email\_address@domain.invalid](mailto: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})

---

<div class="post-metadata">

**Author:** ![anne001](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/34f0e0/32.png) [@anne001](https://www.ruby-forum.com/u/anne001)\
**Post date:** [February 21, 2006, 3:16am UTC](https://www.ruby-forum.com/t/can-opengl-work-with-an-object-method/71123/5 "2006-02-21T03:16:51Z")

</div>

On 2/21/06, Logan C. [removed\_email\_address@domain.invalid](mailto: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))); \

---

<div class="post-metadata">

**Author:** ![anne001](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/34f0e0/32.png) [@anne001](https://www.ruby-forum.com/u/anne001)\
**Post date:** [February 21, 2006, 12:24pm UTC](https://www.ruby-forum.com/t/can-opengl-work-with-an-object-method/71123/6 "2006-02-21T12:24:59Z")

</div>

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})

---

<div class="post-metadata">

**Author:** ![anne001](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/34f0e0/32.png) [@anne001](https://www.ruby-forum.com/u/anne001)\
**Post date:** [February 21, 2006, 10:04am UTC](https://www.ruby-forum.com/t/can-opengl-work-with-an-object-method/71123/7 "2006-02-21T10:04:48Z")

</div>

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 🙂

---

<div class="post-metadata">

**Author:** ![anne001](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/34f0e0/32.png) [@anne001](https://www.ruby-forum.com/u/anne001)\
**Post date:** [February 21, 2006, 6:14pm UTC](https://www.ruby-forum.com/t/can-opengl-work-with-an-object-method/71123/8 "2006-02-21T18:14:31Z")

</div>

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 ☹
