Importing SDLPerl 2.2
[sdlgit/SDL_perl.git] / test / OpenGL / tutorial / lesson05.sdlpl
CommitLineData
bfd90409 1#!/usr/bin/env perl
2#
3# lesson05.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
31use strict;
32use Getopt::Long;
33use SDL;
34use SDL::App;
35use SDL::OpenGL;
36use SDL::Event;
37use SDL::Cursor;
38
39my $arg_screen_width =640;
40my $arg_screen_height=512;
41my $arg_fullscreen=0;
42my $delay = 30;
43
44GetOptions(
45 "width:i" => \$arg_screen_width,
46 "height:i" => \$arg_screen_height,
47 "fullscreen!" => \$arg_fullscreen,
48 "delay:i" => \$delay,
49 ) or die $!;
50
51main();
52exit;
53
54
55sub main
56 {
57 my $done=0;
58
59 my $app = new SDL::App ( -title => "Jeff Molofee's GL Code Tutorial ... NeHe '99",
60 -icon => "icon.png",
61 -width => $arg_screen_width,
62 -height =>$arg_screen_height,
63 -opengl => 1,
64 );
65
66 $app->fullscreen if ($arg_fullscreen);
67
68 SDL::ShowCursor(0);
69
70 my $event = new SDL::Event;
71 $event->set(SDL_SYSWMEVENT(),SDL_IGNORE());#
72
73 InitGL($arg_screen_width, $arg_screen_height);
74
75 while ( ! $done ) {
76
77 DrawGLScene();
78
79 $app->sync();
80 $app->delay($delay);
81
82 $event->pump;
83 $event->poll;
84
85
86 if ( $event->type == SDL_QUIT() ) {
87 $done = 1;
88 }
89
90 if ( $event->type == SDL_KEYDOWN() ) {
91 if ( $event->key_sym == SDLK_ESCAPE() ) {
92 $done = 1;
93 }
94 }
95 }
96 }
97
98
99
100
101
102
103#########################################################################
104#Pretty much in original form, but 'Perlised'
105
106
107
108#/* rotation angle for the triangle. */
109my $rtri = 0.0;
110
111#/* rotation angle for the quadrilateral. */
112my $rquad = 0.0;
113
114
115sub InitGL
116 {
117 my ($Width, $Height)=@_;
118
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
125
126 glMatrixMode(GL_PROJECTION);
127 glLoadIdentity(); # Reset The Projection Matrix
128
129 gluPerspective(45.0, $Width/$Height, 0.1, 100.0); # Calculate The Aspect Ratio Of The Window
130
131 glMatrixMode(GL_MODELVIEW);
132 }
133
134
135
136# The main drawing function.
137sub DrawGLScene
138 {
139 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); # Clear The Screen And The Depth Buffer
140 glLoadIdentity(); # Reset The View
141
142 glTranslate(-1.5,0.0,-6.0); # Move Left 1.5 Units And Into The Screen 6.0
143
144 glRotate($rtri,0.0,1.0,0.0); # Rotate The Pyramid On The Y axis
145
146 # draw a pyramid (in smooth coloring mode)
147 glBegin(GL_POLYGON); # start drawing a pyramid
148
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)
156
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)
164
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)
172
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)
180
181 glEnd(); # Done Drawing The Pyramid
182
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
185
186 glRotate($rquad,1.0,1.0,1.0); # Rotate The Cube On X, Y, and Z
187
188 # draw a cube (6 quadrilaterals)
189 glBegin(GL_QUADS); # start drawing the cube.
190
191 # top of 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)
197
198 # bottom of cube
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)
204
205 # front of cube
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)
211
212 # back of cube.
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)
218
219 # left of cube
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)
225
226 # Right of cube
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
233
234 $rtri+=15.0; # Increase The Rotation Variable For The Triangle
235 $rquad-=15.0; # Decrease The Rotation Variable For The Quad
236
237
238 }
239
240
241
242