Importing SDLPerl 2.2
[sdlgit/SDL_perl.git] / test / OpenGL / test5.sdlpl
1 #!/usr/bin/env perl
2 #
3 # test5.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 my $app = new SDL::App  -w => 800, -h => 600, -d => 16, -gl => 1;
38 my $toggle;
39
40 my $knots = pack "f8", 0,0,0,0,1,1,1,1;
41 my $edgePts = pack "f10", 0,0,1,0,1,1,0,1,0,0;
42 my $curvePts = pack "f8", 0.25,0.5,0.25,0.75,0.75,0.75,0.75,0.5;
43 my $curveKnots = pack "f8", 0,0,0,0,1,1,1,1;
44 my $pwlPts = pack "f8", 0.75, 0.5, 0.5, 0.25, 0.25, 0.5, 0, 0;
45
46 sub init {
47         glViewport(0,0,800,600);
48         glMatrixMode(GL_PROJECTION());
49         glLoadIdentity();
50         glFrustum (-0.1,0.1,-0.075,0.075,0.3,100.0 );
51         glMatrixMode(GL_MODELVIEW());
52         glLoadIdentity();
53         glTranslate(0,0,-15);
54         glClearColor(0.0, 0.0, 0.0, 0.0);       
55         glShadeModel(GL_SMOOTH());
56 }
57
58 sub initlight {
59         glEnable(GL_LIGHTING());
60         glEnable(GL_LIGHT0());
61         glEnable(GL_DEPTH_TEST());
62         glEnable(GL_AUTO_NORMAL());
63         glEnable(GL_NORMALIZE());
64         glLight(GL_LIGHT0(),GL_AMBIENT(),0.3,0.3,0.3,1.0);
65         glLight(GL_LIGHT0(),GL_POSITION(), 1.0,0.0,2.0,1.0);
66         glMaterial(GL_FRONT(), GL_DIFFUSE(),0.6,0.6,0.6,1.0);
67         glMaterial(GL_FRONT(), GL_SPECULAR(), 1.0, 1.0, 1.0, 1.0);
68         glMaterial(GL_FRONT(), GL_SHININESS(), 40.0);
69 }
70
71 my ($a,$b) = (0,90);
72
73 my $ctrldata;
74 sub initpts {
75         my @points;
76         for my $u ( 0 .. 3 ) {
77                 for my $v ( 0 .. 3 ) {
78                         push @points, 2.0 * ($u - 1.5);
79                         push @points, 2.0 * ($v - 1.5);
80                         if (( $u == 1 || $u == 2 ) && ( $v == 1 || $v == 2 )) {
81                                 push @points,  3.0;
82                         } else {
83                                 push @points, -3.0;
84                         }
85                 }
86         }
87         $ctrldata = pack "f48", @points;
88 }
89
90 sub display {
91         glClear(GL_COLOR_BUFFER_BIT() | GL_DEPTH_BUFFER_BIT());
92         glPushMatrix();
93         glRotate($a%360,0,1,0);
94         glRotate($b%360,-1,0,0);
95         glScale(0.5,0.5,0.5);
96         my $nurb = gluNewNurbsRenderer();
97         gluNurbsProperty($nurb,GLU_CULLING,GL_TRUE);
98         gluBeginSurface($nurb);
99                 gluNurbsSurface($nurb, 8, $knots, 8, $knots, 
100                         4*3, 3, $ctrldata, 
101                         4, 4, GL_MAP2_VERTEX_3);
102         if ($toggle) {
103                 gluBeginTrim($nurb);
104                         gluPwlCurve($nurb,5,$edgePts,2,GLU_MAP1_TRIM_2);
105                 gluEndTrim($nurb);
106                 gluBeginTrim($nurb);
107                         gluNurbsCurve($nurb,8,$curveKnots, 2, $curvePts, 4, GLU_MAP1_TRIM_2);
108                         gluPwlCurve($nurb,3,$pwlPts,2,GLU_MAP1_TRIM_2);
109                 gluEndTrim($nurb);
110         }
111                 
112         gluEndSurface($nurb);
113
114         glPopMatrix();
115         $app->sync();
116 }
117
118 init();
119 initlight();
120 initpts();
121 display();
122
123 print STDERR <<USAGE;
124 Press:
125 q                       Quit
126 t                       Toggle Curve & Trim
127 f                       Toggle Fullscreen
128 Up/Down/Left/Right      Rotate
129
130 USAGE
131
132 my $event = new SDL::Event;
133 $app->loop ({
134                 SDL_QUIT() => sub { exit(); }, 
135                 SDL_KEYDOWN() => sub { 
136                         my ($event) = @_;
137                         if ( $event->key_sym() == SDLK_f ) {
138                                 $app->fullscreen();
139                                 display(); 
140                         } elsif ( $event->key_sym() == SDLK_t ) {
141                                 $toggle = $toggle ? 0 : 1;
142                                 display();
143                         } elsif ( $event->key_sym() == SDLK_q ) {
144                                 exit();
145                         } else {
146                                 if ($event->key_sym() == SDLK_LEFT()) {
147                                         $a -= 10; 
148                                 } elsif ($event->key_sym() == SDLK_RIGHT()) {
149                                         $a += 10; 
150                                 } elsif ($event->key_sym() == SDLK_UP()) {
151                                         $b += 10;
152                                 } elsif ($event->key_sym() == SDLK_DOWN()) {
153                                         $b -= 10;       
154                                 }
155                                 display(); 
156                         }
157                 },
158 });
159