fCubemap.pas 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. unit fCubemap;
  2. interface
  3. uses
  4. System.SysUtils,
  5. System.Classes,
  6. Vcl.Graphics,
  7. Vcl.Imaging.Jpeg,
  8. Vcl.Controls,
  9. Vcl.Forms,
  10. Vcl.Dialogs,
  11. Vcl.StdCtrls,
  12. GLS.Scene,
  13. GLS.SceneViewer,
  14. GLS.Objects,
  15. GLS.Texture,
  16. GLS.Context,
  17. GLS.Utils,
  18. GLS.Coordinates,
  19. GLS.BaseClasses,
  20. GLS.GeomObjects;
  21. type
  22. TFormCubeMap = class(TForm)
  23. GLScene1: TGLScene;
  24. GLSceneViewer1: TGLSceneViewer;
  25. GLCamera1: TGLCamera;
  26. DummyCube1: TGLDummyCube;
  27. GLLightSource1: TGLLightSource;
  28. ButtonApply: TButton;
  29. Teapot1: TGLTeapot;
  30. procedure GLSceneViewer1MouseMove(Sender: TObject; Shift: TShiftState;
  31. X, Y: Integer);
  32. procedure GLSceneViewer1MouseDown(Sender: TObject;
  33. Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
  34. procedure ButtonApplyClick(Sender: TObject);
  35. procedure GLSceneViewer1BeforeRender(Sender: TObject);
  36. procedure FormCreate(Sender: TObject);
  37. private
  38. CubmapSupported: Boolean;
  39. CubemapPath: TFilename;
  40. Cubemap: TGLTexture;
  41. public
  42. mx, my: Integer;
  43. end;
  44. var
  45. FormCubeMap: TFormCubeMap;
  46. implementation
  47. {$R *.dfm}
  48. procedure TFormCubeMap.FormCreate(Sender: TObject);
  49. begin
  50. // Our cube map images are here
  51. SetGLSceneMediaDir();
  52. CubemapPath := GetCurrentDir() + '\Cubemaps';
  53. SetCurrentDir(CubemapPath);
  54. end;
  55. procedure TFormCubeMap.GLSceneViewer1BeforeRender(Sender: TObject);
  56. begin
  57. CubmapSupported := GL.ARB_texture_cube_map;
  58. GLSceneViewer1.BeforeRender := nil;
  59. end;
  60. procedure TFormCubeMap.ButtonApplyClick(Sender: TObject);
  61. begin
  62. // Cube map warning message
  63. // If you don't check and turn off cube maps yourself in your apps when
  64. // cube maps aren't supported and will just turn off texturing
  65. // (ie. no error generated, just a different output)
  66. if not CubmapSupported then
  67. begin
  68. ShowMessage('Your graphics board does not support cube maps...');
  69. Exit;
  70. end;
  71. with Teapot1.Material.Texture do
  72. begin
  73. // We need a CubeMapImage, which unlike the "regular Images" stores
  74. // multiple images.
  75. ImageClassName := TGLCubeMapImage.ClassName;
  76. with Image as TGLCubeMapImage do
  77. begin
  78. // Load all 6 texture map components of the cube map
  79. // The 'PX', 'NX', etc. refer to 'positive X', 'negative X', etc.
  80. // and follow the RenderMan specs/conventions
  81. Picture[cmtPX].LoadFromFile('cm_left.jpg');
  82. Picture[cmtNX].LoadFromFile('cm_right.jpg');
  83. Picture[cmtPY].LoadFromFile('cm_top.jpg');
  84. Picture[cmtNY].LoadFromFile('cm_bottom.jpg');
  85. Picture[cmtPZ].LoadFromFile('cm_back.jpg');
  86. Picture[cmtNZ].LoadFromFile('cm_front.jpg');
  87. end;
  88. // Select reflection cube map environment mapping
  89. // This is the mode you'll most commonly use with cube maps, normal cube
  90. // map generation is also supported (used for diffuse environment lighting)
  91. MappingMode := tmmCubeMapReflection;
  92. // That's all folks, let us see the thing!
  93. Disabled := False;
  94. end;
  95. ButtonApply.Visible := False;
  96. end;
  97. // standard issue handlers for mouse movement
  98. procedure TFormCubeMap.GLSceneViewer1MouseDown(Sender: TObject;
  99. Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
  100. begin
  101. mx := x;
  102. my := y;
  103. end;
  104. procedure TFormCubeMap.GLSceneViewer1MouseMove(Sender: TObject;
  105. Shift: TShiftState; X, Y: Integer);
  106. begin
  107. if Shift <> [] then
  108. begin
  109. if ssLeft in Shift then
  110. GLCamera1.MoveAroundTarget(my - y, mx - x)
  111. else
  112. GLCamera1.RotateTarget(my - y, mx - x);
  113. mx := x;
  114. my := y;
  115. end;
  116. end;
  117. end.