1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
|
#!/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 Tk;
use OpenGL;
eval { $top = MainWindow->new(-container => 1, -bg => 'white') }
or $top = MainWindow->new();
#$top = MainWindow->new();
#$top->configure(-container => 1);
my $kid;
sub CreateKid {
return if $kid;
my $par = shift;
$id = $par->WindowId;
print " window id: $id -> ", (sprintf '%#x', $$id),"\n";
$w = $par->Width;
$h = $par->Height;
glpOpenWindow(width=>$w,height=>$h,parent=>$$id, steal => $par->cget('-container'));
$kid = 1;
}
sub ResetKid {
return unless $kid;
$w = $top->Width;
$h = $top->Height;
glViewport(0,0,$w,$h);
# glFlush(); # Does not help...
print "viewport change: $w,$h\n";
DrawKid();
}
sub DrawKid {
return if $redraw_pending++;
$top->DoWhenIdle(\&DrawKid1);
}
sub DrawKid1 {
print STDERR "enter draw $w,$h\n";
$redraw_pending = 0;
return unless $kid;
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();
glColor3f(0,1,0);
glBegin(GL_POLYGON);
glVertex2f(-1,-1);
glVertex2f(1,0.1);
glVertex2f(1,-0.1);
glEnd();
glBegin(GL_POLYGON);
glVertex2f(-1,-1);
glVertex2f(0.1,1);
glVertex2f(-0.1,1);
glEnd();
glBegin(GL_POLYGON);
glVertex2f(1,-1);
glVertex2f(-1,0.1);
glVertex2f(-1,-0.1);
glEnd();
glBegin(GL_POLYGON);
glVertex2f(1,-1);
glVertex2f(0.1,1);
glVertex2f(-0.1,1);
glEnd();
glBegin(GL_POLYGON);
glVertex2f(-1,1);
glVertex2f(1,0.1);
glVertex2f(1,-0.1);
glEnd();
glBegin(GL_POLYGON);
glVertex2f(-1,1);
glVertex2f(0.1,-1);
glVertex2f(-0.1,-1);
glEnd();
glBegin(GL_POLYGON);
glVertex2f(1,1);
glVertex2f(-1,0.1);
glVertex2f(-1,-0.1);
glEnd();
glBegin(GL_POLYGON);
glVertex2f(1,1);
glVertex2f(0.1,-1);
glVertex2f(-0.1,-1);
glEnd();
glFlush();
}
sub DoKey
{
return if $kid;
my $w = shift;
CreateKid $w;
DrawKid;
}
$top->bind("<Any-KeyPress>",\&DoKey);
$top->bind("<KeyPress-q>",[$top,'destroy']);
$top->bind("<KeyPress-Escape>",[$top,'destroy']);
$top->bind("<Configure>",\&ResetKid);
$top->bind("<Visibility>",\&DrawKid);
Tk::MainLoop();
|