663bfd409784a02e040d04be0fe2ffd26edead7a
[sdlgit/SDL-Site.git] / pages / SDL-Cookbook-OpenGL.html-inc
1 <div class="pod">
2 <!-- INDEX START -->
3 <h3 id="TOP">Index</h3>
4
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>
9 </ul>
10 </li>
11 <li><a href="#SEE_ALSO">SEE ALSO</a>
12 </li>
13 </ul><hr />
14 <!-- INDEX END -->
15
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>
19
20 </div>
21 <h1 id="CATEGORY">CATEGORY</h1><p><a href="#TOP" class="toplink">Top</a></p>
22 <div id="CATEGORY_CONTENT">
23 <p>Cookbook</p>
24
25 </div>
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>
30
31 </div>
32 <h2 id="EXAMPLE">EXAMPLE</h2>
33 <div id="EXAMPLE_CONTENT">
34 <p>Expanded from Floyd-ATC's OpenGL example.</p>
35 <pre>
36
37
38         use strict;
39         use warnings;
40         use SDL;
41         use SDLx::App;
42         use SDL::Mouse;
43         use SDL::Video;
44         use SDL::Events;
45         use SDL::Event;
46         use OpenGL qw(:all);
47
48 </pre>
49 <p>You can use OpenGL as needed here.</p>
50 <pre>   my ($SDLAPP, $WIDTH, $HEIGHT, $SDLEVENT);
51
52         $| = 1;
53         $WIDTH = 1024;
54         $HEIGHT = 768;
55         $SDLAPP = SDLx::App-&gt;new(-title =&gt; &quot;Opengl App&quot;, -width =&gt; $WIDTH, -height =&gt; $HEIGHT, -gl =&gt; 1);
56         $SDLEVENT = SDL::Event-&gt;new;
57
58 </pre>
59 <p>SDLx::App can start an OpenGL application with the parameter -gl =&gt; 1.</p>
60 <pre>   glEnable(GL_DEPTH_TEST);
61         glMatrixMode(GL_PROJECTION);
62         glLoadIdentity;
63         gluPerspective(60, $WIDTH / $HEIGHT, 1, 1000);
64         glTranslatef(0, 0, -20);
65
66 </pre>
67 <p>Above we enable GL and set the correct prespective</p>
68 <pre>   while (1) {
69           &amp;handlepolls;
70           glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
71           glRotatef(.1, 1, 1, 1);
72           &amp;drawscene;
73           $SDLAPP-&gt;sync;
74         }
75
76 </pre>
77 <p>For SDLx::App sync handles the GL buffer clean.</p>
78 <pre>   sub drawscene {
79           my ($color, $x, $y, $z);
80
81           for (-2 .. 2) {
82             glPushMatrix;
83             glTranslatef($_ * 3, 0, 0);
84             glColor3d(1, 0, 0);
85             &amp;draw_cube;
86             glPopMatrix;
87           }
88
89           return &quot;&quot;;
90         }
91
92
93
94
95         sub draw_cube {
96           my (@indices, @vertices, $face, $vertex, $index, $coords);
97
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]);
104
105           glBegin(GL_QUADS);
106
107           foreach my $face (0..5) {
108             foreach my $vertex (0..3) {
109               $index  = $indices[4 * $face + $vertex];
110               $coords = $vertices[$index];
111
112               glVertex3d(@$coords);
113             }
114           }
115
116           glEnd;
117
118           return &quot;&quot;;
119         }
120
121 </pre>
122 <p>Below we can use SDL::Events as normal:</p>
123 <pre>   sub handlepolls {
124           my ($type, $key);
125
126           SDL::Events::pump_events();
127
128           while (SDL::Events::poll_event($SDLEVENT)) {
129             $type = $SDLEVENT-&gt;type();
130             $key = ($type == 2 or $type == 3) ? $SDLEVENT-&gt;key_sym : &quot;&quot;;
131
132             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) }
133             elsif ($type == 2) { print &quot;You are pressing $key\n&quot; }
134             elsif ($type == 3) { print &quot;You released $key\n&quot; }
135             elsif ($type == 12) { exit }
136             else { print &quot;TYPE $type UNKNOWN!\n&quot; }
137
138             if ($type == 2) {
139               if ($key eq &quot;q&quot; or $key eq &quot;escape&quot;) { exit }
140             }
141           }
142
143           return &quot;&quot;;
144         }
145
146 </pre>
147
148 </div>
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>
152
153 </div>
154 </div>