11 struct bspfile_header {
16 static int count_nodes(struct bspnode *n);
17 static void free_tree(struct bspnode *n);
19 static struct bspnode *new_node(struct g3d_vertex *v, int vnum);
20 static struct bspnode *add_poly_tree(struct bspnode *n, struct g3d_vertex *v, int vnum);
22 static void save_bsp_tree(struct bspnode *n, FILE *fp);
23 static struct bspnode *load_bsp_tree(FILE *fp);
25 int init_bsp(struct bsptree *bsp)
31 void destroy_bsp(struct bsptree *bsp)
36 int save_bsp(struct bsptree *bsp, const char *fname)
39 struct bspfile_header hdr;
41 if(!(fp = fopen(fname, "wb"))) {
42 fprintf(stderr, "save_bsp: failed to open %s for writing\n", fname);
46 memcpy(hdr.magic, "MBSP", 4);
47 hdr.num_nodes = count_nodes(bsp->root);
48 fwrite(&hdr, sizeof hdr, 1, fp);
50 save_bsp_tree(bsp->root, fp);
56 int load_bsp(struct bsptree *bsp, const char *fname)
59 struct bspfile_header hdr;
61 if(!(fp = fopen(fname, "rb"))) {
62 fprintf(stderr, "load_bsp: failed to open %s\n", fname);
66 if(fread(&hdr, sizeof hdr, 1, fp) < 1) {
67 fprintf(stderr, "load_bsp: %s: failed to read header\n", fname);
71 if(memcmp(hdr.magic, "MBSP", 4) != 0) {
72 fprintf(stderr, "load_bsp: %s: invalid magic\n", fname);
76 bsp->root = load_bsp_tree(fp);
82 int bsp_add_poly(struct bsptree *bsp, struct g3d_vertex *v, int vnum)
87 if(!(n = add_poly_tree(bsp->root, v, vnum))) {
88 fprintf(stderr, "bsp_add_poly: failed to add polygon\n");
95 void bsp_add_mesh(struct bsptree *bsp, struct g3d_mesh *m)
98 struct g3d_vertex v[4];
99 struct g3d_vertex *vptr = m->varr;
100 uint16_t *iptr = m->iarr;
102 nfaces = m->iarr ? m->icount / m->prim : m->vcount / m->prim;
104 for(i=0; i<nfaces; i++) {
105 for(j=0; j<m->prim; j++) {
107 v[j] = m->varr[*iptr++];
112 bsp_add_poly(bsp, v, m->prim);
116 void draw_bsp(struct bsptree *bsp, float view_x, float view_y, float view_z)
120 static int count_nodes(struct bspnode *n)
123 return count_nodes(n->front) + count_nodes(n->back) + 1;
126 static void free_tree(struct bspnode *n)
136 static struct bspnode *new_node(struct g3d_vertex *v, int vnum)
141 if(!(n = malloc(sizeof *n)) || !(n->verts = malloc(vnum * sizeof *n->verts))) {
142 fprintf(stderr, "failed to allocate BSP tree node\n");
146 va.x = v[1].x - v[0].x;
147 va.y = v[1].y - v[0].y;
148 va.z = v[1].z - v[0].z;
149 vb.x = v[2].x - v[0].x;
150 vb.y = v[2].y - v[0].y;
151 vb.z = v[2].z - v[0].z;
152 norm = v3_cross(va, vb);
158 n->plane.nx = norm.x;
159 n->plane.ny = norm.y;
160 n->plane.nz = norm.z;
163 memcpy(n->verts, v, vnum * sizeof *n->verts);
165 n->front = n->back = 0;
169 static struct bspnode *add_poly_tree(struct bspnode *n, struct g3d_vertex *v, int vnum)
171 struct bspnode *nres;
172 int clipres, clipped_vnum;
173 struct g3d_vertex *clipped;
174 struct cplane negplane;
177 return new_node(v, vnum);
180 clipped = alloca((vnum + 1) * sizeof *clipped);
182 clipres = clip_poly(clipped, &clipped_vnum, v, vnum, &n->plane);
183 if(clipres > 0) { /* polygon completely in the positive subspace */
184 if(!(nres = add_poly_tree(n->front, v, vnum))) {
189 if(clipres < 0) { /* polygon completely in the negative subspace */
190 if(!(nres = add_poly_tree(n->back, v, vnum))) {
196 /* polygon is straddling the plane */
197 if(!(nres = add_poly_tree(n->front, clipped, clipped_vnum))) {
202 negplane.x = n->plane.x;
203 negplane.y = n->plane.y;
204 negplane.z = n->plane.z;
205 negplane.nx = -n->plane.nx;
206 negplane.ny = -n->plane.ny;
207 negplane.nz = -n->plane.nz;
209 clipres = clip_poly(clipped, &clipped_vnum, v, vnum, &negplane);
210 assert(clipres == 0);
212 if(!(nres = add_poly_tree(n->back, clipped, clipped_vnum))) {
219 static void save_bsp_tree(struct bspnode *n, FILE *fp)
224 static struct bspnode *load_bsp_tree(FILE *fp)