5 # Copyright (C) 2005 David J. Goehrig <dgoehrig@cpan.org>
7 # ------------------------------------------------------------------------------
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.
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.
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
23 # ------------------------------------------------------------------------------
25 # Please feel free to send questions, suggestions or improvements to:
39 my $arg_screen_width =640;
40 my $arg_screen_height=512;
45 "width:i" => \$arg_screen_width,
46 "height:i" => \$arg_screen_height,
47 "fullscreen!" => \$arg_fullscreen,
59 my $app = new SDL::App ( -title => "Jeff Molofee's GL Code Tutorial ... NeHe '99",
61 -width => $arg_screen_width,
62 -height =>$arg_screen_height,
66 $app->fullscreen if ($arg_fullscreen);
70 my $event = new SDL::Event;
71 $event->set(SDL_SYSWMEVENT(),SDL_IGNORE());#
73 InitGL($arg_screen_width, $arg_screen_height);
86 if ( $event->type == SDL_QUIT() ) {
90 if ( $event->type == SDL_KEYDOWN() ) {
91 if ( $event->key_sym == SDLK_ESCAPE() ) {
103 #########################################################################
104 #Pretty much in original form, but 'Perlised'
108 #/* rotation angle for the triangle. */
111 #/* rotation angle for the quadrilateral. */
117 my ($Width, $Height)=@_;
119 glViewport(0, 0, $Width, $Height);
120 glClearColor(0.0, 0.0, 0.0, 0.0); # This Will Clear The Background Color To Black
121 glClearDepth(1.0); # Enables Clearing Of The Depth Buffer
122 glDepthFunc(GL_LESS); # The Type Of Depth Test To Do
123 glEnable(GL_DEPTH_TEST); # Enables Depth Testing
124 glShadeModel(GL_SMOOTH); # Enables Smooth Color Shading
126 glMatrixMode(GL_PROJECTION);
127 glLoadIdentity(); # Reset The Projection Matrix
129 gluPerspective(45.0, $Width/$Height, 0.1, 100.0); # Calculate The Aspect Ratio Of The Window
131 glMatrixMode(GL_MODELVIEW);
136 # The main drawing function.
139 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); # Clear The Screen And The Depth Buffer
140 glLoadIdentity(); # Reset The View
142 glTranslate(-1.5,0.0,-6.0); # Move Left 1.5 Units And Into The Screen 6.0
144 glRotate($rtri,0.0,1.0,0.0); # Rotate The Pyramid On The Y axis
146 # draw a pyramid (in smooth coloring mode)
147 glBegin(GL_POLYGON); # start drawing a pyramid
149 # front face of pyramid
150 glColor(1.0,0.0,0.0); # Set The Color To Red
151 glVertex(0.0, 1.0, 0.0); # Top of triangle (front)
152 glColor(0.0,1.0,0.0); # Set The Color To Green
153 glVertex(-1.0,-1.0, 1.0); # left of triangle (front)
154 glColor(0.0,0.0,1.0); # Set The Color To Blue
155 glVertex(1.0,-1.0, 1.0); # right of traingle (front)
157 # right face of pyramid
158 glColor(1.0,0.0,0.0); # Red
159 glVertex( 0.0, 1.0, 0.0); # Top Of Triangle (Right)
160 glColor(0.0,0.0,1.0); # Blue
161 glVertex( 1.0,-1.0, 1.0); # Left Of Triangle (Right)
162 glColor(0.0,1.0,0.0); # Green
163 glVertex( 1.0,-1.0, -1.0); # Right Of Triangle (Right)
165 # back face of pyramid
166 glColor(1.0,0.0,0.0); # Red
167 glVertex( 0.0, 1.0, 0.0); # Top Of Triangle (Back)
168 glColor(0.0,1.0,0.0); # Green
169 glVertex( 1.0,-1.0, -1.0); # Left Of Triangle (Back)
170 glColor(0.0,0.0,1.0); # Blue
171 glVertex(-1.0,-1.0, -1.0); # Right Of Triangle (Back)
173 # left face of pyramid.
174 glColor(1.0,0.0,0.0); # Red
175 glVertex( 0.0, 1.0, 0.0); # Top Of Triangle (Left)
176 glColor(0.0,0.0,1.0); # Blue
177 glVertex(-1.0,-1.0,-1.0); # Left Of Triangle (Left)
178 glColor(0.0,1.0,0.0); # Green
179 glVertex(-1.0,-1.0, 1.0); # Right Of Triangle (Left)
181 glEnd(); # Done Drawing The Pyramid
183 glLoadIdentity(); # make sure we're no longer rotated.
184 glTranslate(1.5,0.0,-7.0); # Move Right 3 Units, and back into the screen 7
186 glRotate($rquad,1.0,1.0,1.0); # Rotate The Cube On X, Y, and Z
188 # draw a cube (6 quadrilaterals)
189 glBegin(GL_QUADS); # start drawing the cube.
192 glColor(0.0,1.0,0.0); # Set The Color To Blue
193 glVertex( 1.0, 1.0,-1.0); # Top Right Of The Quad (Top)
194 glVertex(-1.0, 1.0,-1.0); # Top Left Of The Quad (Top)
195 glVertex(-1.0, 1.0, 1.0); # Bottom Left Of The Quad (Top)
196 glVertex( 1.0, 1.0, 1.0); # Bottom Right Of The Quad (Top)
199 glColor(1.0,0.5,0.0); # Set The Color To Orange
200 glVertex( 1.0,-1.0, 1.0); # Top Right Of The Quad (Bottom)
201 glVertex(-1.0,-1.0, 1.0); # Top Left Of The Quad (Bottom)
202 glVertex(-1.0,-1.0,-1.0); # Bottom Left Of The Quad (Bottom)
203 glVertex( 1.0,-1.0,-1.0); # Bottom Right Of The Quad (Bottom)
206 glColor(1.0,0.0,0.0); # Set The Color To Red
207 glVertex( 1.0, 1.0, 1.0); # Top Right Of The Quad (Front)
208 glVertex(-1.0, 1.0, 1.0); # Top Left Of The Quad (Front)
209 glVertex(-1.0,-1.0, 1.0); # Bottom Left Of The Quad (Front)
210 glVertex( 1.0,-1.0, 1.0); # Bottom Right Of The Quad (Front)
213 glColor(1.0,1.0,0.0); # Set The Color To Yellow
214 glVertex( 1.0,-1.0,-1.0); # Top Right Of The Quad (Back)
215 glVertex(-1.0,-1.0,-1.0); # Top Left Of The Quad (Back)
216 glVertex(-1.0, 1.0,-1.0); # Bottom Left Of The Quad (Back)
217 glVertex( 1.0, 1.0,-1.0); # Bottom Right Of The Quad (Back)
220 glColor(0.0,0.0,1.0); # Blue
221 glVertex(-1.0, 1.0, 1.0); # Top Right Of The Quad (Left)
222 glVertex(-1.0, 1.0,-1.0); # Top Left Of The Quad (Left)
223 glVertex(-1.0,-1.0,-1.0); # Bottom Left Of The Quad (Left)
224 glVertex(-1.0,-1.0, 1.0); # Bottom Right Of The Quad (Left)
227 glColor(1.0,0.0,1.0); # Set The Color To Violet
228 glVertex( 1.0, 1.0,-1.0); # Top Right Of The Quad (Right)
229 glVertex( 1.0, 1.0, 1.0); # Top Left Of The Quad (Right)
230 glVertex( 1.0,-1.0, 1.0); # Bottom Left Of The Quad (Right)
231 glVertex( 1.0,-1.0,-1.0); # Bottom Right Of The Quad (Right)
232 glEnd(); # Done Drawing The Cube
234 $rtri+=15.0; # Increase The Rotation Variable For The Triangle
235 $rquad-=15.0; # Decrease The Rotation Variable For The Quad