Perl and OpenGL?
I recently started pondering what kind of throughput a well-written Perl+OpenGL program could get. This is nothing like a well-written program, just a stright port of an old GL experiment I had lying around to make sure that SDL and OpenGL were working properly in my Perl install. Its a good start though! Now I need to decide if this is something I really want to pursue.
#!/usr/bin/env perl
use 5.016;
use warnings;
use strict;
use SDL;
use SDL::Video;
use SDL::Event;
use SDL::Events;
use OpenGL;
use Math::Trig;
my $WIDTH = 1440;
my $HEIGHT = 900;
my $FOV = 80;
SDL::init(SDL_INIT_VIDEO);
SDL::Video::set_video_mode($WIDTH, $HEIGHT, 32, SDL_OPENGL);
glShadeModel(GL_SMOOTH);
glCullFace(GL_BACK);
glFrontFace(GL_CCW);
glEnable(GL_CULL_FACE);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glHint(GL_POINT_SMOOTH_HINT, GL_NICEST);
{
my $fov = tan(deg2rad($FOV/2));
my $near = 1;
my $far = 100;
my $left = $fov * $near;
my $top = $left * $HEIGHT/$WIDTH;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-$left, $left, -$top, $top, $near, $far);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
my $o = 0.01;
my $left = -20;
my $right = 20;
my $front = -30;
my $back = -100;
my $depth = 10;
my $centre = 10;
my $c = 0;
my $e = SDL::Event->new;
while (!SDL::Events::poll_event($e) || $e->type != SDL_KEYDOWN || $e->key_sym != SDLK_ESCAPE) {
glClearColor(0.0, 0.0, 0.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBegin(GL_QUADS);
for (my $n = 0; $n < 1; $n += $o) {
my $k = $c+$n;
my $ky1 = cos(2*pi*$k);
my $ky2 = cos(2*pi*($k+$o));
my $x1 = $left + $n*($right-$left);
my $x2 = $x1 + $o*($right-$left);
for (my $m = 0; $m < 1; $m += $o) {
my $j = $c+$m;
my $jy1 = sin(2*pi*$j);
my $jy2 = sin(2*pi*($j+$o));
my $z1 = $front + $m*($back-$front);
my $z2 = $z1 + $o*($back-$front);
my $front_y1 = $ky1 * $jy1 * $depth - $centre;
my $front_y2 = $ky2 * $jy1 * $depth - $centre;
my $back_y1 = $ky1 * $jy2 * $depth - $centre;
my $back_y2 = $ky2 * $jy2 * $depth - $centre;
glColor3f($n, $m, 1);
glVertex3f($x2, $front_y2, $z1);
glVertex3f($x2, $back_y2, $z2);
glVertex3f($x1, $back_y1, $z2);
glVertex3f($x1, $front_y1, $z1);
}
}
glEnd();
$c += $o;
$c -= 1 if $c > 1;
SDL::Video::GL_swap_buffers
}