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
|
#!/usr/bin/env perl
use strict;
use warnings;
use 5.010;
use OpenGL qw(:all);
use Math::Trig;
use Time::HiRes qw(sleep time);
####### GL HANDLERS
sub handler_resize {
glViewport(0, 0, $_[0], $_[1]);
}
sub handler_idle {
handler_render();
glFlush();
glutSwapBuffers();
sleep(1 / 50);
}
sub init_gl {
glutInit();
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
glutInitWindowSize(512, 512);
glutCreateWindow($_[0]);
glutReshapeFunc (\&handler_resize);
glutIdleFunc (\&handler_idle);
glutDisplayFunc (\&handler_render);
}
sub init_vp {
glClearColor(0.0, 0.0, 0.0, 0.0);
glColor3f(1.0, 1.0, 1.0);
glMatrixMode(GL_PROJECTION);
glOrtho(-1.0, 1.0, 1.0, -1.0, 1, -1);
glMatrixMode(GL_TEXTURE);
glMatrixMode(GL_MODELVIEW);
glEnable(GL_TEXTURE_2D);
}
sub handler_render {
# ...
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glPushMatrix();
glBegin(GL_QUADS);
glTexCoord2f(0, 0); glVertex2f(-1, -1);
glTexCoord2f(0, 1); glVertex2f(-1, 1);
glTexCoord2f(1, 1); glVertex2f( 1, 1);
glTexCoord2f(1, 0); glVertex2f( 1, -1);
glEnd();
glPopMatrix();
}
# load the data
my $data = do { local $/; open(my $fh, "<", $ARGV[0]) or die("Can't open $ARGV[0]: $!"); <$fh>; };
# texture resolution:
my $res;
if (defined($ARGV[1])) {
# if specified by user, use that value.
$res = $ARGV[1];
print "texture resolution: $res (user defined)\n";
} elsif ($ARGV[0] =~ /-(\d+)$/) {
# otherwise, derive from filename (files generated by dizzy contain that info)
$res = $1;
print "texture resolution: $res (from filename)\n";
} elsif (sqrt(length($data)) == int(sqrt(length($data)))) {
# or from file size?
$res = sqrt(length($data));
print "texture resolution: $res (from filesize)\n";
} else {
print "unknown texture resolution, please specify as second argument.\n";
exit(1);
}
# init stuff
init_gl("file=$ARGV[0] res=$res");
init_vp();
# allocate new texture ID
my $new_texture = (glGenTextures_p(1))[0];
glBindTexture(GL_TEXTURE_2D, $new_texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D_s(
GL_TEXTURE_2D, 0,
GL_LUMINANCE,
$res, $res,
0,
GL_LUMINANCE,
GL_FLOAT,
$data
);
glutMainLoop();
|