#include <stdlib.h>
#include <string.h>
#include <assert.h>
+#if defined(__WATCOMC__) || defined(_MSC_VER) || defined(__DJGPP__)
+#include <malloc.h>
+#else
+#include <alloca.h>
+#endif
#include "bsptree.h"
#include "dynarr.h"
#include "inttypes.h"
static struct bspnode *new_node(struct g3d_vertex *v, int vnum);
static struct bspnode *add_poly_tree(struct bspnode *n, struct g3d_vertex *v, int vnum);
+static void draw_bsp_tree(struct bspnode *n, const vec3_t *vdir);
static void save_bsp_tree(struct bspnode *n, FILE *fp);
static struct bspnode *load_bsp_tree(FILE *fp);
void draw_bsp(struct bsptree *bsp, float view_x, float view_y, float view_z)
{
+ vec3_t vdir;
+ vdir.x = view_x;
+ vdir.y = view_y;
+ vdir.z = view_z;
+ draw_bsp_tree(bsp->root, &vdir);
}
static int count_nodes(struct bspnode *n)
return n;
}
+#undef DRAW_NGONS
+
+#ifndef DRAW_NGONS
+static void debug_draw_poly(struct g3d_vertex *varr, int vcount)
+{
+ int i, nfaces = vcount - 2;
+ int vbuf_size = nfaces * 3;
+ struct g3d_vertex *vbuf = alloca(vbuf_size * sizeof *vbuf);
+ struct g3d_vertex *vptr = varr + 1;
+
+ for(i=0; i<nfaces; i++) {
+ vbuf[i * 3] = varr[0];
+ vbuf[i * 3 + 1] = *vptr++;
+ vbuf[i * 3 + 2] = *vptr;
+ }
+
+ g3d_draw_indexed(G3D_TRIANGLES, vbuf, vbuf_size, 0, 0);
+}
+#endif
+
+static void draw_bsp_tree(struct bspnode *n, const vec3_t *vdir)
+{
+ float dot;
+
+ if(!n) return;
+
+ dot = vdir->x * n->plane.nx + vdir->y * n->plane.ny + vdir->z * n->plane.nz;
+ if(dot >= 0.0f) {
+ draw_bsp_tree(n->front, vdir);
+#ifdef DRAW_NGONS
+ g3d_draw_indexed(n->vcount, n->verts, n->vcount, 0, 0);
+#else
+ debug_draw_poly(n->verts, n->vcount);
+#endif
+ draw_bsp_tree(n->back, vdir);
+ } else {
+ draw_bsp_tree(n->back, vdir);
+#ifdef DRAW_NGONS
+ g3d_draw_indexed(n->vcount, n->verts, n->vcount, 0, 0);
+#else
+ debug_draw_poly(n->verts, n->vcount);
+#endif
+ draw_bsp_tree(n->front, vdir);
+ }
+}
+
static void save_bsp_tree(struct bspnode *n, FILE *fp)
{
/* TODO */