First commit of SDL_Perl-2.1.3
[sdlgit/SDL_perl.git] / test / OpenGL / tutorial / lesson02.pl
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
10 use strict;
11 use Getopt::Long;
12
13 use SDL;
14 use SDL::App;
15 use SDL::OpenGL;
16 use SDL::Event;
17
18 my $arg_screen_width =640;
19 my $arg_screen_height=512;
20 my $arg_fullscreen=0;
21
22 GetOptions(
23            "width:i"        => \$arg_screen_width,
24            "height:i"       => \$arg_screen_height,
25            "fullscreen!"    => \$arg_fullscreen,
26
27           ) or die $!;
28
29 main();
30 exit;
31
32
33 sub main
34   {  
35    my $done=0;
36
37    my $app = new SDL::App ( -title => "Jeff Molofee's GL Code Tutorial ... NeHe '99", 
38                             -icon => "icon.png",
39                             -width => $arg_screen_width,
40                             -height =>$arg_screen_height,
41                             -d => 16,
42                             -gl => 1,
43                             
44                           );
45         $app->fullscreen() if ($arg_fullscreen);
46    
47    SDL::ShowCursor (0);   
48    
49    my $event = new SDL::Event;
50    $event->set(SDL_SYSWMEVENT,SDL_IGNORE);#
51
52    InitGL($arg_screen_width, $arg_screen_height);
53
54    while ( ! $done ) {
55
56     DrawGLScene();
57
58     $app->sync();
59    
60     $event->pump;
61     $event->poll;
62     
63     
64     if ( $event->type == SDL_QUIT ) {
65      $done = 1;
66     }
67
68     if ( $event->type == SDL_KEYDOWN ) {
69      if ( $event->key_sym == SDLK_ESCAPE ) {
70       $done = 1;
71      }
72     }
73    }
74   }
75
76
77
78
79
80
81 #########################################################################
82 #Pretty much in original form, but 'Perlised' 
83
84
85 sub InitGL
86   {
87    my ($Width, $Height)=@_;
88
89    glViewport(0, 0, $Width, $Height);
90    glClearColor(0.0, 0.0, 0.0, 0.0);                            # This Will Clear The Background Color To Black
91    glClearDepth(1.0);                                           # Enables Clearing Of The Depth Buffer
92    glDepthFunc(GL_LESS());                                      # The Type Of Depth Test To Do
93    glEnable(GL_DEPTH_TEST());                                   # Enables Depth Testing
94    glShadeModel(GL_SMOOTH());                                   # Enables Smooth Color Shading
95    
96    glMatrixMode(GL_PROJECTION());
97    glLoadIdentity();                                            # Reset The Projection Matrix
98    
99    gluPerspective(45.0, $Width/$Height, 0.1, 100.0);            # Calculate The Aspect Ratio Of The Window
100    
101    glMatrixMode(GL_MODELVIEW());
102   }
103
104
105
106 # The main drawing function.
107 sub DrawGLScene
108   {
109    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);          # Clear The Screen And The Depth Buffer
110    glLoadIdentity();                                            # Reset The View
111    
112    glTranslate(-1.5,0.0,-6.0);                                  # Move Left 1.5 Units And Into The Screen 6.0
113    
114    # draw a triangle
115    glBegin(GL_POLYGON);                                         # start drawing a polygon
116    glVertex( 0.0, 1.0, 0.0);                                    # Top
117    glVertex( 1.0,-1.0, 0.0);                                    # Bottom Right
118    glVertex(-1.0,-1.0, 0.0);                                    # Bottom Left   
119    glEnd();                                                     # we're done with the polygon
120    
121    glTranslate(3.0,0.0,0.0);                                    # Move Right 3 Units
122    
123    # draw a square (quadrilateral)
124    glBegin(GL_QUADS);                                           # start drawing a polygon (4 sided)
125    glVertex(-1.0, 1.0, 0.0);                                    # Top Left
126    glVertex( 1.0, 1.0, 0.0);                                    # Top Right
127    glVertex( 1.0,-1.0, 0.0);                                    # Bottom Right
128    glVertex(-1.0,-1.0, 0.0);                                    # Bottom Left   
129    glEnd();                                                     # done with the polygon
130    
131    
132   }
133
134
135
136