Added SDL_Pallete Capability
[sdlgit/SDL_perl.git] / test / OpenGL / tutorial / lesson03.pl
CommitLineData
8fde61e3 1#!/usr/bin/perl -w
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)
5#
6# If you've found this code useful, please let me know.
7#
8# Visit me at www.demonews.com/hosted/nehe
9
10use strict;
11use Getopt::Long;
12
13use SDL;
14use SDL::App;
15use SDL::OpenGL;
16use SDL::Event;
17
18my $arg_screen_width =640;
19my $arg_screen_height=512;
20my $arg_fullscreen=0;
21
22GetOptions(
23 "width:i" => \$arg_screen_width,
24 "height:i" => \$arg_screen_height,
25 "fullscreen!" => \$arg_fullscreen,
26 ) or die $!;
27
28main();
29exit;
30
31
32sub main
33 {
34 my $done=0;
35
36 my $app = new SDL::App ( -title => "Jeff Molofee's GL Code Tutorial ... NeHe '99",
37 -icon => "icon.png",
38 -width => $arg_screen_width,
39 -height =>$arg_screen_height,
40 -d => 16,
41 -opengl => 1,
42 );
43 $app->fullscreen() if $arg_fullscreen;
44
45 SDL::ShowCursor(0);
46
47 my $event = new SDL::Event;
48 $event->set(SDL_SYSWMEVENT,SDL_IGNORE);#
49
50
51
52 InitGL($arg_screen_width, $arg_screen_height);
53
54 while ( ! $done ) {
55
56 DrawGLScene();
57
58 $app->sync();
59
60 $event->pump;
61 $event->poll;
62
63
64 if ( $event->type == SDL_QUIT ) {
65 $done = 1;
66 }
67
68 if ( $event->type == SDL_KEYDOWN ) {
69 if ( $event->key_sym == SDLK_ESCAPE ) {
70 $done = 1;
71 }
72 }
73 }
74 }
75
76
77
78
79
80
81#########################################################################
82#Pretty much in original form, but 'Perlised'
83
84
85sub InitGL
86 {
87 my ($Width, $Height)=@_;
88
89 glViewport(0, 0, $Width, $Height);
90 glClearColor(0.0, 0.0, 0.0, 0.0); # This Will Clear The Background Color To Black
91 glClearDepth(1.0); # Enables Clearing Of The Depth Buffer
92 glDepthFunc(GL_LESS); # The Type Of Depth Test To Do
93 glEnable(GL_DEPTH_TEST); # Enables Depth Testing
94 glShadeModel(GL_SMOOTH); # Enables Smooth Color Shading
95
96 glMatrixMode(GL_PROJECTION);
97 glLoadIdentity(); # Reset The Projection Matrix
98
99 gluPerspective(45.0, $Width/$Height, 0.1, 100.0); # Calculate The Aspect Ratio Of The Window
100
101 glMatrixMode(GL_MODELVIEW);
102 }
103
104
105
106# The main drawing function.
107sub DrawGLScene
108 {
109 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); # Clear The Screen And The Depth Buffer
110 glLoadIdentity(); # Reset The View
111
112 glTranslate(-1.5,0.0,-6.0); # Move Left 1.5 Units And Into The Screen 6.0
113
114 # draw a triangle (in smooth coloring mode)
115 glBegin(GL_POLYGON); # start drawing a polygon
116 glColor(1.0,0.0,0.0); # Set The Color To Red
117 glVertex( 0.0, 1.0, 0.0); # Top
118 glColor(0.0,1.0,0.0); # Set The Color To Green
119 glVertex( 1.0,-1.0, 0.0); # Bottom Right
120 glColor(0.0,0.0,1.0); # Set The Color To Blue
121 glVertex(-1.0,-1.0, 0.0); # Bottom Left
122 glEnd(); # we're done with the polygon (smooth color interpolation)
123
124 glTranslate(3.0,0.0,0.0); # Move Right 3 Units
125
126 # draw a square (quadrilateral)
127 glColor(0.5,0.5,1.0); # set color to a blue shade.
128 glBegin(GL_QUADS); # start drawing a polygon (4 sided)
129 glVertex(-1.0, 1.0, 0.0); # Top Left
130 glVertex( 1.0, 1.0, 0.0); # Top Right
131 glVertex( 1.0,-1.0, 0.0); # Bottom Right
132 glVertex(-1.0,-1.0, 0.0); # Bottom Left
133 glEnd(); # done with the polygon
134
135
136
137
138 }
139
140
141
142