From 70e503a4eb4f241ee955714baf23584ab22f7c97 Mon Sep 17 00:00:00 2001 From: Zack Buhman Date: Sun, 9 Feb 2025 15:23:01 -0600 Subject: [PATCH] improve vertex order and triangle generation --- Makefile | 2 + main.cpp | 121 +++++++++++++++++++++++++++++-------------------------- 2 files changed, 65 insertions(+), 58 deletions(-) diff --git a/Makefile b/Makefile index f4f3d87..c73a5a5 100644 --- a/Makefile +++ b/Makefile @@ -9,6 +9,8 @@ DEBUG = -g -gdwarf-5 CFLAGS += -Wall -Werror -Wfatal-errors CFLAGS += -Wno-error=unused-function +CFLAGS += -Wno-error=unused-but-set-variable +CFLAGS += -Wno-error=unused-variable CFLAGS += -std=c++23 CFLAGS += -I$(SDL)/include -D_REENTRANT CFLAGS += $(shell pkg-config --cflags freetype2) diff --git a/main.cpp b/main.cpp index c1b9421..09473ac 100644 --- a/main.cpp +++ b/main.cpp @@ -478,21 +478,46 @@ void render_tri(SDL_Renderer * renderer, render_line_vtx(renderer, c, a); } -struct tri generate_tri(struct edge * silhouette, - int * order, - int o0, - int o1) +static const vec3 colors[] = { + {1, 0, 0}, // red + {1, 0.5, 0}, // orange + {1, 1, 0}, // yellow + {0, 1, 0}, // green + {0, 1, 1}, // cyan + {0.5, 0, 1}, // purple +}; + +void render_perimeter_wall(SDL_Renderer * renderer, + const vec3 * position, + int a, + int b, + int c, + int d, + int e, + int f + ) { - struct tri tri; - tri.a = silhouette[order[o0]].a; - tri.b = silhouette[order[o0]].b; - if (silhouette[order[o0]].a == silhouette[order[o1]].a || - silhouette[order[o0]].b == silhouette[order[o1]].a) { - tri.c = silhouette[order[o1]].b; - } else { - tri.c = silhouette[order[o1]].a; - } - return tri; + float scale = 0.5f; + + vec3 av = transform_vertex(position[a], scale); + vec3 bv = transform_vertex(position[b], scale); + vec3 cv = transform_vertex(position[c], scale); + vec3 dv = transform_vertex(position[d], scale); + vec3 ev = transform_vertex(position[e], scale); + vec3 fv = transform_vertex(position[f], scale); + + assert(SDL_SetRenderDrawColorFloat(renderer, colors[0].x, colors[0].y, colors[0].z, 1)); + render_line_vtx(renderer, av, bv); + assert(SDL_SetRenderDrawColorFloat(renderer, colors[1].x, colors[1].y, colors[1].z, 1)); + render_line_vtx(renderer, bv, cv); + assert(SDL_SetRenderDrawColorFloat(renderer, colors[2].x, colors[2].y, colors[2].z, 1)); + render_line_vtx(renderer, cv, dv); + assert(SDL_SetRenderDrawColorFloat(renderer, colors[3].x, colors[3].y, colors[3].z, 1)); + render_line_vtx(renderer, dv, ev); + assert(SDL_SetRenderDrawColorFloat(renderer, colors[4].x, colors[4].y, colors[4].z, 1)); + render_line_vtx(renderer, ev, fv); + assert(SDL_SetRenderDrawColorFloat(renderer, colors[5].x, colors[5].y, colors[5].z, 1)); + render_line_vtx(renderer, fv, av); } void render_silhouette(SDL_Renderer * renderer, @@ -516,66 +541,46 @@ void render_silhouette(SDL_Renderer * renderer, assert(ix == 6); - int order[6]; - order[0] = 0; - int order_ix = 1; - int vtx = silhouette[0].b; + int last_ix = 0; + int order_ix = 0; + int order_vtx[6]; + order_vtx[order_ix++] = silhouette[0].b; - // calculate edge ordering + // calculate vertex ordering while (order_ix < 6) { for (int i = 1; i < 6; i++) { - if (i == order[order_ix - 1]) + if (i == last_ix) continue; - if (silhouette[i].a == vtx) { - vtx = silhouette[i].b; - order[order_ix++] = i; + int last_vtx = order_vtx[order_ix - 1]; + if (last_vtx == silhouette[i].a) { + last_ix = i; + order_vtx[order_ix++] = silhouette[i].b; break; } - if (silhouette[i].b == vtx) { - vtx = silhouette[i].a; - order[order_ix++] = i; + if (last_vtx == silhouette[i].b) { + last_ix = i; + order_vtx[order_ix++] = silhouette[i].a; break; } } } - assert(order_ix == 6); - - const vec3 colors[] = { - {1, 0, 0}, // red - {1, 0.5, 0}, // orange - {1, 1, 0}, // yellow - {0, 1, 0}, // green - {0, 1, 1}, // cyan - {0.5, 0, 1}, // purple - }; - /* - for (int i = 0; i < 6; i++) { - assert(SDL_SetRenderDrawColorFloat(renderer, colors[i].x, colors[i].y, colors[i].z, 1)); - - vec3 ap = position[silhouette[order[i]].a]; - vec3 bp = position[silhouette[order[i]].b]; - - float scale = 0.5f; - vec3 av = transform_vertex(ap, scale); - vec3 bv = transform_vertex(bp, scale); - render_line_vtx(renderer, av, bv); - } + render_perimeter_wall(renderer, position, + order_vtx[0], + order_vtx[1], + order_vtx[2], + order_vtx[3], + order_vtx[4], + order_vtx[5]); */ // render triangulation - /* - {0.a, 0.b}, {1.a, 1.b}, {0._, 1._} - {2.a, 2.b}, {3.a, 3.b}, {2._, 3._} - {4.a, 4.b}, {5.a, 5.b}, {4._, 5._} - {0._, 1._}, {2._, 3._}, {4._, 5._} - */ - struct tri q = generate_tri(silhouette, order, 0, 1); - struct tri r = generate_tri(silhouette, order, 2, 3); - struct tri s = generate_tri(silhouette, order, 4, 5); - struct tri t = {q.c, r.c, s.c}; + struct tri q = {order_vtx[0], order_vtx[1], order_vtx[2]}; + struct tri r = {order_vtx[2], order_vtx[3], order_vtx[4]}; + struct tri s = {order_vtx[4], order_vtx[5], order_vtx[0]}; + struct tri t = {order_vtx[0], order_vtx[2], order_vtx[4]}; render_tri(renderer, position, colors[1], q); render_tri(renderer, position, colors[2], r);