Clean up. This dir is still on experimental branch
[sdlgit/SDL_perl.git] / test / OpenGL / tutorial / lesson04.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 my $delay=30;
22
23 GetOptions(
24            "width:i"        => \$arg_screen_width,
25            "height:i"       => \$arg_screen_height,
26            "fullscreen!"    => \$arg_fullscreen,
27            "delay:i"        => \$delay,
28           ) or die <<USAGE;
29 Usage $0 -w 800 -h 600 -d 10 --fullscreen 
30 USAGE
31
32 #/* rotation angle for the triangle. */
33 my $rtri = 0.0;
34
35 #/* rotation angle for the quadrilateral. */
36 my $rquad = 0.0;
37
38
39 main();
40 exit;
41
42
43 sub main
44   {  
45    my $done=0;
46    my $app = new SDL::App ( -title => "Jeff Molofee's GL Code Tutorial ... NeHe '99", 
47                             -icon => "icon.png",
48                             -width => $arg_screen_width,
49                             -height =>$arg_screen_height,
50                             -gl => 1,
51                           );
52    $app->fullscreen() if $arg_fullscreen;
53    
54    SDL::ShowCursor(0);   
55    
56    my $event = new SDL::Event;
57    $event->set(SDL_SYSWMEVENT,SDL_IGNORE);#
58    
59
60
61    InitGL($arg_screen_width, $arg_screen_height);
62
63    while ( ! $done ) {
64
65     DrawGLScene();
66
67     $app->sync();
68     $app->delay($delay);
69    
70     $event->pump;
71     $event->poll;
72     
73     
74     if ( $event->type == SDL_QUIT ) {
75      $done = 1;
76     }
77
78     if ( $event->type == SDL_KEYDOWN ) {
79      if ( $event->key_sym == SDLK_ESCAPE ) {
80       $done = 1;
81      }
82     }
83    }
84   }
85
86
87
88
89
90
91 #########################################################################
92 #Pretty much in original form, but 'Perlised' 
93
94
95
96
97
98 sub InitGL
99   {
100    my ($Width, $Height)=@_;
101
102    glViewport(0, 0, $Width, $Height);
103    glClearColor(0.0, 0.0, 0.0, 0.0);                            # This Will Clear The Background Color To Black
104    glClearDepth(1.0);                                           # Enables Clearing Of The Depth Buffer
105    glDepthFunc(GL_LESS);                                        # The Type Of Depth Test To Do
106    glEnable(GL_DEPTH_TEST);                                     # Enables Depth Testing
107    glShadeModel(GL_SMOOTH);                                     # Enables Smooth Color Shading
108    
109    glMatrixMode(GL_PROJECTION);
110    glLoadIdentity();                                            # Reset The Projection Matrix
111    
112    gluPerspective(45.0, $Width/$Height, 0.1, 100.0);            # Calculate The Aspect Ratio Of The Window
113    
114    glMatrixMode(GL_MODELVIEW);
115   }
116
117
118
119 # The main drawing function.
120 sub DrawGLScene
121   {
122    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);          # Clear The Screen And The Depth Buffer
123    glLoadIdentity();                                            # Reset The View
124    
125    glTranslate(-1.5,0.0,-6.0);          # Move Left 1.5 Units And Into The Screen 6.0
126         
127   glRotate($rtri,0.0,1.0,0.0);          # Rotate The Triangle On The Y axis 
128   # draw a triangle (in smooth coloring mode)
129   glBegin(GL_POLYGON);                          # start drawing a polygon
130   glColor(1.0,0.0,0.0);                 # Set The Color To Red
131   glVertex( 0.0, 1.0, 0.0);             # Top
132   glColor(0.0,1.0,0.0);                 # Set The Color To Green
133   glVertex( 1.0,-1.0, 0.0);             # Bottom Right
134   glColor(0.0,0.0,1.0);                 # Set The Color To Blue
135   glVertex(-1.0,-1.0, 0.0);             # Bottom Left   
136   glEnd();                                      # we're done with the polygon (smooth color interpolation)
137
138   glLoadIdentity();                             # make sure we're no longer rotated.
139   glTranslate(1.5,0.0,-6.0);            # Move Right 3 Units, and back into the screen 6.0
140         
141   glRotate($rquad,1.0,0.0,0.0);         # Rotate The Quad On The X axis 
142   # draw a square (quadrilateral)
143   glColor(0.5,0.5,1.0);                 # set color to a blue shade.
144   glBegin(GL_QUADS);                            # start drawing a polygon (4 sided)
145   glVertex(-1.0, 1.0, 0.0);             # Top Left
146   glVertex( 1.0, 1.0, 0.0);             # Top Right
147   glVertex( 1.0,-1.0, 0.0);             # Bottom Right
148   glVertex(-1.0,-1.0, 0.0);             # Bottom Left   
149   glEnd();                                      # done with the polygon
150
151   $rtri+=15.0;                                  # Increase The Rotation Variable For The Triangle
152   $rquad-=15.0;                                 # Decrease The Rotation Variable For The Quad 
153  
154    
155   }
156
157
158
159