瀏覽代碼

REVIEWED: models_skybox example

Now supports dynamic panoramic view, just drag and drop
raysan5 5 年之前
父節點
當前提交
8d41683917

+ 32 - 9
examples/models/models_skybox.c

@@ -2,10 +2,10 @@
 *
 *   raylib [models] example - Skybox loading and drawing
 *
-*   This example has been created using raylib 1.8 (www.raylib.com)
+*   This example has been created using raylib 3.1 (www.raylib.com)
 *   raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
 *
-*   Copyright (c) 2017 Ramon Santamaria (@raysan5)
+*   Copyright (c) 2017-2020 Ramon Santamaria (@raysan5)
 *
 ********************************************************************************************/
 
@@ -46,14 +46,15 @@ int main(void)
     SetShaderValue(shdrCubemap, GetShaderLocation(shdrCubemap, "equirectangularMap"), (int[1]){ 0 }, UNIFORM_INT);
 
     // Load HDR panorama (sphere) texture
-    Texture2D texHDR = LoadTexture("resources/dresden_square.hdr");
+    char panoFileName[256] = { 0 };
+    TextCopy(panoFileName, "resources/dresden_square_2k.hdr");
+    Texture2D panorama = LoadTexture(panoFileName);
 
     // Generate cubemap (texture with 6 quads-cube-mapping) from panorama HDR texture
     // NOTE: New texture is generated rendering to texture, shader computes the sphre->cube coordinates mapping
-    skybox.materials[0].maps[MAP_CUBEMAP].texture = GenTextureCubemap(shdrCubemap, texHDR, 512);
+    skybox.materials[0].maps[MAP_CUBEMAP].texture = GenTextureCubemap(shdrCubemap, panorama, 1024);
 
-    UnloadTexture(texHDR);      // Texture not required anymore, cubemap already generated
-    UnloadShader(shdrCubemap);  // Unload cubemap generation shader, not required anymore
+    UnloadTexture(panorama);    // Texture not required anymore, cubemap already generated
 
     SetCameraMode(camera, CAMERA_FIRST_PERSON);  // Set a first person camera mode
 
@@ -66,6 +67,30 @@ int main(void)
         // Update
         //----------------------------------------------------------------------------------
         UpdateCamera(&camera);              // Update camera
+        
+        // Load new cubemap texture on drag&drop
+        if (IsFileDropped())
+        {
+            int count = 0;
+            char **droppedFiles = GetDroppedFiles(&count);
+
+            if (count == 1)         // Only support one file dropped
+            {
+                if (IsFileExtension(droppedFiles[0], ".png;.jpg;.hdr;.bmp;.tga"))
+                {
+                    // Unload current cubemap texture and load new one
+                    UnloadTexture(skybox.materials[0].maps[MAP_CUBEMAP].texture);
+                    panorama = LoadTexture(droppedFiles[0]);
+                    TextCopy(panoFileName, droppedFiles[0]);
+                    
+                    // Generate cubemap from panorama texture
+                    skybox.materials[0].maps[MAP_CUBEMAP].texture = GenTextureCubemap(shdrCubemap, panorama, 512);
+                    UnloadTexture(panorama);
+                }
+            }
+
+            ClearDroppedFiles();    // Clear internal buffers
+        }
         //----------------------------------------------------------------------------------
 
         // Draw
@@ -75,13 +100,11 @@ int main(void)
             ClearBackground(RAYWHITE);
 
             BeginMode3D(camera);
-
                 DrawModel(skybox, (Vector3){0, 0, 0}, 1.0f, WHITE);
-
                 DrawGrid(10, 1.0f);
-
             EndMode3D();
 
+            DrawText(TextFormat("Panorama image from hdrihaven.com: %s", GetFileName(panoFileName)), 10, GetScreenHeight() - 20, 10, BLACK);
             DrawFPS(10, 10);
 
         EndDrawing();

+ 0 - 0
examples/models/resources/dresden_square.hdr → examples/models/resources/dresden_square_1k.hdr


二進制
examples/models/resources/dresden_square_2k.hdr