fSkyBoxD.pas 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. unit fSkyBoxD;
  2. interface
  3. uses
  4. Winapi.Windows,
  5. Winapi.OpenGL,
  6. System.SysUtils,
  7. System.Classes,
  8. Vcl.Graphics,
  9. Vcl.Controls,
  10. Vcl.Forms,
  11. Vcl.Dialogs,
  12. Vcl.ExtCtrls,
  13. Vcl.Imaging.Jpeg,
  14. GLS.Scene,
  15. GLScene.VectorTypes,
  16. GLScene.Keyboard,
  17. GLS.Texture,
  18. GLS.SkyDome,
  19. GLS.Cadencer,
  20. GLS.Navigator,
  21. GLS.SceneViewer,
  22. GLS.XCollection,
  23. GLS.LensFlare,
  24. GLS.Objects,
  25. GLS.Material,
  26. GLS.Coordinates,
  27. GLS.BaseClasses,
  28. GLS.SimpleNavigation,
  29. GLScene.Utils;
  30. type
  31. TFormSkybox = class(TForm)
  32. GLScene1: TGLScene;
  33. GLCamera1: TGLCamera;
  34. GLMatLibCubeMap: TGLMaterialLibrary;
  35. GLLightSource1: TGLLightSource;
  36. Castle: TGLDummyCube;
  37. GLCube1: TGLCube;
  38. GLCube11: TGLCube;
  39. GLCube111: TGLCube;
  40. GLCube112: TGLCube;
  41. GLCube2: TGLCube;
  42. GLCube21: TGLCube;
  43. GLCube211: TGLCube;
  44. GLCube212: TGLCube;
  45. GLNavigator1: TGLNavigator;
  46. GLCadencer1: TGLCadencer;
  47. GLUserInterface1: TGLUserInterface;
  48. GLLensFlare1: TGLLensFlare;
  49. GLSceneViewer1: TGLSceneViewer;
  50. GLSkyBox1: TGLSkyBox;
  51. GLSkyBox2: TGLSkyBox;
  52. GLSphere1: TGLSphere;
  53. GLSphere2: TGLSphere;
  54. procedure FormCreate(Sender: TObject);
  55. procedure GLCadencer1Progress(Sender: TObject;
  56. const deltaTime, newTime: Double);
  57. private
  58. procedure HandleKeys(d: Double);
  59. function LoadTexture(Matname, Filename: string): TGLLibMaterial;
  60. public
  61. PathToAsset: TFileName;
  62. end;
  63. var
  64. FormSkybox: TFormSkybox;
  65. implementation //-------------------------------------------------------------
  66. {$R *.dfm}
  67. function TFormSkybox.LoadTexture(Matname, Filename: string): TGLLibMaterial;
  68. begin
  69. Result := GLMatLibCubeMap.AddTextureMaterial(Matname, Filename);
  70. Result.Material.Texture.Disabled := False;
  71. Result.Material.Texture.TextureMode := tmDecal;
  72. end;
  73. //------------------------------------------------
  74. procedure TFormSkybox.FormCreate(Sender: TObject);
  75. begin
  76. PathToAsset := GetCurrentAssetPath();
  77. SetCurrentDir(PathToAsset + '\cubemap');
  78. GLMatLibCubeMap.TexturePaths := GetCurrentDir();
  79. // Skybox cubemaps
  80. LoadTexture('Left', 'icecraterlf.jpg');
  81. LoadTexture('Right', 'icecraterrt.jpg');
  82. LoadTexture('Top', 'icecraterup.jpg');
  83. LoadTexture('Bottom', 'icecraterdn.jpg');
  84. LoadTexture('Front', 'icecraterft.jpg');
  85. LoadTexture('Back', 'icecraterbk.jpg');
  86. // back to folder with textures
  87. SetCurrentDir(PathToAsset + '\texture');
  88. with LoadTexture('Clouds', 'Clouds.jpg') do
  89. begin
  90. // Add transparency to clouds
  91. Material.BlendingMode := bmTransparency;
  92. Material.FrontProperties.Diffuse.Alpha := 0.2;
  93. // scale the clouds texture
  94. TextureScale.X := 8;
  95. TextureScale.Y := 8;
  96. end;
  97. // bricks
  98. with LoadTexture('Bricks', 'rawwall.jpg') do
  99. begin
  100. TextureScale.X := 1;
  101. TextureScale.Y := 32;
  102. Material.Texture.TextureMode := tmModulate;
  103. end;
  104. with LoadTexture('Bricks2', 'marbletiles.jpg') do
  105. begin
  106. TextureScale.X := 6;
  107. TextureScale.Y := 1;
  108. Material.Texture.TextureMode := tmModulate;
  109. end;
  110. // Moon
  111. SetCurrentDir(PathToAsset + '\map');
  112. LoadTexture('Moon', 'moon.jpg').Material.Texture.TextureMode := tmModulate;
  113. // -----------------------------------------
  114. // Assign materials to objects
  115. // -----------------------------------------
  116. GLCube1.Material.LibMaterialName := 'Bricks';
  117. GLCube11.Material.LibMaterialName := 'Bricks';
  118. GLCube111.Material.LibMaterialName := 'Bricks';
  119. GLCube112.Material.LibMaterialName := 'Bricks';
  120. GLCube2.Material.LibMaterialName := 'Bricks2';
  121. GLCube21.Material.LibMaterialName := 'Bricks2';
  122. GLCube21.Material.LibMaterialName := 'Bricks2';
  123. GLCube211.Material.LibMaterialName := 'Bricks2';
  124. GLCube212.Material.LibMaterialName := 'Bricks2';
  125. GLSphere1.Material.LibMaterialName := 'Moon';
  126. GLSphere2.Material.LibMaterialName := 'Moon';
  127. //GLUserInterface1.MouseLookActive := true;
  128. end;
  129. //------------------------------------------------
  130. procedure TFormSkybox.GLCadencer1Progress(Sender: TObject;
  131. const deltaTime, newTime: Double);
  132. begin
  133. // Make clouds slide
  134. with GLMatLibCubeMap.Materials.GetLibMaterialByName('Clouds') do
  135. begin
  136. TextureOffset.X := TextureOffset.X + deltaTime * 0.02;
  137. TextureOffset.y := TextureOffset.y + deltaTime * 0.03;
  138. end;
  139. // Rotate moons
  140. GLSphere1.Turn(deltaTime * 7);
  141. GLSphere2.Turn(deltaTime * 10);
  142. HandleKeys(deltaTime);
  143. GLUserInterface1.Mouselook;
  144. GLUserInterface1.MouseUpdate;
  145. GLSceneViewer1.Invalidate();
  146. end;
  147. //------------------------------------------------
  148. procedure TFormSkybox.HandleKeys(d: Double);
  149. begin
  150. if IsKeyDown('W') or IsKeyDown('Z') then
  151. GLCamera1.Move(d);
  152. if IsKeyDown('S') then
  153. GLCamera1.Move(-d);
  154. if IsKeyDown('A') or IsKeyDown('A') then
  155. GLCamera1.Slide(-d);
  156. if IsKeyDown('D') then
  157. GLCamera1.Slide(d);
  158. if IsKeyDown(VK_ESCAPE) then
  159. Close;
  160. end;
  161. end.