diff --git a/examples/models/models_skybox.c b/examples/models/models_skybox.c new file mode 100644 index 000000000..e07c9cfa5 --- /dev/null +++ b/examples/models/models_skybox.c @@ -0,0 +1,94 @@ +/******************************************************************************************* +* +* raylib [models] example - Skybox loading and drawing +* +* This example has been created using raylib 1.8 (www.raylib.com) +* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details) +* +* Copyright (c) 2017 Ramon Santamaria (@raysan5) +* +********************************************************************************************/ + +#include "raylib.h" +#include "raymath.h" + +int main() +{ + // Initialization + //-------------------------------------------------------------------------------------- + int screenWidth = 800; + int screenHeight = 450; + + InitWindow(screenWidth, screenHeight, "raylib [models] example - skybox loading and drawing"); + + // Define the camera to look into our 3d world + Camera camera = {{ 1.0f, 1.0f, 1.0f }, { 0.0f, 0.0f, 0.0f }, { 0.0f, 1.0f, 0.0f }, 45.0f }; + + // Load skybox model and shader + #define PATH_SKYBOX_VS "resources/shaders/skybox.vs" // Path to skybox vertex shader + #define PATH_SKYBOX_FS "resources/shaders/skybox.fs" // Path to skybox vertex shader + #define PATH_TEXTURES_HDR "resources/pinetree.hdr" + #define LOC_CUSTOM_SKYRESOLUTION 15 + + Mesh cube = GenMeshCube(1.0f, 1.0f, 1.0f); + Model skybox = LoadModelFromMesh(cube, false); + skybox.material.shader = LoadShader(PATH_SKYBOX_VS, PATH_SKYBOX_FS); + + Texture2D texHDR = LoadTexture(PATH_TEXTURES_HDR); + skybox.material.maps[TEXMAP_CUBEMAP].tex = rlGenMapCubemap(texHDR, 512); + SetShaderValuei(skybox.material.shader, GetShaderLocation(skybox.material.shader, "environmentMap"), (int[1]){ TEXMAP_CUBEMAP }, 1); + + // Get skybox shader locations + int skyProjectionLoc = GetShaderLocation(skybox.material.shader, "projection"); + skybox.material.shader.locs[LOC_MATRIX_VIEW] = GetShaderLocation(skybox.material.shader, "view"); + + // Then before rendering, configure the viewport to the actual screen dimensions + Matrix proj = MatrixPerspective(45.0, (double)GetScreenWidth()/(double)GetScreenHeight(), 0.01, 1000.0); + MatrixTranspose(&proj); + SetShaderValueMatrix(skybox.material.shader, skyProjectionLoc, proj); + + SetCameraMode(camera, CAMERA_ORBITAL); // Set an orbital camera mode + + SetTargetFPS(60); // Set our game to run at 60 frames-per-second + //-------------------------------------------------------------------------------------- + + // Main game loop + while (!WindowShouldClose()) // Detect window close button or ESC key + { + // Update + //---------------------------------------------------------------------------------- + UpdateCamera(&camera); // Update camera + + Matrix view = MatrixLookAt(camera.position, camera.target, camera.up); + SetShaderValueMatrix(skybox.material.shader, skybox.material.shader.locs[LOC_MATRIX_VIEW], view); + //---------------------------------------------------------------------------------- + + // Draw + //---------------------------------------------------------------------------------- + BeginDrawing(); + + ClearBackground(RAYWHITE); + + Begin3dMode(camera); + + DrawModel(skybox, VectorZero(), 1.0f, RED); + + DrawGrid(10, 1.0f); + + End3dMode(); + + DrawFPS(10, 10); + + EndDrawing(); + //---------------------------------------------------------------------------------- + } + + // De-Initialization + //-------------------------------------------------------------------------------------- + UnloadModel(skybox); // Unload skybox model + + CloseWindow(); // Close window and OpenGL context + //-------------------------------------------------------------------------------------- + + return 0; +} diff --git a/examples/models/resources/pinetree.hdr b/examples/models/resources/pinetree.hdr new file mode 100644 index 000000000..9d4306e08 Binary files /dev/null and b/examples/models/resources/pinetree.hdr differ diff --git a/examples/models/resources/shaders/cubemap.fs b/examples/models/resources/shaders/cubemap.fs new file mode 100644 index 000000000..09ae62f58 --- /dev/null +++ b/examples/models/resources/shaders/cubemap.fs @@ -0,0 +1,38 @@ +/******************************************************************************************* +* +* rPBR [shader] - Equirectangular to cubemap fragment shader +* +* Copyright (c) 2017 Victor Fisac +* +**********************************************************************************************/ + +#version 330 + +// Input vertex attributes (from vertex shader) +in vec3 fragPos; + +// Input uniform values +uniform sampler2D equirectangularMap; + +// Output fragment color +out vec4 finalColor; + +vec2 SampleSphericalMap(vec3 v) +{ + vec2 uv = vec2(atan(v.z, v.x), asin(v.y)); + uv *= vec2(0.1591, 0.3183); + uv += 0.5; + return uv; +} + +void main() +{ + // Normalize local position + vec2 uv = SampleSphericalMap(normalize(fragPos)); + + // Fetch color from texture map + vec3 color = texture(equirectangularMap, uv).rgb; + + // Calculate final fragment color + finalColor = vec4(color, 1.0); +} diff --git a/examples/models/resources/shaders/cubemap.vs b/examples/models/resources/shaders/cubemap.vs new file mode 100644 index 000000000..6e0bf4e10 --- /dev/null +++ b/examples/models/resources/shaders/cubemap.vs @@ -0,0 +1,28 @@ +/******************************************************************************************* +* +* rPBR [shader] - Equirectangular to cubemap vertex shader +* +* Copyright (c) 2017 Victor Fisac +* +**********************************************************************************************/ + +#version 330 + +// Input vertex attributes +in vec3 vertexPosition; + +// Input uniform values +uniform mat4 projection; +uniform mat4 view; + +// Output vertex attributes (to fragment shader) +out vec3 fragPos; + +void main() +{ + // Calculate fragment position based on model transformations + fragPos = vertexPosition; + + // Calculate final vertex position + gl_Position = projection*view*vec4(vertexPosition, 1.0); +} diff --git a/examples/models/resources/shaders/skybox.fs b/examples/models/resources/shaders/skybox.fs new file mode 100644 index 000000000..0bd2f320c --- /dev/null +++ b/examples/models/resources/shaders/skybox.fs @@ -0,0 +1,31 @@ +/******************************************************************************************* +* +* rPBR [shader] - Background skybox fragment shader +* +* Copyright (c) 2017 Victor Fisac +* +**********************************************************************************************/ + +#version 330 + +// Input vertex attributes (from vertex shader) +in vec3 fragPos; + +// Input uniform values +uniform samplerCube environmentMap; + +// Output fragment color +out vec4 finalColor; + +void main() +{ + // Fetch color from texture map + vec3 color = texture(environmentMap, fragPos).rgb; + + // Apply gamma correction + color = color/(color + vec3(1.0)); + color = pow(color, vec3(1.0/2.2)); + + // Calculate final fragment color + finalColor = vec4(color, 1.0); +} diff --git a/examples/models/resources/shaders/skybox.vs b/examples/models/resources/shaders/skybox.vs new file mode 100644 index 000000000..f40d615c7 --- /dev/null +++ b/examples/models/resources/shaders/skybox.vs @@ -0,0 +1,32 @@ +/******************************************************************************************* +* +* rPBR [shader] - Background skybox vertex shader +* +* Copyright (c) 2017 Victor Fisac +* +**********************************************************************************************/ + +#version 330 + +// Input vertex attributes +in vec3 vertexPosition; + +// Input uniform values +uniform mat4 projection; +uniform mat4 view; + +// Output vertex attributes (to fragment shader) +out vec3 fragPos; + +void main() +{ + // Calculate fragment position based on model transformations + fragPos = vertexPosition; + + // Remove translation from the view matrix + mat4 rotView = mat4(mat3(view)); + vec4 clipPos = projection*rotView*vec4(vertexPosition, 1.0); + + // Calculate final vertex position + gl_Position = clipPos.xyww; +}