glutdemova.pp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. {
  2. GL units for Free Pascal - GLUT demo
  3. 1999 Sebastian Guenther, [email protected]
  4. 2008 Jonas Maebe (converted to use vertex arrays)
  5. You may use this source as starting point for your own programs; consider it
  6. as Public Domain.
  7. }
  8. {$mode objfpc}
  9. program GLUTDemoVA;
  10. uses
  11. GL, GLU, GLUT;
  12. const
  13. FPCImg: array[0..4, 0..10] of Byte =
  14. ((1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1),
  15. (1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0),
  16. (1, 1, 1, 0, 1, 1, 1, 0, 1, 0, 0),
  17. (1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0),
  18. (1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1));
  19. var
  20. counter: Integer;
  21. (*
  22. 3 __ 2
  23. /| |
  24. 7/0|_ |1
  25. |/ /
  26. +--+
  27. 4 5
  28. back plane: 0-3-2-1
  29. *)
  30. const
  31. colors: array[0..7, 0..2] of Single =
  32. ((0, 0, 0), (0, 0, 1), (0, 1, 0), (0, 1, 1),
  33. (1, 0, 0), (1, 0, 1), (1, 1, 0), (1, 1, 1));
  34. corners: array[0..7, 0..2] of Single =
  35. ((-1, -1, -1), (+1, -1, -1), (+1, +1, -1), (-1, +1, -1),
  36. (-1, -1, +1), (+1, -1, +1), (+1, +1, +1), (-1, +1, +1));
  37. indices: array[0..5] of array[0..3] of GLubyte =
  38. (
  39. { all vertices must be in ccw order }
  40. (0,3,1,2), // back
  41. (4,5,7,6), // front
  42. (3,0,7,4), // left
  43. (1,2,5,6), // right
  44. (3,7,2,6), // top
  45. (5,4,1,0) // bottom
  46. );
  47. procedure DrawCube;
  48. begin
  49. glEnableClientState(GL_VERTEX_ARRAY);
  50. glEnableClientState(GL_COLOR_ARRAY);
  51. glVertexPointer(3,GL_FLOAT,0,@corners);
  52. glColorPointer(3,GL_FLOAT,0,@colors);
  53. // this will also draw a bunch of triangles inside the cube, but
  54. // may still be faster than the commented-out sequence below due
  55. // to less calls into the renderer
  56. glDrawElements(GL_TRIANGLE_STRIP,24,GL_UNSIGNED_BYTE,@indices[0]);
  57. (*
  58. glDrawElements(GL_TRIANGLE_STRIP,4,GL_UNSIGNED_BYTE,@indices[0]);
  59. glDrawElements(GL_TRIANGLE_STRIP,4,GL_UNSIGNED_BYTE,@indices[1]);
  60. glDrawElements(GL_TRIANGLE_STRIP,4,GL_UNSIGNED_BYTE,@indices[2]);
  61. glDrawElements(GL_TRIANGLE_STRIP,4,GL_UNSIGNED_BYTE,@indices[3]);
  62. glDrawElements(GL_TRIANGLE_STRIP,4,GL_UNSIGNED_BYTE,@indices[4]);
  63. glDrawElements(GL_TRIANGLE_STRIP,4,GL_UNSIGNED_BYTE,@indices[5]);
  64. *)
  65. glDisableClientState(GL_VERTEX_ARRAY);
  66. glDisableClientState(GL_COLOR_ARRAY);
  67. end;
  68. procedure DisplayWindow; cdecl;
  69. var
  70. x, y: Integer;
  71. begin
  72. Inc(counter);
  73. // counter:=145;
  74. glClearColor(0, 0, 0.2, 1);
  75. glClear(GL_COLOR_BUFFER_BIT+GL_DEPTH_BUFFER_BIT);
  76. glPushMatrix;
  77. glTranslatef(0, 0, Sin(Single(counter) / 20.0) * 5.0 - 5.0);
  78. glRotatef(Sin(Single(counter) / 200.0) * 720.0, 0, 1, 0);
  79. glRotatef(counter, 0, 0, 1);
  80. for y := 0 to 4 do
  81. for x := 0 to 10 do
  82. if FPCImg[y, x] > 0 then begin
  83. glPushMatrix;
  84. glRotatef(x * Sin(Single(counter) / 5.0), 0, 1, 0);
  85. glRotatef(y * Sin(Single(counter) / 12.0) * 4.0, 0, 0, 1);
  86. glTranslatef((x - 5) * 1, (2 - y) * 1, 0);
  87. glScalef(0.4, 0.4, 0.4);
  88. glRotatef(counter, 0.5, 1, 0);
  89. DrawCube;
  90. glPopMatrix;
  91. end;
  92. glPopMatrix;
  93. // Inc(counter);
  94. glutSwapBuffers;
  95. end;
  96. procedure OnTimer(value: Integer); cdecl;
  97. begin
  98. glutPostRedisplay;
  99. glutTimerFunc(20, @OnTimer, 0);
  100. end;
  101. begin
  102. glutInit(@argc, argv);
  103. glutInitDisplayMode(GLUT_RGB or GLUT_DOUBLE or GLUT_DEPTH);
  104. glutCreateWindow('Free Pascal GLUT demo');
  105. glutDisplayFunc(@DisplayWindow);
  106. glutTimerFunc(20, @OnTimer, 0);
  107. WriteLn;
  108. WriteLn('GL info:');
  109. WriteLn(' Vendor: ', PChar(glGetString(GL_VENDOR)));
  110. WriteLn(' Renderer: ', PChar(glGetString(GL_RENDERER)));
  111. WriteLn(' Version: ', PChar(glGetString(GL_VERSION)));
  112. WriteLn(' Extensions: ', PChar(glGetString(GL_EXTENSIONS)));
  113. // Enable backface culling
  114. glEnable(GL_CULL_FACE);
  115. // Set up depth buffer
  116. glEnable(GL_DEPTH_TEST);
  117. glDepthFunc(GL_LESS);
  118. // Set up projection matrix
  119. glMatrixMode(GL_PROJECTION);
  120. glLoadIdentity;
  121. gluPerspective(90, 1.3, 0.1, 100);
  122. glMatrixMode(GL_MODELVIEW);
  123. glLoadIdentity;
  124. glTranslatef(0, 0, -5.5);
  125. WriteLn('Starting...');
  126. glutMainLoop;
  127. end.