2

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

4

1 回答 1

1

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

脚本已更改,因此 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();
于 2013-07-04T08:53:08.880 回答