GLPolyhedron.pas 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. //
  2. // This unit is part of the GLScene Engine, http://glscene.org
  3. //
  4. unit GLPolyhedron;
  5. (* Standard polyhedrons *)
  6. interface
  7. {$I GLScene.inc}
  8. uses
  9. Winapi.OpenGL,
  10. System.Classes,
  11. OpenGLTokens,
  12. GLScene,
  13. GLContext,
  14. GLVectorGeometry,
  15. GLObjects,
  16. GLVectorFileObjects,
  17. GLMesh,
  18. GLRenderContextInfo;
  19. type
  20. (* The tetrahedron has no texture coordinates defined, ie. without using
  21. a texture generation mode, no texture will be mapped. *)
  22. TGLTetrahedron = class(TGLBaseMesh)
  23. public
  24. procedure BuildList(var rci: TGLRenderContextInfo); override;
  25. end;
  26. (* The octahedron has no texture coordinates defined, ie. without using
  27. a texture generation mode, no texture will be mapped. *)
  28. TGLOctahedron = class(TGLBaseMesh)
  29. public
  30. procedure BuildList(var rci: TGLRenderContextInfo); override;
  31. end;
  32. (* The hexahedron has no texture coordinates defined, ie. without using
  33. a texture generation mode, no texture will be mapped. *)
  34. TGLHexahedron = class(TGLBaseMesh)
  35. public
  36. procedure BuildList(var rci: TGLRenderContextInfo); override;
  37. end;
  38. (* The dodecahedron has no texture coordinates defined, ie. without using
  39. a texture generation mode, no texture will be mapped. *)
  40. TGLDodecahedron = class(TGLBaseMesh)
  41. public
  42. procedure BuildList(var rci: TGLRenderContextInfo); override;
  43. end;
  44. (* The icosahedron has no texture coordinates defined, ie. without using
  45. a texture generation mode, no texture will be mapped. *)
  46. TGLIcosahedron = class(TGLBaseMesh)
  47. public
  48. procedure BuildList(var rci: TGLRenderContextInfo); override;
  49. end;
  50. //-------------------------------------------------------------
  51. implementation
  52. //-------------------------------------------------------------
  53. //--------------------
  54. //-------------------- TGLTetrahedron ------------------------
  55. //--------------------
  56. procedure TGLTetrahedron.BuildList(var rci: TGLRenderContextInfo);
  57. const
  58. Vertices: packed array [0 .. 3] of TAffineVector =
  59. ((X: 1.0; Y: 1.0; Z: 1.0),
  60. (X: 1.0; Y: -1.0; Z: -1.0),
  61. (X: -1.0; Y: 1.0; Z: -1.0),
  62. (X: -1.0; Y: -1.0; Z: 1.0));
  63. Triangles: packed array [0 .. 3] of packed array [0 .. 2] of Byte =
  64. ((0, 1, 3),
  65. (2, 1, 0),
  66. (3, 2, 0),
  67. (1, 2, 3));
  68. var
  69. i, j: Integer;
  70. n: TAffineVector;
  71. faceIndices: PByteArray;
  72. begin
  73. for i := 0 to 3 do
  74. begin
  75. faceIndices := @triangles[i, 0];
  76. n := CalcPlaneNormal(vertices[faceIndices^[0]], vertices[faceIndices^[1]],
  77. vertices[faceIndices^[2]]);
  78. gl.Normal3fv(@n);
  79. gl.Begin_(GL_TRIANGLES);
  80. for j := 0 to 2 do
  81. gl.Vertex3fv(@vertices[faceIndices^[j]]);
  82. gl.End_();
  83. end;
  84. end;
  85. //--------------------
  86. //-------------------- TGLOctahedron ------------------------
  87. //--------------------
  88. procedure TGLOctahedron.BuildList(var rci: TGLRenderContextInfo);
  89. const
  90. Vertices: packed array [0 .. 5] of TAffineVector =
  91. ((X: 1.0; Y: 0.0; Z: 0.0),
  92. (X:-1.0; Y: 0.0; Z: 0.0),
  93. (X: 0.0; Y: 1.0; Z: 0.0),
  94. (X: 0.0; Y: -1.0; Z: 0.0),
  95. (X: 0.0; Y: 0.0; Z: 1.0),
  96. (X: 0.0; Y: 0.0; Z: -1.0));
  97. Triangles: packed array [0 .. 7] of packed array [0 .. 2] of Byte =
  98. ((0, 4, 2),
  99. (1, 2, 4),
  100. (0, 3, 4),
  101. (1, 4, 3),
  102. (0, 2, 5),
  103. (1, 5, 2),
  104. (0, 5, 3),
  105. (1, 3, 5));
  106. var
  107. i, j: Integer;
  108. n: TAffineVector;
  109. faceIndices: PByteArray;
  110. begin
  111. for i := 0 to 7 do
  112. begin
  113. faceIndices := @triangles[i, 0];
  114. n := CalcPlaneNormal(vertices[faceIndices^[0]], vertices[faceIndices^[1]],
  115. vertices[faceIndices^[2]]);
  116. gl.Normal3fv(@n);
  117. gl.Begin_(GL_TRIANGLES);
  118. for j := 0 to 2 do
  119. gl.Vertex3fv(@vertices[faceIndices^[j]]);
  120. gl.End_();
  121. end;
  122. end;
  123. // ------------------
  124. // ------------------ TGLHexahedron ------------------
  125. // ------------------
  126. procedure TGLHexahedron.BuildList(var rci: TGLRenderContextInfo);
  127. const
  128. Vertices: packed array [0 .. 7] of TAffineVector =
  129. ((X:-1; Y:-1; Z:-1),
  130. (X: 1; Y:-1; Z:-1),
  131. (X: 1; Y:-1; Z: 1),
  132. (X:-1; Y:-1; Z: 1),
  133. (X:-1; Y: 1; Z:-1),
  134. (X: 1; Y: 1; Z:-1),
  135. (X: 1; Y: 1; Z: 1),
  136. (X:-1; Y: 1; Z: 1));
  137. Quadrangles: packed array [0 .. 5] of packed array [0 .. 3] of Byte =
  138. ((0, 1, 2, 3),
  139. (3, 2, 6, 7),
  140. (7, 6, 5, 4),
  141. (4, 5, 1, 0),
  142. (0, 3, 7, 4),
  143. (1, 5, 6, 2));
  144. var
  145. i, j: Integer;
  146. n: TAffineVector;
  147. faceIndices: PByteArray;
  148. begin
  149. for i := 0 to 4 do
  150. begin
  151. faceIndices := @Quadrangles[i, 0];
  152. n := CalcPlaneNormal(vertices[faceIndices^[0]], vertices[faceIndices^[1]], vertices[faceIndices^[2]]);
  153. gl.Normal3fv(@n);
  154. gl.Begin_(GL_QUADS);
  155. for j := 0 to 7 do
  156. gl.Vertex3fv(@vertices[faceIndices^[j]]);
  157. gl.End_();
  158. end;
  159. end;
  160. // ------------------
  161. // ------------------ TGLDodecahedron ------------------
  162. // ------------------
  163. procedure TGLDodecahedron.BuildList(var rci: TGLRenderContextInfo);
  164. const
  165. A = 1.61803398875 * 0.3; // (Sqrt(5)+1)/2
  166. B = 0.61803398875 * 0.3; // (Sqrt(5)-1)/2
  167. C = 1 * 0.3;
  168. const
  169. Vertices: packed array [0 .. 19] of TAffineVector =
  170. ((X: - A; Y: 0; Z: B), (X: - A; Y: 0; Z: - B), (X: A; Y: 0; Z: - B),
  171. (X: A; Y: 0; Z: B), (X: B; Y: - A; Z: 0), (X: - B; Y: - A; Z: 0),
  172. (X: - B; Y: A; Z: 0), (X: B; Y: A; Z: 0), (X: 0; Y: B; Z: - A),
  173. (X: 0; Y: - B; Z: - A), (X: 0; Y: - B; Z: A), (X: 0; Y: B; Z: A),
  174. (X: - C; Y: - C; Z: C), (X: - C; Y: - C; Z: - C), (X: C; Y: - C; Z: - C),
  175. (X: C; Y: - C; Z: C), (X: - C; Y: C; Z: C), (X: - C; Y: C; Z: - C),
  176. (X: C; Y: C; Z: - C), (X: C; Y: C; Z: C));
  177. Polygons: packed array [0 .. 11] of packed array [0 .. 4] of Byte =
  178. ((0, 12, 10, 11, 16), (1, 17, 8, 9, 13), (2, 14, 9, 8, 18),
  179. (3, 19, 11, 10, 15), (4, 14, 2, 3, 15), (5, 12, 0, 1, 13),
  180. (6, 17, 1, 0, 16), (7, 19, 3, 2, 18), (8, 17, 6, 7, 18),
  181. (9, 14, 4, 5, 13), (10, 12, 5, 4, 15), (11, 19, 7, 6, 16));
  182. var
  183. i, j: Integer;
  184. n: TAffineVector;
  185. faceIndices: PByteArray;
  186. begin
  187. for i := 0 to 11 do
  188. begin
  189. faceIndices := @polygons[i, 0];
  190. n := CalcPlaneNormal(vertices[faceIndices^[0]], vertices[faceIndices^[1]],
  191. vertices[faceIndices^[2]]);
  192. gl.Normal3fv(@n);
  193. // gl.Begin_(GL_TRIANGLE_FAN);
  194. // for j := 0 to 4 do
  195. // gl.Vertex3fv(@vertices[faceIndices^[j]]);
  196. // gl.End_();
  197. gl.Begin_(GL_TRIANGLES);
  198. for j := 1 to 3 do
  199. begin
  200. gl.Vertex3fv(@vertices[faceIndices^[0]]);
  201. gl.Vertex3fv(@vertices[faceIndices^[j]]);
  202. gl.Vertex3fv(@vertices[faceIndices^[j+1]]);
  203. end;
  204. gl.End_();
  205. end;
  206. end;
  207. // ------------------
  208. // ------------------ TGLIcosahedron ------------------
  209. // ------------------
  210. procedure TGLIcosahedron.BuildList(var rci: TGLRenderContextInfo);
  211. const
  212. A = 0.5;
  213. B = 0.30901699437; // 1/(1+Sqrt(5))
  214. const
  215. Vertices: packed array [0 .. 11] of TAffineVector =
  216. ((X: 0; Y: - B; Z: - A), (X: 0; Y: - B; Z: A), (X: 0; Y: B; Z: - A),
  217. (X: 0; Y: B; Z: A), (X: - A; Y: 0; Z: - B), (X: - A; Y: 0; Z: B),
  218. (X: A; Y: 0; Z: - B), (X: A; Y: 0; Z: B), (X: - B; Y: - A; Z: 0),
  219. (X: - B; Y: A; Z: 0), (X: B; Y: - A; Z: 0), (X: B; Y: A; Z: 0));
  220. Triangles: packed array [0 .. 19] of packed array [0 .. 2] of Byte =
  221. ((2, 9, 11), (3, 11, 9), (3, 5, 1), (3, 1, 7), (2, 6, 0),
  222. (2, 0, 4), (1, 8, 10), (0, 10, 8), (9, 4, 5), (8, 5, 4), (11, 7, 6),
  223. (10, 6, 7), (3, 9, 5), (3, 7, 11), (2, 4, 9), (2, 11, 6), (0, 8, 4),
  224. (0, 6, 10), (1, 5, 8), (1, 10, 7));
  225. var
  226. i, j: Integer;
  227. n: TAffineVector;
  228. faceIndices: PByteArray;
  229. begin
  230. for i := 0 to 19 do
  231. begin
  232. faceIndices := @triangles[i, 0];
  233. n := CalcPlaneNormal(vertices[faceIndices^[0]], vertices[faceIndices^[1]],
  234. vertices[faceIndices^[2]]);
  235. gl.Normal3fv(@n);
  236. gl.Begin_(GL_TRIANGLES);
  237. for j := 0 to 2 do
  238. gl.Vertex3fv(@vertices[faceIndices^[j]]);
  239. gl.End_();
  240. end;
  241. end;
  242. //==================================================================
  243. initialization
  244. //==================================================================
  245. RegisterClasses([TGLDodecahedron, TGLIcosahedron, TGLHexahedron, TGLOctahedron, TGLTetrahedron]);
  246. end.