25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

271 satır
13 KiB

3 yıl önce
3 yıl önce
3 yıl önce
3 yıl önce
3 yıl önce
3 yıl önce
3 yıl önce
3 yıl önce
3 yıl önce
3 yıl önce
3 yıl önce
3 yıl önce
3 yıl önce
3 yıl önce
3 yıl önce
  1. /*******************************************************************************************
  2. *
  3. * raylib [models] example - Skybox loading and drawing
  4. *
  5. * Example originally created with raylib 1.8, last time updated with raylib 4.0
  6. *
  7. * Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
  8. * BSD-like license that allows static linking with closed source software
  9. *
  10. * Copyright (c) 2017-2024 Ramon Santamaria (@raysan5)
  11. *
  12. ********************************************************************************************/
  13. #include "raylib.h"
  14. #include "rlgl.h"
  15. #include "raymath.h" // Required for: MatrixPerspective(), MatrixLookAt()
  16. #if defined(PLATFORM_DESKTOP)
  17. #define GLSL_VERSION 330
  18. #else // PLATFORM_ANDROID, PLATFORM_WEB
  19. #define GLSL_VERSION 100
  20. #endif
  21. // Generate cubemap (6 faces) from equirectangular (panorama) texture
  22. static TextureCubemap GenTextureCubemap(Shader shader, Texture2D panorama, int size, int format);
  23. //------------------------------------------------------------------------------------
  24. // Program main entry point
  25. //------------------------------------------------------------------------------------
  26. int main(void)
  27. {
  28. // Initialization
  29. //--------------------------------------------------------------------------------------
  30. const int screenWidth = 800;
  31. const int screenHeight = 450;
  32. InitWindow(screenWidth, screenHeight, "raylib [models] example - skybox loading and drawing");
  33. // Define the camera to look into our 3d world
  34. Camera camera = { 0 };
  35. camera.position = (Vector3){ 1.0f, 1.0f, 1.0f }; // Camera position
  36. camera.target = (Vector3){ 4.0f, 1.0f, 4.0f }; // Camera looking at point
  37. camera.up = (Vector3){ 0.0f, 1.0f, 0.0f }; // Camera up vector (rotation towards target)
  38. camera.fovy = 45.0f; // Camera field-of-view Y
  39. camera.projection = CAMERA_PERSPECTIVE; // Camera projection type
  40. // Load skybox model
  41. Mesh cube = GenMeshCube(1.0f, 1.0f, 1.0f);
  42. Model skybox = LoadModelFromMesh(cube);
  43. // Set this to true to use an HDR Texture, Note that raylib must be built with HDR Support for this to work SUPPORT_FILEFORMAT_HDR
  44. bool useHDR = false;
  45. // Load skybox shader and set required locations
  46. // NOTE: Some locations are automatically set at shader loading
  47. skybox.materials[0].shader = LoadShader(TextFormat("resources/shaders/glsl%i/skybox.vs", GLSL_VERSION),
  48. TextFormat("resources/shaders/glsl%i/skybox.fs", GLSL_VERSION));
  49. SetShaderValue(skybox.materials[0].shader, GetShaderLocation(skybox.materials[0].shader, "environmentMap"), (int[1]){ MATERIAL_MAP_CUBEMAP }, SHADER_UNIFORM_INT);
  50. SetShaderValue(skybox.materials[0].shader, GetShaderLocation(skybox.materials[0].shader, "doGamma"), (int[1]) { useHDR ? 1 : 0 }, SHADER_UNIFORM_INT);
  51. SetShaderValue(skybox.materials[0].shader, GetShaderLocation(skybox.materials[0].shader, "vflipped"), (int[1]){ useHDR ? 1 : 0 }, SHADER_UNIFORM_INT);
  52. // Load cubemap shader and setup required shader locations
  53. Shader shdrCubemap = LoadShader(TextFormat("resources/shaders/glsl%i/cubemap.vs", GLSL_VERSION),
  54. TextFormat("resources/shaders/glsl%i/cubemap.fs", GLSL_VERSION));
  55. SetShaderValue(shdrCubemap, GetShaderLocation(shdrCubemap, "equirectangularMap"), (int[1]){ 0 }, SHADER_UNIFORM_INT);
  56. char skyboxFileName[256] = { 0 };
  57. if (useHDR)
  58. {
  59. TextCopy(skyboxFileName, "resources/dresden_square_2k.hdr");
  60. // Load HDR panorama (sphere) texture
  61. Texture2D panorama = LoadTexture(skyboxFileName);
  62. // Generate cubemap (texture with 6 quads-cube-mapping) from panorama HDR texture
  63. // NOTE 1: New texture is generated rendering to texture, shader calculates the sphere->cube coordinates mapping
  64. // NOTE 2: It seems on some Android devices WebGL, fbo does not properly support a FLOAT-based attachment,
  65. // despite texture can be successfully created.. so using PIXELFORMAT_UNCOMPRESSED_R8G8B8A8 instead of PIXELFORMAT_UNCOMPRESSED_R32G32B32A32
  66. skybox.materials[0].maps[MATERIAL_MAP_CUBEMAP].texture = GenTextureCubemap(shdrCubemap, panorama, 1024, PIXELFORMAT_UNCOMPRESSED_R8G8B8A8);
  67. UnloadTexture(panorama); // Texture not required anymore, cubemap already generated
  68. }
  69. else
  70. {
  71. Image img = LoadImage("resources/skybox.png");
  72. skybox.materials[0].maps[MATERIAL_MAP_CUBEMAP].texture = LoadTextureCubemap(img, CUBEMAP_LAYOUT_AUTO_DETECT); // CUBEMAP_LAYOUT_PANORAMA
  73. UnloadImage(img);
  74. }
  75. DisableCursor(); // Limit cursor to relative movement inside the window
  76. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  77. //--------------------------------------------------------------------------------------
  78. // Main game loop
  79. while (!WindowShouldClose()) // Detect window close button or ESC key
  80. {
  81. // Update
  82. //----------------------------------------------------------------------------------
  83. UpdateCamera(&camera, CAMERA_FIRST_PERSON);
  84. // Load new cubemap texture on drag&drop
  85. if (IsFileDropped())
  86. {
  87. FilePathList droppedFiles = LoadDroppedFiles();
  88. if (droppedFiles.count == 1) // Only support one file dropped
  89. {
  90. if (IsFileExtension(droppedFiles.paths[0], ".png;.jpg;.hdr;.bmp;.tga"))
  91. {
  92. // Unload current cubemap texture to load new one
  93. UnloadTexture(skybox.materials[0].maps[MATERIAL_MAP_CUBEMAP].texture);
  94. if (useHDR)
  95. {
  96. // Load HDR panorama (sphere) texture
  97. Texture2D panorama = LoadTexture(droppedFiles.paths[0]);
  98. // Generate cubemap from panorama texture
  99. skybox.materials[0].maps[MATERIAL_MAP_CUBEMAP].texture = GenTextureCubemap(shdrCubemap, panorama, 1024, PIXELFORMAT_UNCOMPRESSED_R8G8B8A8);
  100. UnloadTexture(panorama); // Texture not required anymore, cubemap already generated
  101. }
  102. else
  103. {
  104. Image img = LoadImage(droppedFiles.paths[0]);
  105. skybox.materials[0].maps[MATERIAL_MAP_CUBEMAP].texture = LoadTextureCubemap(img, CUBEMAP_LAYOUT_AUTO_DETECT);
  106. UnloadImage(img);
  107. }
  108. TextCopy(skyboxFileName, droppedFiles.paths[0]);
  109. }
  110. }
  111. UnloadDroppedFiles(droppedFiles); // Unload filepaths from memory
  112. }
  113. //----------------------------------------------------------------------------------
  114. // Draw
  115. //----------------------------------------------------------------------------------
  116. BeginDrawing();
  117. ClearBackground(RAYWHITE);
  118. BeginMode3D(camera);
  119. // We are inside the cube, we need to disable backface culling!
  120. rlDisableBackfaceCulling();
  121. rlDisableDepthMask();
  122. DrawModel(skybox, (Vector3){0, 0, 0}, 1.0f, WHITE);
  123. rlEnableBackfaceCulling();
  124. rlEnableDepthMask();
  125. DrawGrid(10, 1.0f);
  126. EndMode3D();
  127. if (useHDR) DrawText(TextFormat("Panorama image from hdrihaven.com: %s", GetFileName(skyboxFileName)), 10, GetScreenHeight() - 20, 10, BLACK);
  128. else DrawText(TextFormat(": %s", GetFileName(skyboxFileName)), 10, GetScreenHeight() - 20, 10, BLACK);
  129. DrawFPS(10, 10);
  130. EndDrawing();
  131. //----------------------------------------------------------------------------------
  132. }
  133. // De-Initialization
  134. //--------------------------------------------------------------------------------------
  135. UnloadShader(skybox.materials[0].shader);
  136. UnloadTexture(skybox.materials[0].maps[MATERIAL_MAP_CUBEMAP].texture);
  137. UnloadModel(skybox); // Unload skybox model
  138. CloseWindow(); // Close window and OpenGL context
  139. //--------------------------------------------------------------------------------------
  140. return 0;
  141. }
  142. // Generate cubemap texture from HDR texture
  143. static TextureCubemap GenTextureCubemap(Shader shader, Texture2D panorama, int size, int format)
  144. {
  145. TextureCubemap cubemap = { 0 };
  146. rlDisableBackfaceCulling(); // Disable backface culling to render inside the cube
  147. // STEP 1: Setup framebuffer
  148. //------------------------------------------------------------------------------------------
  149. unsigned int rbo = rlLoadTextureDepth(size, size, true);
  150. cubemap.id = rlLoadTextureCubemap(0, size, format, 1);
  151. unsigned int fbo = rlLoadFramebuffer();
  152. rlFramebufferAttach(fbo, rbo, RL_ATTACHMENT_DEPTH, RL_ATTACHMENT_RENDERBUFFER, 0);
  153. rlFramebufferAttach(fbo, cubemap.id, RL_ATTACHMENT_COLOR_CHANNEL0, RL_ATTACHMENT_CUBEMAP_POSITIVE_X, 0);
  154. // Check if framebuffer is complete with attachments (valid)
  155. if (rlFramebufferComplete(fbo)) TraceLog(LOG_INFO, "FBO: [ID %i] Framebuffer object created successfully", fbo);
  156. //------------------------------------------------------------------------------------------
  157. // STEP 2: Draw to framebuffer
  158. //------------------------------------------------------------------------------------------
  159. // NOTE: Shader is used to convert HDR equirectangular environment map to cubemap equivalent (6 faces)
  160. rlEnableShader(shader.id);
  161. // Define projection matrix and send it to shader
  162. Matrix matFboProjection = MatrixPerspective(90.0*DEG2RAD, 1.0, rlGetCullDistanceNear(), rlGetCullDistanceFar());
  163. rlSetUniformMatrix(shader.locs[SHADER_LOC_MATRIX_PROJECTION], matFboProjection);
  164. // Define view matrix for every side of the cubemap
  165. Matrix fboViews[6] = {
  166. MatrixLookAt((Vector3){ 0.0f, 0.0f, 0.0f }, (Vector3){ 1.0f, 0.0f, 0.0f }, (Vector3){ 0.0f, -1.0f, 0.0f }),
  167. MatrixLookAt((Vector3){ 0.0f, 0.0f, 0.0f }, (Vector3){ -1.0f, 0.0f, 0.0f }, (Vector3){ 0.0f, -1.0f, 0.0f }),
  168. MatrixLookAt((Vector3){ 0.0f, 0.0f, 0.0f }, (Vector3){ 0.0f, 1.0f, 0.0f }, (Vector3){ 0.0f, 0.0f, 1.0f }),
  169. MatrixLookAt((Vector3){ 0.0f, 0.0f, 0.0f }, (Vector3){ 0.0f, -1.0f, 0.0f }, (Vector3){ 0.0f, 0.0f, -1.0f }),
  170. MatrixLookAt((Vector3){ 0.0f, 0.0f, 0.0f }, (Vector3){ 0.0f, 0.0f, 1.0f }, (Vector3){ 0.0f, -1.0f, 0.0f }),
  171. MatrixLookAt((Vector3){ 0.0f, 0.0f, 0.0f }, (Vector3){ 0.0f, 0.0f, -1.0f }, (Vector3){ 0.0f, -1.0f, 0.0f })
  172. };
  173. rlViewport(0, 0, size, size); // Set viewport to current fbo dimensions
  174. // Activate and enable texture for drawing to cubemap faces
  175. rlActiveTextureSlot(0);
  176. rlEnableTexture(panorama.id);
  177. for (int i = 0; i < 6; i++)
  178. {
  179. // Set the view matrix for the current cube face
  180. rlSetUniformMatrix(shader.locs[SHADER_LOC_MATRIX_VIEW], fboViews[i]);
  181. // Select the current cubemap face attachment for the fbo
  182. // WARNING: This function by default enables->attach->disables fbo!!!
  183. rlFramebufferAttach(fbo, cubemap.id, RL_ATTACHMENT_COLOR_CHANNEL0, RL_ATTACHMENT_CUBEMAP_POSITIVE_X + i, 0);
  184. rlEnableFramebuffer(fbo);
  185. // Load and draw a cube, it uses the current enabled texture
  186. rlClearScreenBuffers();
  187. rlLoadDrawCube();
  188. // ALTERNATIVE: Try to use internal batch system to draw the cube instead of rlLoadDrawCube
  189. // for some reason this method does not work, maybe due to cube triangles definition? normals pointing out?
  190. // TODO: Investigate this issue...
  191. //rlSetTexture(panorama.id); // WARNING: It must be called after enabling current framebuffer if using internal batch system!
  192. //rlClearScreenBuffers();
  193. //DrawCubeV(Vector3Zero(), Vector3One(), WHITE);
  194. //rlDrawRenderBatchActive();
  195. }
  196. //------------------------------------------------------------------------------------------
  197. // STEP 3: Unload framebuffer and reset state
  198. //------------------------------------------------------------------------------------------
  199. rlDisableShader(); // Unbind shader
  200. rlDisableTexture(); // Unbind texture
  201. rlDisableFramebuffer(); // Unbind framebuffer
  202. rlUnloadFramebuffer(fbo); // Unload framebuffer (and automatically attached depth texture/renderbuffer)
  203. // Reset viewport dimensions to default
  204. rlViewport(0, 0, rlGetFramebufferWidth(), rlGetFramebufferHeight());
  205. rlEnableBackfaceCulling();
  206. //------------------------------------------------------------------------------------------
  207. cubemap.width = size;
  208. cubemap.height = size;
  209. cubemap.mipmaps = 1;
  210. cubemap.format = format;
  211. return cubemap;
  212. }