Importing SDLPerl 2.2
[sdlgit/SDL_perl.git] / test / OpenGL / tutorial / lesson03.sdlpl
1 #!/usr/bin/env perl
2 #
3 # lesson03.pl
4 #
5 # Copyright (C) 2005 David J. Goehrig <dgoehrig@cpan.org>
6 #
7 # ------------------------------------------------------------------------------
8 #
9 # This library is free software; you can redistribute it and/or
10 # modify it under the terms of the GNU Lesser General Public
11 # License as published by the Free Software Foundation; either
12 # version 2.1 of the License, or (at your option) any later version.
13
14 # This library is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 # Lesser General Public License for more details.
18
19 # You should have received a copy of the GNU Lesser General Public
20 # License along with this library; if not, write to the Free Software
21 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
22 #
23 # ------------------------------------------------------------------------------
24 #
25 # Please feel free to send questions, suggestions or improvements to:
26 #
27 #       David J. Goehrig
28 #       dgoehrig@cpan.org
29
30 use strict;
31 use Getopt::Long;
32 use SDL;
33 use SDL::App;
34 use SDL::OpenGL;
35 use SDL::Event;
36
37 my $arg_screen_width =640;
38 my $arg_screen_height=512;
39 my $arg_fullscreen=0;
40
41 GetOptions(
42            "width:i"        => \$arg_screen_width,
43            "height:i"       => \$arg_screen_height,
44            "fullscreen!"    => \$arg_fullscreen,
45           ) or die $!;
46
47 main();
48 exit;
49
50
51 sub main
52   {  
53    my $done=0;
54    
55    my $app = new SDL::App ( -title => "Jeff Molofee's GL Code Tutorial ... NeHe '99", 
56                             -icon => "icon.png",
57                             -width => $arg_screen_width,
58                             -height =>$arg_screen_height,
59                              -d => 16,
60                              -opengl => 1,
61                           );
62    $app->fullscreen() if $arg_fullscreen;
63    
64    SDL::ShowCursor(0);   
65    
66    my $event = new SDL::Event;
67    $event->set(SDL_SYSWMEVENT,SDL_IGNORE);#
68    
69
70
71    InitGL($arg_screen_width, $arg_screen_height);
72
73    while ( ! $done ) {
74
75     DrawGLScene();
76
77     $app->sync();
78    
79     $event->pump;
80     $event->poll;
81     
82     
83     if ( $event->type == SDL_QUIT ) {
84      $done = 1;
85     }
86
87     if ( $event->type == SDL_KEYDOWN ) {
88      if ( $event->key_sym == SDLK_ESCAPE ) {
89       $done = 1;
90      }
91     }
92    }
93   }
94
95
96
97
98
99
100 #########################################################################
101 #Pretty much in original form, but 'Perlised' 
102
103
104 sub InitGL
105   {
106    my ($Width, $Height)=@_;
107
108    glViewport(0, 0, $Width, $Height);
109    glClearColor(0.0, 0.0, 0.0, 0.0);                            # This Will Clear The Background Color To Black
110    glClearDepth(1.0);                                           # Enables Clearing Of The Depth Buffer
111    glDepthFunc(GL_LESS);                                        # The Type Of Depth Test To Do
112    glEnable(GL_DEPTH_TEST);                                     # Enables Depth Testing
113    glShadeModel(GL_SMOOTH);                                     # Enables Smooth Color Shading
114    
115    glMatrixMode(GL_PROJECTION);
116    glLoadIdentity();                                            # Reset The Projection Matrix
117    
118    gluPerspective(45.0, $Width/$Height, 0.1, 100.0);            # Calculate The Aspect Ratio Of The Window
119    
120    glMatrixMode(GL_MODELVIEW);
121   }
122
123
124
125 # The main drawing function.
126 sub DrawGLScene
127   {
128    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);          # Clear The Screen And The Depth Buffer
129    glLoadIdentity();                                            # Reset The View
130    
131    glTranslate(-1.5,0.0,-6.0);          # Move Left 1.5 Units And Into The Screen 6.0
132         
133    # draw a triangle (in smooth coloring mode)
134    glBegin(GL_POLYGON);                         # start drawing a polygon
135    glColor(1.0,0.0,0.0);                        # Set The Color To Red
136    glVertex( 0.0, 1.0, 0.0);                    # Top
137    glColor(0.0,1.0,0.0);                        # Set The Color To Green
138    glVertex( 1.0,-1.0, 0.0);                    # Bottom Right
139    glColor(0.0,0.0,1.0);                        # Set The Color To Blue
140    glVertex(-1.0,-1.0, 0.0);                    # Bottom Left   
141    glEnd();                                     # we're done with the polygon (smooth color interpolation)      
142    
143    glTranslate(3.0,0.0,0.0);                    # Move Right 3 Units
144    
145    # draw a square (quadrilateral)
146    glColor(0.5,0.5,1.0);                        # set color to a blue shade.
147    glBegin(GL_QUADS);                           # start drawing a polygon (4 sided)
148    glVertex(-1.0, 1.0, 0.0);                    # Top Left
149    glVertex( 1.0, 1.0, 0.0);                    # Top Right
150    glVertex( 1.0,-1.0, 0.0);                    # Bottom Right
151    glVertex(-1.0,-1.0, 0.0);                    # Bottom Left   
152    glEnd();                                     # done with the polygon
153
154    
155    
156    
157   }
158
159
160
161