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