PageRenderTime 21ms CodeModel.GetById 8ms app.highlight 10ms RepoModel.GetById 2ms app.codeStats 0ms

/t_backcompat/OpenGL/tutorial/lesson03.pl

http://github.com/PerlGameDev/SDL
Perl | 122 lines | 80 code | 29 blank | 13 comment | 8 complexity | c7d3a54d0aca0cc756d3dcee37a93775 MD5 | raw file
  1#!/usr/bin/perl -w
  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)
  5#
  6# If you've found this code useful, please let me know.
  7#
  8# Visit me at www.demonews.com/hosted/nehe
  9
 10use strict;
 11use Getopt::Long;
 12
 13use SDL;
 14use SDLx::App;
 15use SDL::OpenGL;
 16use SDL::Event;
 17
 18my $arg_screen_width  = 640;
 19my $arg_screen_height = 512;
 20my $arg_fullscreen    = 0;
 21
 22GetOptions(
 23	"width:i"     => \$arg_screen_width,
 24	"height:i"    => \$arg_screen_height,
 25	"fullscreen!" => \$arg_fullscreen,
 26) or die $!;
 27
 28main();
 29exit;
 30
 31sub main {
 32	my $done = 0;
 33
 34	my $app = SDLx::App->new(
 35		-title  => "Jeff Molofee's GL Code Tutorial ... NeHe '99",
 36		-icon   => "icon.png",
 37		-width  => $arg_screen_width,
 38		-height => $arg_screen_height,
 39		-d      => 16,
 40		-opengl => 1,
 41	);
 42	$app->fullscreen() if $arg_fullscreen;
 43
 44	SDL::ShowCursor(0);
 45
 46	my $event = SDL::Event->new;
 47	$event->set( SDL_SYSWMEVENT, SDL_IGNORE ); #
 48
 49	InitGL( $arg_screen_width, $arg_screen_height );
 50
 51	while ( !$done ) {
 52
 53		DrawGLScene();
 54
 55		$app->sync();
 56
 57		$event->pump;
 58		$event->poll;
 59
 60		if ( $event->type == SDL_QUIT ) {
 61			$done = 1;
 62		}
 63
 64		if ( $event->type == SDL_KEYDOWN ) {
 65			if ( $event->key_sym == SDLK_ESCAPE ) {
 66				$done = 1;
 67			}
 68		}
 69	}
 70}
 71
 72#########################################################################
 73#Pretty much in original form, but 'Perlised'
 74
 75sub InitGL {
 76	my ( $Width, $Height ) = @_;
 77
 78	glViewport( 0, 0, $Width, $Height );
 79	glClearColor( 0.0, 0.0, 0.0, 0.0 ); # This Will Clear The Background Color To Black
 80	glClearDepth(1.0);                  # Enables Clearing Of The Depth Buffer
 81	glDepthFunc(GL_LESS);               # The Type Of Depth Test To Do
 82	glEnable(GL_DEPTH_TEST);            # Enables Depth Testing
 83	glShadeModel(GL_SMOOTH);            # Enables Smooth Color Shading
 84
 85	glMatrixMode(GL_PROJECTION);
 86	glLoadIdentity();                   # Reset The Projection Matrix
 87
 88	gluPerspective( 45.0, $Width / $Height, 0.1, 100.0 ); # Calculate The Aspect Ratio Of The Window
 89
 90	glMatrixMode(GL_MODELVIEW);
 91}
 92
 93# The main drawing function.
 94sub DrawGLScene {
 95	glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); # Clear The Screen And The Depth Buffer
 96	glLoadIdentity();                                     # Reset The View
 97
 98	glTranslate( -1.5, 0.0, -6.0 );                       # Move Left 1.5 Units And Into The Screen 6.0
 99
100	# draw a triangle (in smooth coloring mode)
101	glBegin(GL_POLYGON);                                  # start drawing a polygon
102	glColor( 1.0, 0.0, 0.0 );                             # Set The Color To Red
103	glVertex( 0.0, 1.0, 0.0 );                            # Top
104	glColor( 0.0, 1.0, 0.0 );                             # Set The Color To Green
105	glVertex( 1.0, -1.0, 0.0 );                           # Bottom Right
106	glColor( 0.0, 0.0, 1.0 );                             # Set The Color To Blue
107	glVertex( -1.0, -1.0, 0.0 );                          # Bottom Left
108	glEnd();                                              # we're done with the polygon (smooth color interpolation)
109
110	glTranslate( 3.0, 0.0, 0.0 );                         # Move Right 3 Units
111
112	# draw a square (quadrilateral)
113	glColor( 0.5, 0.5, 1.0 );                             # set color to a blue shade.
114	glBegin(GL_QUADS);                                    # start drawing a polygon (4 sided)
115	glVertex( -1.0, 1.0,  0.0 );                          # Top Left
116	glVertex( 1.0,  1.0,  0.0 );                          # Top Right
117	glVertex( 1.0,  -1.0, 0.0 );                          # Bottom Right
118	glVertex( -1.0, -1.0, 0.0 );                          # Bottom Left
119	glEnd();                                              # done with the polygon
120
121}
122