Browse Source

Fixes gl state after HDR texture functions. (#1621)

Updates skybox demo to show how to do both HDR and non HDR skyboxes

Co-authored-by: Jeffery Myers <[email protected]>
Jeffery Myers 4 years ago
parent
commit
0f10c44578

+ 52 - 19
examples/models/models_skybox.c

@@ -10,6 +10,9 @@
 ********************************************************************************************/
 ********************************************************************************************/
 
 
 #include "raylib.h"
 #include "raylib.h"
+#include "rlgl.h"
+
+bool useHDR = false;
 
 
 int main(void)
 int main(void)
 {
 {
@@ -35,7 +38,8 @@ int main(void)
     skybox.materials[0].shader = LoadShader("resources/shaders/glsl100/skybox.vs", "resources/shaders/glsl100/skybox.fs");
     skybox.materials[0].shader = LoadShader("resources/shaders/glsl100/skybox.vs", "resources/shaders/glsl100/skybox.fs");
 #endif
 #endif
     SetShaderValue(skybox.materials[0].shader, GetShaderLocation(skybox.materials[0].shader, "environmentMap"), (int[1]){ MAP_CUBEMAP }, UNIFORM_INT);
     SetShaderValue(skybox.materials[0].shader, GetShaderLocation(skybox.materials[0].shader, "environmentMap"), (int[1]){ MAP_CUBEMAP }, UNIFORM_INT);
-    SetShaderValue(skybox.materials[0].shader, GetShaderLocation(skybox.materials[0].shader, "vflipped"), (int[1]){ 1 }, UNIFORM_INT);
+    SetShaderValue(skybox.materials[0].shader, GetShaderLocation(skybox.materials[0].shader, "doGamma"), (int[1]) { useHDR ? 1 : 0 }, UNIFORM_INT);
+    SetShaderValue(skybox.materials[0].shader, GetShaderLocation(skybox.materials[0].shader, "vflipped"), (int[1]){ useHDR ? 1 : 0 }, UNIFORM_INT);
 
 
     // Load cubemap shader and setup required shader locations
     // Load cubemap shader and setup required shader locations
 #if defined(PLATFORM_DESKTOP)
 #if defined(PLATFORM_DESKTOP)
@@ -45,18 +49,29 @@ int main(void)
 #endif
 #endif
     SetShaderValue(shdrCubemap, GetShaderLocation(shdrCubemap, "equirectangularMap"), (int[1]){ 0 }, UNIFORM_INT);
     SetShaderValue(shdrCubemap, GetShaderLocation(shdrCubemap, "equirectangularMap"), (int[1]){ 0 }, UNIFORM_INT);
 
 
-    // Load HDR panorama (sphere) texture
-    char panoFileName[256] = { 0 };
-    TextCopy(panoFileName, "resources/dresden_square_2k.hdr");
-    Texture2D panorama = LoadTexture(panoFileName);
+    char skyboxFileName[256] = { 0 };
+
+    if (useHDR)
+    {
+        TextCopy(skyboxFileName, "resources/dresden_square_2k.hdr");
+
+        // Load HDR panorama (sphere) texture
+        Texture2D panorama = panorama = LoadTexture(skyboxFileName);
 
 
-    // Generate cubemap (texture with 6 quads-cube-mapping) from panorama HDR texture
-    // NOTE 1: New texture is generated rendering to texture, shader calculates the sphere->cube coordinates mapping
-    // NOTE 2: It seems on some Android devices WebGL, fbo does not properly support a FLOAT-based attachment,
-    // despite texture can be successfully created.. so using UNCOMPRESSED_R8G8B8A8 instead of UNCOMPRESSED_R32G32B32A32
-    skybox.materials[0].maps[MAP_CUBEMAP].texture = GenTextureCubemap(shdrCubemap, panorama, 1024, UNCOMPRESSED_R8G8B8A8);
+        // Generate cubemap (texture with 6 quads-cube-mapping) from panorama HDR texture
+        // NOTE 1: New texture is generated rendering to texture, shader calculates the sphere->cube coordinates mapping
+        // NOTE 2: It seems on some Android devices WebGL, fbo does not properly support a FLOAT-based attachment,
+        // despite texture can be successfully created.. so using UNCOMPRESSED_R8G8B8A8 instead of UNCOMPRESSED_R32G32B32A32
+        skybox.materials[0].maps[MAP_CUBEMAP].texture = GenTextureCubemap(shdrCubemap, panorama, 1024, UNCOMPRESSED_R8G8B8A8);
 
 
-    UnloadTexture(panorama);    // Texture not required anymore, cubemap already generated
+        UnloadTexture(panorama);    // Texture not required anymore, cubemap already generated
+    }
+    else
+    {
+        Image img = LoadImage("resources/skybox.png");
+        skybox.materials[0].maps[MAP_CUBEMAP].texture = LoadTextureCubemap(img, CUBEMAP_AUTO_DETECT);
+        UnloadImage(img);
+    }
 
 
     SetCameraMode(camera, CAMERA_FIRST_PERSON);  // Set a first person camera mode
     SetCameraMode(camera, CAMERA_FIRST_PERSON);  // Set a first person camera mode
 
 
@@ -82,12 +97,22 @@ int main(void)
                 {
                 {
                     // Unload current cubemap texture and load new one
                     // Unload current cubemap texture and load new one
                     UnloadTexture(skybox.materials[0].maps[MAP_CUBEMAP].texture);
                     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, 1024, UNCOMPRESSED_R8G8B8A8);
-                    UnloadTexture(panorama);
+                    if (useHDR)
+                    {
+                        Texture2D panorama = LoadTexture(droppedFiles[0]);
+                        
+                        // Generate cubemap from panorama texture
+                        skybox.materials[0].maps[MAP_CUBEMAP].texture = GenTextureCubemap(shdrCubemap, panorama, 1024, UNCOMPRESSED_R8G8B8A8);
+                        UnloadTexture(panorama);
+                    }
+                    else
+                    {
+                        Image img = LoadImage(droppedFiles[0]);
+                        skybox.materials[0].maps[MAP_CUBEMAP].texture = LoadTextureCubemap(img, CUBEMAP_AUTO_DETECT);
+                        UnloadImage(img);
+                    }
+
+                    TextCopy(skyboxFileName, droppedFiles[0]);
                 }
                 }
             }
             }
 
 
