bring poly back

main
Dominik Madarász 2024-08-15 11:11:46 +02:00
parent ba1d6e783e
commit 6398fd62ad
7 changed files with 236 additions and 72 deletions

View File

@ -456,6 +456,7 @@ typedef struct plane { vec3 p, n; } plane;
typedef struct capsule { vec3 a, b; float r; } capsule;
typedef struct ray { vec3 p, d; } ray;
typedef struct triangle { vec3 p0,p1,p2; } triangle;
typedef struct poly { vec3* verts; int cnt; } poly;
typedef union frustum { struct { vec4 l, r, t, b, n, f; }; vec4 pl[6]; float v[24]; } frustum;
typedef struct hit {
union {
@ -504,6 +505,10 @@ typedef struct hit {
frustum frustum_build(mat44 projview);
int frustum_test_sphere(frustum f, sphere s);
int frustum_test_aabb(frustum f, aabb a);
poly poly_alloc(int cnt);
void poly_free(poly *p);
poly pyramid(vec3 from, vec3 to, float size);
poly diamond(vec3 from, vec3 to, float size);
void collide_demo();
enum COOK_FLAGS {
COOK_SYNC = 0,

View File

@ -15700,6 +15700,7 @@ typedef struct plane { vec3 p, n;
typedef struct capsule { vec3 a, b; float r; } capsule;
typedef struct ray { vec3 p, d; } ray;
typedef struct triangle { vec3 p0,p1,p2; } triangle;
typedef struct poly { vec3* verts; int cnt; } poly;
typedef union frustum { struct { vec4 l, r, t, b, n, f; }; vec4 pl[6]; float v[24]; } frustum;
#define line(...) C_CAST(line, __VA_ARGS__)
@ -15709,6 +15710,7 @@ typedef union frustum { struct { vec4 l, r, t, b, n, f; }; vec4 pl[6]; float v
#define capsule(...) C_CAST(capsule, __VA_ARGS__)
#define ray(p,normdir) C_CAST(ray, p, normdir)
#define triangle(...) C_CAST(triangle, __VA_ARGS__)
#define poly(...) C_CAST(poly, __VA_ARGS__)
#define frustum(...) C_CAST(frustum, __VA_ARGS__)
// ----------------------------------------------------------------------------
@ -15776,6 +15778,12 @@ API frustum frustum_build(mat44 projview);
API int frustum_test_sphere(frustum f, sphere s);
API int frustum_test_aabb(frustum f, aabb a);
API poly poly_alloc(int cnt);
API void poly_free(poly *p);
API poly pyramid(vec3 from, vec3 to, float size); // poly_free() required
API poly diamond(vec3 from, vec3 to, float size); // poly_free() required
API void collide_demo(); // debug draw collisions
#line 0
@ -369518,11 +369526,56 @@ int ui_audio() {
#line 0
#line 1 "v4k_collide.c"
/* poly */
poly poly_alloc(int cnt) {
poly p = {0};
p.cnt = cnt;
p.verts = REALLOC(p.verts, sizeof(p.verts[0]) * cnt); // array_resize(p.verts, cnt);
return p;
}
void poly_free(poly *p) {
REALLOC(p->verts, 0); // array_free(p->verts);
poly z = {0};
*p = z;
}
/* plane */
vec4 plane4(vec3 p, vec3 n) {
return vec34(n, -dot3(n,p));
}
/* pyramid */
poly pyramid(vec3 from, vec3 to, float size) {
/* calculate axis */
vec3 up, right, forward = norm3( sub3(to, from) );
ortho3(&right, &up, forward);
/* calculate extend */
vec3 xext = scale3(right, size);
vec3 yext = scale3(up, size);
vec3 nxext = scale3(right, -size);
vec3 nyext = scale3(up, -size);
/* calculate base vertices */
poly p = {0};
p.verts = REALLOC(p.verts, sizeof(p.verts[0]) * (5+1)); p.cnt = 5; /*+1 for diamond case*/ // array_resize(p.verts, 5+1); p.cnt = 5;
p.verts[0] = add3(add3(from, xext), yext); /*a*/
p.verts[1] = add3(add3(from, xext), nyext); /*b*/
p.verts[2] = add3(add3(from, nxext), nyext); /*c*/
p.verts[3] = add3(add3(from, nxext), yext); /*d*/
p.verts[4] = to; /*r*/
return p;
}
/* pyramid */
poly diamond(vec3 from, vec3 to, float size) {
vec3 mid = add3(from, scale3(sub3(to, from), 0.5f));
poly p = pyramid(mid, to, size);
p.verts[5] = from; p.cnt = 6;
return p;
}
// ---
static void transform_(vec3 *r, vec3 v, const float *r33, vec3 t3) {
@ -386620,35 +386673,35 @@ void ddraw_cylinder(vec3 center, float height, int segments) {
ddraw_prism(center, 1, -height, vec3(0,1,0), segments);
}
void ddraw_diamond(vec3 from, vec3 to, float size) {
// poly p = diamond(from, to, size);
// vec3 *dmd = p.verts;
poly p = diamond(from, to, size);
vec3 *dmd = p.verts;
// vec3 *a = dmd + 0;
// vec3 *b = dmd + 1;
// vec3 *c = dmd + 2;
// vec3 *d = dmd + 3;
// vec3 *t = dmd + 4;
// vec3 *f = dmd + 5;
vec3 *a = dmd + 0;
vec3 *b = dmd + 1;
vec3 *c = dmd + 2;
vec3 *d = dmd + 3;
vec3 *t = dmd + 4;
vec3 *f = dmd + 5;
// /* draw vertices */
// ddraw_line(*a, *b);
// ddraw_line(*b, *c);
// ddraw_line(*c, *d);
// ddraw_line(*d, *a);
/* draw vertices */
ddraw_line(*a, *b);
ddraw_line(*b, *c);
ddraw_line(*c, *d);
ddraw_line(*d, *a);
// /* draw roof */
// ddraw_line(*a, *t);
// ddraw_line(*b, *t);
// ddraw_line(*c, *t);
// ddraw_line(*d, *t);
/* draw roof */
ddraw_line(*a, *t);
ddraw_line(*b, *t);
ddraw_line(*c, *t);
ddraw_line(*d, *t);
// /* draw floor */
// ddraw_line(*a, *f);
// ddraw_line(*b, *f);
// ddraw_line(*c, *f);
// ddraw_line(*d, *f);
/* draw floor */
ddraw_line(*a, *f);
ddraw_line(*b, *f);
ddraw_line(*c, *f);
ddraw_line(*d, *f);
// poly_free(&p);
poly_free(&p);
}
void ddraw_cone(vec3 center, vec3 top, float radius) {
vec3 diff3 = sub3(top, center);

View File

@ -1,8 +1,53 @@
/* poly */
poly poly_alloc(int cnt) {
poly p = {0};
p.cnt = cnt;
p.verts = REALLOC(p.verts, sizeof(p.verts[0]) * cnt); // array_resize(p.verts, cnt);
return p;
}
void poly_free(poly *p) {
REALLOC(p->verts, 0); // array_free(p->verts);
poly z = {0};
*p = z;
}
/* plane */
vec4 plane4(vec3 p, vec3 n) {
return vec34(n, -dot3(n,p));
}
/* pyramid */
poly pyramid(vec3 from, vec3 to, float size) {
/* calculate axis */
vec3 up, right, forward = norm3( sub3(to, from) );
ortho3(&right, &up, forward);
/* calculate extend */
vec3 xext = scale3(right, size);
vec3 yext = scale3(up, size);
vec3 nxext = scale3(right, -size);
vec3 nyext = scale3(up, -size);
/* calculate base vertices */
poly p = {0};
p.verts = REALLOC(p.verts, sizeof(p.verts[0]) * (5+1)); p.cnt = 5; /*+1 for diamond case*/ // array_resize(p.verts, 5+1); p.cnt = 5;
p.verts[0] = add3(add3(from, xext), yext); /*a*/
p.verts[1] = add3(add3(from, xext), nyext); /*b*/
p.verts[2] = add3(add3(from, nxext), nyext); /*c*/
p.verts[3] = add3(add3(from, nxext), yext); /*d*/
p.verts[4] = to; /*r*/
return p;
}
/* pyramid */
poly diamond(vec3 from, vec3 to, float size) {
vec3 mid = add3(from, scale3(sub3(to, from), 0.5f));
poly p = pyramid(mid, to, size);
p.verts[5] = from; p.cnt = 6;
return p;
}
// ---
static void transform_(vec3 *r, vec3 v, const float *r33, vec3 t3) {

View File

@ -10,6 +10,7 @@ typedef struct plane { vec3 p, n;
typedef struct capsule { vec3 a, b; float r; } capsule;
typedef struct ray { vec3 p, d; } ray;
typedef struct triangle { vec3 p0,p1,p2; } triangle;
typedef struct poly { vec3* verts; int cnt; } poly;
typedef union frustum { struct { vec4 l, r, t, b, n, f; }; vec4 pl[6]; float v[24]; } frustum;
#define line(...) C_CAST(line, __VA_ARGS__)
@ -19,6 +20,7 @@ typedef union frustum { struct { vec4 l, r, t, b, n, f; }; vec4 pl[6]; float v
#define capsule(...) C_CAST(capsule, __VA_ARGS__)
#define ray(p,normdir) C_CAST(ray, p, normdir)
#define triangle(...) C_CAST(triangle, __VA_ARGS__)
#define poly(...) C_CAST(poly, __VA_ARGS__)
#define frustum(...) C_CAST(frustum, __VA_ARGS__)
// ----------------------------------------------------------------------------
@ -86,4 +88,10 @@ API frustum frustum_build(mat44 projview);
API int frustum_test_sphere(frustum f, sphere s);
API int frustum_test_aabb(frustum f, aabb a);
API poly poly_alloc(int cnt);
API void poly_free(poly *p);
API poly pyramid(vec3 from, vec3 to, float size); // poly_free() required
API poly diamond(vec3 from, vec3 to, float size); // poly_free() required
API void collide_demo(); // debug draw collisions

View File

@ -524,35 +524,35 @@ void ddraw_cylinder(vec3 center, float height, int segments) {
ddraw_prism(center, 1, -height, vec3(0,1,0), segments);
}
void ddraw_diamond(vec3 from, vec3 to, float size) {
// poly p = diamond(from, to, size);
// vec3 *dmd = p.verts;
poly p = diamond(from, to, size);
vec3 *dmd = p.verts;
// vec3 *a = dmd + 0;
// vec3 *b = dmd + 1;
// vec3 *c = dmd + 2;
// vec3 *d = dmd + 3;
// vec3 *t = dmd + 4;
// vec3 *f = dmd + 5;
vec3 *a = dmd + 0;
vec3 *b = dmd + 1;
vec3 *c = dmd + 2;
vec3 *d = dmd + 3;
vec3 *t = dmd + 4;
vec3 *f = dmd + 5;
// /* draw vertices */
// ddraw_line(*a, *b);
// ddraw_line(*b, *c);
// ddraw_line(*c, *d);
// ddraw_line(*d, *a);
/* draw vertices */
ddraw_line(*a, *b);
ddraw_line(*b, *c);
ddraw_line(*c, *d);
ddraw_line(*d, *a);
// /* draw roof */
// ddraw_line(*a, *t);
// ddraw_line(*b, *t);
// ddraw_line(*c, *t);
// ddraw_line(*d, *t);
/* draw roof */
ddraw_line(*a, *t);
ddraw_line(*b, *t);
ddraw_line(*c, *t);
ddraw_line(*d, *t);
// /* draw floor */
// ddraw_line(*a, *f);
// ddraw_line(*b, *f);
// ddraw_line(*c, *f);
// ddraw_line(*d, *f);
/* draw floor */
ddraw_line(*a, *f);
ddraw_line(*b, *f);
ddraw_line(*c, *f);
ddraw_line(*d, *f);
// poly_free(&p);
poly_free(&p);
}
void ddraw_cone(vec3 center, vec3 top, float radius) {
vec3 diff3 = sub3(top, center);

View File

@ -4716,11 +4716,56 @@ int ui_audio() {
#line 0
#line 1 "v4k_collide.c"
/* poly */
poly poly_alloc(int cnt) {
poly p = {0};
p.cnt = cnt;
p.verts = REALLOC(p.verts, sizeof(p.verts[0]) * cnt); // array_resize(p.verts, cnt);
return p;
}
void poly_free(poly *p) {
REALLOC(p->verts, 0); // array_free(p->verts);
poly z = {0};
*p = z;
}
/* plane */
vec4 plane4(vec3 p, vec3 n) {
return vec34(n, -dot3(n,p));
}
/* pyramid */
poly pyramid(vec3 from, vec3 to, float size) {
/* calculate axis */
vec3 up, right, forward = norm3( sub3(to, from) );
ortho3(&right, &up, forward);
/* calculate extend */
vec3 xext = scale3(right, size);
vec3 yext = scale3(up, size);
vec3 nxext = scale3(right, -size);
vec3 nyext = scale3(up, -size);
/* calculate base vertices */
poly p = {0};
p.verts = REALLOC(p.verts, sizeof(p.verts[0]) * (5+1)); p.cnt = 5; /*+1 for diamond case*/ // array_resize(p.verts, 5+1); p.cnt = 5;
p.verts[0] = add3(add3(from, xext), yext); /*a*/
p.verts[1] = add3(add3(from, xext), nyext); /*b*/
p.verts[2] = add3(add3(from, nxext), nyext); /*c*/
p.verts[3] = add3(add3(from, nxext), yext); /*d*/
p.verts[4] = to; /*r*/
return p;
}
/* pyramid */
poly diamond(vec3 from, vec3 to, float size) {
vec3 mid = add3(from, scale3(sub3(to, from), 0.5f));
poly p = pyramid(mid, to, size);
p.verts[5] = from; p.cnt = 6;
return p;
}
// ---
static void transform_(vec3 *r, vec3 v, const float *r33, vec3 t3) {
@ -21818,35 +21863,35 @@ void ddraw_cylinder(vec3 center, float height, int segments) {
ddraw_prism(center, 1, -height, vec3(0,1,0), segments);
}
void ddraw_diamond(vec3 from, vec3 to, float size) {
// poly p = diamond(from, to, size);
// vec3 *dmd = p.verts;
poly p = diamond(from, to, size);
vec3 *dmd = p.verts;
// vec3 *a = dmd + 0;
// vec3 *b = dmd + 1;
// vec3 *c = dmd + 2;
// vec3 *d = dmd + 3;
// vec3 *t = dmd + 4;
// vec3 *f = dmd + 5;
vec3 *a = dmd + 0;
vec3 *b = dmd + 1;
vec3 *c = dmd + 2;
vec3 *d = dmd + 3;
vec3 *t = dmd + 4;
vec3 *f = dmd + 5;
// /* draw vertices */
// ddraw_line(*a, *b);
// ddraw_line(*b, *c);
// ddraw_line(*c, *d);
// ddraw_line(*d, *a);
/* draw vertices */
ddraw_line(*a, *b);
ddraw_line(*b, *c);
ddraw_line(*c, *d);
ddraw_line(*d, *a);
// /* draw roof */
// ddraw_line(*a, *t);
// ddraw_line(*b, *t);
// ddraw_line(*c, *t);
// ddraw_line(*d, *t);
/* draw roof */
ddraw_line(*a, *t);
ddraw_line(*b, *t);
ddraw_line(*c, *t);
ddraw_line(*d, *t);
// /* draw floor */
// ddraw_line(*a, *f);
// ddraw_line(*b, *f);
// ddraw_line(*c, *f);
// ddraw_line(*d, *f);
/* draw floor */
ddraw_line(*a, *f);
ddraw_line(*b, *f);
ddraw_line(*c, *f);
ddraw_line(*d, *f);
// poly_free(&p);
poly_free(&p);
}
void ddraw_cone(vec3 center, vec3 top, float radius) {
vec3 diff3 = sub3(top, center);

View File

@ -1767,6 +1767,7 @@ typedef struct plane { vec3 p, n;
typedef struct capsule { vec3 a, b; float r; } capsule;
typedef struct ray { vec3 p, d; } ray;
typedef struct triangle { vec3 p0,p1,p2; } triangle;
typedef struct poly { vec3* verts; int cnt; } poly;
typedef union frustum { struct { vec4 l, r, t, b, n, f; }; vec4 pl[6]; float v[24]; } frustum;
#define line(...) C_CAST(line, __VA_ARGS__)
@ -1776,6 +1777,7 @@ typedef union frustum { struct { vec4 l, r, t, b, n, f; }; vec4 pl[6]; float v
#define capsule(...) C_CAST(capsule, __VA_ARGS__)
#define ray(p,normdir) C_CAST(ray, p, normdir)
#define triangle(...) C_CAST(triangle, __VA_ARGS__)
#define poly(...) C_CAST(poly, __VA_ARGS__)
#define frustum(...) C_CAST(frustum, __VA_ARGS__)
// ----------------------------------------------------------------------------
@ -1843,6 +1845,12 @@ API frustum frustum_build(mat44 projview);
API int frustum_test_sphere(frustum f, sphere s);
API int frustum_test_aabb(frustum f, aabb a);
API poly poly_alloc(int cnt);
API void poly_free(poly *p);
API poly pyramid(vec3 from, vec3 to, float size); // poly_free() required
API poly diamond(vec3 from, vec3 to, float size); // poly_free() required
API void collide_demo(); // debug draw collisions
#line 0