在perl tk中嵌入opengl

时间:2013-06-10 18:20:01

标签: perl opengl tk

我有一个用perl Tk编写的应用程序,在画布上显示一些数据。现在我希望能够使用opengl生成数据的3d视图。有没有办法在Tk窗口中嵌入OpenGL小部件或窗口? 我在perlmonks中看到了一些posts使用Tk和glpCreateWindow来创建一个新的独立OpenGL窗口,但我希望OpenGL“canvas”位于主Tk窗口内。

1 个答案:

答案 0 :(得分:1)

这是对OpenGL perl模块附带的tk_demo脚本的改编 https://metacpan.org/source/CHM/OpenGL-0.66/examples/tk_demo.pl

脚本已更改,因此opengl绘图显示在Frame小部件中,而不是Toplevel / MainWindow小部件中,因此其他Tk小部件可能包含在同一Toplevel中。甚至可以调整Tk窗口的大小,opengl绘图也会相应调整。

但是有一个缺陷:似乎无法使用afterIdle()启动opengl绘图;此时容器框架似乎尚未存在,并且发生X11错误。这里也不可能使用waitVisibility()。因此,使用after(1000,...)

的解决方法
#!/usr/local/bin/perl
#
# This is an example of combining the tk module and opengl
# You have to have TK installed for this to work.
# this program opens a window and when you hit a key in
# the window a callback that does some opengl stuff is
# executed. 
# Yes, this is a totally lame program, but its a proof
# of concept sort of thing.
# We'll get something better next time :-)
#

use lib ('blib');
use strict;
use Tk;
use OpenGL;

my $top = MainWindow->new();
$top->Label(-text => "Hello, OpenGL!")->pack;
my $f = $top->Frame(-bg => "green",  -width => 200, -height => 200)->pack(-expand => 1, -fill => "both");
$top->Button(-text => "Exit", -command => sub { $top->destroy })->pack;

my $kid;

sub CreateKid {
  my $par = shift;
  my $id = hex($par->id);
  print " window id: $id -> ", (sprintf '%#x', $id),"\n";
  my ($w, $h) = ($par->Width, $par->Height);
  my ($xbord, $ybord) = (int($w/8), int($h/8));
  $kid = glpOpenWindow( x => $xbord, y => $ybord, width=> ($w-2*$xbord),
            height=> ($h-2*$ybord),parent=>$id);
}

sub ResetKid {
  return unless $kid;
  my $par = shift;
  my $w = $par->Width;
  my $h = $par->Height;
  my ($xbord, $ybord) = (int($w/8), int($h/8));
  $w = $w-2*$xbord;
  $h = $h-2*$ybord;
  glpMoveResizeWindow($xbord,$ybord,$w,$h);
  glViewport(0,0,$w,$h);
  print "viewport $w x $h, origin $xbord, $ybord\n";
  DrawKid();
}

my $pending = 0;
sub DrawKid {
    return unless $kid;
    return if $pending++;
    $top->DoWhenIdle(\&DrawKid_do);
}
sub DrawKid_do {
    return unless $kid;
    $pending = 0;
    print "Drawing...\n";
    glClearColor(0,0,1,1);
    glClear(GL_COLOR_BUFFER_BIT);
    glOrtho(-1,1,-1,1,-1,1);

    glColor3f(1,0,0);
    glBegin(GL_POLYGON);
      glVertex2f(-0.5,-0.5);
      glVertex2f(-0.5, 0.5);
      glVertex2f( 0.5, 0.5);
      glVertex2f( 0.5,-0.5);
    glEnd();
    glFlush();
}
sub DrawKid1 {
    return unless $kid;
    print "Visibility change\n";
    DrawKid;
}
sub DrawKid2 {
    return unless $kid;
    print "Expose change\n";
    DrawKid;
}

sub DoKey
{
    my $w = shift;
    return if $kid;
    CreateKid $w;
    DrawKid;
}

sub DoMouse
{
    shift;
    my ($b,$p) = (shift,shift);
    print "mouse-$b $p\n";
}


$f->after(1000, sub { DoKey($f) });
$f->bind("<Any-ButtonPress>",[\&DoMouse, Ev('b'), Ev('@')]);
$f->bind("<KeyPress-q>",[$top, 'destroy']);
$f->bind("<KeyPress-Escape>",[$top, 'destroy']);
$f->bind("<Configure>",\&ResetKid);
$f->bind("<Visibility>",\&DrawKid1);
$f->bind("<Expose>",\&DrawKid2);

Tk::MainLoop();