3 <h3 id="TOP">Index</h3>
5 <ul><li><a href="#NAME">NAME</a></li>
6 <li><a href="#CATEGORY">CATEGORY</a></li>
7 <li><a href="#DESCRIPTION">DESCRIPTION</a>
8 <ul><li><a href="#EXAMPLE">EXAMPLE</a></li>
11 <li><a href="#SEE_ALSO">SEE ALSO</a>
16 <h1 id="NAME">NAME</h1><p><a href="#TOP" class="toplink">Top</a></p>
17 <div id="NAME_CONTENT">
18 <p>SDL::Cookbook::OpenGL - Using SDL with OpenGL</p>
21 <h1 id="CATEGORY">CATEGORY</h1><p><a href="#TOP" class="toplink">Top</a></p>
22 <div id="CATEGORY_CONTENT">
26 <h1 id="DESCRIPTION">DESCRIPTION</h1><p><a href="#TOP" class="toplink">Top</a></p>
27 <div id="DESCRIPTION_CONTENT">
28 <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>
29 <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>
32 <h2 id="EXAMPLE">EXAMPLE</h2>
33 <div id="EXAMPLE_CONTENT">
34 <p>Expanded from Floyd-ATC's OpenGL example.</p>
49 <p>You can use OpenGL as needed here.</p>
50 <pre> my ($SDLAPP, $WIDTH, $HEIGHT, $SDLEVENT);
55 $SDLAPP = SDLx::App->new(title => "Opengl App", width => $WIDTH, height => $HEIGHT, gl => 1);
56 $SDLEVENT = SDL::Event->new;
59 <p>SDLx::App can start an OpenGL application with the parameter gl => 1.</p>
60 <pre> glEnable(GL_DEPTH_TEST);
61 glMatrixMode(GL_PROJECTION);
63 gluPerspective(60, $WIDTH / $HEIGHT, 1, 1000);
64 glTranslatef(0, 0, -20);
67 <p>Above we enable GL and set the correct prespective</p>
70 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
71 glRotatef(.1, 1, 1, 1);
77 <p>For SDLx::App sync handles the GL buffer clean.</p>
79 my ($color, $x, $y, $z);
83 glTranslatef($_ * 3, 0, 0);
96 my (@indices, @vertices, $face, $vertex, $index, $coords);
98 @indices = qw(4 5 6 7 1 2 6 5 0 1 5 4
99 0 3 2 1 0 4 7 3 2 3 7 6);
100 @vertices = ([-1, -1, -1], [ 1, -1, -1],
101 [ 1, 1, -1], [-1, 1, -1],
102 [-1, -1, 1], [ 1, -1, 1],
103 [ 1, 1, 1], [-1, 1, 1]);
107 foreach my $face (0..5) {
108 foreach my $vertex (0..3) {
109 $index = $indices[4 * $face + $vertex];
110 $coords = $vertices[$index];
112 glVertex3d(@$coords);
122 <p>Below we can use SDL::Events as normal:</p>
123 <pre> sub handlepolls {
126 SDL::Events::pump_events();
128 while (SDL::Events::poll_event($SDLEVENT)) {
129 $type = $SDLEVENT->type();
130 $key = ($type == 2 or $type == 3) ? $SDLEVENT->key_sym : "";
132 if ($type == 4) { printf("You moved the mouse! x=%s y=%s xrel=%s yrel=%s\n", $SDLEVENT->motion_x, $SDLEVENT->motion_y, $SDLEVENT->motion_xrel, $SDLEVENT->motion_yrel) }
133 elsif ($type == 2) { print "You are pressing $key\n" }
134 elsif ($type == 3) { print "You released $key\n" }
135 elsif ($type == 12) { exit }
136 else { print "TYPE $type UNKNOWN!\n" }
139 if ($key eq "q" or $key eq "escape") { exit }
149 <h1 id="SEE_ALSO">SEE ALSO</h1><p><a href="#TOP" class="toplink">Top</a></p>
150 <div id="SEE_ALSO_CONTENT">
151 <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>