您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

260 行
12 KiB

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