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.

102 line
4.4 KiB

  1. /*******************************************************************************************
  2. *
  3. * raylib [models] example - Skybox loading and drawing
  4. *
  5. * This example has been created using raylib 1.8 (www.raylib.com)
  6. * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
  7. *
  8. * Copyright (c) 2017 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. Texture2D texHDR = LoadTexture("resources/dresden_square.hdr");
  42. // Generate cubemap (texture with 6 quads-cube-mapping) from panorama HDR texture
  43. // NOTE: New texture is generated rendering to texture, shader computes the sphre->cube coordinates mapping
  44. skybox.materials[0].maps[MAP_CUBEMAP].texture = GenTextureCubemap(shdrCubemap, texHDR, 512);
  45. UnloadTexture(texHDR); // Texture not required anymore, cubemap already generated
  46. UnloadShader(shdrCubemap); // Unload cubemap generation shader, not required anymore
  47. SetCameraMode(camera, CAMERA_FIRST_PERSON); // Set a first person camera mode
  48. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  49. //--------------------------------------------------------------------------------------
  50. // Main game loop
  51. while (!WindowShouldClose()) // Detect window close button or ESC key
  52. {
  53. // Update
  54. //----------------------------------------------------------------------------------
  55. UpdateCamera(&camera); // Update camera
  56. //----------------------------------------------------------------------------------
  57. // Draw
  58. //----------------------------------------------------------------------------------
  59. BeginDrawing();
  60. ClearBackground(RAYWHITE);
  61. BeginMode3D(camera);
  62. DrawModel(skybox, (Vector3){0, 0, 0}, 1.0f, WHITE);
  63. DrawGrid(10, 1.0f);
  64. EndMode3D();
  65. DrawFPS(10, 10);
  66. EndDrawing();
  67. //----------------------------------------------------------------------------------
  68. }
  69. // De-Initialization
  70. //--------------------------------------------------------------------------------------
  71. UnloadShader(skybox.materials[0].shader);
  72. UnloadTexture(skybox.materials[0].maps[MAP_CUBEMAP].texture);
  73. UnloadModel(skybox); // Unload skybox model
  74. CloseWindow(); // Close window and OpenGL context
  75. //--------------------------------------------------------------------------------------
  76. return 0;
  77. }