PageRenderTime 39ms CodeModel.GetById 14ms app.highlight 22ms RepoModel.GetById 1ms app.codeStats 0ms

/t_backcompat/OpenGL/test5.pl

http://github.com/PerlGameDev/SDL
Perl | 139 lines | 118 code | 17 blank | 4 comment | 22 complexity | a2b1697cdcdc24d5cd71756f01b3a71b MD5 | raw file
  1#!/usr/bin/env perl
  2#
  3# Bezier Surface example
  4#
  5
  6use SDL;
  7use SDLx::App;
  8use SDL::Event;
  9use SDL::OpenGL;
 10
 11my $app = SDLx::App->new( -w => 800, -h => 600, -d => 16, -gl => 1 );
 12
 13my $knots      = pack "f8",  0,    0,   0,    0,    1,    1,    1,    1;
 14my $edgePts    = pack "f10", 0,    0,   1,    0,    1,    1,    0,    1, 0, 0;
 15my $curvePts   = pack "f8",  0.25, 0.5, 0.25, 0.75, 0.75, 0.75, 0.75, 0.5;
 16my $curveKnots = pack "f8",  0,    0,   0,    0,    1,    1,    1,    1;
 17my $pwlPts     = pack "f8",  0.75, 0.5, 0.5,  0.25, 0.25, 0.5,  0,    0;
 18
 19sub init {
 20	glViewport( 0, 0, 800, 600 );
 21	glMatrixMode( GL_PROJECTION() );
 22	glLoadIdentity();
 23	glFrustum( -0.1, 0.1, -0.075, 0.075, 0.3, 100.0 );
 24	glMatrixMode( GL_MODELVIEW() );
 25	glLoadIdentity();
 26	glTranslate( 0, 0, -15 );
 27	glClearColor( 0.0, 0.0, 0.0, 0.0 );
 28	glShadeModel( GL_SMOOTH() );
 29}
 30
 31sub initlight {
 32	glEnable( GL_LIGHTING() );
 33	glEnable( GL_LIGHT0() );
 34	glEnable( GL_DEPTH_TEST() );
 35	glEnable( GL_AUTO_NORMAL() );
 36	glEnable( GL_NORMALIZE() );
 37	glLight( GL_LIGHT0(), GL_AMBIENT(),  0.3, 0.3, 0.3, 1.0 );
 38	glLight( GL_LIGHT0(), GL_POSITION(), 1.0, 0.0, 2.0, 1.0 );
 39	glMaterial( GL_FRONT(), GL_DIFFUSE(),  0.6, 0.6, 0.6, 1.0 );
 40	glMaterial( GL_FRONT(), GL_SPECULAR(), 1.0, 1.0, 1.0, 1.0 );
 41	glMaterial( GL_FRONT(), GL_SHININESS(), 40.0 );
 42}
 43
 44my ( $a, $b ) = ( 0, 90 );
 45
 46my $ctrldata;
 47
 48sub initpts {
 49	my @points;
 50	for my $u ( 0 .. 3 ) {
 51		for my $v ( 0 .. 3 ) {
 52			push @points, 2.0 * ( $u - 1.5 );
 53			push @points, 2.0 * ( $v - 1.5 );
 54			if ( ( $u == 1 || $u == 2 ) && ( $v == 1 || $v == 2 ) ) {
 55				push @points, 3.0;
 56			} else {
 57				push @points, -3.0;
 58			}
 59		}
 60	}
 61	$ctrldata = pack "f48", @points;
 62}
 63
 64sub display {
 65	glClear( GL_COLOR_BUFFER_BIT() | GL_DEPTH_BUFFER_BIT() );
 66	glPushMatrix();
 67	glRotate( $a % 360, 0,  1, 0 );
 68	glRotate( $b % 360, -1, 0, 0 );
 69	glScale( 0.5, 0.5, 0.5 );
 70	$nurb = gluNewNurbsRenderer();
 71	gluNurbsProperty( $nurb, GLU_CULLING, GL_TRUE );
 72	gluBeginSurface($nurb);
 73	gluNurbsSurface(
 74		$nurb, 8, $knots, 8, $knots, 4 * 3, 3, $ctrldata, 4, 4,
 75		GL_MAP2_VERTEX_3
 76	);
 77
 78	if ($toggle) {
 79		gluBeginTrim($nurb);
 80		gluPwlCurve( $nurb, 5, $edgePts, 2, GLU_MAP1_TRIM_2 );
 81		gluEndTrim($nurb);
 82		gluBeginTrim($nurb);
 83		gluNurbsCurve(
 84			$nurb, 8, $curveKnots, 2, $curvePts, 4,
 85			GLU_MAP1_TRIM_2
 86		);
 87		gluPwlCurve( $nurb, 3, $pwlPts, 2, GLU_MAP1_TRIM_2 );
 88		gluEndTrim($nurb);
 89	}
 90
 91	gluEndSurface($nurb);
 92
 93	glPopMatrix();
 94	$app->sync();
 95}
 96
 97init();
 98initlight();
 99initpts();
100display();
101
102print STDERR <<USAGE;
103Press:
104q			Quit
105t 			Toggle Curve & Trim
106f			Toggle Fullscreen
107Up/Down/Left/Right	Rotate
108
109USAGE
110
111my $event = SDL::Event->new;
112$app->loop(
113	{   SDL_QUIT()    => sub { exit(); },
114		SDL_KEYDOWN() => sub {
115			my ($event) = @_;
116			if ( $event->key_sym() == SDLK_f ) {
117				$app->fullscreen();
118				display();
119			} elsif ( $event->key_sym() == SDLK_t ) {
120				$toggle = $toggle ? 0 : 1;
121				display();
122			} elsif ( $event->key_sym() == SDLK_q ) {
123				exit();
124			} else {
125				if ( $event->key_sym() == SDLK_LEFT() ) {
126					$a -= 10;
127				} elsif ( $event->key_sym() == SDLK_RIGHT() ) {
128					$a += 10;
129				} elsif ( $event->key_sym() == SDLK_UP() ) {
130					$b += 10;
131				} elsif ( $event->key_sym() == SDLK_DOWN() ) {
132					$b -= 10;
133				}
134				display();
135			}
136		},
137	}
138);
139