Clean up. This dir is still on experimental branch
[sdlgit/SDL_perl.git] / test / OpenGL / tutorial / lesson05.pl
1 #!/usr/bin/perl 
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 use SDL::Cursor;     
18
19 my $arg_screen_width =640;
20 my $arg_screen_height=512;
21 my $arg_fullscreen=0;
22 my $delay = 30;
23
24 GetOptions(
25            "width:i"        => \$arg_screen_width,
26            "height:i"       => \$arg_screen_height,
27            "fullscreen!"    => \$arg_fullscreen,
28            "delay:i"        => \$delay,
29           ) or die $!;
30
31 main();
32 exit;
33
34
35 sub main
36   {  
37    my $done=0;
38    
39    my $app = new SDL::App ( -title => "Jeff Molofee's GL Code Tutorial ... NeHe '99", 
40                             -icon => "icon.png",
41                             -width => $arg_screen_width,
42                             -height =>$arg_screen_height,
43                             -opengl => 1,
44                           );
45
46         $app->fullscreen if ($arg_fullscreen);
47    
48    SDL::ShowCursor(0);   
49    
50    my $event = new SDL::Event;
51    $event->set(SDL_SYSWMEVENT(),SDL_IGNORE());#
52    
53    InitGL($arg_screen_width, $arg_screen_height);
54
55    while ( ! $done ) {
56
57     DrawGLScene();
58
59     $app->sync();
60     $app->delay($delay);
61    
62     $event->pump;
63     $event->poll;
64     
65     
66     if ( $event->type == SDL_QUIT() ) {
67      $done = 1;
68     }
69
70     if ( $event->type == SDL_KEYDOWN() ) {
71      if ( $event->key_sym == SDLK_ESCAPE() ) {
72       $done = 1;
73      }
74     }
75    }
76   }
77
78
79
80
81
82
83 #########################################################################
84 #Pretty much in original form, but 'Perlised' 
85
86
87
88 #/* rotation angle for the triangle. */
89 my $rtri = 0.0;
90
91 #/* rotation angle for the quadrilateral. */
92 my $rquad = 0.0;
93
94
95 sub InitGL
96   {
97    my ($Width, $Height)=@_;
98
99    glViewport(0, 0, $Width, $Height);
100    glClearColor(0.0, 0.0, 0.0, 0.0);                            # This Will Clear The Background Color To Black
101    glClearDepth(1.0);                                           # Enables Clearing Of The Depth Buffer
102    glDepthFunc(GL_LESS);                                        # The Type Of Depth Test To Do
103    glEnable(GL_DEPTH_TEST);                                     # Enables Depth Testing
104    glShadeModel(GL_SMOOTH);                                     # Enables Smooth Color Shading
105    
106    glMatrixMode(GL_PROJECTION);
107    glLoadIdentity();                                            # Reset The Projection Matrix
108    
109    gluPerspective(45.0, $Width/$Height, 0.1, 100.0);            # Calculate The Aspect Ratio Of The Window
110    
111    glMatrixMode(GL_MODELVIEW);
112   }
113
114
115
116 # The main drawing function.
117 sub DrawGLScene
118   {
119    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);          # Clear The Screen And The Depth Buffer
120    glLoadIdentity();                                            # Reset The View
121    
122   glTranslate(-1.5,0.0,-6.0);           # Move Left 1.5 Units And Into The Screen 6.0
123         
124   glRotate($rtri,0.0,1.0,0.0);          # Rotate The Pyramid On The Y axis 
125
126   # draw a pyramid (in smooth coloring mode)
127   glBegin(GL_POLYGON);                          # start drawing a pyramid
128
129   # front face of pyramid
130   glColor(1.0,0.0,0.0);                 # Set The Color To Red
131   glVertex(0.0, 1.0, 0.0);                      # Top of triangle (front)
132   glColor(0.0,1.0,0.0);                 # Set The Color To Green
133   glVertex(-1.0,-1.0, 1.0);             # left of triangle (front)
134   glColor(0.0,0.0,1.0);                 # Set The Color To Blue
135   glVertex(1.0,-1.0, 1.0);                      # right of traingle (front)     
136
137   # right face of pyramid
138   glColor(1.0,0.0,0.0);                 # Red
139   glVertex( 0.0, 1.0, 0.0);             # Top Of Triangle (Right)
140   glColor(0.0,0.0,1.0);                 # Blue
141   glVertex( 1.0,-1.0, 1.0);             # Left Of Triangle (Right)
142   glColor(0.0,1.0,0.0);                 # Green
143   glVertex( 1.0,-1.0, -1.0);            # Right Of Triangle (Right)
144
145   # back face of pyramid
146   glColor(1.0,0.0,0.0);                 # Red
147   glVertex( 0.0, 1.0, 0.0);             # Top Of Triangle (Back)
148   glColor(0.0,1.0,0.0);                 # Green
149   glVertex( 1.0,-1.0, -1.0);            # Left Of Triangle (Back)
150   glColor(0.0,0.0,1.0);                 # Blue
151   glVertex(-1.0,-1.0, -1.0);            # Right Of Triangle (Back)
152
153   # left face of pyramid.
154   glColor(1.0,0.0,0.0);                 # Red
155   glVertex( 0.0, 1.0, 0.0);             # Top Of Triangle (Left)
156   glColor(0.0,0.0,1.0);                 # Blue
157   glVertex(-1.0,-1.0,-1.0);             # Left Of Triangle (Left)
158   glColor(0.0,1.0,0.0);                 # Green
159   glVertex(-1.0,-1.0, 1.0);             # Right Of Triangle (Left)
160
161   glEnd();                                      # Done Drawing The Pyramid
162
163   glLoadIdentity();                             # make sure we're no longer rotated.
164   glTranslate(1.5,0.0,-7.0);            # Move Right 3 Units, and back into the screen 7
165         
166   glRotate($rquad,1.0,1.0,1.0);         # Rotate The Cube On X, Y, and Z
167
168   # draw a cube (6 quadrilaterals)
169   glBegin(GL_QUADS);                            # start drawing the cube.
170   
171   # top of cube
172   glColor(0.0,1.0,0.0);                 # Set The Color To Blue
173   glVertex( 1.0, 1.0,-1.0);             # Top Right Of The Quad (Top)
174   glVertex(-1.0, 1.0,-1.0);             # Top Left Of The Quad (Top)
175   glVertex(-1.0, 1.0, 1.0);             # Bottom Left Of The Quad (Top)
176   glVertex( 1.0, 1.0, 1.0);             # Bottom Right Of The Quad (Top)
177
178   # bottom of cube
179   glColor(1.0,0.5,0.0);                 # Set The Color To Orange
180   glVertex( 1.0,-1.0, 1.0);             # Top Right Of The Quad (Bottom)
181   glVertex(-1.0,-1.0, 1.0);             # Top Left Of The Quad (Bottom)
182   glVertex(-1.0,-1.0,-1.0);             # Bottom Left Of The Quad (Bottom)
183   glVertex( 1.0,-1.0,-1.0);             # Bottom Right Of The Quad (Bottom)
184
185   # front of cube
186   glColor(1.0,0.0,0.0);                 # Set The Color To Red
187   glVertex( 1.0, 1.0, 1.0);             # Top Right Of The Quad (Front)
188   glVertex(-1.0, 1.0, 1.0);             # Top Left Of The Quad (Front)
189   glVertex(-1.0,-1.0, 1.0);             # Bottom Left Of The Quad (Front)
190   glVertex( 1.0,-1.0, 1.0);             # Bottom Right Of The Quad (Front)
191
192   # back of cube.
193   glColor(1.0,1.0,0.0);                 # Set The Color To Yellow
194   glVertex( 1.0,-1.0,-1.0);             # Top Right Of The Quad (Back)
195   glVertex(-1.0,-1.0,-1.0);             # Top Left Of The Quad (Back)
196   glVertex(-1.0, 1.0,-1.0);             # Bottom Left Of The Quad (Back)
197   glVertex( 1.0, 1.0,-1.0);             # Bottom Right Of The Quad (Back)
198
199   # left of cube
200   glColor(0.0,0.0,1.0);                 # Blue
201   glVertex(-1.0, 1.0, 1.0);             # Top Right Of The Quad (Left)
202   glVertex(-1.0, 1.0,-1.0);             # Top Left Of The Quad (Left)
203   glVertex(-1.0,-1.0,-1.0);             # Bottom Left Of The Quad (Left)
204   glVertex(-1.0,-1.0, 1.0);             # Bottom Right Of The Quad (Left)
205
206   # Right of cube
207   glColor(1.0,0.0,1.0);                 # Set The Color To Violet
208   glVertex( 1.0, 1.0,-1.0);             # Top Right Of The Quad (Right)
209   glVertex( 1.0, 1.0, 1.0);             # Top Left Of The Quad (Right)
210   glVertex( 1.0,-1.0, 1.0);             # Bottom Left Of The Quad (Right)
211   glVertex( 1.0,-1.0,-1.0);             # Bottom Right Of The Quad (Right)
212   glEnd();                                      # Done Drawing The Cube
213
214   $rtri+=15.0;                                  # Increase The Rotation Variable For The Triangle
215   $rquad-=15.0;                                 # Decrease The Rotation Variable For The Quad 
216  
217    
218   }
219
220
221
222