Commit | Line | Data |
ca0a3441 |
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> |
c7e8d3c6 |
11 | <li><a href="#SEE_ALSO">SEE ALSO</a></li> |
12 | <li><a href="#AUTHORS">AUTHORS</a> |
ca0a3441 |
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 | |
801213bd |
49 | </pre> |
50 | <p>You can use OpenGL as needed here.</p> |
51 | <pre> my ($SDLAPP, $WIDTH, $HEIGHT, $SDLEVENT); |
ca0a3441 |
52 | |
53 | $| = 1; |
54 | $WIDTH = 1024; |
55 | $HEIGHT = 768; |
505f308d |
56 | $SDLAPP = SDLx::App->new(title => "Opengl App", width => $WIDTH, height => $HEIGHT, gl => 1); |
ca0a3441 |
57 | $SDLEVENT = SDL::Event->new; |
58 | |
801213bd |
59 | </pre> |
505f308d |
60 | <p>SDLx::App can start an OpenGL application with the parameter gl => 1.</p> |
801213bd |
61 | <pre> glEnable(GL_DEPTH_TEST); |
ca0a3441 |
62 | glMatrixMode(GL_PROJECTION); |
63 | glLoadIdentity; |
64 | gluPerspective(60, $WIDTH / $HEIGHT, 1, 1000); |
65 | glTranslatef(0, 0, -20); |
66 | |
801213bd |
67 | </pre> |
68 | <p>Above we enable GL and set the correct prespective</p> |
69 | <pre> while (1) { |
ca0a3441 |
70 | &handlepolls; |
71 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
72 | glRotatef(.1, 1, 1, 1); |
73 | &drawscene; |
74 | $SDLAPP->sync; |
75 | } |
76 | |
801213bd |
77 | </pre> |
78 | <p>For SDLx::App sync handles the GL buffer clean.</p> |
79 | <pre> sub drawscene { |
ca0a3441 |
80 | my ($color, $x, $y, $z); |
81 | |
82 | for (-2 .. 2) { |
83 | glPushMatrix; |
84 | glTranslatef($_ * 3, 0, 0); |
85 | glColor3d(1, 0, 0); |
86 | &draw_cube; |
87 | glPopMatrix; |
88 | } |
89 | |
90 | return ""; |
91 | } |
92 | |
93 | |
94 | |
95 | |
ca0a3441 |
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 ""; |
120 | } |
121 | |
801213bd |
122 | </pre> |
123 | <p>Below we can use SDL::Events as normal:</p> |
124 | <pre> sub handlepolls { |
ca0a3441 |
125 | my ($type, $key); |
126 | |
127 | SDL::Events::pump_events(); |
128 | |
129 | while (SDL::Events::poll_event($SDLEVENT)) { |
130 | $type = $SDLEVENT->type(); |
131 | $key = ($type == 2 or $type == 3) ? $SDLEVENT->key_sym : ""; |
132 | |
133 | 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) } |
134 | elsif ($type == 2) { print "You are pressing $key\n" } |
135 | elsif ($type == 3) { print "You released $key\n" } |
136 | elsif ($type == 12) { exit } |
137 | else { print "TYPE $type UNKNOWN!\n" } |
138 | |
139 | if ($type == 2) { |
140 | if ($key eq "q" or $key eq "escape") { exit } |
141 | } |
142 | } |
143 | |
144 | return ""; |
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"> |
1dbe1697 |
152 | <p><a href="http://search.cpan.org/perldoc?perl">perl</a> <a href="SDLx-App.html">SDLx::App</a> <a href="http://search.cpan.org/perldoc?OpenGL">OpenGL</a> </p> |
ca0a3441 |
153 | |
154 | </div> |
c7e8d3c6 |
155 | <h1 id="AUTHORS">AUTHORS</h1><p><a href="#TOP" class="toplink">Top</a></p> |
156 | <div id="AUTHORS_CONTENT"> |
1dbe1697 |
157 | <p>See <a href="/SDL.html#AUTHORS">/SDL.html#AUTHORS</a>.</p> |
c7e8d3c6 |
158 | |
159 | |
160 | |
161 | |
162 | |
163 | </div> |
ca0a3441 |
164 | </div> |