You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

273 regels
13 KiB

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