Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

I decided I wanted to do some graphics earlier this month, so I pulled down the openGL module and started gnawing on the spinning cube demo that came with it.

I quickly found that it is very easy to break things, blind your viewpoint and generally mess things up with transformations being off. And I also found that it took forever to see what was going on when I had to run the script over and over.

So I thought; hey, this is a scripting language. Let me use the power of the eval, and have it do the work for me!

And thus I split it into two scripts. One with the basics to initialize and loop, the other to edit while it is being continuously re-evaled. Now, I can add a minus sign to a formula, and see the results immediately on screen. I can start defining a polygon or triangle strip, and watch as the shape grows when I enter new points.

I would say it has sped up everything by 10x, since I can just try things effortlessly, and actually *see* the differences flip back and forth.

Readmore for Sample code :

Main loop

#!/usr/bin/perl -w use strict; use warnings; use OpenGL qw/ :all /; use constant R30D => 0.523598776; eval {glutInit(); 1} or die qq { This requires GLUT. }; glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH | GLUT_ALPHA) +; glutInitWindowSize(500, 500); my $Window_ID = glutCreateWindow( "Open Gl Window" ); # Register the callback function to do the drawing. glutDisplayFunc(\&cbRenderScene); # If there's nothing to do, draw. glutIdleFunc(\&cbRenderScene); # It's a good idea to know when our window's resized. glutReshapeFunc(\&resizeEvent); # And let's get some input. # glutKeyboardFunc(\&cbKeyPressed); # glutSpecialFunc(\&cbSpecialKeyPressed); # glutPassiveMotionFunc(\&mouseMove); # glutMotionFunc(\&mouseMove); # glutMouseFunc(\&mouseClick); # Pass off control to OpenGL. # Above functions are called as appropriate. glutMainLoop(); exit 0; sub resizeEvent { my ($Width, $Height) = @_; print "Resize: $Width, $Height\n"; # Let's not core dump, no matter what. $Height = 1 if ($Height <1); glViewport(0, 0, $Width, $Height); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(45.0,$Width/$Height,0.1,100.0); glMatrixMode(GL_MODELVIEW); $main::window_Width = $Width; $main::window_Height = $Height; } sub cbRenderScene { eval {do '';}; print $@ . "\r"; # Take a quick nap to avoid wasting CPU select (undef,undef,undef,0.1); }

And a demo which renders some colored hexagons.

no warnings; # Draw a grid of hexagons glMatrixMode(GL_MODELVIEW); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glColor3f(1,1,0); glPointSize(10); glLineWidth(1); glDisable(GL_DEPTH_TEST); glLoadIdentity(); glTranslatef(-5,-8,-30); glPushMatrix(); glTranslatef(0,0,0); for my $x (0..10) { glTranslatef(cos(R30D),sin(R30D),0); glPushMatrix(); for my $y(0..10) { glTranslatef(0,1,0); drawHex([$x%2,$y%2,0], [0,.5-$y/20,$y/10]); } glPopMatrix(); } glPopMatrix(); glutSwapBuffers(); sub drawHex { my $border=shift; my $fill = shift; my $r = shift || 0.5; my $bwidth = shift || 0.05; $r = $r-2*$bwidth; my $s = $r / cos(R30D); my $h = $s * sin(R30D); #print "$h, $r, $s"; if (defined $fill) { glColor3f(@$fill); glBegin(GL_POLYGON); glVertex2f($bwidth+$h,-$r); glVertex2f($bwidth+$h+$s,-$r); glVertex2f($bwidth+2*$h+$s,0); glVertex2f($bwidth+$h+$s, $r); glVertex2f($bwidth+$h, $r); glVertex2f($bwidth,0); glEnd(); } if (defined $border) { glColor3f(@$border); glBegin(GL_LINE_LOOP); glVertex2f($bwidth+$h,-$r); glVertex2f($bwidth+$h+$s,-$r); glVertex2f($bwidth+2*$h+$s,0); glVertex2f($bwidth+$h+$s, $r); glVertex2f($bwidth+$h, $r); glVertex2f($bwidth,0); glEnd(); } }

In reply to Coding for OpenGL "live" by SuicideJunkie

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others lurking in the Monastery: (8)
    As of 2015-12-02 03:14 GMT
    Find Nodes?
      Voting Booth?

      My keyboard shows this many letters:

      Results (32 votes), past polls