Author: japhb
Date: Mon Nov 10 15:50:59 2008
New Revision: 32501
Added:
trunk/examples/opengl/static-triangle.p6
trunk/examples/opengl/triangle.p6
Modified:
trunk/MANIFEST
Log:
[OpenGL] Translate first two OpenGL examples to Perl 6
* Add new examples/opengl/{static-,}triangle.p6, fairly
direct conversions of the .pir equivalents to Perl 6.
* Yes, they work under Rakudo now!
* i386 NCI JIT is still broken, so build Parrot with
Configure.pl --jitcapable=0 on i386 platforms.
Modified: trunk/MANIFEST
==============================================================================
--- trunk/MANIFEST (original)
+++ trunk/MANIFEST Mon Nov 10 15:50:59 2008
@@ -716,7 +716,9 @@
examples/nci/xlibtest.pir [main]doc
examples/nci/xlibtest.rb [main]doc
examples/opengl/shapes.pir [main]doc
+examples/opengl/static-triangle.p6 [main]doc
examples/opengl/static-triangle.pir [main]doc
+examples/opengl/triangle.p6 [main]doc
examples/opengl/triangle.pir [main]doc
examples/pasm/cat.pasm [main]doc
examples/pasm/fact.pasm [main]doc
Added: trunk/examples/opengl/static-triangle.p6
==============================================================================
--- (empty file)
+++ trunk/examples/opengl/static-triangle.p6 Mon Nov 10 15:50:59 2008
@@ -0,0 +1,70 @@
+use v6;
+
+=begin pod
+
+=head1 TITLE
+
+static-triangle.p6 - Minimal GL setup/render for Rakudo -> Parrot -> NCI tests
+
+=head1 SYNOPSIS
+
+ $ cd parrot-home
+ $ make perl6
+ $ cd parrot-home/runtime/parrot/library
+ $ ../../../perl6 ../../../examples/opengl/static-triangle.p6
+
+=head1 DESCRIPTION
+
+This is a Perl 6 conversion of F<static-triangle.pir>, itself a simplified
+version of F<triangle.pir> which attempts to remove everything not absolutely
+necessary to get a recognizeable success. This should make it easier to
+debug problems with the Parrot NCI system and Rakudo's ability to use Parrot
+NCI modules.
+
+To quit the example, close the window using your window manager (using
+the X in the corner of the window title bar, for example), since all
+keyboard handling has been removed.
+
+=end pod
+
+
+use OpenGL;
+use NCI::call_toolkit_init;
+
+# .include 'opengl_defines.pasm'
+my $GL_COLOR_BUFFER_BIT = 0x4000;
+my $GL_DEPTH_BUFFER_BIT = 0x0100;
+my $GL_TRIANGLES = 4;
+my $GLUT_DOUBLE = 2;
+my $GLUT_RGBA = 0;
+
+
+OpenGL::_export_all_functions();
+
[EMAIL PROTECTED] = NCI::call_toolkit_init(&glutInit, @*ARGS);
+
+glutInitDisplayMode($GLUT_DOUBLE +| $GLUT_RGBA);
+
+my $window = glutCreateWindow('Test');
+
+glutDisplayFunc(&draw);
+
+glutMainLoop();
+
+sub draw {
+ glClear($GL_COLOR_BUFFER_BIT +| $GL_DEPTH_BUFFER_BIT);
+
+ glBegin($GL_TRIANGLES);
+ glColor3f(1, 0, 0); glVertex3f(-.5, -.5, 0);
+ glColor3f(0, 1, 0); glVertex3f( .5, -.5, 0);
+ glColor3f(0, 0, 1); glVertex3f(0 , .5, 0);
+ glEnd();
+
+ glutSwapBuffers();
+}
+
+# Local Variables:
+# mode: pir
+# fill-column: 100
+# End:
+# vim: expandtab shiftwidth=4 ft=perl6:
Added: trunk/examples/opengl/triangle.p6
==============================================================================
--- (empty file)
+++ trunk/examples/opengl/triangle.p6 Mon Nov 10 15:50:59 2008
@@ -0,0 +1,101 @@
+use v6;
+
+=begin pod
+
+=head1 TITLE
+
+triangle.p6 - Initialize GLUT and render a simple OpenGL animation
+
+=head1 SYNOPSIS
+
+ $ cd parrot-home
+ $ make perl6
+ $ cd parrot-home/runtime/parrot/library
+ $ ../../../perl6 ../../../examples/opengl/triangle.p6
+
+=head1 DESCRIPTION
+
+This simple example shows how to load the OpenGL/GLU/GLUT wrapper, create
+a small GLUT window and register the appropriate callbacks, and finally
+display a simple OpenGL animation until the user closes the window. It is
+a simple translation of F<triangle.pir> to Perl 6.
+
+To quit the example, press C<Q> or the C<ESCAPE> key, or close the window
+using your window manager (using the X in the corner of the window title
+bar, for example). To pause or restart the animation, press any other
+ASCII key.
+
+For a more complex and well-behaved example, try F<shapes.p6>.
+
+=end pod
+
+
+use OpenGL;
+use NCI::call_toolkit_init;
+
+# .include 'opengl_defines.pasm'
+my $GL_COLOR_BUFFER_BIT = 0x4000;
+my $GL_DEPTH_BUFFER_BIT = 0x0100;
+my $GL_TRIANGLES = 4;
+my $GLUT_DOUBLE = 2;
+my $GLUT_RGBA = 0;
+
+
+OpenGL::_export_all_functions();
+
[EMAIL PROTECTED] = NCI::call_toolkit_init(&glutInit, @*ARGS);
+
+glutInitDisplayMode($GLUT_DOUBLE +| $GLUT_RGBA);
+
+my $window = glutCreateWindow('Test');
+
+glutDisplayFunc( &draw );
+glutIdleFunc( &idle );
+glutKeyboardFunc( &keyboard );
+
+my $rotating = 1;
+my $prev_time = time();
+
+glutMainLoop();
+
+sub draw {
+ glClear($GL_COLOR_BUFFER_BIT +| $GL_DEPTH_BUFFER_BIT);
+
+ glBegin($GL_TRIANGLES);
+ glColor3f(1, 0, 0); glVertex3f(-.5, -.5, 0);
+ glColor3f(0, 1, 0); glVertex3f( .5, -.5, 0);
+ glColor3f(0, 0, 1); glVertex3f(0 , .5, 0);
+ glEnd();
+
+ glutSwapBuffers();
+}
+
+sub idle {
+ my $now = time();
+ my $dt = 360 * ($now - $prev_time);
+ $prev_time = $now;
+
+ if ($rotating) {
+ glRotatef($dt, 0, 1, 0);
+ glutPostRedisplay();
+ }
+}
+
+sub keyboard($key, $x, $y) {
+ # For ESCAPE, 'Q', and 'q', exit program
+ if ($key == 27 | 81 | 113) {
+ glutDestroyWindow($window);
+ exit();
+ }
+ # For all other keys, just toggle rotation
+ else {
+ $rotating = !$rotating;
+ }
+}
+
+
+# Local Variables:
+# mode: pir
+# fill-column: 100
+# End:
+# vim: expandtab shiftwidth=4 ft=perl6: