GBE.Cubemap.pas 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. unit GBE.Cubemap;
  2. (*
  3. The TGBECubemap allows you to generate a cubemap from an image
  4. of 12 thumbnails (3 rows, 4 columns).
  5. Based on code by Gregory Bersegeay
  6. *)
  7. interface
  8. uses
  9. System.SysUtils,
  10. System.Classes,
  11. System.Types,
  12. System.Math.Vectors,
  13. FMX.Types,
  14. FMX.Controls3D,
  15. FMX.Objects3D,
  16. FMX.Graphics,
  17. System.UITypes,
  18. FMX.Materials,
  19. FMX.types3D,
  20. FMX.Materialsources;
  21. type
  22. TGBECubemap = class(TMesh)
  23. private
  24. protected
  25. public
  26. constructor Create(AOwner: TComponent); override;
  27. destructor Destroy; override;
  28. procedure GenerateCubemap;
  29. published
  30. property Locked default False;
  31. property HitTest default False;
  32. property Visible default True;
  33. end;
  34. procedure Register;
  35. implementation // -------------------------------------------------------------
  36. // TGBECubemap
  37. constructor TGBECubemap.Create(AOwner: TComponent);
  38. begin
  39. inherited;
  40. TwoSide := True;
  41. GenerateCubemap;
  42. end;
  43. procedure TGBECubemap.GenerateCubemap;
  44. begin
  45. self.Data.Clear;
  46. (*
  47. There are 18 points to be able to apply the texture correctly
  48. (8 points are enough for the cube, but then we can only associate one point
  49. of the texture with a vertex, so we duplicate the necessary vertices
  50. to be able to apply the texture correctly on the 6 faces).
  51. *)
  52. self.Data.Points :=
  53. '-1 -1 1, 1 -1 1, -1 1 1, 1 1 1, 1 -1 -1, 1 1 -1, -1 -1 -1, -1 1 -1, -1 -1 1, -1 1 1,' +
  54. // faces Left, Front, Right, Back
  55. '-1 -1 1, 1 -1 1, 1 -1 -1, -1 -1 -1, -1 1 -1, -1 1 1, 1 1 -1, 1 1 1';
  56. (*
  57. Top and Bottom faces
  58. Positioning the texture at each point
  59. *)
  60. self.Data.TexCoordinates :=
  61. '0.0 0.34, 0.25 0.34, 0.0 0.66, 0.25 0.66, 0.5 0.34, 0.5 0.66, 0.75 0.34, 0.75 0.66, 1 0.34, 1 0.66,'
  62. + ' 0.25 0.0, 0.25 0.34, 0.5 0.34, 0.5 0.0, 0.5 1, 0.25 1, 0.5 0.66, 0.25 0.66';
  63. // Creation and indexing of triangles according to need
  64. self.Data.TriangleIndices :=
  65. '0 1 2 ,2 1 3 ,1 4 3, 3 4 5, 4 6 5, 5 6 7, 6 8 7, 7 8 9, 10 11 12, 12 10 13, 14 15 16, 16 15 17';
  66. end;
  67. destructor TGBECubemap.Destroy;
  68. begin
  69. inherited;
  70. end;
  71. // ---------------------------------------------------------------------------
  72. procedure Register;
  73. begin
  74. RegisterComponents('GXScene GBE', [TGBECubemap]);
  75. end;
  76. end.