fSkyBoxD.pas 4.6 KB

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