First commit of SDL_Perl-2.1.3
[sdlgit/SDL_perl.git] / test / OpenGL / test4.pl
1 #!/usr/bin/env perl
2 #
3 # Bezier Surface example 
4 #
5
6 use SDL;
7 use SDL::App;
8 use SDL::Event;
9 use SDL::OpenGL;
10
11 my $app = new SDL::App  -w => 800, -h => 600, -d => 16, -gl => 1;
12
13 my @points = (  [-1.5, -1.5,  4.0 ], [-0.5, -1.5,  2.0 ],
14                 [-0.5, -1.5, -1.0 ], [ 1.5, -1.5,  2.0 ],
15                 [-1.5, -0.5,  1.0 ], [-0.5, -0.5,  3.0 ],
16                 [ 0.5, -0.5,  0.0 ], [ 1.5, -0.5, -1.0 ], 
17                 [-1.5,  0.5,  4.0 ], [-0.5,  0.5,  0.0 ],
18                 [ 0.5,  0.5,  3.0 ], [ 1.5,  0.5,  4.0 ],
19                 [-1.5,  1.5, -2.0 ], [-0.5,  1.5, -2.0 ],
20                 [ 0.5,  1.5,  0.0 ], [ 1.5,  1.5, -1.0 ],
21         );
22
23 my $ctrlpoints = pack "d48", map { @$_ } @points;
24
25 sub init {
26         
27         glViewport(0,0,800,600);
28         glMatrixMode(GL_PROJECTION());
29         glLoadIdentity();
30
31         glFrustum (-0.1,0.1,-0.075,0.075,0.3,100.0 );
32         
33         glMatrixMode(GL_MODELVIEW());
34         glLoadIdentity();
35         
36         glTranslate(0,0,-15);
37
38         glClearColor(0.0, 0.0, 0.0, 0.0);       
39         glMap2(GL_MAP2_VERTEX_3(), 0, 1, 3, 4, 0, 1, 12, 4, $ctrlpoints);
40         glEnable(GL_MAP2_VERTEX_3());
41         glMapGrid2(20,0,1,20,0,1);
42         glEnable(GL_DEPTH_TEST);
43         glShadeModel(GL_SMOOTH());
44 }
45
46 sub initlight {
47
48         glEnable(GL_LIGHTING());
49         glEnable(GL_LIGHT0());
50
51         glLight(GL_LIGHT0(),GL_AMBIENT(),0.2,0.2,0.2,1.0);
52         glLight(GL_LIGHT0(),GL_POSITION(), 0.0,0.0,2.0,1.0);
53
54         glMaterial(GL_FRONT(), GL_DIFFUSE(),0.6,0.6,0.6,1.0);
55         glMaterial(GL_FRONT(), GL_SPECULAR(), 1.0, 1.0, 1.0, 1.0);
56         glMaterial(GL_FRONT(), GL_SHININESS(), 50.0);
57
58 }
59
60 my ($a1,$a2) = (89,305);
61
62 sub display {
63         glClear(GL_COLOR_BUFFER_BIT() | GL_DEPTH_BUFFER_BIT());
64         glColor(1.0,1.0,1.0);
65         glPushMatrix();
66         glRotate($a1 % 360, 0.0, 1.0, 1.0);
67         glRotate($a2 % 360, 1.0, 1.0, 0.0);
68         if ($toggle) {
69                 glEvalMesh2(GL_FILL,0,20,0,20);
70         } else {
71                 glBegin(GL_LINE_STRIP);
72                 for my $j ( 0 .. 8 ) {
73                         for my $i ( 0 .. 30 ) {
74                                 glEvalCoord2($i/30,$j/8);
75                         }
76                         for my $i ( 0 .. 30 ) {
77                                 glEvalCoord2($j/8,$i/30);
78                         }
79                 }
80                 glEnd();
81         }
82         glPopMatrix();
83         $app->sync();
84 }
85
86 print STDERR <<USAGE;
87 $0
88         Press:  t       Toggle wireframe / solid
89                 f       Toggle fullscreen
90                 q       Quit
91                 any     Rotate Bezier Surface
92 USAGE
93
94 init();
95 initlight();
96 display();
97
98 my $event = new SDL::Event;
99 $app->loop ({
100                 SDL_QUIT() => sub { exit(); }, 
101                 SDL_KEYDOWN() => sub { 
102                         my ($event) = @_;
103                         if ( $event->key_sym() == SDLK_f ) {
104                                 $app->fullscreen();
105                                 display(); 
106                         } elsif ( $event->key_sym() == SDLK_t ) {
107                                 $toggle = $toggle ? 0 : 1;
108                                 display();
109                         } elsif ( $event->key_sym() == SDLK_q ) {
110                                 exit();
111                         } else {
112                                 $a1 += 33; $a2 += 5;  
113                                 display(); 
114                         }
115                 },
116 });
117