2 # This code was created by Jeff Molofee '99
3 # (ported to SDL by Sam Lantinga '2000)
4 # (ported to Linux/SDL by Ti Leggett '01)
6 # Lesson18 adapted by JusTiCe8 @2007 from others lesson and
7 # ported to new Perl SDL, comments stripped (see Nehe lessons)
9 # If you've found this code useful, please let me know.
11 # Visit Jeff at http://nehe.gamedev.net/
13 # or for port-specific comments, questions, bugreports etc.
14 # email to leggett@eecs.tulane.edu (C/SDL)
15 # or justice8@wanadoo.fr (SDL-Perl)
22 use SDL::OpenGL::Constants;
27 use constant NUM_TEXTURES => 3;
29 my $arg_screen_width = 640;
30 my $arg_screen_height = 480;
31 my $arg_fullscreen = 0;
37 my ($p1, $p2) = (0, 1);
39 my ($xrot, $yrot, $xspeed, $yspeed);
42 my $LightAmbient = [ 0.5, 0.5, 0.5, 1.0 ];
43 my $LightDiffuse = [ 1.0, 1.0, 1.0, 1.0 ];
44 my $LightPosition = [ 0.0, 0.0, 2.0, 1.0 ];
50 # this flag is used to limit key strokes event
55 "width:i" => \$arg_screen_width,
56 "height:i" => \$arg_screen_height,
57 "fullscreen!" => \$arg_fullscreen,
71 my $app = new SDL::App
73 -title => "Jeff Molofee's lesson18: Quadratic",
75 -width => $arg_screen_width,
76 -height => $arg_screen_height,
80 $app->fullscreen if ($arg_fullscreen);
84 my $event = new SDL::Event;
85 $event->set (SDL_SYSWMEVENT (), SDL_IGNORE ());
87 InitGL ($arg_screen_width, $arg_screen_height);
89 my $prev_ticks = $app->ticks ();
90 my $fps_counter = 0.0;
97 $app->delay ($delay) if ($delay > 0);
100 my $ticks_now = $app->ticks ();
101 $fps_counter += $ticks_now - $prev_ticks;
102 $prev_ticks = $ticks_now;
104 if( $fps_counter >= 5000.0)
106 $fps = $frames_drawn / ($fps_counter / 1000.0);
107 printf("%d frames in %.2f seconds = %.3f FPS\n", $frames_drawn, $fps_counter / 1000.0, $fps);
115 $done = 1 if ($event->type == &SDL_QUIT );
117 if ($event->type == &SDL_KEYDOWN )
119 $done = 1 if ($event->key_sym == &SDLK_ESCAPE );
121 if ($event->key_sym == &SDLK_f and !$pressed)
123 $filter = 1 + $filter % 3;
124 #print "Filter = $filter\n";
127 elsif ($event->key_sym == &SDLK_l and !$pressed)
132 glDisable ( &GL_LIGHTING );
134 glEnable ( &GL_LIGHTING );
138 elsif ($event->key_sym == &SDLK_SPACE and !$pressed)
140 $object = ++$object % 6;
143 elsif ($event->key_sym == &SDLK_PAGEUP)
147 elsif ($event->key_sym == &SDLK_PAGEDOWN)
151 elsif ($event->key_sym == &SDLK_UP)
155 elsif ($event->key_sym == &SDLK_DOWN)
159 elsif ($event->key_sym == &SDLK_RIGHT)
163 elsif ($event->key_sym == &SDLK_LEFT)
168 elsif ($event->type == &SDL_KEYUP)
178 my ($Width, $Height) = @_;
179 ($xrot, $yrot) = (0.0, 0.0);
181 glViewport (0, 0, $Width, $Height);
184 glEnable (&GL_TEXTURE_2D);
185 glShadeModel (&GL_SMOOTH);
187 glClearColor (0.0, 0.0, 0.0, 0.0);
190 glEnable (&GL_DEPTH_TEST);
191 glDepthFunc (&GL_LESS); # GL_EQUAL as source tutorial don't work (?)
192 glHint ( &GL_PERSPECTIVE_CORRECTION_HINT, &GL_NICEST );
194 glLight ( &GL_LIGHT1, &GL_AMBIENT, @$LightAmbient );
195 glLight ( &GL_LIGHT1, &GL_DIFFUSE, @{$LightDiffuse} );
196 glLight ( &GL_LIGHT1, &GL_POSITION, @{$LightPosition} );
198 glEnable ( &GL_LIGHT1 );
200 $quadratic = gluNewQuadric ();
201 #gluQuadricNormals ( $quadratic, &GLU_SMOOTH );
202 gluQuadricTexture ( $quadratic, &GL_TRUE );
204 glMatrixMode (&GL_PROJECTION);
207 gluPerspective (45.0, $Width/$Height, 0.1, 100.0);
209 glMatrixMode (&GL_MODELVIEW);
217 glNormal ( 0.0, 0.0, 1.0);
218 glTexCoord (1.0, 0.0); glVertex (-1.0, -1.0, 1.0);
219 glTexCoord (0.0, 0.0); glVertex ( 1.0, -1.0, 1.0);
220 glTexCoord (0.0, 1.0); glVertex ( 1.0, 1.0, 1.0);
221 glTexCoord (1.0, 1.0); glVertex (-1.0, 1.0, 1.0);
223 glNormal ( 0.0, 0.0,-1.0);
224 glTexCoord (0.0, 0.0); glVertex (-1.0, -1.0, -1.0);
225 glTexCoord (0.0, 1.0); glVertex (-1.0, 1.0, -1.0);
226 glTexCoord (1.0, 1.0); glVertex ( 1.0, 1.0, -1.0);
227 glTexCoord (1.0, 0.0); glVertex ( 1.0, -1.0, -1.0);
229 glNormal ( 0.0, 1.0, 0.0);
230 glTexCoord( 1.0, 1.0); glVertex (-1.0, 1.0, -1.0);
231 glTexCoord (1.0, 0.0); glVertex (-1.0, 1.0, 1.0);
232 glTexCoord (0.0, 0.0); glVertex ( 1.0, 1.0, 1.0);
233 glTexCoord (0.0, 1.0); glVertex ( 1.0, 1.0, -1.0);
235 glNormal ( 0.0, -1.0, 0.0);
236 glTexCoord (0.0, 1.0); glVertex (-1.0, -1.0, -1.0);
237 glTexCoord (1.0, 1.0); glVertex ( 1.0, -1.0, -1.0);
238 glTexCoord (1.0, 0.0); glVertex ( 1.0, -1.0, 1.0);
239 glTexCoord (0.0, 0.0); glVertex (-1.0, -1.0, 1.0);
241 glNormal ( 1.0, 0.0, 0.0);
242 glTexCoord (0.0, 0.0); glVertex ( 1.0, -1.0, -1.0);
243 glTexCoord (0.0, 1.0); glVertex ( 1.0, 1.0, -1.0);
244 glTexCoord (1.0, 1.0); glVertex ( 1.0, 1.0, 1.0);
245 glTexCoord (1.0, 0.0); glVertex ( 1.0, -1.0, 1.0);
247 glNormal (-1.0, 0.0, 0.0);
248 glTexCoord (1.0, 0.0); glVertex (-1.0, -1.0, -1.0);
249 glTexCoord (0.0, 0.0); glVertex (-1.0, -1.0, 1.0);
250 glTexCoord (0.0, 1.0); glVertex (-1.0, 1.0, 1.0);
251 glTexCoord (1.0, 1.0); glVertex (-1.0, 1.0, -1.0);
259 glClear(&GL_COLOR_BUFFER_BIT | &GL_DEPTH_BUFFER_BIT);
262 glTranslate ( 0.0, 0.0, $z);
264 glRotate ($xrot, 1.0, 0.0, 0.0);
265 glRotate ($yrot, 0.0, 1.0, 0.0);
267 glBindTexture(&GL_TEXTURE_2D, $filter);
275 glTranslate ( 0.0, 0.0, -1.5 );
276 gluCylinder ( $quadratic, 1.0, 1.0, 3.0, 32, 32 );
280 gluDisk ( $quadratic, 0.5, 1.5, 32, 32 );
284 gluSphere ( $quadratic, 1.3, 32, 32 );
288 glTranslate ( 0.0, 0.0, -1.5 );
289 gluCylinder ( $quadratic, 1.0, 0.0, 3.0, 32, 32 );
308 gluPartialDisk ( $quadratic, 0.5, 1.5, 32, 32, $part1, $part2 - $part1 );
318 my ($pixels, $width, $height, $size) = ImageLoad ('data/wall.bmp');
319 #print "LoadGLTexture: w:$width h:$height s:$size\n";
321 my $textures = glGenTextures (NUM_TEXTURES);
322 unless ($$textures[0])
324 print "Could not generate textures\n";
328 glBindTexture (&GL_TEXTURE_2D, 1); #$$textures[0]);
342 glTexParameter(&GL_TEXTURE_2D, &GL_TEXTURE_MAG_FILTER, &GL_NEAREST);
343 glTexParameter(&GL_TEXTURE_2D, &GL_TEXTURE_MIN_FILTER, &GL_NEAREST);
346 glBindTexture (&GL_TEXTURE_2D, 2);
360 glTexParameter (&GL_TEXTURE_2D, &GL_TEXTURE_MIN_FILTER, &GL_LINEAR);
361 glTexParameter (&GL_TEXTURE_2D, &GL_TEXTURE_MAG_FILTER, &GL_LINEAR);
364 glBindTexture (&GL_TEXTURE_2D, 3);
366 glTexParameter (&GL_TEXTURE_2D, &GL_TEXTURE_MIN_FILTER, &GL_LINEAR_MIPMAP_NEAREST);
367 glTexParameter (&GL_TEXTURE_2D, &GL_TEXTURE_MAG_FILTER, &GL_LINEAR);
379 my $glerr = glGetError ();
382 print "Problem setting up 2d Texture (dimensions not a power of 2?)): ".gluErrorString ($glerr)."\n";
390 my $filename = shift;
391 return unless (defined $filename and -e $filename);
393 #somthing needs to keep the ref count alive for objects which represents data in C space (they have no ref count):
396 #makes use of SDL: BMP loader.
397 my $surface = new SDL::Surface (-name => $filename);
399 my $width = $surface->width ();
400 my $height = $surface->height ();
401 my $bytespp = $surface->bytes_per_pixel ();
402 my $size = $width * $height * $bytespp;
404 return ($surface->pixels (), $width, $height, $size);
407 my $surface_pixels = $surface->pixels ();
408 my $surface_size = $width * $height * $surface->bytes_per_pixel ();
409 my $raw_pixels = reverse $surface_pixels;
411 #do a conversion (the pixel data is accessable as a simple string)
412 my $pixels = $raw_pixels;
413 my $pre_conv = $pixels;
416 for (my $y = 0; $y< $height; $y++)
418 # calculate offset into the image (a string)
419 my $y_pos = $y * $width * $bytespp;
421 # extract 1 pixel row
422 my $row = substr ($pre_conv, $y_pos, $width*$bytespp);
424 # turn the BMP BGR order into OpenGL RGB order;
425 $row =~ s/\G(.)(.)(.)/$3$2$1/gms;
426 $new_pixels .= reverse $row;
429 $raw_pixels = $new_pixels;
430 push @ref, $raw_pixels, $surface;
432 # we could have created another SDL surface frm the '$raw_pixel's... oh well.
433 return ($raw_pixels, $width, $height, $size);
439 gluDeleteQuadric ($quadratic);
443 print "Q=$quadratic\n";
445 Dump ($quadratic, 5);
446 gluDeleteQuadric ($quadratic);
447 Dump ($quadratic, 5);
449 print "Q=$quadratic\n";
450 Dump ($quadratic, 5);