Importing SDLPerl 2.2
[sdlgit/SDL_perl.git] / test / OpenGL / test4.sdlpl
1 #!/usr/bin/env perl
2 #
3 # test4.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 SDL;
33 use SDL::App;
34 use SDL::Event;
35 use SDL::OpenGL;
36
37 our $toggle = 0;
38 my $app = new SDL::App  -w => 800, -h => 600, -d => 16, -gl => 1;
39
40 my @points = (  [-1.5, -1.5,  4.0 ], [-0.5, -1.5,  2.0 ],
41                 [-0.5, -1.5, -1.0 ], [ 1.5, -1.5,  2.0 ],
42                 [-1.5, -0.5,  1.0 ], [-0.5, -0.5,  3.0 ],
43                 [ 0.5, -0.5,  0.0 ], [ 1.5, -0.5, -1.0 ], 
44                 [-1.5,  0.5,  4.0 ], [-0.5,  0.5,  0.0 ],
45                 [ 0.5,  0.5,  3.0 ], [ 1.5,  0.5,  4.0 ],
46                 [-1.5,  1.5, -2.0 ], [-0.5,  1.5, -2.0 ],
47                 [ 0.5,  1.5,  0.0 ], [ 1.5,  1.5, -1.0 ],
48         );
49
50 my $ctrlpoints = pack "d48", map { @$_ } @points;
51
52 sub init {
53         
54         glViewport(0,0,800,600);
55         glMatrixMode(GL_PROJECTION());
56         glLoadIdentity();
57
58         glFrustum (-0.1,0.1,-0.075,0.075,0.3,100.0 );
59         
60         glMatrixMode(GL_MODELVIEW());
61         glLoadIdentity();
62         
63         glTranslate(0,0,-15);
64
65         glClearColor(0.0, 0.0, 0.0, 0.0);       
66         glMap2(GL_MAP2_VERTEX_3(), 0, 1, 3, 4, 0, 1, 12, 4, $ctrlpoints);
67         glEnable(GL_MAP2_VERTEX_3());
68         glMapGrid2(20,0,1,20,0,1);
69         glEnable(GL_DEPTH_TEST);
70         glShadeModel(GL_SMOOTH());
71 }
72
73 sub initlight {
74
75         glEnable(GL_LIGHTING());
76         glEnable(GL_LIGHT0());
77
78         glLight(GL_LIGHT0(),GL_AMBIENT(),0.2,0.2,0.2,1.0);
79         glLight(GL_LIGHT0(),GL_POSITION(), 0.0,0.0,2.0,1.0);
80
81         glMaterial(GL_FRONT(), GL_DIFFUSE(),0.6,0.6,0.6,1.0);
82         glMaterial(GL_FRONT(), GL_SPECULAR(), 1.0, 1.0, 1.0, 1.0);
83         glMaterial(GL_FRONT(), GL_SHININESS(), 50.0);
84
85 }
86
87 my ($a1,$a2) = (89,305);
88
89 sub display {
90         glClear(GL_COLOR_BUFFER_BIT() | GL_DEPTH_BUFFER_BIT());
91         glColor(1.0,1.0,1.0);
92         glPushMatrix();
93         glRotate($a1 % 360, 0.0, 1.0, 1.0);
94         glRotate($a2 % 360, 1.0, 1.0, 0.0);
95         if ($toggle) {
96                 glEvalMesh2(GL_FILL,0,20,0,20);
97         } else {
98                 glBegin(GL_LINE_STRIP);
99                 for my $j ( 0 .. 8 ) {
100                         for my $i ( 0 .. 30 ) {
101                                 glEvalCoord2($i/30,$j/8);
102                         }
103                         for my $i ( 0 .. 30 ) {
104                                 glEvalCoord2($j/8,$i/30);
105                         }
106                 }
107                 glEnd();
108         }
109         glPopMatrix();
110         $app->sync();
111 }
112
113 print STDERR <<USAGE;
114 $0
115         Press:  t       Toggle wireframe / solid
116                 f       Toggle fullscreen
117                 q       Quit
118                 any     Rotate Bezier Surface
119 USAGE
120
121 init();
122 initlight();
123 display();
124
125 my $event = new SDL::Event;
126 $app->loop ({
127                 SDL_QUIT() => sub { exit(); }, 
128                 SDL_KEYDOWN() => sub { 
129                         my ($event) = @_;
130                         if ( $event->key_sym() == SDLK_f ) {
131                                 $app->fullscreen();
132                                 display(); 
133                         } elsif ( $event->key_sym() == SDLK_t ) {
134                                 $toggle = $toggle ? 0 : 1;
135                                 display();
136                         } elsif ( $event->key_sym() == SDLK_q ) {
137                                 exit();
138                         } else {
139                                 $a1 += 33; $a2 += 5;  
140                                 display(); 
141                         }
142                 },
143 });
144