Update
[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         my ($SDLAPP, $WIDTH, $HEIGHT, $SDLEVENT);
49
50         $| = 1;
51         $WIDTH = 1024;
52         $HEIGHT = 768;
53         $SDLAPP = SDLx::App-&gt;new(-title =&gt; &quot;Opengl App&quot;, -width =&gt; $WIDTH, -height =&gt; $HEIGHT, -gl =&gt; 1);
54         $SDLEVENT = SDL::Event-&gt;new;
55
56
57
58
59         glEnable(GL_DEPTH_TEST);
60         glMatrixMode(GL_PROJECTION);
61         glLoadIdentity;
62         gluPerspective(60, $WIDTH / $HEIGHT, 1, 1000);
63         glTranslatef(0, 0, -20);
64
65
66
67
68         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
77
78
79
80
81
82         sub drawscene {
83           my ($color, $x, $y, $z);
84
85           for (-2 .. 2) {
86             glPushMatrix;
87             glTranslatef($_ * 3, 0, 0);
88             glColor3d(1, 0, 0);
89             &amp;draw_cube;
90             glPopMatrix;
91           }
92
93           return &quot;&quot;;
94         }
95
96
97
98
99
100
101
102
103
104
105
106
107
108         sub draw_cube {
109           my (@indices, @vertices, $face, $vertex, $index, $coords);
110
111           @indices = qw(4 5 6 7   1 2 6 5   0 1 5 4
112                         0 3 2 1   0 4 7 3   2 3 7 6);
113           @vertices = ([-1, -1, -1], [ 1, -1, -1],
114                        [ 1,  1, -1], [-1,  1, -1],
115                        [-1, -1,  1], [ 1, -1,  1],
116                        [ 1,  1,  1], [-1,  1,  1]);
117
118           glBegin(GL_QUADS);
119
120           foreach my $face (0..5) {
121             foreach my $vertex (0..3) {
122               $index  = $indices[4 * $face + $vertex];
123               $coords = $vertices[$index];
124
125               glVertex3d(@$coords);
126             }
127           }
128
129           glEnd;
130
131           return &quot;&quot;;
132         }
133
134
135
136
137
138
139
140
141
142
143         sub handlepolls {
144           my ($type, $key);
145
146           SDL::Events::pump_events();
147
148           while (SDL::Events::poll_event($SDLEVENT)) {
149             $type = $SDLEVENT-&gt;type();
150             $key = ($type == 2 or $type == 3) ? $SDLEVENT-&gt;key_sym : &quot;&quot;;
151
152             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) }
153             elsif ($type == 2) { print &quot;You are pressing $key\n&quot; }
154             elsif ($type == 3) { print &quot;You released $key\n&quot; }
155             elsif ($type == 12) { exit }
156             else { print &quot;TYPE $type UNKNOWN!\n&quot; }
157
158             if ($type == 2) {
159               if ($key eq &quot;q&quot; or $key eq &quot;escape&quot;) { exit }
160             }
161           }
162
163           return &quot;&quot;;
164         }
165
166 </pre>
167
168 </div>
169 <h1 id="SEE_ALSO">SEE ALSO</h1><p><a href="#TOP" class="toplink">Top</a></p>
170 <div id="SEE_ALSO_CONTENT">
171 <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>
172
173 </div>
174 </div>