started BSP tree
[dosdemo] / src / bsptree.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <assert.h>
5 #include "bsptree.h"
6 #include "dynarr.h"
7 #include "inttypes.h"
8 #include "polyclip.h"
9 #include "vmath.h"
10
11 struct bspfile_header {
12         char magic[4];
13         uint32_t num_nodes;
14 };
15
16 static int count_nodes(struct bspnode *n);
17 static void free_tree(struct bspnode *n);
18
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);
21
22 static void save_bsp_tree(struct bspnode *n, FILE *fp);
23 static struct bspnode *load_bsp_tree(FILE *fp);
24
25 int init_bsp(struct bsptree *bsp)
26 {
27         bsp->root = 0;
28         return 0;
29 }
30
31 void destroy_bsp(struct bsptree *bsp)
32 {
33         free_tree(bsp->root);
34 }
35
36 int save_bsp(struct bsptree *bsp, const char *fname)
37 {
38         FILE *fp;
39         struct bspfile_header hdr;
40
41         if(!(fp = fopen(fname, "wb"))) {
42                 fprintf(stderr, "save_bsp: failed to open %s for writing\n", fname);
43                 return -1;
44         }
45
46         memcpy(hdr.magic, "MBSP", 4);
47         hdr.num_nodes = count_nodes(bsp->root);
48         fwrite(&hdr, sizeof hdr, 1, fp);
49
50         save_bsp_tree(bsp->root, fp);
51
52         fclose(fp);
53         return 0;
54 }
55
56 int load_bsp(struct bsptree *bsp, const char *fname)
57 {
58         FILE *fp;
59         struct bspfile_header hdr;
60
61         if(!(fp = fopen(fname, "rb"))) {
62                 fprintf(stderr, "load_bsp: failed to open %s\n", fname);
63                 return -1;
64         }
65
66         if(fread(&hdr, sizeof hdr, 1, fp) < 1) {
67                 fprintf(stderr, "load_bsp: %s: failed to read header\n", fname);
68                 fclose(fp);
69                 return -1;
70         }
71         if(memcmp(hdr.magic, "MBSP", 4) != 0) {
72                 fprintf(stderr, "load_bsp: %s: invalid magic\n", fname);
73                 fclose(fp);
74                 return -1;
75         }
76         bsp->root = load_bsp_tree(fp);
77
78         fclose(fp);
79         return 0;
80 }
81
82 int bsp_add_poly(struct bsptree *bsp, struct g3d_vertex *v, int vnum)
83 {
84         struct bspnode *n;
85         assert(vnum >= 3);
86
87         if(!(n = add_poly_tree(bsp->root, v, vnum))) {
88                 fprintf(stderr, "bsp_add_poly: failed to add polygon\n");
89                 return -1;
90         }
91         bsp->root = n;
92         return 0;
93 }
94
95 void bsp_add_mesh(struct bsptree *bsp, struct g3d_mesh *m)
96 {
97         int i, j, nfaces;
98         struct g3d_vertex v[4];
99         struct g3d_vertex *vptr = m->varr;
100         uint16_t *iptr = m->iarr;
101
102         nfaces = m->iarr ? m->icount / m->prim : m->vcount / m->prim;
103
104         for(i=0; i<nfaces; i++) {
105                 for(j=0; j<m->prim; j++) {
106                         if(m->iarr) {
107                                 v[j] = m->varr[*iptr++];
108                         } else {
109                                 v[j] = *vptr++;
110                         }
111                 }
112                 bsp_add_poly(bsp, v, m->prim);
113         }
114 }
115
116 void draw_bsp(struct bsptree *bsp, float view_x, float view_y, float view_z)
117 {
118 }
119
120 static int count_nodes(struct bspnode *n)
121 {
122         if(!n) return 0;
123         return count_nodes(n->front) + count_nodes(n->back) + 1;
124 }
125
126 static void free_tree(struct bspnode *n)
127 {
128         if(n) {
129                 free_tree(n->front);
130                 free_tree(n->back);
131                 free(n);
132         }
133 }
134
135
136 static struct bspnode *new_node(struct g3d_vertex *v, int vnum)
137 {
138         struct bspnode *n;
139         vec3_t va, vb, norm;
140
141         if(!(n = malloc(sizeof *n)) || !(n->verts = malloc(vnum * sizeof *n->verts))) {
142                 fprintf(stderr, "failed to allocate BSP tree node\n");
143                 free(n);
144                 return 0;
145         }
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);
153         v3_normalize(&norm);
154
155         n->plane.x = v[0].x;
156         n->plane.y = v[0].y;
157         n->plane.z = v[0].z;
158         n->plane.nx = norm.x;
159         n->plane.ny = norm.y;
160         n->plane.nz = norm.z;
161
162         n->vcount = vnum;
163         memcpy(n->verts, v, vnum * sizeof *n->verts);
164
165         n->front = n->back = 0;
166         return n;
167 }
168
169 static struct bspnode *add_poly_tree(struct bspnode *n, struct g3d_vertex *v, int vnum)
170 {
171         struct bspnode *nres;
172         int clipres, clipped_vnum;
173         struct g3d_vertex *clipped;
174         struct cplane negplane;
175
176         if(!n) {
177                 return new_node(v, vnum);
178         }
179
180         clipped = alloca((vnum + 1) * sizeof *clipped);
181
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))) {
185                         return 0;
186                 }
187                 n->front = nres;
188         }
189         if(clipres < 0) {       /* polygon completely in the negative subspace */
190                 if(!(nres = add_poly_tree(n->back, v, vnum))) {
191                         return 0;
192                 }
193                 n->back = nres;
194         }
195
196         /* polygon is straddling the plane */
197         if(!(nres = add_poly_tree(n->front, clipped, clipped_vnum))) {
198                 return 0;
199         }
200         n->front = nres;
201
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;
208
209         clipres = clip_poly(clipped, &clipped_vnum, v, vnum, &negplane);
210         assert(clipres == 0);
211
212         if(!(nres = add_poly_tree(n->back, clipped, clipped_vnum))) {
213                 return 0;
214         }
215         n->back = nres;
216         return n;
217 }
218
219 static void save_bsp_tree(struct bspnode *n, FILE *fp)
220 {
221         /* TODO */
222 }
223
224 static struct bspnode *load_bsp_tree(FILE *fp)
225 {
226         return 0;       /* TODO */
227 }