models_skybox.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. bool useHDR = false;
  14. int main(void)
  15. {
  16. // Initialization
  17. //--------------------------------------------------------------------------------------
  18. const int screenWidth = 800;
  19. const int screenHeight = 450;
  20. InitWindow(screenWidth, screenHeight, "raylib [models] example - skybox loading and drawing");
  21. // Define the camera to look into our 3d world
  22. Camera camera = { { 1.0f, 1.0f, 1.0f }, { 4.0f, 1.0f, 4.0f }, { 0.0f, 1.0f, 0.0f }, 45.0f, 0 };
  23. // Load skybox model
  24. Mesh cube = GenMeshCube(1.0f, 1.0f, 1.0f);
  25. Model skybox = LoadModelFromMesh(cube);
  26. // Load skybox shader and set required locations
  27. // NOTE: Some locations are automatically set at shader loading
  28. #if defined(PLATFORM_DESKTOP)
  29. skybox.materials[0].shader = LoadShader("resources/shaders/glsl330/skybox.vs", "resources/shaders/glsl330/skybox.fs");
  30. #else // PLATFORM_RPI, PLATFORM_ANDROID, PLATFORM_WEB
  31. skybox.materials[0].shader = LoadShader("resources/shaders/glsl100/skybox.vs", "resources/shaders/glsl100/skybox.fs");
  32. #endif
  33. SetShaderValue(skybox.materials[0].shader, GetShaderLocation(skybox.materials[0].shader, "environmentMap"), (int[1]){ MATERIAL_MAP_CUBEMAP }, SHADER_UNIFORM_INT);
  34. SetShaderValue(skybox.materials[0].shader, GetShaderLocation(skybox.materials[0].shader, "doGamma"), (int[1]) { useHDR ? 1 : 0 }, SHADER_UNIFORM_INT);
  35. SetShaderValue(skybox.materials[0].shader, GetShaderLocation(skybox.materials[0].shader, "vflipped"), (int[1]){ useHDR ? 1 : 0 }, SHADER_UNIFORM_INT);
  36. // Load cubemap shader and setup required shader locations
  37. #if defined(PLATFORM_DESKTOP)
  38. Shader shdrCubemap = LoadShader("resources/shaders/glsl330/cubemap.vs", "resources/shaders/glsl330/cubemap.fs");
  39. #else // PLATFORM_RPI, PLATFORM_ANDROID, PLATFORM_WEB
  40. Shader shdrCubemap = LoadShader("resources/shaders/glsl100/cubemap.vs", "resources/shaders/glsl100/cubemap.fs");
  41. #endif
  42. SetShaderValue(shdrCubemap, GetShaderLocation(shdrCubemap, "equirectangularMap"), (int[1]){ 0 }, SHADER_UNIFORM_INT);
  43. char skyboxFileName[256] = { 0 };
  44. if (useHDR)
  45. {
  46. TextCopy(skyboxFileName, "resources/dresden_square_2k.hdr");
  47. // Load HDR panorama (sphere) texture
  48. Texture2D panorama = panorama = LoadTexture(skyboxFileName);
  49. // Generate cubemap (texture with 6 quads-cube-mapping) from panorama HDR texture
  50. // NOTE 1: New texture is generated rendering to texture, shader calculates the sphere->cube coordinates mapping
  51. // NOTE 2: It seems on some Android devices WebGL, fbo does not properly support a FLOAT-based attachment,
  52. // despite texture can be successfully created.. so using PIXELFORMAT_UNCOMPRESSED_R8G8B8A8 instead of PIXELFORMAT_UNCOMPRESSED_R32G32B32A32
  53. skybox.materials[0].maps[MATERIAL_MAP_CUBEMAP].texture = rlGenTextureCubemap(shdrCubemap, panorama, 1024, PIXELFORMAT_UNCOMPRESSED_R8G8B8A8);
  54. UnloadTexture(panorama); // Texture not required anymore, cubemap already generated
  55. }
  56. else
  57. {
  58. Image img = LoadImage("resources/skybox.png");
  59. skybox.materials[0].maps[MATERIAL_MAP_CUBEMAP].texture = LoadTextureCubemap(img, CUBEMAP_LAYOUT_AUTO_DETECT); // CUBEMAP_LAYOUT_PANORAMA
  60. UnloadImage(img);
  61. }
  62. SetCameraMode(camera, CAMERA_FIRST_PERSON); // Set a first person camera mode
  63. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  64. //--------------------------------------------------------------------------------------
  65. // Main game loop
  66. while (!WindowShouldClose()) // Detect window close button or ESC key
  67. {
  68. // Update
  69. //----------------------------------------------------------------------------------
  70. UpdateCamera(&camera); // Update camera
  71. // Load new cubemap texture on drag&drop
  72. if (IsFileDropped())
  73. {
  74. int count = 0;
  75. char **droppedFiles = GetDroppedFiles(&count);
  76. if (count == 1) // Only support one file dropped
  77. {
  78. if (IsFileExtension(droppedFiles[0], ".png;.jpg;.hdr;.bmp;.tga"))
  79. {
  80. // Unload current cubemap texture and load new one
  81. UnloadTexture(skybox.materials[0].maps[MATERIAL_MAP_CUBEMAP].texture);
  82. if (useHDR)
  83. {
  84. Texture2D panorama = LoadTexture(droppedFiles[0]);
  85. // Generate cubemap from panorama texture
  86. skybox.materials[0].maps[MATERIAL_MAP_CUBEMAP].texture = rlGenTextureCubemap(shdrCubemap, panorama, 1024, PIXELFORMAT_UNCOMPRESSED_R8G8B8A8);
  87. UnloadTexture(panorama);
  88. }
  89. else
  90. {
  91. Image img = LoadImage(droppedFiles[0]);
  92. skybox.materials[0].maps[MATERIAL_MAP_CUBEMAP].texture = LoadTextureCubemap(img, CUBEMAP_LAYOUT_AUTO_DETECT);
  93. UnloadImage(img);
  94. }
  95. TextCopy(skyboxFileName, droppedFiles[0]);
  96. }
  97. }
  98. ClearDroppedFiles(); // Clear internal buffers
  99. }
  100. //----------------------------------------------------------------------------------
  101. // Draw
  102. //----------------------------------------------------------------------------------
  103. BeginDrawing();
  104. ClearBackground(RAYWHITE);
  105. BeginMode3D(camera);
  106. // We are inside the cube, we need to disable backface culling!
  107. rlDisableBackfaceCulling();
  108. rlDisableDepthMask();
  109. DrawModel(skybox, (Vector3){0, 0, 0}, 1.0f, WHITE);
  110. rlEnableBackfaceCulling();
  111. rlEnableDepthMask();
  112. DrawGrid(10, 1.0f);
  113. EndMode3D();
  114. if (useHDR)
  115. DrawText(TextFormat("Panorama image from hdrihaven.com: %s", GetFileName(skyboxFileName)), 10, GetScreenHeight() - 20, 10, BLACK);
  116. else
  117. DrawText(TextFormat(": %s", GetFileName(skyboxFileName)), 10, GetScreenHeight() - 20, 10, BLACK);
  118. DrawFPS(10, 10);
  119. EndDrawing();
  120. //----------------------------------------------------------------------------------
  121. }
  122. // De-Initialization
  123. //--------------------------------------------------------------------------------------
  124. UnloadShader(skybox.materials[0].shader);
  125. UnloadTexture(skybox.materials[0].maps[MATERIAL_MAP_CUBEMAP].texture);
  126. UnloadModel(skybox); // Unload skybox model
  127. CloseWindow(); // Close window and OpenGL context
  128. //--------------------------------------------------------------------------------------
  129. return 0;
  130. }