glutdemoes.pp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. library GLUTDemoES;
  10. uses
  11. gles11, math;
  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..3] of Single =
  32. ((0, 0, 0, 1), (0, 0, 1, 1), (0, 1, 0, 1), (0, 1, 1, 1),
  33. (1, 0, 0, 1), (1, 0, 1, 1), (1, 1, 0, 1), (1, 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. if (glGetError() <> GL_NO_ERROR) then
  51. halt(10);
  52. glEnableClientState(GL_COLOR_ARRAY);
  53. if (glGetError() <> GL_NO_ERROR) then
  54. halt(11);
  55. glVertexPointer(3,GL_FLOAT,0,@corners);
  56. if (glGetError() <> GL_NO_ERROR) then
  57. halt(12);
  58. glColorPointer(4,GL_FLOAT,0,@colors);
  59. if (glGetError() <> GL_NO_ERROR) then
  60. halt(13);
  61. // this will also draw a bunch of triangles inside the cube, but
  62. // may still be faster than the commented-out sequence below due
  63. // to less calls into the renderer
  64. glDrawElements(GL_TRIANGLE_STRIP,24,GL_UNSIGNED_BYTE,@indices[0]);
  65. if (glGetError() <> GL_NO_ERROR) then
  66. halt(14);
  67. (*
  68. glDrawElements(GL_TRIANGLE_STRIP,4,GL_UNSIGNED_BYTE,@indices[0]);
  69. glDrawElements(GL_TRIANGLE_STRIP,4,GL_UNSIGNED_BYTE,@indices[1]);
  70. glDrawElements(GL_TRIANGLE_STRIP,4,GL_UNSIGNED_BYTE,@indices[2]);
  71. glDrawElements(GL_TRIANGLE_STRIP,4,GL_UNSIGNED_BYTE,@indices[3]);
  72. glDrawElements(GL_TRIANGLE_STRIP,4,GL_UNSIGNED_BYTE,@indices[4]);
  73. glDrawElements(GL_TRIANGLE_STRIP,4,GL_UNSIGNED_BYTE,@indices[5]);
  74. *)
  75. glDisableClientState(GL_VERTEX_ARRAY);
  76. if (glGetError() <> GL_NO_ERROR) then
  77. halt(15);
  78. glDisableClientState(GL_COLOR_ARRAY);
  79. if (glGetError() <> GL_NO_ERROR) then
  80. halt(16);
  81. end;
  82. procedure glDraw; cdecl; export;
  83. var
  84. x, y: Integer;
  85. begin
  86. Inc(counter);
  87. // counter:=145;
  88. glClearColor(0, 0, 0.2, 1);
  89. if (glGetError() <> GL_NO_ERROR) then
  90. halt(8);
  91. glClear(GL_COLOR_BUFFER_BIT+GL_DEPTH_BUFFER_BIT);
  92. if (glGetError() <> GL_NO_ERROR) then
  93. halt(9);
  94. glPushMatrix;
  95. glTranslatef(0, 0, Sin(Single(counter) / 20.0) * 5.0 - 5.0);
  96. glRotatef(Sin(Single(counter) / 200.0) * 720.0, 0, 1, 0);
  97. glRotatef(counter, 0, 0, 1);
  98. for y := 0 to 4 do
  99. for x := 0 to 10 do
  100. if FPCImg[y, x] > 0 then begin
  101. glPushMatrix;
  102. glRotatef(x * Sin(Single(counter) / 5.0), 0, 1, 0);
  103. glRotatef(y * Sin(Single(counter) / 12.0) * 4.0, 0, 0, 1);
  104. glTranslatef((x - 5) * 1, (2 - y) * 1, 0);
  105. glScalef(0.4, 0.4, 0.4);
  106. glRotatef(counter, 0.5, 1, 0);
  107. DrawCube;
  108. glPopMatrix;
  109. end;
  110. glPopMatrix;
  111. end;
  112. { gluperspective replacement, from http://iphonedevelopment.blogspot.com/2008/12/gluperspective.html }
  113. procedure gluperspective(fovy, aspect, znear, zfar: GLfloat);
  114. var
  115. xmin, xmax, ymin, ymax: GLfloat;
  116. begin
  117. // Start in projection mode.
  118. glMatrixMode(GL_PROJECTION);
  119. glLoadIdentity();
  120. ymax := zNear * tan(fovy * pi / 360.0);
  121. ymin := -ymax;
  122. xmin := ymin * aspect;
  123. xmax := ymax * aspect;
  124. glfrustumf(xmin, xmax, ymin, ymax, zNear, zFar);
  125. end;
  126. procedure glInit; cdecl; export;
  127. begin
  128. // Enable backface culling
  129. glEnable(GL_CULL_FACE);
  130. if (glGetError() <> GL_NO_ERROR) then
  131. halt(1);
  132. // Set up depth buffer
  133. glEnable(GL_DEPTH_TEST);
  134. if (glGetError() <> GL_NO_ERROR) then
  135. halt(2);
  136. glDepthFunc(GL_LESS);
  137. if (glGetError() <> GL_NO_ERROR) then
  138. halt(3);
  139. // Set up projection matrix
  140. glMatrixMode(GL_PROJECTION);
  141. if (glGetError() <> GL_NO_ERROR) then
  142. halt(4);
  143. glLoadIdentity;
  144. gluPerspective(90, 1.3, 0.1, 100);
  145. if (glGetError() <> GL_NO_ERROR) then
  146. halt(5);
  147. glMatrixMode(GL_MODELVIEW);
  148. if (glGetError() <> GL_NO_ERROR) then
  149. halt(6);
  150. glLoadIdentity;
  151. glTranslatef(0, 0, -5.5);
  152. if (glGetError() <> GL_NO_ERROR) then
  153. halt(7);
  154. end;
  155. end.