1SDL::Cookbook::OpenGL(3U)ser Contributed Perl DocumentatiSoDnL::Cookbook::OpenGL(3)
2
3
4

NAME

6       SDL::Cookbook::OpenGL - Using SDL with OpenGL
7

CATEGORY

9       Cookbook
10

DESCRIPTION

12       As of release 2.5 SDL no longer maintains it's own bindings of OpenGL.
13       Support for OpenGL has been moved over to a more mature implementation.
14
15       This implementation is the POGL project. OpenGL is faster and more
16       complete; and works with SDL seamlessly.
17
18   EXAMPLE
19       Expanded from Floyd-ATC's OpenGL example.
20
21               use strict;
22               use warnings;
23               use SDL;
24               use SDLx::App;
25               use SDL::Mouse;
26               use SDL::Video;
27               use SDL::Events;
28               use SDL::Event;
29               use OpenGL qw(:all);
30
31       You can use OpenGL as needed here.
32
33               my ($SDLAPP, $WIDTH, $HEIGHT, $SDLEVENT);
34
35               $| = 1;
36               $WIDTH = 1024;
37               $HEIGHT = 768;
38               $SDLAPP = SDLx::App->new(title => "OpenGL App", width => $WIDTH, height => $HEIGHT, gl => 1);
39               $SDLEVENT = SDL::Event->new;
40
41       SDLx::App can start an OpenGL application with the parameter gl => 1.
42
43               glEnable(GL_DEPTH_TEST);
44               glMatrixMode(GL_PROJECTION);
45               glLoadIdentity;
46               gluPerspective(60, $WIDTH / $HEIGHT, 1, 1000);
47               glTranslatef(0, 0, -20);
48
49       Above we enable GL and set the correct perspective
50
51               while (1) {
52                 &handlepolls;
53                 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
54                 glRotatef(.1, 1, 1, 1);
55                 &drawscene;
56                 $SDLAPP->sync;
57               }
58
59       For SDLx::App sync handles the GL buffer clean.
60
61               sub drawscene {
62                 my ($color, $x, $y, $z);
63
64                 for (-2 .. 2) {
65                   glPushMatrix;
66                   glTranslatef($_ * 3, 0, 0);
67                   glColor3d(1, 0, 0);
68                   &draw_cube;
69                   glPopMatrix;
70                 }
71
72                 return "";
73               }
74
75
76               sub draw_cube {
77                 my (@indices, @vertices, $face, $vertex, $index, $coords);
78
79                 @indices = qw(4 5 6 7   1 2 6 5   0 1 5 4
80                               0 3 2 1   0 4 7 3   2 3 7 6);
81                 @vertices = ([-1, -1, -1], [ 1, -1, -1],
82                              [ 1,  1, -1], [-1,  1, -1],
83                              [-1, -1,  1], [ 1, -1,  1],
84                              [ 1,  1,  1], [-1,  1,  1]);
85
86                 glBegin(GL_QUADS);
87
88                 foreach my $face (0..5) {
89                   foreach my $vertex (0..3) {
90                     $index  = $indices[4 * $face + $vertex];
91                     $coords = $vertices[$index];
92
93                     glVertex3d(@$coords);
94                   }
95                 }
96
97                 glEnd;
98
99                 return "";
100               }
101
102       Below we can use SDL::Events as normal:
103
104               sub handlepolls {
105                 my ($type, $key);
106
107                 SDL::Events::pump_events();
108
109                 while (SDL::Events::poll_event($SDLEVENT)) {
110                   $type = $SDLEVENT->type();
111                   $key = ($type == 2 or $type == 3) ? $SDLEVENT->key_sym : "";
112
113                   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) }
114                   elsif ($type == 2) { print "You are pressing $key\n" }
115                   elsif ($type == 3) { print "You released $key\n" }
116                   elsif ($type == 12) { exit }
117                   else { print "TYPE $type UNKNOWN!\n" }
118
119                   if ($type == 2) {
120                     if ($key eq "q" or $key eq "escape") { exit }
121                   }
122                 }
123
124                 return "";
125               }
126

SEE ALSO

128       perl SDLx::App OpenGL
129

AUTHORS

131       See "AUTHORS" in SDL.
132
133
134
135perl v5.36.0                      2022-07-22          SDL::Cookbook::OpenGL(3)
Impressum