Update
[sdlgit/SDL-Site.git] / pages / SDL-Cookbook-OpenGL.html-inc
diff --git a/pages/SDL-Cookbook-OpenGL.html-inc b/pages/SDL-Cookbook-OpenGL.html-inc
new file mode 100644 (file)
index 0000000..11328ab
--- /dev/null
@@ -0,0 +1,174 @@
+<div class="pod">
+<!-- INDEX START -->
+<h3 id="TOP">Index</h3>
+
+<ul><li><a href="#NAME">NAME</a></li>
+<li><a href="#CATEGORY">CATEGORY</a></li>
+<li><a href="#DESCRIPTION">DESCRIPTION</a>
+<ul><li><a href="#EXAMPLE">EXAMPLE</a></li>
+</ul>
+</li>
+<li><a href="#SEE_ALSO">SEE ALSO</a>
+</li>
+</ul><hr />
+<!-- INDEX END -->
+
+<h1 id="NAME">NAME</h1><p><a href="#TOP" class="toplink">Top</a></p>
+<div id="NAME_CONTENT">
+<p>SDL::Cookbook::OpenGL - Using SDL with OpenGL</p>
+
+</div>
+<h1 id="CATEGORY">CATEGORY</h1><p><a href="#TOP" class="toplink">Top</a></p>
+<div id="CATEGORY_CONTENT">
+<p>Cookbook</p>
+
+</div>
+<h1 id="DESCRIPTION">DESCRIPTION</h1><p><a href="#TOP" class="toplink">Top</a></p>
+<div id="DESCRIPTION_CONTENT">
+<p>As of release 2.5 SDL no longer maintains it's own bindings of openGL. Support for openGL has been moved over to a more mature implementation. </p>
+<p>This implementation is the POGL project. <a href="http://search.cpan.org/perldoc?OpenGL">OpenGL</a> is faster and more complete; and works with SDL seemlessly.</p>
+
+</div>
+<h2 id="EXAMPLE">EXAMPLE</h2>
+<div id="EXAMPLE_CONTENT">
+<p>Expanded from Floyd-ATC's OpenGL example.</p>
+<pre>
+
+
+       use strict;
+       use warnings;
+       use SDL;
+       use SDLx::App;
+       use SDL::Mouse;
+       use SDL::Video;
+       use SDL::Events;
+       use SDL::Event;
+       use OpenGL qw(:all);
+
+       my ($SDLAPP, $WIDTH, $HEIGHT, $SDLEVENT);
+
+       $| = 1;
+       $WIDTH = 1024;
+       $HEIGHT = 768;
+       $SDLAPP = SDLx::App-&gt;new(-title =&gt; &quot;Opengl App&quot;, -width =&gt; $WIDTH, -height =&gt; $HEIGHT, -gl =&gt; 1);
+       $SDLEVENT = SDL::Event-&gt;new;
+
+
+
+
+       glEnable(GL_DEPTH_TEST);
+       glMatrixMode(GL_PROJECTION);
+       glLoadIdentity;
+       gluPerspective(60, $WIDTH / $HEIGHT, 1, 1000);
+       glTranslatef(0, 0, -20);
+
+
+
+
+       while (1) {
+         &amp;handlepolls;
+         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
+         glRotatef(.1, 1, 1, 1);
+         &amp;drawscene;
+         $SDLAPP-&gt;sync;
+       }
+
+
+
+
+
+
+
+       sub drawscene {
+         my ($color, $x, $y, $z);
+
+         for (-2 .. 2) {
+           glPushMatrix;
+           glTranslatef($_ * 3, 0, 0);
+           glColor3d(1, 0, 0);
+           &amp;draw_cube;
+           glPopMatrix;
+         }
+
+         return &quot;&quot;;
+       }
+
+
+
+
+
+
+
+
+
+
+
+
+
+       sub draw_cube {
+         my (@indices, @vertices, $face, $vertex, $index, $coords);
+
+         @indices = qw(4 5 6 7   1 2 6 5   0 1 5 4
+                       0 3 2 1   0 4 7 3   2 3 7 6);
+         @vertices = ([-1, -1, -1], [ 1, -1, -1],
+                      [ 1,  1, -1], [-1,  1, -1],
+                      [-1, -1,  1], [ 1, -1,  1],
+                      [ 1,  1,  1], [-1,  1,  1]);
+
+         glBegin(GL_QUADS);
+
+         foreach my $face (0..5) {
+           foreach my $vertex (0..3) {
+             $index  = $indices[4 * $face + $vertex];
+             $coords = $vertices[$index];
+
+             glVertex3d(@$coords);
+           }
+         }
+
+         glEnd;
+
+         return &quot;&quot;;
+       }
+
+
+
+
+
+
+
+
+
+
+       sub handlepolls {
+         my ($type, $key);
+
+         SDL::Events::pump_events();
+
+         while (SDL::Events::poll_event($SDLEVENT)) {
+           $type = $SDLEVENT-&gt;type();
+           $key = ($type == 2 or $type == 3) ? $SDLEVENT-&gt;key_sym : &quot;&quot;;
+
+           if ($type == 4) { printf(&quot;You moved the mouse! x=%s y=%s xrel=%s yrel=%s\n&quot;, $SDLEVENT-&gt;motion_x, $SDLEVENT-&gt;motion_y, $SDLEVENT-&gt;motion_xrel, $SDLEVENT-&gt;motion_yrel) }
+           elsif ($type == 2) { print &quot;You are pressing $key\n&quot; }
+           elsif ($type == 3) { print &quot;You released $key\n&quot; }
+           elsif ($type == 12) { exit }
+           else { print &quot;TYPE $type UNKNOWN!\n&quot; }
+
+           if ($type == 2) {
+             if ($key eq &quot;q&quot; or $key eq &quot;escape&quot;) { exit }
+           }
+         }
+
+         return &quot;&quot;;
+       }
+
+</pre>
+
+</div>
+<h1 id="SEE_ALSO">SEE ALSO</h1><p><a href="#TOP" class="toplink">Top</a></p>
+<div id="SEE_ALSO_CONTENT">
+<p><a href="http://search.cpan.org/perldoc?perl">perl</a> <a href="http://search.cpan.org/perldoc?SDLx::App">SDLx::App</a> <a href="http://search.cpan.org/perldoc?OpenGL">OpenGL</a></p>
+
+</div>
+</div>
\ No newline at end of file