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.

127 lines
5.8 KiB

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