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

244 行
11 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 = false;
  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. if (useHDR)
  46. {
  47. TextCopy(skyboxFileName, "resources/dresden_square_2k.hdr");
  48. // Load HDR panorama (sphere) texture
  49. Texture2D panorama = panorama = LoadTexture(skyboxFileName);
  50. // Generate cubemap (texture with 6 quads-cube-mapping) from panorama HDR texture
  51. // NOTE 1: New texture is generated rendering to texture, shader calculates the sphere->cube coordinates mapping
  52. // NOTE 2: It seems on some Android devices WebGL, fbo does not properly support a FLOAT-based attachment,
  53. // despite texture can be successfully created.. so using PIXELFORMAT_UNCOMPRESSED_R8G8B8A8 instead of PIXELFORMAT_UNCOMPRESSED_R32G32B32A32
  54. skybox.materials[0].maps[MATERIAL_MAP_CUBEMAP].texture = GenTextureCubemap(shdrCubemap, panorama, 1024, PIXELFORMAT_UNCOMPRESSED_R8G8B8A8);
  55. UnloadTexture(panorama); // Texture not required anymore, cubemap already generated
  56. }
  57. else
  58. {
  59. Image img = LoadImage("resources/skybox.png");
  60. skybox.materials[0].maps[MATERIAL_MAP_CUBEMAP].texture = LoadTextureCubemap(img, CUBEMAP_LAYOUT_AUTO_DETECT); // CUBEMAP_LAYOUT_PANORAMA
  61. UnloadImage(img);
  62. }
  63. SetCameraMode(camera, CAMERA_FIRST_PERSON); // Set a first person camera mode
  64. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  65. //--------------------------------------------------------------------------------------
  66. // Main game loop
  67. while (!WindowShouldClose()) // Detect window close button or ESC key
  68. {
  69. // Update
  70. //----------------------------------------------------------------------------------
  71. UpdateCamera(&camera); // Update camera
  72. // Load new cubemap texture on drag&drop
  73. if (IsFileDropped())
  74. {
  75. int count = 0;
  76. char **droppedFiles = GetDroppedFiles(&count);
  77. if (count == 1) // Only support one file dropped
  78. {
  79. if (IsFileExtension(droppedFiles[0], ".png;.jpg;.hdr;.bmp;.tga"))
  80. {
  81. // Unload current cubemap texture and load new one
  82. UnloadTexture(skybox.materials[0].maps[MATERIAL_MAP_CUBEMAP].texture);
  83. if (useHDR)
  84. {
  85. Texture2D panorama = LoadTexture(droppedFiles[0]);
  86. // Generate cubemap from panorama texture
  87. skybox.materials[0].maps[MATERIAL_MAP_CUBEMAP].texture = GenTextureCubemap(shdrCubemap, panorama, 1024, PIXELFORMAT_UNCOMPRESSED_R8G8B8A8);
  88. UnloadTexture(panorama);
  89. }
  90. else
  91. {
  92. Image img = LoadImage(droppedFiles[0]);
  93. skybox.materials[0].maps[MATERIAL_MAP_CUBEMAP].texture = LoadTextureCubemap(img, CUBEMAP_LAYOUT_AUTO_DETECT);
  94. UnloadImage(img);
  95. }
  96. TextCopy(skyboxFileName, droppedFiles[0]);
  97. }
  98. }
  99. ClearDroppedFiles(); // Clear internal buffers
  100. }
  101. //----------------------------------------------------------------------------------
  102. // Draw
  103. //----------------------------------------------------------------------------------
  104. BeginDrawing();
  105. ClearBackground(RAYWHITE);
  106. BeginMode3D(camera);
  107. // We are inside the cube, we need to disable backface culling!
  108. rlDisableBackfaceCulling();
  109. rlDisableDepthMask();
  110. DrawModel(skybox, (Vector3){0, 0, 0}, 1.0f, WHITE);
  111. rlEnableBackfaceCulling();
  112. rlEnableDepthMask();
  113. DrawGrid(10, 1.0f);
  114. EndMode3D();
  115. if (useHDR)
  116. DrawText(TextFormat("Panorama image from hdrihaven.com: %s", GetFileName(skyboxFileName)), 10, GetScreenHeight() - 20, 10, BLACK);
  117. else
  118. 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. for (int i = 0; i < 6; i++)
  165. {
  166. rlSetUniformMatrix(shader.locs[SHADER_LOC_MATRIX_VIEW], fboViews[i]);
  167. rlFramebufferAttach(fbo, cubemap.id, RL_ATTACHMENT_COLOR_CHANNEL0, RL_ATTACHMENT_CUBEMAP_POSITIVE_X + i, 0);
  168. rlEnableFramebuffer(fbo);
  169. rlSetTexture(panorama.id); // WARNING: It must be called after enabling current framebuffer if using internal batch system!
  170. rlClearScreenBuffers();
  171. DrawCubeV(Vector3Zero(), Vector3One(), WHITE);
  172. rlDrawRenderBatchActive();
  173. }
  174. //------------------------------------------------------------------------------------------
  175. // STEP 3: Unload framebuffer and reset state
  176. //------------------------------------------------------------------------------------------
  177. rlDisableShader(); // Unbind shader
  178. rlDisableTexture(); // Unbind texture
  179. rlDisableFramebuffer(); // Unbind framebuffer
  180. rlUnloadFramebuffer(fbo); // Unload framebuffer (and automatically attached depth texture/renderbuffer)
  181. // Reset viewport dimensions to default
  182. rlViewport(0, 0, rlGetFramebufferWidth(), rlGetFramebufferHeight());
  183. rlEnableBackfaceCulling();
  184. //------------------------------------------------------------------------------------------
  185. cubemap.width = size;
  186. cubemap.height = size;
  187. cubemap.mipmaps = 1;
  188. cubemap.format = PIXELFORMAT_UNCOMPRESSED_R32G32B32;
  189. return cubemap;
  190. }