Commit | Line | Data |
bfd90409 |
1 | #!/usr/bin/env perl |
2 | # |
3 | # lesson04.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 | my $delay=30; |
42 | |
43 | GetOptions( |
44 | "width:i" => \$arg_screen_width, |
45 | "height:i" => \$arg_screen_height, |
46 | "fullscreen!" => \$arg_fullscreen, |
47 | "delay:i" => \$delay, |
48 | ) or die <<USAGE; |
49 | Usage $0 -w 800 -h 600 -d 10 --fullscreen |
50 | USAGE |
51 | |
52 | #/* rotation angle for the triangle. */ |
53 | my $rtri = 0.0; |
54 | |
55 | #/* rotation angle for the quadrilateral. */ |
56 | my $rquad = 0.0; |
57 | |
58 | |
59 | main(); |
60 | exit; |
61 | |
62 | |
63 | sub main |
64 | { |
65 | my $done=0; |
66 | my $app = new SDL::App ( -title => "Jeff Molofee's GL Code Tutorial ... NeHe '99", |
67 | -icon => "icon.png", |
68 | -width => $arg_screen_width, |
69 | -height =>$arg_screen_height, |
70 | -gl => 1, |
71 | ); |
72 | $app->fullscreen() if $arg_fullscreen; |
73 | |
74 | SDL::ShowCursor(0); |
75 | |
76 | my $event = new SDL::Event; |
77 | $event->set(SDL_SYSWMEVENT,SDL_IGNORE);# |
78 | |
79 | |
80 | |
81 | InitGL($arg_screen_width, $arg_screen_height); |
82 | |
83 | while ( ! $done ) { |
84 | |
85 | DrawGLScene(); |
86 | |
87 | $app->sync(); |
88 | $app->delay($delay); |
89 | |
90 | $event->pump; |
91 | $event->poll; |
92 | |
93 | |
94 | if ( $event->type == SDL_QUIT ) { |
95 | $done = 1; |
96 | } |
97 | |
98 | if ( $event->type == SDL_KEYDOWN ) { |
99 | if ( $event->key_sym == SDLK_ESCAPE ) { |
100 | $done = 1; |
101 | } |
102 | } |
103 | } |
104 | } |
105 | |
106 | |
107 | |
108 | |
109 | |
110 | |
111 | ######################################################################### |
112 | #Pretty much in original form, but 'Perlised' |
113 | |
114 | |
115 | |
116 | |
117 | |
118 | sub InitGL |
119 | { |
120 | my ($Width, $Height)=@_; |
121 | |
122 | glViewport(0, 0, $Width, $Height); |
123 | glClearColor(0.0, 0.0, 0.0, 0.0); # This Will Clear The Background Color To Black |
124 | glClearDepth(1.0); # Enables Clearing Of The Depth Buffer |
125 | glDepthFunc(GL_LESS); # The Type Of Depth Test To Do |
126 | glEnable(GL_DEPTH_TEST); # Enables Depth Testing |
127 | glShadeModel(GL_SMOOTH); # Enables Smooth Color Shading |
128 | |
129 | glMatrixMode(GL_PROJECTION); |
130 | glLoadIdentity(); # Reset The Projection Matrix |
131 | |
132 | gluPerspective(45.0, $Width/$Height, 0.1, 100.0); # Calculate The Aspect Ratio Of The Window |
133 | |
134 | glMatrixMode(GL_MODELVIEW); |
135 | } |
136 | |
137 | |
138 | |
139 | # The main drawing function. |
140 | sub DrawGLScene |
141 | { |
142 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); # Clear The Screen And The Depth Buffer |
143 | glLoadIdentity(); # Reset The View |
144 | |
145 | glTranslate(-1.5,0.0,-6.0); # Move Left 1.5 Units And Into The Screen 6.0 |
146 | |
147 | glRotate($rtri,0.0,1.0,0.0); # Rotate The Triangle On The Y axis |
148 | # draw a triangle (in smooth coloring mode) |
149 | glBegin(GL_POLYGON); # start drawing a polygon |
150 | glColor(1.0,0.0,0.0); # Set The Color To Red |
151 | glVertex( 0.0, 1.0, 0.0); # Top |
152 | glColor(0.0,1.0,0.0); # Set The Color To Green |
153 | glVertex( 1.0,-1.0, 0.0); # Bottom Right |
154 | glColor(0.0,0.0,1.0); # Set The Color To Blue |
155 | glVertex(-1.0,-1.0, 0.0); # Bottom Left |
156 | glEnd(); # we're done with the polygon (smooth color interpolation) |
157 | |
158 | glLoadIdentity(); # make sure we're no longer rotated. |
159 | glTranslate(1.5,0.0,-6.0); # Move Right 3 Units, and back into the screen 6.0 |
160 | |
161 | glRotate($rquad,1.0,0.0,0.0); # Rotate The Quad On The X axis |
162 | # draw a square (quadrilateral) |
163 | glColor(0.5,0.5,1.0); # set color to a blue shade. |
164 | glBegin(GL_QUADS); # start drawing a polygon (4 sided) |
165 | glVertex(-1.0, 1.0, 0.0); # Top Left |
166 | glVertex( 1.0, 1.0, 0.0); # Top Right |
167 | glVertex( 1.0,-1.0, 0.0); # Bottom Right |
168 | glVertex(-1.0,-1.0, 0.0); # Bottom Left |
169 | glEnd(); # done with the polygon |
170 | |
171 | $rtri+=15.0; # Increase The Rotation Variable For The Triangle |
172 | $rquad-=15.0; # Decrease The Rotation Variable For The Quad |
173 | |
174 | |
175 | } |
176 | |
177 | |
178 | |
179 | |