diff options
Diffstat (limited to 'platformer/src/render.c')
| -rw-r--r-- | platformer/src/render.c | 82 |
1 files changed, 69 insertions, 13 deletions
diff --git a/platformer/src/render.c b/platformer/src/render.c index a91b561..381a018 100644 --- a/platformer/src/render.c +++ b/platformer/src/render.c @@ -7,7 +7,8 @@ * player's drop shadow. * * FITS IN: Implements render.h. Reads Camera (camera.h), Player (player.h) - * and world_half_extent()/world_ground_height() (world.h). Uses the + * and the level (world_half_extent(), world_box_count()/world_box() + * and world_floor_below() from world.h). Uses the * GL functions that window.c loaded through GLEW. * * IMPLEMENT: @@ -26,22 +27,35 @@ * what makes motion readable in a blank world, so explain that in a comment. * 2. Unit cube: 36 vertices (GL_TRIANGLES), spanning x,z in [-0.5, 0.5] * and y in [0, 1] so its origin is at the BOTTOM center, matching - * Body.pos being the feet. + * Body.pos being the feet. Its faces are stored in a fixed order + * (-X, +X, -Y, +Y, -Z, +Z), 6 vertices each, so a single face can be + * drawn on its own. * 3. Shadow disc: a GL_TRIANGLE_FAN circle of radius 1 in the XZ plane * (~24 segments). * * - render_frame(cam, player, aspect): * 1. glClearColor(SKY_COLOR_R/G/B), clear color + depth. * 2. vp = camera_projection(cam, aspect) * camera_view(cam). - * 3. Grid: u_mvp = vp. Draw minor lines in a dim color, major lines brighter. - * 4. Shadow (drawn BEFORE the player): - * model = translate(player x, ground_y + 0.01, player z) * scale(r, 1, r) + * 3. Ground: the cube scaled into a flat slab whose top is y = 0, drawn + * first WITHOUT depth writes (everything else is on or above it, so it + * never hides anything, and grid lines on its surface can't z-fight it). + * 4. Grid: u_mvp = vp. Draw minor lines in a dim color, major lines brighter. + * 5. Platforms: each world_box() is the cube scaled to the box. Draw it + * face by face with a fixed brightness per face (top brightest, bottom + * darkest, sides in between and different from each other) times the + * platform's color from a small palette. WHY: with no lighting, a + * one-color box is a flat silhouette and you can't tell its top from its + * sides, which makes jumps impossible to judge. Per-face shading is + * the cheapest way to read shape, and it needs no lighting code. + * 6. Shadow (drawn BEFORE the player): + * model = translate(player x, floor_y + 0.01, player z) * scale(r, 1, r) * with r around PLAYER_HALF_WIDTH * 1.2. Dark color. The +0.01 lift * stops z-fighting with the ground. Comment WHY the shadow exists: * in SM64 the blob under Mario is the main cue for where you'll land. * Without it, jumps in 3D are very hard to judge. - * ground_y = world_ground_height(player x, player z). - * 5. Player box: + * floor_y = world_floor_below(body.pos, body.half_width): the surface + * the player would land on, whether that's the ground or a platform. + * 7. Player box: * model = translate(body.pos) * rotate_y(facing_yaw) * * scale(2*PLAYER_HALF_WIDTH, PLAYER_HEIGHT, 2*PLAYER_HALF_WIDTH) * Bright color (e.g. red). Also draw a small second box as a "nose" on @@ -52,7 +66,8 @@ * - render_shutdown: delete the VAOs, VBOs and the program. * * CONVENTIONS: `s_` statics for GL handles. Model matrices built from - * math3d.h helpers. There's no lighting, so flat colors are fine. + * math3d.h helpers. There's no lighting: flat colors, plus the + * fixed per-face shading on platforms. * * NOT IN SCOPE: Lighting, textures, models, text/HUD, anti-aliasing, * interpolating between ticks (a README roadmap item). @@ -90,6 +105,21 @@ static const float COLOR_SHADOW[4] = { 0.12f, 0.16f, 0.12f, 1.0f }; static const float COLOR_PLAYER[4] = { 0.85f, 0.15f, 0.15f, 1.0f }; static const float COLOR_NOSE[4] = { 1.00f, 0.85f, 0.20f, 1.0f }; +/* Platform colors, picked per platform by index so neighbors vary. */ +static const float PLATFORM_PALETTE[][3] = { + { 0.80f, 0.72f, 0.55f }, /* sandstone */ + { 0.62f, 0.66f, 0.74f }, /* slate */ + { 0.78f, 0.55f, 0.42f }, /* terracotta */ + { 0.70f, 0.70f, 0.66f }, /* concrete */ +}; +#define PLATFORM_PALETTE_SIZE ((int)(sizeof PLATFORM_PALETTE / sizeof PLATFORM_PALETTE[0])) + +/* Brightness per cube face, in the cube's face order (-X, +X, -Y, +Y, -Z, +Z). + * Top brightest, bottom darkest, and no two touching sides the same, so the + * box's shape reads without any lighting. */ +static const float FACE_SHADE[6] = { 0.70f, 0.82f, 0.45f, 1.00f, 0.60f, 0.90f }; +#define CUBE_FACE_VERTS 6 + /* A VAO + VBO of positions only (3 floats per vertex). */ typedef struct { GLuint vao, vbo; @@ -306,15 +336,40 @@ static void draw_grid(Mat4 vp) glDrawArrays(GL_LINES, s_grid_minor_count, s_grid.vertex_count - s_grid_minor_count); } +/* One platform, drawn face by face with FACE_SHADE so its top and sides + * read as different surfaces (see the prompt above for why). */ +static void draw_platform(Mat4 vp, Box box, const float rgb[3]) +{ + Vec3 size = vec3_sub(box.max, box.min); + Vec3 bottom = vec3_make(0.5f * (box.min.x + box.max.x), box.min.y, + 0.5f * (box.min.z + box.max.z)); + Mat4 mvp = mat4_mul(vp, mat4_mul(mat4_translate(bottom), mat4_scale(size))); + + glBindVertexArray(s_cube.vao); + for (int face = 0; face < 6; face++) { + float s = FACE_SHADE[face]; + float color[4] = { rgb[0] * s, rgb[1] * s, rgb[2] * s, 1.0f }; + set_uniforms(mvp, color); + glDrawArrays(GL_TRIANGLES, face * CUBE_FACE_VERTS, CUBE_FACE_VERTS); + } +} + +static void draw_platforms(Mat4 vp) +{ + for (int i = 0; i < world_box_count(); i++) + draw_platform(vp, world_box(i), PLATFORM_PALETTE[i % PLATFORM_PALETTE_SIZE]); +} + /* The drop shadow. In SM64 the blob under Mario is the main cue for where * you'll land: without it, depth is very hard to judge in 3D and jumps become - * guesswork. It sits on the GROUND below the player, not at the player's height. */ + * guesswork. It sits on the surface BELOW the player (the ground or a platform + * top), not at the player's height. */ static void draw_shadow(Mat4 vp, const Player *player) { - Vec3 pos = player->body.pos; - float ground_y = world_ground_height(pos.x, pos.z); - float r = PLAYER_HALF_WIDTH * SHADOW_RADIUS_SCALE; - Mat4 model = mat4_mul(mat4_translate(vec3_make(pos.x, ground_y + SHADOW_LIFT, pos.z)), + Vec3 pos = player->body.pos; + float floor_y = world_floor_below(pos, player->body.half_width); + float r = PLAYER_HALF_WIDTH * SHADOW_RADIUS_SCALE; + Mat4 model = mat4_mul(mat4_translate(vec3_make(pos.x, floor_y + SHADOW_LIFT, pos.z)), mat4_scale(vec3_make(r, 1.0f, r))); draw_mesh(&s_disc, GL_TRIANGLE_FAN, mat4_mul(vp, model), COLOR_SHADOW); } @@ -365,6 +420,7 @@ void render_frame(const Camera *cam, const Player *player, float aspect) draw_ground(vp); draw_grid(vp); + draw_platforms(vp); draw_shadow(vp, player); /* before the player, so the player is drawn over it */ draw_player(vp, player); |
