Importing SDLPerl 2.2
[sdlgit/SDL_perl.git] / test / OpenGL / test2.sdlpl
1 #!/usr/bin/env perl
2 #
3 # test2.pl
4 #
5 # Copyright (C) 2005 David J. Goehrig <dgoehrig@cpan.org>
6 #
7 # ------------------------------------------------------------------------------
8 #
9 # This library is free software; you can redistribute it and/or
10 # modify it under the terms of the GNU Lesser General Public
11 # License as published by the Free Software Foundation; either
12 # version 2.1 of the License, or (at your option) any later version.
13
14 # This library is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 # Lesser General Public License for more details.
18
19 # You should have received a copy of the GNU Lesser General Public
20 # License along with this library; if not, write to the Free Software
21 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
22 #
23 # ------------------------------------------------------------------------------
24 #
25 # Please feel free to send questions, suggestions or improvements to:
26 #
27 #       David J. Goehrig
28 #       dgoehrig@cpan.org
29 #
30
31 use SDL;
32 use SDL::App;
33 use SDL::Surface;
34 use SDL::Event;
35 use SDL::OpenGL;
36
37 package SDL::OpenGL::Cube;
38 use SDL;
39 use SDL::OpenGL;
40
41 my $vertex_array = pack "d24", 
42         -0.5,-0.5,-0.5, 0.5,-0.5,-0.5, 0.5,0.5,-0.5, -0.5,0.5,-0.5, # back
43         -0.5,-0.5,0.5,  0.5,-0.5,0.5,  0.5,0.5,0.5,  -0.5,0.5,0.5 ;  # front
44
45 my $indicies = pack "C24",      
46                         4,5,6,7,        # front
47                         1,2,6,5,        # right
48                         0,1,5,4,        # bottom
49                         0,3,2,1,        # back
50                         0,4,7,3,        # left
51                         2,3,7,6;        # top
52
53 sub new {
54         my $proto = shift;
55         my $class = ref($proto) || $proto;
56         my $self = {};
57         bless $self,$class;
58         $self;
59 }
60
61 sub draw {
62         my ($self) = @_;
63         $self->color();
64         glEnableClientState(SDL::GL_VERTEX_ARRAY());
65         glVertexPointer(3,SDL::GL_DOUBLE(),0,$vertex_array);
66         glDrawElements(SDL::GL_QUADS(), 24, SDL::GL_UNSIGNED_BYTE(), $indicies);
67 }
68
69 sub color {
70         my ($self,@colors) = @_;
71
72         if (@colors) {
73                 $$self{colored} = 1;
74                 die "SDL::OpenGL::Cube::color requires 24 floating point color values\n"
75                         unless (scalar(@colors) == 24);
76                 $$self{-colors} = pack "f24",@colors;
77         }
78
79         if ($$self{colored}) {
80                 glEnableClientState(SDL::GL_COLOR_ARRAY);
81                 glColorPointer(3,SDL::GL_FLOAT,0,$$self{-colors});
82         } else {
83                 glDisableClientState(SDL::GL_COLOR_ARRAY);
84         }
85 }
86
87
88 1;
89
90
91 $delay = $ARGV[0] || 5;
92
93 print "Starting $0\n";
94
95 my $app = new SDL::App  -w => 800, -h => 600, -d => 16, -gl =>1;
96
97 print "Initializing OpenGL settings\n";
98 printf "%-24s%s\n", "GL_RED_SIZE ", $app->attribute( SDL::SDL_GL_RED_SIZE() );
99 printf "%-24s%s\n", "GL_GREEN_SIZE ", $app->attribute( SDL::SDL_GL_GREEN_SIZE());
100 printf "%-24s%s\n", "GL_BLUE_SIZE ", $app->attribute( SDL::SDL_GL_BLUE_SIZE() );
101 printf "%-24s%s\n", "GL_DEPTH_SIZE ", $app->attribute( SDL::SDL_GL_DEPTH_SIZE() );
102 printf "%-24s%s\n", "GL_DOUBLEBUFFER ", $app->attribute( SDL::SDL_GL_DOUBLEBUFFER() );
103
104 $angle = 0;     
105 $other = 0;
106         
107 my @colors =  (
108         1.0,1.0,0.0,    1.0,0.0,0.0,    0.0,1.0,0.0, 0.0,0.0,1.0,       #back
109         0.4,0.4,0.4,    0.3,0.3,0.3,    0.2,0.2,0.2, 0.1,0.1,0.1 );     #front
110         
111
112 $cube = new SDL::OpenGL::Cube;
113 $cube->color(@colors);
114
115 $white = new SDL::OpenGL::Cube;
116
117 $toggle = 1;
118
119 glEnable(GL_CULL_FACE);
120 glFrontFace(GL_CCW);
121 glCullFace(GL_BACK);
122
123 sub DrawScene {
124
125         glClear( SDL::GL_DEPTH_BUFFER_BIT() 
126                 | SDL::GL_COLOR_BUFFER_BIT());
127
128         glLoadIdentity();
129
130         glTranslate(0,0,-6.0);
131         glRotate($angle % 360,1,1,0);
132         glRotate($other % 360,0,1,1);
133
134         $angle += 6;
135         $other += $angle % 5;
136
137         glColor(1,1,1);
138         $toggle ? $cube->draw() : $white->draw();
139
140         $app->sync();
141
142 }
143
144 sub InitView {
145         glViewport(0,0,800,600);
146
147         glMatrixMode(SDL::GL_PROJECTION());
148         glLoadIdentity();
149
150         if ( @_ ) {
151                 gluPerspective(45.0,4/3,0.1,100.0);
152         } else {
153                 glFrustum(-0.1,0.1,-0.075,0.075,0.3,100.0);
154         }
155
156         glMatrixMode(SDL::GL_MODELVIEW());
157         glLoadIdentity();
158 }
159
160 InitView();
161
162 DrawScene();
163 $app->sync();
164
165 my $event = new SDL::Event;
166
167 for (;;) {
168         for (0 .. 5) {
169                         $event->pump(); 
170                         $event->poll();
171                         exit(0) if ($event->type() == SDL::SDL_QUIT()); 
172                         if (SDL::GetKeyState(SDL::SDLK_SPACE()) == SDL::SDL_PRESSED()) {
173                                 $toggle = 0;
174                         } else {
175                                 $toggle = 1;
176                         }
177                         $app->delay($delay);
178                 }
179         DrawScene();
180 }
181