PageRenderTime 41ms CodeModel.GetById 20ms app.highlight 19ms RepoModel.GetById 1ms app.codeStats 0ms

/t_backcompat/OpenGL/test4.pl

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