From bf7eceb7d1cef825a64a1db7a20628f7d7005654 Mon Sep 17 00:00:00 2001 From: John Tsiombikas Date: Fri, 3 Feb 2023 17:49:02 +0200 Subject: [PATCH] foo --- src/dynarr.c | 2 +- src/dynarr.h | 2 +- src/geom.c | 45 ++++++++++++++++++++++----------------------- 3 files changed, 24 insertions(+), 25 deletions(-) diff --git a/src/dynarr.c b/src/dynarr.c index 59bbf8c..4cc79e7 100644 --- a/src/dynarr.c +++ b/src/dynarr.c @@ -76,7 +76,7 @@ void *dynarr_clear(void *da) } /* stack semantics */ -void *dynarr_push(void *da, void *item) +void *dynarr_push(void *da, const void *item) { struct arrdesc *desc; int nelem; diff --git a/src/dynarr.h b/src/dynarr.h index 8690b5a..a11d0ea 100644 --- a/src/dynarr.h +++ b/src/dynarr.h @@ -36,7 +36,7 @@ int dynarr_size(void *da); void *dynarr_clear(void *da); /* stack semantics */ -void *dynarr_push(void *da, void *item); +void *dynarr_push(void *da, const void *item); void *dynarr_pop(void *da); /* Finalize the array. No more resizing is possible after this call. diff --git a/src/geom.c b/src/geom.c index 0b94385..81e9df4 100644 --- a/src/geom.c +++ b/src/geom.c @@ -1,3 +1,4 @@ +#include #include "geom.h" #include "dynarr.h" @@ -46,7 +47,8 @@ int plane_poly(const struct plane *plane, struct poly *poly, float size) { static const float corn[][2] = {{1,1}, {-1,1}, {-1,-1}, {1, -1}}; int i; - cgm_vec3 pos, up, right, dir; + cgm_vec3 up, right; + struct vertex *vptr; size *= 0.5f; if(!(poly->verts = dynarr_alloc(4, sizeof *poly->verts))) { @@ -60,17 +62,20 @@ int plane_poly(const struct plane *plane, struct poly *poly, float size) } cgm_vcross(&right, &up, &plane->norm); cgm_vnormalize(&right); - cgm_vcross(&up, &plane.norm, &right); + cgm_vcross(&up, &plane->norm, &right); cgm_vnormalize(&up); + vptr = poly->verts; for(i=0; i<4; i++) { - v.x = plane->pt.x + (right.x * corn[i][0] + up.x * corn[i][1]) * size; - v.y = plane->pt.y + (right.y * corn[i][0] + up.y * corn[i][1]) * size; - v.z = plane->pt.z + (right.z * corn[i][0] + up.z * corn[i][1]) * size; - - - + vptr->pos.x = plane->pt.x + (right.x * corn[i][0] + up.x * corn[i][1]) * size; + vptr->pos.y = plane->pt.y + (right.y * corn[i][0] + up.y * corn[i][1]) * size; + vptr->pos.z = plane->pt.z + (right.z * corn[i][0] + up.z * corn[i][1]) * size; + vptr->norm = plane->norm; + vptr->uv.x = vptr->uv.y = 0; + vptr++; + } + return 0; } float ray_plane(const cgm_ray *ray, const struct plane *plane) @@ -136,21 +141,15 @@ static int clip_edge(struct poly *pout, const struct vertex *v0, const struct ve { float d0, d1, t; cgm_ray ray; - int i, vnum = dynarr_size(pout->verts); struct vertex vnew; d0 = plane_sdist(plane, &v0->pos); d1 = plane_sdist(plane, &v1->pos); - ray.orig = v0->pos; + ray.origin = v0->pos; ray.dir = v1->pos; cgm_vsub(&ray.dir, &v0->pos); - for(i=0; i<3; i++) { - ray.origin[i] = pos0[i]; - ray.dir[i] = pos1[i] - pos0[i]; - } - if(d0 >= 0.0) { /* start inside */ if(d1 >= 0.0) { @@ -160,11 +159,11 @@ static int clip_edge(struct poly *pout, const struct vertex *v0, const struct ve } else { /* going out */ t = ray_plane(&ray, plane); - cgm_raypos(&vnew->pos, &ray, t); + cgm_raypos(&vnew.pos, &ray, t); - cgm_vlerp(&vnew->norm, &v0->norm, &v1->norm, t); - vnew->uv.x = cgm_lerp(v0->uv.x, v1->uv.x, t); - vnew->uv.y = cgm_lerp(v0->uv.y, v1->uv.y, t); + cgm_vlerp(&vnew.norm, &v0->norm, &v1->norm, t); + vnew.uv.x = cgm_lerp(v0->uv.x, v1->uv.x, t); + vnew.uv.y = cgm_lerp(v0->uv.y, v1->uv.y, t); /* append new vertex on the intersection point */ DYNARR_PUSH(pout->verts, &vnew); } @@ -173,11 +172,11 @@ static int clip_edge(struct poly *pout, const struct vertex *v0, const struct ve if(d1 >= 0) { /* going in */ t = ray_plane(&ray, plane); - cgm_raypos(&vnew->pos, &ray, t); + cgm_raypos(&vnew.pos, &ray, t); - cgm_vlerp(&vnew->norm, &v0->norm, &v1->norm, t); - vnew->uv.x = cgm_lerp(v0->uv.x, v1->uv.x, t); - vnew->uv.y = cgm_lerp(v0->uv.y, v1->uv.y, t); + cgm_vlerp(&vnew.norm, &v0->norm, &v1->norm, t); + vnew.uv.x = cgm_lerp(v0->uv.x, v1->uv.x, t); + vnew.uv.y = cgm_lerp(v0->uv.y, v1->uv.y, t); /* append new vertex on the intersection point */ DYNARR_PUSH(pout->verts, &vnew); /* then append v1 ... */ -- 1.7.10.4