diff --git a/main.c b/main.c index 5f9d4dd..8494bf3 100644 --- a/main.c +++ b/main.c @@ -6,6 +6,7 @@ #include "stdlib.h" #include "stdint.h" #include "time.h" +#include "rlgl.h" //---------------------------------------------------------------------------------- // Defines and Macros @@ -214,6 +215,49 @@ int main(void) int locCutoff = GetShaderLocation(shAlphaClip, "u_alphaCutoff"); float cutoff = 0.3f; // tweak to taste; 0.3 - 0.5 works well for tree PNGs SetShaderValue(shAlphaClip, locCutoff, &cutoff, SHADER_UNIFORM_FLOAT); + + + + // --- Skybox setup --- + // Generate cube mesh + Mesh cube = GenMeshCube(1.0f, 1.0f, 1.0f); + Model skybox = LoadModelFromMesh(cube); + + // Load skybox shader + // Make sure the .vs/.fs are in your resources/shaders/glsl330/ folder + Shader skyboxShader = LoadShader( + "resources/shaders/skybox.vs", + "resources/shaders/skybox.fs" + ); + + // Attach shader to skybox + skybox.materials[0].shader = skyboxShader; + + // Load your cubemap (cross layout image) + Image img = LoadImage("resources/cubemap/cubemap.png"); + // AUTO_DETECT will figure out layout (works for cross 4x3, panorama, etc.) + skybox.materials[0].maps[MATERIAL_MAP_CUBEMAP].texture = + LoadTextureCubemap(img, CUBEMAP_LAYOUT_AUTO_DETECT); + UnloadImage(img); + + // Tell the shader which material map is the environmentMap + int mapType = MATERIAL_MAP_CUBEMAP; + SetShaderValue(skyboxShader, + GetShaderLocation(skyboxShader, "environmentMap"), + &mapType, SHADER_UNIFORM_INT); + + // Flip/gamma options (set to 0 for PNG cross) + int vflipped = 0; + int doGamma = 0; + SetShaderValue(skyboxShader, GetShaderLocation(skyboxShader, "vflipped"), &vflipped, SHADER_UNIFORM_INT); + SetShaderValue(skyboxShader, GetShaderLocation(skyboxShader, "doGamma"), &doGamma, SHADER_UNIFORM_INT); + + // Done – now you can draw it + + + + + //-------------------------------------------------------------------------------------- // Main game loop @@ -333,6 +377,22 @@ int main(void) ClearBackground(BLACK); BeginMode3D(g.player.camera); + + rlDisableBackfaceCulling(); + rlDisableDepthMask(); + + // keep it centered on the camera + skybox.transform = MatrixTranslate( + g.player.camera.position.x, + g.player.camera.position.y, + g.player.camera.position.z + ); + + DrawModel(skybox, (Vector3){0}, 1.0f, WHITE); + + rlEnableDepthMask(); + rlEnableBackfaceCulling(); + DrawLevel(mdlTile); BeginShaderMode(shAlphaClip); diff --git a/resources/cubemap/back.png b/resources/cubemap/back.png new file mode 100644 index 0000000..ad4bfb1 Binary files /dev/null and b/resources/cubemap/back.png differ diff --git a/resources/cubemap/bottom.png b/resources/cubemap/bottom.png new file mode 100644 index 0000000..7806f64 Binary files /dev/null and b/resources/cubemap/bottom.png differ diff --git a/resources/cubemap/cubemap.png b/resources/cubemap/cubemap.png new file mode 100644 index 0000000..6f30378 Binary files /dev/null and b/resources/cubemap/cubemap.png differ diff --git a/resources/cubemap/front.png b/resources/cubemap/front.png new file mode 100644 index 0000000..1d0ea9a Binary files /dev/null and b/resources/cubemap/front.png differ diff --git a/resources/cubemap/left.png b/resources/cubemap/left.png new file mode 100644 index 0000000..aeef23b Binary files /dev/null and b/resources/cubemap/left.png differ diff --git a/resources/cubemap/right.png b/resources/cubemap/right.png new file mode 100644 index 0000000..e6bcb90 Binary files /dev/null and b/resources/cubemap/right.png differ diff --git a/resources/cubemap/top.png b/resources/cubemap/top.png new file mode 100644 index 0000000..85257d3 Binary files /dev/null and b/resources/cubemap/top.png differ diff --git a/resources/shaders/skybox.fs b/resources/shaders/skybox.fs new file mode 100644 index 0000000..d71fef0 --- /dev/null +++ b/resources/shaders/skybox.fs @@ -0,0 +1,30 @@ +#version 330 + +// Input vertex attributes (from vertex shader) +in vec3 fragPosition; + +// Input uniform values +uniform samplerCube environmentMap; +uniform bool vflipped; +uniform bool doGamma; + +// Output fragment color +out vec4 finalColor; + +void main() +{ + // Fetch color from texture map + vec3 color = vec3(0.0); + + if (vflipped) color = texture(environmentMap, vec3(fragPosition.x, -fragPosition.y, fragPosition.z)).rgb; + else color = texture(environmentMap, fragPosition).rgb; + + if (doGamma)// 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/resources/shaders/skybox.vs b/resources/shaders/skybox.vs new file mode 100644 index 0000000..f41d469 --- /dev/null +++ b/resources/shaders/skybox.vs @@ -0,0 +1,24 @@ +#version 330 + +// Input vertex attributes +in vec3 vertexPosition; + +// Input uniform values +uniform mat4 matProjection; +uniform mat4 matView; + +// Output vertex attributes (to fragment shader) +out vec3 fragPosition; + +void main() +{ + // Calculate fragment position based on model transformations + fragPosition = vertexPosition; + + // Remove translation from the view matrix + mat4 rotView = mat4(mat3(matView)); + vec4 clipPos = matProjection*rotView*vec4(vertexPosition, 1.0); + + // Calculate final vertex position + gl_Position = clipPos; +}