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> |
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 | |
801213bd |
48 | </pre> |
49 | <p>You can use OpenGL as needed here.</p> |
50 | <pre> my ($SDLAPP, $WIDTH, $HEIGHT, $SDLEVENT); |
ca0a3441 |
51 | |
52 | $| = 1; |
53 | $WIDTH = 1024; |
54 | $HEIGHT = 768; |
55 | $SDLAPP = SDLx::App->new(-title => "Opengl App", -width => $WIDTH, -height => $HEIGHT, -gl => 1); |
56 | $SDLEVENT = SDL::Event->new; |
57 | |
801213bd |
58 | </pre> |
59 | <p>SDLx::App can start an OpenGL application with the parameter -gl => 1.</p> |
60 | <pre> glEnable(GL_DEPTH_TEST); |
ca0a3441 |
61 | glMatrixMode(GL_PROJECTION); |
62 | glLoadIdentity; |
63 | gluPerspective(60, $WIDTH / $HEIGHT, 1, 1000); |
64 | glTranslatef(0, 0, -20); |
65 | |
801213bd |
66 | </pre> |
67 | <p>Above we enable GL and set the correct prespective</p> |
68 | <pre> while (1) { |
ca0a3441 |
69 | &handlepolls; |
70 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
71 | glRotatef(.1, 1, 1, 1); |
72 | &drawscene; |
73 | $SDLAPP->sync; |
74 | } |
75 | |
801213bd |
76 | </pre> |
77 | <p>For SDLx::App sync handles the GL buffer clean.</p> |
78 | <pre> sub drawscene { |
ca0a3441 |
79 | my ($color, $x, $y, $z); |
80 | |
81 | for (-2 .. 2) { |
82 | glPushMatrix; |
83 | glTranslatef($_ * 3, 0, 0); |
84 | glColor3d(1, 0, 0); |
85 | &draw_cube; |
86 | glPopMatrix; |
87 | } |
88 | |
89 | return ""; |
90 | } |
91 | |
92 | |
93 | |
94 | |
ca0a3441 |
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 ""; |
119 | } |
120 | |
801213bd |
121 | </pre> |
122 | <p>Below we can use SDL::Events as normal:</p> |
123 | <pre> sub handlepolls { |
ca0a3441 |
124 | my ($type, $key); |
125 | |
126 | SDL::Events::pump_events(); |
127 | |
128 | while (SDL::Events::poll_event($SDLEVENT)) { |
129 | $type = $SDLEVENT->type(); |
130 | $key = ($type == 2 or $type == 3) ? $SDLEVENT->key_sym : ""; |
131 | |
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" } |
137 | |
138 | if ($type == 2) { |
139 | if ($key eq "q" or $key eq "escape") { exit } |
140 | } |
141 | } |
142 | |
143 | return ""; |
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"> |
801213bd |
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> |
ca0a3441 |
152 | |
153 | </div> |
154 | </div> |