Cleaning up code.

This commit is contained in:
2025-08-31 19:02:18 -05:00
parent b580a34999
commit a1fede92f3

148
main.c
View File

@@ -5,7 +5,6 @@
#include "stddef.h"
#include "stdlib.h"
#include "stdint.h"
#include "time.h"
#include "rlgl.h"
//----------------------------------------------------------------------------------
@@ -71,9 +70,6 @@ static inline float hash01_to_m05p05(uint32_t h_bits)
//----------------------------------------------------------------------------------
//Defining program structure
//----------------------------------------------------------------------------------
//bob
typedef struct
{
Camera camera; // <- now part of the game state
@@ -99,21 +95,6 @@ typedef struct
Player player;
} Master;
// GLSL 330 core fragment shader (desktop)
// Discards fragments with alpha below a threshold so depth is only written on real pixels.
static const char *ALPHA_CLIP_FS =
"#version 330 core\n"
"in vec2 fragTexCoord;\n"
"in vec4 fragColor;\n"
"uniform sampler2D texture0;\n"
"uniform float u_alphaCutoff; // e.g., 0.3\n"
"out vec4 finalColor;\n"
"void main(){\n"
" vec4 texel = texture(texture0, fragTexCoord);\n"
" if(texel.a < u_alphaCutoff) discard;\n"
" finalColor = texel * fragColor;\n"
"}\n";
//------------------------------------------------------------------------------------
// Function Initation
//------------------------------------------------------------------------------------
@@ -154,7 +135,7 @@ int main(void)
g.state = STATE_MENU;
g.shouldExit = 0;
//Define
//Define player values
g.player.sensitivity = (Vector2){ 0.001f, 0.001f };
g.player.lookRotation = (Vector2){ 0 };
g.player.headTimer = 0.0f;
@@ -169,62 +150,55 @@ int main(void)
g.player.position.y + (BOTTOM_HEIGHT + g.player.headLerp),
g.player.position.z,
};
//g.player.camera.target = (Vector3){ 0.0f, 0.0f, 1.0f }; // Camera target
g.player.camera.up = (Vector3){ 0.0f, 1.0f, 0.0f }; // Up vector
g.player.camera.up = (Vector3){ 0.0f, 1.0f, 0.0f };
g.player.camera.fovy = 60.0f;
g.player.camera.projection = CAMERA_PERSPECTIVE;
g.player.cameraMode = CAMERA_FIRST_PERSON;
UpdateCameraAngle(&g);
/*
// Load GLTF model (no animations)
//Model model = LoadModel("resources/3d_models/bad_sphere.glb");
//Vector3 position = { 0.0f, 0.0f, 0.0f };
// Load a 2D image as texture for the billboard
//Billboards always point toward the active camera.
Texture2D sign = LoadTexture("resources/sprites/diard-charles.jpg");
GenTextureMipmaps(&sign);
SetTextureFilter(sign, TEXTURE_FILTER_BILINEAR); // optional but looks nicer
Vector3 bbPos = (Vector3){ 1.0f, 2.5f, 1.0f }; // where in 3D world to place it
float bbSize = 5.0f; // billboard height in world units
*/
// Load a 2D image as texture for the billboard
//Billboards always point toward the active camera.
Texture2D tree = LoadTexture("resources/sprites/tree.png");
GenTextureMipmaps(&tree);
SetTextureFilter(tree, TEXTURE_FILTER_BILINEAR); // optional but looks nicer
Vector3 treepos = (Vector3){ 0.0f, 0.0f, 0.0f }; // where in 3D world to place it
float treeSize = 15.0f; // billboard height in world units
Vector3 treepos = (Vector3){ 0.0f, 0.0f, 0.0f }; // where in 3D world to place it
float treeSize = 15.0f; // billboard height in world units
float cutoffAlpha = 0.3f; // Threshold for transparency cut off.
// --- Grass tile setup ---
Texture2D texGrass = LoadTexture("resources/textures/grass_textures/Grass_01.png"); // adjust path
// Grass tile setup
Texture2D texGrass = LoadTexture("resources/textures/grass_textures/Grass_01.png");
GenTextureMipmaps(&texGrass);
SetTextureFilter(texGrass, TEXTURE_FILTER_TRILINEAR); // nicer at angles
SetTextureWrap(texGrass, TEXTURE_WRAP_REPEAT);
// A 3D plane (centered at origin) of size tileSize x tileSize
const float tileSize = 5.0f; // match your DrawLevel tileSize
const float tileSize = TILE_SIZE;
Mesh meshTile = GenMeshPlane(tileSize, tileSize, 1, 1);
Model mdlTile = LoadModelFromMesh(meshTile);
mdlTile.materials[0].maps[MATERIAL_MAP_ALBEDO].texture = texGrass;
Shader shAlphaClip = LoadShaderFromMemory(NULL, ALPHA_CLIP_FS);
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"
@@ -232,31 +206,22 @@ int main(void)
// 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);
skybox.materials[0].maps[MATERIAL_MAP_CUBEMAP].texture = LoadTextureCubemap(img, CUBEMAP_LAYOUT_AUTO_DETECT);
UnloadImage(img); //no longer need image loaded.
// Tell the shader which material map is the environmentMap
int mapType = MATERIAL_MAP_CUBEMAP;
SetShaderValue(skyboxShader,
GetShaderLocation(skyboxShader, "environmentMap"),
&mapType, SHADER_UNIFORM_INT);
SetShaderValue(skyboxShader, GetShaderLocation(skyboxShader, "environmentMap"), &mapType, SHADER_UNIFORM_INT);
// Flip/gamma options (set to 0 for PNG cross)
//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);
// Load the fog-of-war shader
Shader fog = LoadShader(
"resources/shaders/fog.vs",
"resources/shaders/fog.fs"
);
Shader fog = LoadShader("resources/shaders/fog.vs", "resources/shaders/fog.fs");
// Grab uniform locations (cache them)
int locPlayer = GetShaderLocation(fog, "u_playerPos");
@@ -271,7 +236,6 @@ int main(void)
// 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");
@@ -279,21 +243,24 @@ int main(void)
// --- 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);
// Youll reuse the *same* shader for trees (alpha cut).
// We wont assign it to a Model because billboards arent models;
// instead well render trees inside BeginShaderMode(fog) so they use it.
// Fog/vignette tuning
float inner = 0.0f; // start radius (center under player)
float outer = 45.0f; // how far until full fog
float minShadow = 0.45f; // 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, 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);
SetShaderValue(fog, locSharpness, &sharpness, SHADER_UNIFORM_FLOAT);
//--------------------------------------------------------------------------------------
// Main game loop
@@ -378,17 +345,6 @@ int main(void)
g.player.lean.y = Lerp(g.player.lean.y, forward*0.015f, 10.0f*delta);
UpdateCameraAngle(&g);
/*
int bbdifx = g.player.position.x - bbPos.x;
int bbdifz = g.player.position.z - bbPos.z;
bbPos.x += 0.01 * bbdifx;
bbPos.z += 0.01 * bbdifz;
*/
break;
};
}
@@ -412,69 +368,50 @@ 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();
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);
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();
// Draw info box
DrawRectangle(5, 5, 330, 75, Fade(SKYBLUE, 0.5f));
DrawRectangleLines(5, 5, 330, 75, BLUE);
DrawRectangle(5, 5, 330*1.5, 75*1.25, Fade(SKYBLUE, 0.5f));
DrawRectangleLines(5, 5, 330*1.5, 75*1.25, BLUE);
DrawText("Camera controls:", 15, 15, 10, BLACK);
DrawText("- Move keys: W, A, S, D, Space, Left-Ctrl", 15, 30, 10, BLACK);
DrawText("- Look around: arrow keys or mouse", 15, 45, 10, BLACK);
DrawText(TextFormat("- Velocity Len: (%06.3f)", Vector2Length((Vector2){ g.player.velocity.x, g.player.velocity.z })), 15, 60, 10, BLACK);
DrawText("Camera controls:", 15, 15, 20, BLACK);
DrawText("- Move keys: W, A, S, D, Space, Left-Ctrl", 15, 30, 20, BLACK);
DrawText("- Look around: arrow keys or mouse", 15, 45, 20, BLACK);
DrawText(TextFormat("- Velocity Len: (%06.3f)", Vector2Length((Vector2){ g.player.velocity.x, g.player.velocity.z })), 15, 60, 20, BLACK);
break;
}
@@ -482,8 +419,8 @@ int main(void)
EndDrawing();
//----------------------------------------------------------------------------------
};
//--------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------
// De-Initialization
//--------------------------------------------------------------------------------------
// Detach the texture from the model so UnloadModel won't try to free it.
@@ -492,7 +429,6 @@ int main(void)
// Now free resources you loaded:
UnloadModel(mdlTile); // frees the model and its mesh/VBOs
UnloadTexture(texGrass); // frees the grass texture you loaded
UnloadTexture(sign); // frees billboard texture
UnloadTexture(tree); // frees tree texture
CloseWindow(); // Close window and OpenGL context