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