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)
6 # If you've found this code useful, please let me know.
8 # Visit me at www.demonews.com/hosted/nehe
19 my $arg_screen_width =640;
20 my $arg_screen_height=512;
25 "width:i" => \$arg_screen_width,
26 "height:i" => \$arg_screen_height,
27 "fullscreen!" => \$arg_fullscreen,
39 my $app = new SDL::App ( -title => "Jeff Molofee's GL Code Tutorial ... NeHe '99",
41 -width => $arg_screen_width,
42 -height =>$arg_screen_height,
46 $app->fullscreen if ($arg_fullscreen);
50 my $event = new SDL::Event;
51 $event->set(SDL_SYSWMEVENT(),SDL_IGNORE());#
53 InitGL($arg_screen_width, $arg_screen_height);
66 if ( $event->type == SDL_QUIT() ) {
70 if ( $event->type == SDL_KEYDOWN() ) {
71 if ( $event->key_sym == SDLK_ESCAPE() ) {
83 #########################################################################
84 #Pretty much in original form, but 'Perlised'
88 #/* rotation angle for the triangle. */
91 #/* rotation angle for the quadrilateral. */
97 my ($Width, $Height)=@_;
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
106 glMatrixMode(GL_PROJECTION);
107 glLoadIdentity(); # Reset The Projection Matrix
109 gluPerspective(45.0, $Width/$Height, 0.1, 100.0); # Calculate The Aspect Ratio Of The Window
111 glMatrixMode(GL_MODELVIEW);
116 # The main drawing function.
119 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); # Clear The Screen And The Depth Buffer
120 glLoadIdentity(); # Reset The View
122 glTranslate(-1.5,0.0,-6.0); # Move Left 1.5 Units And Into The Screen 6.0
124 glRotate($rtri,0.0,1.0,0.0); # Rotate The Pyramid On The Y axis
126 # draw a pyramid (in smooth coloring mode)
127 glBegin(GL_POLYGON); # start drawing a pyramid
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)
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)
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)
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)
161 glEnd(); # Done Drawing The Pyramid
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
166 glRotate($rquad,1.0,1.0,1.0); # Rotate The Cube On X, Y, and Z
168 # draw a cube (6 quadrilaterals)
169 glBegin(GL_QUADS); # start drawing the 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)
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)
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)
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)
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)
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
214 $rtri+=15.0; # Increase The Rotation Variable For The Triangle
215 $rquad-=15.0; # Decrease The Rotation Variable For The Quad