summaryrefslogtreecommitdiff
path: root/platformer/src/render.c
diff options
context:
space:
mode:
Diffstat (limited to 'platformer/src/render.c')
-rw-r--r--platformer/src/render.c84
1 files changed, 84 insertions, 0 deletions
diff --git a/platformer/src/render.c b/platformer/src/render.c
index 782c653..65237f5 100644
--- a/platformer/src/render.c
+++ b/platformer/src/render.c
@@ -65,6 +65,11 @@
* Bright color (e.g. red). Also draw a small second box as a "nose" on
* the front face (+Z in model space) so the facing direction is visible.
* That makes the turn rate tunable by eye.
+ * 7b. Zones (world_zone): no fill at all (a player sees straight through),
+ * just short black dashes spaced sparsely over the sides and top
+ * (not the bottom, which would z-fight the ground), so a test zone's
+ * extent is visible without hiding anything. Built once in
+ * render_init into one mesh; zones don't move.
* 8. FPS counter (skipped while fps <= 0, i.e. not measured yet): the text
* "<fps> FPS", right-aligned in the top-right corner, white with a
* dark drop shadow so it reads over both sky and ground. Glyphs come
@@ -113,6 +118,9 @@
#define NOSE_SIZE 0.3f /* m, edge length of the facing-direction "nose" box */
#define NOSE_HEIGHT 0.6f /* nose height as a fraction of PLAYER_HEIGHT */
#define GROUND_THICKNESS 1.0f /* m, the ground slab under the grid */
+#define ZONE_DASH_SPACING 3.0f /* m between dashes (and between rows of them) on a zone's faces */
+#define ZONE_DASH_LENGTH 0.8f /* m, each dash */
+#define ZONE_DASH_WIDTH 0.08f /* m, each dash's thickness */
#define HUD_PIXEL 0.006f /* size of one font pixel, as a fraction of the window height */
#define HUD_MARGIN 0.02f /* gap between the FPS text and the window corner, in window heights */
#define HUD_SHADOW_OFFSET 0.5f /* text shadow offset down and right, in font pixels */
@@ -132,6 +140,7 @@ static const float COLOR_WALL_HANG[3] = { 0.58f, 0.46f, 0.78f }; /* violet: s
static const float COLOR_CLIFF[3] = { 0.52f, 0.47f, 0.42f }; /* warm grey rock */
static const float COLOR_CLIFF_HANG[3] = { 0.56f, 0.48f, 0.62f }; /* violet-tinted rock: hangable terrain */
static const float COLOR_WATER[3] = { 0.22f, 0.45f, 0.72f }; /* river blue */
+static const float COLOR_ZONE_DASH[4] = { 0.00f, 0.00f, 0.00f, 1.0f };
static const float COLOR_HUD_TEXT[4] = { 1.00f, 1.00f, 1.00f, 1.0f };
static const float COLOR_HUD_SHADOW[4] = { 0.00f, 0.00f, 0.00f, 1.0f };
@@ -184,6 +193,7 @@ static GLuint s_program;
static GLint s_u_mvp, s_u_color;
static Mesh s_grid, s_cube, s_disc;
static GLsizei s_grid_minor_count; /* grid VBO holds minor lines first, then major */
+static Mesh s_zone_dashes; /* every zone's dashes, in world space */
static Mesh s_hud_text; /* the FPS text, in font pixels; rebuilt when the number changes */
static int s_hud_fps = -1; /* the number s_hud_text currently spells (-1 = none yet) */
@@ -462,6 +472,74 @@ static void draw_player(Mat4 vp, const Player *player)
draw_mesh(&s_cube, GL_TRIANGLES, mat4_mul(vp, nose), COLOR_NOSE);
}
+/* ---- Zones ------------------------------------------------------------- */
+
+/* Writes (if out isn't NULL) the dashes on one rectangular face: the face
+ * starts at `origin` and spans u_len along `u` and v_len along `v`. Rows of
+ * dashes every ZONE_DASH_SPACING, every other row shifted half a spacing.
+ * Returns how many vertices that is (6 per dash). */
+static int face_dashes(float *out, Vec3 origin, Vec3 u, float u_len, Vec3 v, float v_len)
+{
+ int count = 0;
+ int row = 0;
+ for (float vpos = 0.5f * ZONE_DASH_SPACING; vpos < v_len; vpos += ZONE_DASH_SPACING, row++) {
+ float shift = (row % 2) ? 0.5f * ZONE_DASH_SPACING : 0.0f;
+ for (float upos = 0.25f * ZONE_DASH_SPACING + shift; upos < u_len; upos += ZONE_DASH_SPACING) {
+ float u1 = fminf(upos + ZONE_DASH_LENGTH, u_len);
+ Vec3 corners[4] = {
+ vec3_add(origin, vec3_add(vec3_scale(u, upos), vec3_scale(v, vpos - 0.5f * ZONE_DASH_WIDTH))),
+ vec3_add(origin, vec3_add(vec3_scale(u, u1), vec3_scale(v, vpos - 0.5f * ZONE_DASH_WIDTH))),
+ vec3_add(origin, vec3_add(vec3_scale(u, u1), vec3_scale(v, vpos + 0.5f * ZONE_DASH_WIDTH))),
+ vec3_add(origin, vec3_add(vec3_scale(u, upos), vec3_scale(v, vpos + 0.5f * ZONE_DASH_WIDTH))),
+ };
+ static const int ORDER[6] = { 0, 1, 2, 0, 2, 3 };
+ for (int k = 0; k < 6 && out; k++) {
+ out[(count + k) * 3 + 0] = corners[ORDER[k]].x;
+ out[(count + k) * 3 + 1] = corners[ORDER[k]].y;
+ out[(count + k) * 3 + 2] = corners[ORDER[k]].z;
+ }
+ count += 6;
+ }
+ }
+ return count;
+}
+
+/* All five dashed faces (four sides and the top) of one zone. */
+static int zone_dashes(float *out, Box b)
+{
+ Vec3 x = vec3_make(1.0f, 0.0f, 0.0f), y = vec3_make(0.0f, 1.0f, 0.0f), z = vec3_make(0.0f, 0.0f, 1.0f);
+ Vec3 size = vec3_sub(b.max, b.min);
+ Vec3 faces[5] = { b.min, vec3_make(b.min.x, b.min.y, b.max.z), b.min,
+ vec3_make(b.max.x, b.min.y, b.min.z), vec3_make(b.min.x, b.max.y, b.min.z) };
+ Vec3 us[5] = { x, x, z, z, x };
+ Vec3 vs[5] = { y, y, y, y, z };
+ float ul[5] = { size.x, size.x, size.z, size.z, size.x };
+ float vl[5] = { size.y, size.y, size.y, size.y, size.z };
+ int count = 0;
+ for (int f = 0; f < 5; f++)
+ count += face_dashes(out ? out + count * 3 : NULL, faces[f], us[f], ul[f], vs[f], vl[f]);
+ return count;
+}
+
+/* One mesh with every zone's dashes: count them, then fill them in. */
+static bool build_zone_dashes(void)
+{
+ int count = 0;
+ for (int i = 0; i < world_zone_count(); i++)
+ count += zone_dashes(NULL, world_zone(i).bounds);
+ float *verts = malloc((size_t)(count > 0 ? count : 1) * 3 * sizeof(float));
+ if (!verts) {
+ fprintf(stderr, "render: out of memory building zone dashes\n");
+ return false;
+ }
+ int filled = 0;
+ for (int i = 0; i < world_zone_count(); i++)
+ filled += zone_dashes(verts + filled * 3, world_zone(i).bounds);
+ s_zone_dashes = make_mesh(verts, filled);
+ free(verts);
+ return true;
+}
+
/* ---- FPS counter ------------------------------------------------------- */
/* Returns the font's glyph for ch, or NULL (e.g. a space) to leave a gap. */
@@ -560,6 +638,10 @@ bool render_init(void)
}
build_cube();
build_disc();
+ if (!build_zone_dashes()) {
+ glDeleteProgram(s_program);
+ return false;
+ }
s_hud_text = make_mesh(NULL, 0); /* filled in by build_hud_text() once FPS is known */
return true;
}
@@ -577,6 +659,7 @@ void render_frame(const Camera *cam, const Player *player, float aspect, int fps
draw_platforms(vp);
draw_shadow(vp, player); /* before the player, so the player is drawn over it */
draw_player(vp, player);
+ draw_mesh(&s_zone_dashes, GL_TRIANGLES, vp, COLOR_ZONE_DASH); /* zones: dashes only, see-through */
draw_fps(fps, aspect); /* last: it sits on top of everything */
glBindVertexArray(0);
@@ -587,6 +670,7 @@ void render_shutdown(void)
delete_mesh(&s_grid);
delete_mesh(&s_cube);
delete_mesh(&s_disc);
+ delete_mesh(&s_zone_dashes);
delete_mesh(&s_hud_text);
glDeleteProgram(s_program);
}