@@ -102,11 +127,19 @@ int main(void)
             ClearBackground(RAYWHITE);
             ClearBackground(RAYWHITE);
 
 
             BeginMode3D(camera);
             BeginMode3D(camera);
-                DrawModel(skybox, (Vector3){0, 0, 0}, 1.0f, WHITE);
+                rlDisableBackfaceCulling();
+                rlDisableDepthMask();
+                    DrawModel(skybox, (Vector3){0, 0, 0}, 1.0f, WHITE);
+                rlEnableBackfaceCulling();
+                rlEnableDepthMask();
                 DrawGrid(10, 1.0f);
                 DrawGrid(10, 1.0f);
             EndMode3D();
             EndMode3D();
 
 
-            DrawText(TextFormat("Panorama image from hdrihaven.com: %s", GetFileName(panoFileName)), 10, GetScreenHeight() - 20, 10, BLACK);
+            if (useHDR)
+                DrawText(TextFormat("Panorama image from hdrihaven.com: %s", GetFileName(skyboxFileName)), 10, GetScreenHeight() - 20, 10, BLACK);
+            else
+                DrawText(TextFormat(": %s", GetFileName(skyboxFileName)), 10, GetScreenHeight() - 20, 10, BLACK);
+
             DrawFPS(10, 10);
             DrawFPS(10, 10);
 
 
         EndDrawing();
         EndDrawing();

+ 6 - 3
examples/models/resources/shaders/glsl100/skybox.fs

@@ -8,6 +8,7 @@ varying vec3 fragPosition;
 // Input uniform values
 // Input uniform values
 uniform samplerCube environmentMap;
 uniform samplerCube environmentMap;
 uniform bool vflipped;
 uniform bool vflipped;
