Variable fog of war
This commit is contained in:
75
main.c
75
main.c
@@ -252,7 +252,43 @@ int main(void)
|
||||
SetShaderValue(skyboxShader, GetShaderLocation(skyboxShader, "vflipped"), &vflipped, SHADER_UNIFORM_INT);
|
||||
SetShaderValue(skyboxShader, GetShaderLocation(skyboxShader, "doGamma"), &doGamma, SHADER_UNIFORM_INT);
|
||||
|
||||
// Done – now you can draw it
|
||||
// Load the fog-of-war shader
|
||||
Shader fog = LoadShader(
|
||||
"resources/shaders/fog.vs",
|
||||
"resources/shaders/fog.fs"
|
||||
);
|
||||
|
||||
// Grab uniform locations (cache them)
|
||||
int locPlayer = GetShaderLocation(fog, "u_playerPos");
|
||||
int locRadius = GetShaderLocation(fog, "u_fogRadius");
|
||||
int locFeather = GetShaderLocation(fog, "u_fogFeather");
|
||||
int locFogColor = GetShaderLocation(fog, "u_fogColor");
|
||||
int locCutoff_fog = GetShaderLocation(fog, "u_alphaCutoff");
|
||||
|
||||
int locInner = GetShaderLocation(fog, "u_inner");
|
||||
int locOuter = GetShaderLocation(fog, "u_outer");
|
||||
int locMinShadow = GetShaderLocation(fog, "u_minShadow");
|
||||
// Optional if you added the sharpness curve in the shader
|
||||
int locSharpness = GetShaderLocation(fog, "u_sharpness");
|
||||
|
||||
|
||||
// Let raylib know which matrices the shader expects
|
||||
fog.locs[SHADER_LOC_MATRIX_MODEL] = GetShaderLocation(fog, "matModel");
|
||||
fog.locs[SHADER_LOC_MATRIX_VIEW] = GetShaderLocation(fog, "matView");
|
||||
fog.locs[SHADER_LOC_MATRIX_PROJECTION] = GetShaderLocation(fog, "matProjection");
|
||||
|
||||
// --- Apply shader to your ground tile model ---
|
||||
mdlTile.materials[0].shader = fog; // same shader also works for opaque
|
||||
// (texture0 already set on mdlTile)
|
||||
|
||||
// Defaults for opaque geometry (no alpha cut)
|
||||
float cutoffOpaque = 0.0f;
|
||||
SetShaderValue(fog, locCutoff_fog, &cutoffOpaque, SHADER_UNIFORM_FLOAT);
|
||||
|
||||
// You’ll reuse the *same* shader for trees (alpha cut).
|
||||
// We won’t assign it to a Model because billboards aren’t models;
|
||||
// instead we’ll render trees inside BeginShaderMode(fog) so they use it.
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -376,30 +412,57 @@ int main(void)
|
||||
{
|
||||
ClearBackground(BLACK);
|
||||
|
||||
// Fog/vignette tuning
|
||||
float inner = 0.0f; // start radius (center under player)
|
||||
float outer = 45.0f; // how far until full fog
|
||||
float minShadow = 0.40f; // baseline darkness at the center (0 = none, 1 = pitch black)
|
||||
float sharpness = 2.0f; // steeper edge (optional if shader uses it)
|
||||
Vector4 fogColor = {0.0f, 0.0f, 0.0f, 1.0f};
|
||||
|
||||
SetShaderValue(fog, locPlayer, &g.player.position, SHADER_UNIFORM_VEC3);
|
||||
SetShaderValue(fog, locInner, &inner, SHADER_UNIFORM_FLOAT);
|
||||
SetShaderValue(fog, locOuter, &outer, SHADER_UNIFORM_FLOAT);
|
||||
SetShaderValue(fog, locMinShadow, &minShadow, SHADER_UNIFORM_FLOAT);
|
||||
SetShaderValue(fog, locFogColor, &fogColor, SHADER_UNIFORM_VEC4);
|
||||
|
||||
// Only if you added sharpness
|
||||
SetShaderValue(fog, locSharpness, &sharpness, SHADER_UNIFORM_FLOAT);
|
||||
|
||||
|
||||
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();
|
||||
rlEnableBackfaceCulling();
|
||||
|
||||
DrawLevel(mdlTile);
|
||||
|
||||
|
||||
/*
|
||||
BeginShaderMode(shAlphaClip);
|
||||
{
|
||||
RenderTrees(&g, tree, treepos, treeSize);
|
||||
}
|
||||
EndShaderMode();
|
||||
*/
|
||||
|
||||
// --- Trees use same fog shader but with alpha-cut enabled ---
|
||||
float cutoffAlpha = 0.3f;
|
||||
SetShaderValue(fog, locCutoff_fog, &cutoffAlpha, SHADER_UNIFORM_FLOAT);
|
||||
BeginShaderMode(fog);
|
||||
RenderTrees(&g, tree, treepos, treeSize);
|
||||
EndShaderMode();
|
||||
|
||||
// Restore opaque cutoff for anything else you draw with that material later
|
||||
SetShaderValue(fog, locCutoff_fog, &cutoffOpaque, SHADER_UNIFORM_FLOAT);
|
||||
|
||||
|
||||
//DrawBillboard(g.player.camera, sign, bbPos, bbSize, WHITE); //This was the texture chasing you.
|
||||
EndMode3D();
|
||||
|
||||
Reference in New Issue
Block a user