Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

OpenGL and AnyEvent

by basiliscos (Pilgrim)
on May 29, 2014 at 11:04 UTC ( #1087782=perlquestion: print w/replies, xml ) Need Help??
basiliscos has asked for the wisdom of the Perl Monks concerning the following question:

Hello dear monks!

I'm developing an application using OpenGL. I need some kind of asynchronous behaviour, i.e. when some graphical objects are moving, it should be possible that another events occur.

The common way of writing OpenGL code is to enter to OpenGL/GLUT event loop via: glutMainLoop. Good! But how could I manage schedule-based world mechanics then? I tried to use AnyEvent, but it seems, that simple code like:

my $t; $t = AE::timer 1, 0, sub{ say "Hi!"; undef $t" }

Isn't working, when it enters into glutMainLoop. What can I do to make AE work? (OpenGL backend isn't listed among the available AE backends).

Thanks for any help!

Replies are listed 'Best First'.
Re: OpenGL and AnyEvent
by zentara (Archbishop) on May 29, 2014 at 12:32 UTC
    Hi, this is just a long shot, but it is possible to run 2 different eventloops simultaneously by making one the Master, and other a Slave. The way it works, is your master event loop needs to manually pump the slave loop with a timer, so that the slave loop gets to do an iteration every so-many milliseconds.

    I havn't looked at glutMainloop, but most eventloop systems have a method to force an iteration thru the loop. In Tk, it's DoOneEvent(ALL_EVENTS); and in Gtk2 it is Gtk2->main_iteration while Gtk2->events_pending;

    So here is an example of running a Tk eventloop and a Gtk2 eventloop simultaneously.

    There is a discussion of doing this for glutMainloop at hacking Glut. AnyEvent seems to have AnyEvent->loop; to force one iteration. So possibly you could setup a glut timer to run AnyEvent->loop; every 10 milliseconds, or whatever rate you need.

    #!/usr/bin/perl -w use strict; use Gtk2; use Tk; #setup Tk loop my $mw = MainWindow->new(-title=>'Tk Window'); my $count_tk = 0; my $labtk = $mw->Label(-textvariable =>\$count_tk)->pack; #setup Gtk2 loop Gtk2->init; my $count_gtk = 0; my $window = Gtk2::Window->new('toplevel'); $window->set_title('Gtk2 Window'); my $glabel = Gtk2::Label->new("This is a Gtk2 Label $count_gtk"); $window->add($glabel); $window->show_all; # make Tk loop the master, but you could make Gtk2 master if desired # the lower the repeat rate, i.e. 1 ms, # will give more cpu time to the gtk2 loop # this is sometimes called manually pumping the event loop my $tktimer = $mw->repeat(10, sub{ $count_gtk++; $glabel->set_text("This is a Gtk2 Label $count_gtk"); Gtk2->main_iteration while Gtk2->events_pending; $count_tk++; }); $mw->Button(-text=>' Tk control Quit ', -command => sub{exit} )->pack(); MainLoop; ########################################

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh
Re: OpenGL and AnyEvent
by Corion (Pope) on May 29, 2014 at 11:21 UTC

    From a quick googling, it seems that at least some GLUT implementations, freeglut and openglut implement glutMainLoopEvent(), which is supposed to be called periodically from your other mainloop.

    A second alternative would be to detach OpenGL processing from the rest of your program by using a separate thread to handle OpenGL. But then you need to make sure that all OpenGL-related resources get allocated from that thread. At least, I remember some stories of debugging two threads going tag-team on OpenGL handles in the early days of consumer OpenGL.

      Thank you, Corion!

      I've found that the main trick is to call glutPostRedisplay too, e.g. instead of glutMainLoop

      my $refresh_world = sub { glutMainLoopEvent; glutPostRedisplay; }; my $t; $t = AE::timer 0, 0.01, sub { $refresh_world->(); }; $refresh_world->(); AE::cv->recv;

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1087782]
Front-paged by Arunbear
[Corion]: Meh. I'm reviving some old code, and it seems it used a module DBIx::Loader, but it doesn't exist on CPAN, and I can't even remember writing such a module... Maybe parts of it live on in DBIx::RunSQL, but not the parts I need right now :)

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2018-06-24 08:26 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (126 votes). Check out past polls.