PageRenderTime 64ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/t_backcompat/OpenGL/tutorial/lesson03.pl

http://github.com/PerlGameDev/SDL
Perl | 122 lines | 80 code | 29 blank | 13 comment | 8 complexity | c7d3a54d0aca0cc756d3dcee37a93775 MD5 | raw file
Possible License(s): AGPL-1.0, GPL-2.0
  1. #!/usr/bin/perl -w
  2. # This code was created by Jeff Molofee '99
  3. # (ported to SDL by Sam Lantinga '2000)
  4. # (ported to Perl/SDL by Wayne Keenan '2000)
  5. #
  6. # If you've found this code useful, please let me know.
  7. #
  8. # Visit me at www.demonews.com/hosted/nehe
  9. use strict;
  10. use Getopt::Long;
  11. use SDL;
  12. use SDLx::App;
  13. use SDL::OpenGL;
  14. use SDL::Event;
  15. my $arg_screen_width = 640;
  16. my $arg_screen_height = 512;
  17. my $arg_fullscreen = 0;
  18. GetOptions(
  19. "width:i" => \$arg_screen_width,
  20. "height:i" => \$arg_screen_height,
  21. "fullscreen!" => \$arg_fullscreen,
  22. ) or die $!;
  23. main();
  24. exit;
  25. sub main {
  26. my $done = 0;
  27. my $app = SDLx::App->new(
  28. -title => "Jeff Molofee's GL Code Tutorial ... NeHe '99",
  29. -icon => "icon.png",
  30. -width => $arg_screen_width,
  31. -height => $arg_screen_height,
  32. -d => 16,
  33. -opengl => 1,
  34. );
  35. $app->fullscreen() if $arg_fullscreen;
  36. SDL::ShowCursor(0);
  37. my $event = SDL::Event->new;
  38. $event->set( SDL_SYSWMEVENT, SDL_IGNORE ); #
  39. InitGL( $arg_screen_width, $arg_screen_height );
  40. while ( !$done ) {
  41. DrawGLScene();
  42. $app->sync();
  43. $event->pump;
  44. $event->poll;
  45. if ( $event->type == SDL_QUIT ) {
  46. $done = 1;
  47. }
  48. if ( $event->type == SDL_KEYDOWN ) {
  49. if ( $event->key_sym == SDLK_ESCAPE ) {
  50. $done = 1;
  51. }
  52. }
  53. }
  54. }
  55. #########################################################################
  56. #Pretty much in original form, but 'Perlised'
  57. sub InitGL {
  58. my ( $Width, $Height ) = @_;
  59. glViewport( 0, 0, $Width, $Height );
  60. glClearColor( 0.0, 0.0, 0.0, 0.0 ); # This Will Clear The Background Color To Black
  61. glClearDepth(1.0); # Enables Clearing Of The Depth Buffer
  62. glDepthFunc(GL_LESS); # The Type Of Depth Test To Do
  63. glEnable(GL_DEPTH_TEST); # Enables Depth Testing
  64. glShadeModel(GL_SMOOTH); # Enables Smooth Color Shading
  65. glMatrixMode(GL_PROJECTION);
  66. glLoadIdentity(); # Reset The Projection Matrix
  67. gluPerspective( 45.0, $Width / $Height, 0.1, 100.0 ); # Calculate The Aspect Ratio Of The Window
  68. glMatrixMode(GL_MODELVIEW);
  69. }
  70. # The main drawing function.
  71. sub DrawGLScene {
  72. glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); # Clear The Screen And The Depth Buffer
  73. glLoadIdentity(); # Reset The View
  74. glTranslate( -1.5, 0.0, -6.0 ); # Move Left 1.5 Units And Into The Screen 6.0
  75. # draw a triangle (in smooth coloring mode)
  76. glBegin(GL_POLYGON); # start drawing a polygon
  77. glColor( 1.0, 0.0, 0.0 ); # Set The Color To Red
  78. glVertex( 0.0, 1.0, 0.0 ); # Top
  79. glColor( 0.0, 1.0, 0.0 ); # Set The Color To Green
  80. glVertex( 1.0, -1.0, 0.0 ); # Bottom Right
  81. glColor( 0.0, 0.0, 1.0 ); # Set The Color To Blue
  82. glVertex( -1.0, -1.0, 0.0 ); # Bottom Left
  83. glEnd(); # we're done with the polygon (smooth color interpolation)
  84. glTranslate( 3.0, 0.0, 0.0 ); # Move Right 3 Units
  85. # draw a square (quadrilateral)
  86. glColor( 0.5, 0.5, 1.0 ); # set color to a blue shade.
  87. glBegin(GL_QUADS); # start drawing a polygon (4 sided)
  88. glVertex( -1.0, 1.0, 0.0 ); # Top Left
  89. glVertex( 1.0, 1.0, 0.0 ); # Top Right
  90. glVertex( 1.0, -1.0, 0.0 ); # Bottom Right
  91. glVertex( -1.0, -1.0, 0.0 ); # Bottom Left
  92. glEnd(); # done with the polygon
  93. }