2
0

models_skybox.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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()
  13. {
  14. // Initialization
  15. //--------------------------------------------------------------------------------------
  16. int screenWidth = 800;
  17. 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 }, { 0.0f, 0.0f, 0.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. skybox.material.shader = LoadShader("resources/shaders/skybox.vs", "resources/shaders/skybox.fs");
  27. SetShaderValuei(skybox.material.shader, GetShaderLocation(skybox.material.shader, "environmentMap"), (int[1]){ MAP_CUBEMAP }, 1);
  28. // Load cubemap shader and setup required shader locations
  29. Shader shdrCubemap = LoadShader("resources/shaders/cubemap.vs", "resources/shaders/cubemap.fs");
  30. SetShaderValuei(shdrCubemap, GetShaderLocation(shdrCubemap, "equirectangularMap"), (int[1]){ 0 }, 1);
  31. // Load HDR panorama (sphere) texture
  32. Texture2D texHDR = LoadTexture("resources/dresden_square.hdr");
  33. // Generate cubemap (texture with 6 quads-cube-mapping) from panorama HDR texture
  34. // NOTE: New texture is generated rendering to texture, shader computes the sphre->cube coordinates mapping
  35. skybox.material.maps[MAP_CUBEMAP].texture = GenTextureCubemap(shdrCubemap, texHDR, 512);
  36. UnloadTexture(texHDR); // Texture not required anymore, cubemap already generated
  37. UnloadShader(shdrCubemap); // Unload cubemap generation shader, not required anymore
  38. SetCameraMode(camera, CAMERA_FIRST_PERSON); // Set a first person camera mode
  39. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  40. //--------------------------------------------------------------------------------------
  41. // Main game loop
  42. while (!WindowShouldClose()) // Detect window close button or ESC key
  43. {
  44. // Update
  45. //----------------------------------------------------------------------------------
  46. UpdateCamera(&camera); // Update camera
  47. //----------------------------------------------------------------------------------
  48. // Draw
  49. //----------------------------------------------------------------------------------
  50. BeginDrawing();
  51. ClearBackground(RAYWHITE);
  52. BeginMode3D(camera);
  53. DrawModel(skybox, (Vector3){0, 0, 0}, 1.0f, WHITE);
  54. DrawGrid(10, 1.0f);
  55. EndMode3D();
  56. DrawFPS(10, 10);
  57. EndDrawing();
  58. //----------------------------------------------------------------------------------
  59. }
  60. // De-Initialization
  61. //--------------------------------------------------------------------------------------
  62. UnloadModel(skybox); // Unload skybox model (and textures)
  63. CloseWindow(); // Close window and OpenGL context
  64. //--------------------------------------------------------------------------------------
  65. return 0;
  66. }