2
0

fSkyBox.pas 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. unit fSkyBox;
  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. GLS.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. GLS.Utils;
  29. type
  30. TFormSkyBox = class(TForm)
  31. GLScene1: TGLScene;
  32. GLCamera1: TGLCamera;
  33. GLMaterialLibraryCM: 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. GLSimpleNavigation1: TGLSimpleNavigation;
  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. end;
  62. var
  63. FormSkyBox: TFormSkyBox;
  64. implementation
  65. {$R *.dfm}
  66. function TFormSkyBox.LoadTexture(Matname, Filename: string): TGLLibMaterial;
  67. begin
  68. Result := GLMaterialLibraryCM.AddTextureMaterial(Matname, Filename);
  69. Result.Material.Texture.Disabled := False;
  70. Result.Material.Texture.TextureMode := tmDecal;
  71. end;
  72. procedure TFormSkyBox.FormCreate(Sender: TObject);
  73. var
  74. PathCM: TFileName;
  75. begin
  76. SetGLSceneMediaDir();
  77. PathCM := GetCurrentDir() + '\Cubemaps';
  78. SetCurrentDir(PathCM);
  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. SetGLSceneMediaDir(); // back to media folder with textures
  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. LoadTexture('Moon', 'unwrapped moon.jpg').Material.Texture.TextureMode :=
  111. 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. procedure TFormSkyBox.GLCadencer1Progress(Sender: TObject;
  129. const deltaTime, newTime: Double);
  130. begin
  131. // Make clouds Texture slide
  132. with GLMaterialLibraryCM.Materials.GetLibMaterialByName('Clouds') do
  133. begin
  134. TextureOffset.X := TextureOffset.X + deltaTime * 0.02;
  135. TextureOffset.y := TextureOffset.y + deltaTime * 0.03;
  136. end;
  137. // Rotate moons
  138. GLSphere1.Turn(deltaTime * 7);
  139. GLSphere2.Turn(deltaTime * 10);
  140. HandleKeys(deltaTime);
  141. GLUserInterface1.Mouselook;
  142. GLUserInterface1.MouseUpdate;
  143. GLSceneViewer1.Invalidate;
  144. end;
  145. procedure TFormSkyBox.HandleKeys(d: Double);
  146. begin
  147. if IsKeyDown('W') or IsKeyDown('Z') then
  148. GLCamera1.Move(d);
  149. if IsKeyDown('S') then
  150. GLCamera1.Move(-d);
  151. if IsKeyDown('A') or IsKeyDown('A') then
  152. GLCamera1.Slide(-d);
  153. if IsKeyDown('D') then
  154. GLCamera1.Slide(d);
  155. if IsKeyDown(VK_ESCAPE) then
  156. Close;
  157. end;
  158. end.