freeglutdemo.pp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. { Trivial demo of some freeglut extensions, by Michalis Kamburelis,
  2. parts based on glutdemo.pp. Public domain.
  3. freeglut features:
  4. - when you press escape key, program returns gracefully to main begin...end.
  5. - we show special geometric objects: Sierpinski sponge, cylinder.
  6. - mouse wheel up/down can be used to zoom in/out.
  7. }
  8. {$mode objfpc}
  9. program FreeGlutDemo;
  10. uses
  11. GL, GLU, GLUT, FreeGlut;
  12. var
  13. T: GLFloat;
  14. Zoom: GLFloat = -3;
  15. procedure Display; cdecl;
  16. const
  17. Offset: TGLDouble3 = (0, 0, 0);
  18. begin
  19. glClear(GL_COLOR_BUFFER_BIT + GL_DEPTH_BUFFER_BIT);
  20. glPushMatrix;
  21. glTranslatef(-1, -1, Zoom);
  22. glRotatef(T, 0, 1, 0);
  23. glutWireCylinder(0.5, 1, 32, 8);
  24. glPopMatrix;
  25. glPushMatrix;
  26. glTranslatef(-1, 1, Zoom);
  27. glRotatef(T, 0, 1, 0);
  28. glutSolidCylinder(0.5, 1, 32, 8);
  29. glPopMatrix;
  30. glPushMatrix;
  31. glTranslatef(1, -1, Zoom);
  32. glRotatef(T, 0, 1, 0);
  33. glutWireSierpinskiSponge(3, @Offset, 1);
  34. glPopMatrix;
  35. glPushMatrix;
  36. glTranslatef(1, 1, Zoom);
  37. glRotatef(T, 0, 1, 0);
  38. glutSolidSierpinskiSponge(3, @Offset, 1);
  39. glPopMatrix;
  40. glutSwapBuffers;
  41. end;
  42. procedure Timer(Value: Integer); cdecl;
  43. begin
  44. glutPostRedisplay;
  45. T := T + 1.0;
  46. glutTimerFunc(20, @Timer, 0);
  47. end;
  48. procedure Key(K: Byte; X, Y: Integer); cdecl;
  49. begin
  50. case K of
  51. 27: glutLeaveMainLoop(); // using freeglut you can exit cleanly
  52. end;
  53. end;
  54. procedure Wheel(Wheel, Direction, X, Y: Integer); cdecl;
  55. begin
  56. if Wheel = 0 then
  57. begin
  58. Zoom := Zoom + Direction / 2;
  59. glutPostRedisplay();
  60. end;
  61. end;
  62. begin
  63. glutInit(@argc, argv);
  64. glutInitWindowSize(400, 400);
  65. glutInitDisplayMode(GLUT_RGB or GLUT_DOUBLE or GLUT_DEPTH);
  66. glutCreateWindow('FreeGlut demo');
  67. glutDisplayFunc(@Display);
  68. glutTimerFunc(20, @Timer, 0);
  69. glutKeyboardFunc(@Key);
  70. glutMouseWheelFunc(@Wheel);
  71. glEnable(GL_CULL_FACE); // Enable backface culling
  72. // Set up depth buffer
  73. glEnable(GL_DEPTH_TEST);
  74. glDepthFunc(GL_LESS);
  75. // Set up projection matrix
  76. glMatrixMode(GL_PROJECTION);
  77. glLoadIdentity;
  78. gluPerspective(90, 1.3, 0.1, 100);
  79. glMatrixMode(GL_MODELVIEW);
  80. glLoadIdentity;
  81. glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE, GLUT_ACTION_CONTINUE_EXECUTION);
  82. WriteLn('Starting...');
  83. glutMainLoop;
  84. Writeln('glutMainLoop finished');
  85. end.