+uniform bool doGamma;
 
 
 void main()
 void main()
 {
 {
@@ -19,9 +20,11 @@ void main()
 
 
     vec3 color = vec3(texelColor.x, texelColor.y, texelColor.z);
     vec3 color = vec3(texelColor.x, texelColor.y, texelColor.z);
     
     
-    // Apply gamma correction
-    color = color/(color + vec3(1.0));
-    color = pow(color, vec3(1.0/2.2));
+    if (doGamma)// Apply gamma correction
+    {
+        color = color/(color + vec3(1.0));
+        color = pow(color, vec3(1.0/2.2));
+    }
 
 
     // Calculate final fragment color
     // Calculate final fragment color
     gl_FragColor = vec4(color, 1.0);
     gl_FragColor = vec4(color, 1.0);

+ 6 - 3
examples/models/resources/shaders/glsl330/skybox.fs

@@ -16,6 +16,7 @@ in vec3 fragPosition;
 // Input uniform values
 // Input uniform values
 uniform samplerCube environmentMap;
 uniform samplerCube environmentMap;
 uniform bool vflipped;
 uniform bool vflipped;
+uniform bool doGamma;
 
 
 // Output fragment color
 // Output fragment color
 out vec4 finalColor;
 out vec4 finalColor;
@@ -28,9 +29,11 @@ void main()
     if (vflipped) color = texture(environmentMap, vec3(fragPosition.x, -fragPosition.y, fragPosition.z)).rgb;
     if (vflipped) color = texture(environmentMap, vec3(fragPosition.x, -fragPosition.y, fragPosition.z)).rgb;
     else color = texture(environmentMap, fragPosition).rgb;
     else color = texture(environmentMap, fragPosition).rgb;
 
 
-    // Apply gamma correction
-    color = color/(color + vec3(1.0));
-    color = pow(color, vec3(1.0/2.2));
+    if (doGamma)// Apply gamma correction
+    { 
+        color = color/(color + vec3(1.0));
+        color = pow(color, vec3(1.0/2.2));
+    }
 
 
     // Calculate final fragment color
     // Calculate final fragment color
     finalColor = vec4(color, 1.0);
     finalColor = vec4(color, 1.0);

BIN
examples/models/resources/skybox.png


+ 3 - 3
src/rlgl.h

@@ -3530,7 +3530,7 @@ TextureCubemap GenTextureCubemap(Shader shader, Texture2D panorama, int size, in
 
 
     // Reset viewport dimensions to default
     // Reset viewport dimensions to default
     rlViewport(0, 0, RLGL.State.framebufferWidth, RLGL.State.framebufferHeight);
     rlViewport(0, 0, RLGL.State.framebufferWidth, RLGL.State.framebufferHeight);
-    //rlEnableBackfaceCulling();
+    rlEnableBackfaceCulling();
     //------------------------------------------------------------------------------------------
     //------------------------------------------------------------------------------------------
 
 
     cubemap.width = size;
     cubemap.width = size;
@@ -3603,7 +3603,7 @@ TextureCubemap GenTextureIrradiance(Shader shader, TextureCubemap cubemap, int s
 
 
     // Reset viewport dimensions to default
     // Reset viewport dimensions to default
     rlViewport(0, 0, RLGL.State.framebufferWidth, RLGL.State.framebufferHeight);
     rlViewport(0, 0, RLGL.State.framebufferWidth, RLGL.State.framebufferHeight);
-    //rlEnableBackfaceCulling();
+    rlEnableBackfaceCulling();
     //------------------------------------------------------------------------------------------
     //------------------------------------------------------------------------------------------
 
 
     irradiance.width = size;
     irradiance.width = size;
@@ -3701,7 +3701,7 @@ TextureCubemap GenTexturePrefilter(Shader shader, TextureCubemap cubemap, int si
 
 
     // Reset viewport dimensions to default
     // Reset viewport dimensions to default
     rlViewport(0, 0, RLGL.State.framebufferWidth, RLGL.State.framebufferHeight);
     rlViewport(0, 0, RLGL.State.framebufferWidth, RLGL.State.framebufferHeight);
-    //rlEnableBackfaceCulling();
+    rlEnableBackfaceCulling();
     //------------------------------------------------------------------------------------------
     //------------------------------------------------------------------------------------------
 
 
     prefilter.width = size;
     prefilter.width = size;