File: test5.pl

package info (click to toggle)
sdlperl 1.20.3dfsg-3
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 1,808 kB
  • ctags: 2,171
  • sloc: perl: 7,394; ansic: 232; makefile: 76; sh: 1
file content (132 lines) | stat: -rwxr-xr-x 3,005 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#!/usr/bin/env perl
#
# Bezier Surface example 
#

use SDL;
use SDL::App;
use SDL::Event;
use SDL::OpenGL;

my $app = new SDL::App	-w => 800, -h => 600, -d => 16, -gl => 1;

my $knots = pack "f8", 0,0,0,0,1,1,1,1;
my $edgePts = pack "f10", 0,0,1,0,1,1,0,1,0,0;
my $curvePts = pack "f8", 0.25,0.5,0.25,0.75,0.75,0.75,0.75,0.5;
my $curveKnots = pack "f8", 0,0,0,0,1,1,1,1;
my $pwlPts = pack "f8", 0.75, 0.5, 0.5, 0.25, 0.25, 0.5, 0, 0;

sub init {
	glViewport(0,0,800,600);
	glMatrixMode(GL_PROJECTION());
	glLoadIdentity();
	glFrustum (-0.1,0.1,-0.075,0.075,0.3,100.0 );
	glMatrixMode(GL_MODELVIEW());
	glLoadIdentity();
	glTranslate(0,0,-15);
	glClearColor(0.0, 0.0, 0.0, 0.0);	
	glShadeModel(GL_SMOOTH());
}

sub initlight {
	glEnable(GL_LIGHTING());
	glEnable(GL_LIGHT0());
	glEnable(GL_DEPTH_TEST());
	glEnable(GL_AUTO_NORMAL());
	glEnable(GL_NORMALIZE());
	glLight(GL_LIGHT0(),GL_AMBIENT(),0.3,0.3,0.3,1.0);
	glLight(GL_LIGHT0(),GL_POSITION(), 1.0,0.0,2.0,1.0);
	glMaterial(GL_FRONT(), GL_DIFFUSE(),0.6,0.6,0.6,1.0);
	glMaterial(GL_FRONT(), GL_SPECULAR(), 1.0, 1.0, 1.0, 1.0);
	glMaterial(GL_FRONT(), GL_SHININESS(), 40.0);
}

my ($a,$b) = (0,90);

my $ctrldata;
sub initpts {
	my @points;
	for my $u ( 0 .. 3 ) {
		for my $v ( 0 .. 3 ) {
			push @points, 2.0 * ($u - 1.5);
			push @points, 2.0 * ($v - 1.5);
			if (( $u == 1 || $u == 2 ) && ( $v == 1 || $v == 2 )) {
				push @points,  3.0;
			} else {
				push @points, -3.0;
			}
		}
	}
	$ctrldata = pack "f48", @points;
}

sub display {
	glClear(GL_COLOR_BUFFER_BIT() | GL_DEPTH_BUFFER_BIT());
	glPushMatrix();
	glRotate($a%360,0,1,0);
	glRotate($b%360,-1,0,0);
	glScale(0.5,0.5,0.5);
	$nurb = gluNewNurbsRenderer();
	gluNurbsProperty($nurb,GLU_CULLING,GL_TRUE);
	gluBeginSurface($nurb);
		gluNurbsSurface($nurb, 8, $knots, 8, $knots, 
			4*3, 3, $ctrldata, 
			4, 4, GL_MAP2_VERTEX_3);
	if ($toggle) {
		gluBeginTrim($nurb);
			gluPwlCurve($nurb,5,$edgePts,2,GLU_MAP1_TRIM_2);
		gluEndTrim($nurb);
		gluBeginTrim($nurb);
			gluNurbsCurve($nurb,8,$curveKnots, 2, $curvePts, 4, GLU_MAP1_TRIM_2);
			gluPwlCurve($nurb,3,$pwlPts,2,GLU_MAP1_TRIM_2);
		gluEndTrim($nurb);
	}
		
	gluEndSurface($nurb);

	glPopMatrix();
	$app->sync();
}

init();
initlight();
initpts();
display();

print STDERR <<USAGE;
Press:
q			Quit
t 			Toggle Curve & Trim
f			Toggle Fullscreen
Up/Down/Left/Right	Rotate

USAGE

my $event = new SDL::Event;
$app->loop ({
		SDL_QUIT() => sub { exit(); }, 
		SDL_KEYDOWN() => sub { 
			my ($event) = @_;
			if ( $event->key_sym() == SDLK_f ) {
				$app->fullscreen();
				display(); 
			} elsif ( $event->key_sym() == SDLK_t ) {
				$toggle = $toggle ? 0 : 1;
				display();
			} elsif ( $event->key_sym() == SDLK_q ) {
				exit();
			} else {
				if ($event->key_sym() == SDLK_LEFT()) {
					$a -= 10; 
				} elsif ($event->key_sym() == SDLK_RIGHT()) {
					$a += 10; 
				} elsif ($event->key_sym() == SDLK_UP()) {
					$b += 10;
				} elsif ($event->key_sym() == SDLK_DOWN()) {
					$b -= 10;	
				}
				display(); 
			}
		},
});