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