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 | |
12 | #include "raylib.h" |
13 | |
14 | int main(void) |
15 | { |
16 | // Initialization |
17 | //-------------------------------------------------------------------------------------- |
18 | const int screenWidth = 800; |
19 | const int screenHeight = 450; |
20 | |
21 | InitWindow(screenWidth, screenHeight, "raylib [models] example - skybox loading and drawing" ); |
22 | |
23 | // Define the camera to look into our 3d world |
24 | Camera camera = { { 1.0f, 1.0f, 1.0f }, { 4.0f, 1.0f, 4.0f }, { 0.0f, 1.0f, 0.0f }, 45.0f, 0 }; |
25 | |
26 | // Load skybox model |
27 | Mesh cube = GenMeshCube(1.0f, 1.0f, 1.0f); |
28 | Model skybox = LoadModelFromMesh(cube); |
29 | |
30 | // Load skybox shader and set required locations |
31 | // NOTE: Some locations are automatically set at shader loading |
32 | #if defined(PLATFORM_DESKTOP) |
33 | skybox.materials[0].shader = LoadShader("resources/shaders/glsl330/skybox.vs" , "resources/shaders/glsl330/skybox.fs" ); |
34 | #else // PLATFORM_RPI, PLATFORM_ANDROID, PLATFORM_WEB |
35 | skybox.materials[0].shader = LoadShader("resources/shaders/glsl100/skybox.vs" , "resources/shaders/glsl100/skybox.fs" ); |
36 | #endif |
37 | SetShaderValue(skybox.materials[0].shader, GetShaderLocation(skybox.materials[0].shader, "environmentMap" ), (int[1]){ MAP_CUBEMAP }, UNIFORM_INT); |
38 | |
39 | // Load cubemap shader and setup required shader locations |
40 | #if defined(PLATFORM_DESKTOP) |
41 | Shader shdrCubemap = LoadShader("resources/shaders/glsl330/cubemap.vs" , "resources/shaders/glsl330/cubemap.fs" ); |
42 | #else // PLATFORM_RPI, PLATFORM_ANDROID, PLATFORM_WEB |
43 | Shader shdrCubemap = LoadShader("resources/shaders/glsl100/cubemap.vs" , "resources/shaders/glsl100/cubemap.fs" ); |
44 | #endif |
45 | SetShaderValue(shdrCubemap, GetShaderLocation(shdrCubemap, "equirectangularMap" ), (int[1]){ 0 }, UNIFORM_INT); |
46 | |
47 | // Load HDR panorama (sphere) texture |
48 | Texture2D texHDR = LoadTexture("resources/dresden_square.hdr" ); |
49 | |
50 | // Generate cubemap (texture with 6 quads-cube-mapping) from panorama HDR texture |
51 | // NOTE: New texture is generated rendering to texture, shader computes the sphre->cube coordinates mapping |
52 | skybox.materials[0].maps[MAP_CUBEMAP].texture = GenTextureCubemap(shdrCubemap, texHDR, 512); |
53 | |
54 | UnloadTexture(texHDR); // Texture not required anymore, cubemap already generated |
55 | UnloadShader(shdrCubemap); // Unload cubemap generation shader, not required anymore |
56 | |
57 | SetCameraMode(camera, CAMERA_FIRST_PERSON); // Set a first person camera mode |
58 | |
59 | SetTargetFPS(60); // Set our game to run at 60 frames-per-second |
60 | //-------------------------------------------------------------------------------------- |
61 | |
62 | // Main game loop |
63 | while (!WindowShouldClose()) // Detect window close button or ESC key |
64 | { |
65 | // Update |
66 | //---------------------------------------------------------------------------------- |
67 | UpdateCamera(&camera); // Update camera |
68 | //---------------------------------------------------------------------------------- |
69 | |
70 | // Draw |
71 | //---------------------------------------------------------------------------------- |
72 | BeginDrawing(); |
73 | |
74 | ClearBackground(RAYWHITE); |
75 | |
76 | BeginMode3D(camera); |
77 | |
78 | DrawModel(skybox, (Vector3){0, 0, 0}, 1.0f, WHITE); |
79 | |
80 | DrawGrid(10, 1.0f); |
81 | |
82 | EndMode3D(); |
83 | |
84 | DrawFPS(10, 10); |
85 | |
86 | EndDrawing(); |
87 | //---------------------------------------------------------------------------------- |
88 | } |
89 | |
90 | // De-Initialization |
91 | //-------------------------------------------------------------------------------------- |
92 | UnloadShader(skybox.materials[0].shader); |
93 | UnloadTexture(skybox.materials[0].maps[MAP_CUBEMAP].texture); |
94 | |
95 | UnloadModel(skybox); // Unload skybox model |
96 | |
97 | CloseWindow(); // Close window and OpenGL context |
98 | //-------------------------------------------------------------------------------------- |
99 | |
100 | return 0; |
101 | } |
102 | |