improve vertex order and triangle generation

This commit is contained in:
Zack Buhman 2025-02-09 15:23:01 -06:00
parent 11ee5c9828
commit 70e503a4eb
2 changed files with 65 additions and 58 deletions

View File

@ -9,6 +9,8 @@ DEBUG = -g -gdwarf-5
CFLAGS += -Wall -Werror -Wfatal-errors CFLAGS += -Wall -Werror -Wfatal-errors
CFLAGS += -Wno-error=unused-function CFLAGS += -Wno-error=unused-function
CFLAGS += -Wno-error=unused-but-set-variable
CFLAGS += -Wno-error=unused-variable
CFLAGS += -std=c++23 CFLAGS += -std=c++23
CFLAGS += -I$(SDL)/include -D_REENTRANT CFLAGS += -I$(SDL)/include -D_REENTRANT
CFLAGS += $(shell pkg-config --cflags freetype2) CFLAGS += $(shell pkg-config --cflags freetype2)

121
main.cpp
View File

@ -478,21 +478,46 @@ void render_tri(SDL_Renderer * renderer,
render_line_vtx(renderer, c, a); render_line_vtx(renderer, c, a);
} }
struct tri generate_tri(struct edge * silhouette, static const vec3 colors[] = {
int * order, {1, 0, 0}, // red
int o0, {1, 0.5, 0}, // orange
int o1) {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; float scale = 0.5f;
tri.a = silhouette[order[o0]].a;
tri.b = silhouette[order[o0]].b; vec3 av = transform_vertex(position[a], scale);
if (silhouette[order[o0]].a == silhouette[order[o1]].a || vec3 bv = transform_vertex(position[b], scale);
silhouette[order[o0]].b == silhouette[order[o1]].a) { vec3 cv = transform_vertex(position[c], scale);
tri.c = silhouette[order[o1]].b; vec3 dv = transform_vertex(position[d], scale);
} else { vec3 ev = transform_vertex(position[e], scale);
tri.c = silhouette[order[o1]].a; vec3 fv = transform_vertex(position[f], scale);
}
return tri; 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, void render_silhouette(SDL_Renderer * renderer,
@ -516,66 +541,46 @@ void render_silhouette(SDL_Renderer * renderer,
assert(ix == 6); assert(ix == 6);
int order[6]; int last_ix = 0;
order[0] = 0; int order_ix = 0;
int order_ix = 1; int order_vtx[6];
int vtx = silhouette[0].b; order_vtx[order_ix++] = silhouette[0].b;
// calculate edge ordering // calculate vertex ordering
while (order_ix < 6) { while (order_ix < 6) {
for (int i = 1; i < 6; i++) { for (int i = 1; i < 6; i++) {
if (i == order[order_ix - 1]) if (i == last_ix)
continue; continue;
if (silhouette[i].a == vtx) { int last_vtx = order_vtx[order_ix - 1];
vtx = silhouette[i].b; if (last_vtx == silhouette[i].a) {
order[order_ix++] = i; last_ix = i;
order_vtx[order_ix++] = silhouette[i].b;
break; break;
} }
if (silhouette[i].b == vtx) { if (last_vtx == silhouette[i].b) {
vtx = silhouette[i].a; last_ix = i;
order[order_ix++] = i; order_vtx[order_ix++] = silhouette[i].a;
break; 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++) { render_perimeter_wall(renderer, position,
assert(SDL_SetRenderDrawColorFloat(renderer, colors[i].x, colors[i].y, colors[i].z, 1)); order_vtx[0],
order_vtx[1],
vec3 ap = position[silhouette[order[i]].a]; order_vtx[2],
vec3 bp = position[silhouette[order[i]].b]; order_vtx[3],
order_vtx[4],
float scale = 0.5f; order_vtx[5]);
vec3 av = transform_vertex(ap, scale);
vec3 bv = transform_vertex(bp, scale);
render_line_vtx(renderer, av, bv);
}
*/ */
// render triangulation // render triangulation
/* struct tri q = {order_vtx[0], order_vtx[1], order_vtx[2]};
{0.a, 0.b}, {1.a, 1.b}, {0._, 1._} struct tri r = {order_vtx[2], order_vtx[3], order_vtx[4]};
{2.a, 2.b}, {3.a, 3.b}, {2._, 3._} struct tri s = {order_vtx[4], order_vtx[5], order_vtx[0]};
{4.a, 4.b}, {5.a, 5.b}, {4._, 5._} struct tri t = {order_vtx[0], order_vtx[2], order_vtx[4]};
{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};
render_tri(renderer, position, colors[1], q); render_tri(renderer, position, colors[1], q);
render_tri(renderer, position, colors[2], r); render_tri(renderer, position, colors[2], r);