2 # This code was created by Jeff Molofee '99
3 # (ported to SDL by Sam Lantinga '2000)
4 # (ported to Perl/SDL by Wayne Keenan '2000)
6 # If you've found this code useful, please let me know.
8 # Visit me at www.demonews.com/hosted/nehe
20 my $arg_screen_width =640;
21 my $arg_screen_height=512;
26 "width:i" => \$arg_screen_width,
27 "height:i" => \$arg_screen_height,
28 "fullscreen!" => \$arg_fullscreen,
33 ############################################################
35 my ($xrot, $yrot, $zrot) = (0,0,0);
45 my $app = new SDL::App ( -title => "Jeff Molofee's GL Code Tutorial ... NeHe '99",
46 -icon => "Data/perl.png",
47 -width => $arg_screen_width,
48 -height =>$arg_screen_height,
51 $app->fullscreen() if $arg_fullscreen;
55 my $event = new SDL::Event;
56 $event->set(SDL_SYSWMEVENT,SDL_IGNORE);
58 InitGL($arg_screen_width, $arg_screen_height);
74 if ( $event->type == SDL_QUIT ) {
78 if ( $event->type == SDL_KEYDOWN ) {
79 if ( $event->key_sym == SDLK_ESCAPE ) {
91 #########################################################################
92 #Pretty much in original form, but 'Perlised'
99 my ($Width, $Height) = @_;
101 glViewport(0, 0, $Width, $Height);
103 LoadGLTextures(); # Load The Texture(s)
105 glEnable(GL_TEXTURE_2D()); # Enable Texture Mapping
107 glClearColor(0.0, 0.0, 1.0, 0.0); # This Will Clear The Background Color To Black
108 glClearDepth(1.0); # Enables Clearing Of The Depth Buffer
109 glDepthFunc(GL_LESS); # The Type Of Depth Test To Do
110 glEnable(GL_DEPTH_TEST); # Enables Depth Testing
111 glShadeModel(GL_SMOOTH); # Enables Smooth Color Shading
113 glMatrixMode(GL_PROJECTION);
114 glLoadIdentity(); # Reset The Projection Matrix
116 gluPerspective(45.0, $Width/$Height, 0.1, 100.0); # Calculate The Aspect Ratio Of The Window
118 glMatrixMode(GL_MODELVIEW);
123 # The main drawing function.
126 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); # Clear The Screen And The Depth Buffer
127 glLoadIdentity(); # Reset The View
130 glTranslate(0.0,0.0,-5.0); # move 5 units into the screen.
132 glRotate($xrot,1.0,0.0,0.0); # Rotate On The X Axis
133 glRotate($yrot,0.0,1.0,0.0); # Rotate On The Y Axis
134 glRotate($zrot,0.0,0.0,1.0); # Rotate On The Z Axis
136 glBindTexture(GL_TEXTURE_2D, 1); # choose the texture to use.
138 glBegin(GL_QUADS); # begin drawing a cube
140 # Front Face (note that the texture's corners have to match the quad's corners)
141 glTexCoord(0.0, 0.0); glVertex(-1.0, -1.0, 1.0); # Bottom Left Of The Texture and Quad
142 glTexCoord(1.0, 0.0); glVertex( 1.0, -1.0, 1.0); # Bottom Right Of The Texture and Quad
143 glTexCoord(1.0, 1.0); glVertex( 1.0, 1.0, 1.0); # Top Right Of The Texture and Quad
144 glTexCoord(0.0, 1.0); glVertex(-1.0, 1.0, 1.0); # Top Left Of The Texture and Quad
147 glTexCoord(1.0, 0.0); glVertex(-1.0, -1.0, -1.0); # Bottom Right Of The Texture and Quad
148 glTexCoord(1.0, 1.0); glVertex(-1.0, 1.0, -1.0); # Top Right Of The Texture and Quad
149 glTexCoord(0.0, 1.0); glVertex( 1.0, 1.0, -1.0); # Top Left Of The Texture and Quad
150 glTexCoord(0.0, 0.0); glVertex( 1.0, -1.0, -1.0); # Bottom Left Of The Texture and Quad
153 glTexCoord(0.0, 1.0); glVertex(-1.0, 1.0, -1.0); # Top Left Of The Texture and Quad
154 glTexCoord(0.0, 0.0); glVertex(-1.0, 1.0, 1.0); # Bottom Left Of The Texture and Quad
155 glTexCoord(1.0, 0.0); glVertex( 1.0, 1.0, 1.0); # Bottom Right Of The Texture and Quad
156 glTexCoord(1.0, 1.0); glVertex( 1.0, 1.0, -1.0); # Top Right Of The Texture and Quad
159 glTexCoord(1.0, 1.0); glVertex(-1.0, -1.0, -1.0); # Top Right Of The Texture and Quad
160 glTexCoord(0.0, 1.0); glVertex( 1.0, -1.0, -1.0); # Top Left Of The Texture and Quad
161 glTexCoord(0.0, 0.0); glVertex( 1.0, -1.0, 1.0); # Bottom Left Of The Texture and Quad
162 glTexCoord(1.0, 0.0); glVertex(-1.0, -1.0, 1.0); # Bottom Right Of The Texture and Quad
165 glTexCoord(1.0, 0.0); glVertex( 1.0, -1.0, -1.0); # Bottom Right Of The Texture and Quad
166 glTexCoord(1.0, 1.0); glVertex( 1.0, 1.0, -1.0); # Top Right Of The Texture and Quad
167 glTexCoord(0.0, 1.0); glVertex( 1.0, 1.0, 1.0); # Top Left Of The Texture and Quad
168 glTexCoord(0.0, 0.0); glVertex( 1.0, -1.0, 1.0); # Bottom Left Of The Texture and Quad
171 glTexCoord(0.0, 0.0); glVertex(-1.0, -1.0, -1.0); # Bottom Left Of The Texture and Quad
172 glTexCoord(1.0, 0.0); glVertex(-1.0, -1.0, 1.0); # Bottom Right Of The Texture and Quad
173 glTexCoord(1.0, 1.0); glVertex(-1.0, 1.0, 1.0); # Top Right Of The Texture and Quad
174 glTexCoord(0.0, 1.0); glVertex(-1.0, 1.0, -1.0); # Top Left Of The Texture and Quad
176 glEnd(); # done with the polygon.
178 $xrot+=15.0; # X Axis Rotation
179 $yrot+=15.0; # Y Axis Rotation
180 $zrot+=15.0; # Z Axis Rotation
185 #my $image1,$a; #this can cause a segfault in LoadGLTextures/glTexImage2D !!!
192 #uncomment this for a different method of loading:
193 #my $img_data = read_gfx_file(FILENAME=>"../../ScrollerDemos/backdrop2.h");
194 #my $pixel_ptr = $img_data->{PIXEL_PTR};
195 #my $pic_info = $img_data->{INFO};
196 #my $width = $pic_info->{WIDTH};
197 #my $height = $pic_info->{HEIGHT};
200 #if you uncomment the bit above, comment this out:
202 my $surface=create_SDL_surface_from_file("Data/crate.png");
203 my $width=$surface->width();
204 my $height=$surface->height();
205 my $pitch = $surface->pitch();
206 my $bytespp= $surface->bytes_per_pixel();
207 my $size=$pitch*$height;
208 my $pixels = $surface->pixels();
211 my $textures = glGenTextures(1); #name texture
212 die "Could not genereate textures" unless $$textures[0];
214 glBindTexture(GL_TEXTURE_2D, $$textures[0]); # 2d texture
217 glTexParameter(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); # scale linearly when image bigger than texture
218 glTexParameter(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); # scale linearly when image smalled than texture
220 glTexImage2D(GL_TEXTURE_2D(),
221 0, #level (0 normal, heighr is form mip-mapping)
222 GL_RGB(), #internal format (3=GL_RGB)
225 GL_RGB(), #format RGB color data
226 GL_UNSIGNED_BYTE(), #unsigned bye data
227 $pixels); #ptr to texture data
229 die "Problem setting up 2d Texture (dimensions not a power of 2?)):".glErrorString(glGetError())."\n" if glGetError();
233 sub create_SDL_surface_from_file
237 my $surface = new SDL::Surface( -name => $filename);
248 #alternat loading support:
251 my @sprite_c_heap =();
257 TYPE => "GIMP_HEADER",
262 my $struct = read_gimp_header_image($args{FILENAME});
263 my $size = length $struct->{DATA};
264 my $c_array = new OpenGL::Array $size , GL_UNSIGNED_BYTE;
266 # c_array is the main reason to do the following ref count trickster:
267 # (otherwise the OpenGL:Array goes out of scope and the memory (image) is ZEROed out (and invalidated) by the DESTROY method
268 push @sprite_c_heap, $c_array;
269 push @sprite_area, $struct;
271 $c_array->assign_data(0, $struct->{DATA} ); #take a copy of the data
274 PIXEL_PTR => $c_array->ptr(), #could return $c_array instead to kepe ref count alive
278 #that all needs modularising.....
283 #nasty fixed to 3 byte RGB
284 sub read_gimp_header_image
287 my $cached_file="$file.cached-bin";
289 my ($width, $height,$pixel_format, $data)=(0,0,"RGB","");
291 #due to that fact that this aint the fastest code ever, we keep a cache.
292 if (-e $cached_file and (-C $file >= -C $cached_file))
295 print "Reading cached binary bitmap data : $cached_file\n";
296 open (FH, "<$file.cached-bin") or die "Open: $!";
303 chomp $pixel_format; #but who cares? not here anyway!!!
305 #slurp in the rest of the file (its pixel data)
311 $data=join '', @lines;
316 else # there is no cached file, or the cached file is out of date.
319 open (FH, "<$file") or die "Open: $!";
323 while (defined (my $line=<FH>))
325 $width =$1 if ($line =~ /width\s*=\s*(\d+)/);
326 $height=$1 if ($line =~ /height\s*=\s*(\d+)/);
327 if ($line =~ /^\s+\"(.+)\"$/g)
330 $c =~ s/\\(.)/$1/g; #remove meta guard
336 @data=(ord($1),ord($2),ord($3),ord($4));
338 chr ( ( ( ( $data[0] - 33) << 2) | ( ($data[1] - 33) >> 4) ) ).
339 chr ( ( ( ( ( $data[1] - 33) & 0xF) << 4) | ( ($data[2] - 33) >> 2) ) ).
340 chr ( ( ( ( ( $data[2] - 33) & 0x3) << 6) | ( ($data[3] - 33) ) ) );
350 print "Writing cached binary bitmap data for: $file as $cached_file\n";
352 #create a binary cached copy
353 open (FH, ">$cached_file") or die "Open: $!";
354 binmode FH; #we might have to put up with weak OSes.
355 print FH "$width\n$height\n$pixel_format\n$data";
366 FORMAT => $pixel_format,