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