- load rudimentary level file
[cyberay] / src / level.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <float.h>
4 #include "level.h"
5 #include "treestore.h"
6 #include "mesh.h"
7
8 static struct material *add_material(struct level *lvl, struct material *mtl);
9 static int append_polygons(struct bvhnode *bnode, struct triangle *faces, int num_faces, struct material *mtl);
10
11 int load_level(struct level *lvl, const char *fname)
12 {
13         char *dirname, *ptr;
14         char path[256];
15         struct ts_node *root, *node;
16         struct scenefile scn;
17         struct mesh *mesh;
18         struct material *mtl;
19
20         memset(lvl, 0, sizeof *lvl);
21         if(!(lvl->st_root = calloc(1, sizeof *lvl->st_root)) ||
22                         !(lvl->dyn_root = calloc(1, sizeof *lvl->dyn_root))) {
23                 free(lvl->st_root);
24                 fprintf(stderr, "load_level: failed to allocate bvh root nodes\n");
25                 return -1;
26         }
27
28         cgm_vcons(&lvl->st_root->aabb.vmin, FLT_MAX, FLT_MAX, FLT_MAX);
29         cgm_vcons(&lvl->st_root->aabb.vmax, -FLT_MAX, -FLT_MAX, -FLT_MAX);
30         lvl->dyn_root->aabb = lvl->st_root->aabb;
31
32         dirname = alloca(strlen(fname) + 1);
33         strcpy(dirname, fname);
34         if((ptr = strrchr(dirname, '/'))) {
35                 ptr[1] = 0;
36         } else {
37                 *dirname = 0;
38         }
39
40         if(!(root = ts_load(fname))) {
41                 fprintf(stderr, "load_level: failed to load: %s\n", fname);
42                 return -1;
43         }
44         if(strcmp(root->name, "level") != 0) {
45                 fprintf(stderr, "load_level: invalid level file %s, root is not \"level\"\n", fname);
46                 ts_free_tree(root);
47                 return -1;
48         }
49
50         node = root->child_list;
51         while(node) {
52                 if(strcmp(node->name, "scene") == 0) {
53                         if(!(fname = ts_get_attr_str(node, "file", 0))) {
54                                 fprintf(stderr, "load_level: ignoring \"scene\" without a \"file\" attribute\n");
55                                 goto cont;
56                         }
57                         snprintf(path, sizeof path, "%s%s", dirname, fname);
58                         printf("loading scene file: %s\n", path);
59
60                         if(load_scenefile(&scn, path) == -1) {
61                                 goto cont;
62                         }
63                         mesh = scn.meshlist;
64                         while(mesh) {
65                                 mtl = add_material(lvl, &mesh->mtl);
66                                 append_polygons(lvl->st_root, mesh->faces, mesh->num_faces, mtl);
67                                 mesh = mesh->next;
68                         }
69
70                         destroy_scenefile(&scn);
71                 }
72 cont:   node = node->next;
73         }
74
75         ts_free_tree(root);
76         return 0;
77 }
78
79 void destroy_level(struct level *lvl)
80 {
81         free_bvh_tree(lvl->st_root);
82         free_bvh_tree(lvl->dyn_root);
83         free(lvl->mtls);
84 }
85
86
87 int ray_level(cgm_ray *ray, struct level *lvl, float tmax, struct rayhit *hit)
88 {
89         return 0;
90 }
91
92 static void draw_level_rec(struct bvhnode *bn)
93 {
94         int i, j;
95         struct triangle *tri;
96         struct material *curmtl;
97         float color[4] = {0, 0, 0, 1};
98
99         if(bn->faces) {
100                 tri = bn->faces;
101                 curmtl = tri->mtl;
102
103                 glBegin(GL_TRIANGLES);
104                 for(i=0; i<bn->num_faces; i++) {
105                         if(tri->mtl != curmtl) {
106                                 glEnd();
107                                 color[0] = tri->mtl->color.x;
108                                 color[1] = tri->mtl->color.y;
109                                 color[2] = tri->mtl->color.z;
110                                 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, color);
111                                 curmtl = tri->mtl;
112                                 glBegin(GL_TRIANGLES);
113                         }
114
115                         for(j=0; j<3; j++) {
116                                 glNormal3fv(&tri->v[j].norm.x);
117                                 glTexCoord2fv(&tri->v[j].tex.x);
118                                 glVertex3fv(&tri->v[j].pos.x);
119                         }
120                         tri++;
121                 }
122                 glEnd();
123         }
124
125         bn = bn->sub;
126         while(bn) {
127                 draw_level_rec(bn);
128                 bn = bn->next;
129         }
130 }
131
132 void draw_level(struct level *lvl)
133 {
134         draw_level_rec(lvl->st_root);
135         draw_level_rec(lvl->dyn_root);
136 }
137
138 static struct material *add_material(struct level *lvl, struct material *mtl)
139 {
140         int i, newsz;
141         struct material *tmp;
142
143         for(i=0; i<lvl->num_mtls; i++) {
144                 if(memcmp(lvl->mtls + i, mtl, sizeof *mtl) == 0) {
145                         return lvl->mtls + i;
146                 }
147         }
148
149         if(lvl->num_mtls >= lvl->max_mtls) {
150                 newsz = lvl->max_mtls ? lvl->max_mtls * 2 : 16;
151                 if(!(tmp = realloc(lvl->mtls, newsz * sizeof *lvl->mtls))) {
152                         fprintf(stderr, "add_material: failed to resize materials array to %d\n", newsz);
153                         return 0;
154                 }
155                 lvl->mtls = tmp;
156                 lvl->max_mtls = newsz;
157         }
158         lvl->mtls[lvl->num_mtls] = *mtl;
159
160         return lvl->mtls + lvl->num_mtls++;
161 }
162
163 static int append_polygons(struct bvhnode *bnode, struct triangle *faces, int num_faces, struct material *mtl)
164 {
165         int i, j, newsz;
166         struct triangle *tri;
167
168         newsz = bnode->num_faces + num_faces;
169         if(!(tri = realloc(bnode->faces, newsz * sizeof *bnode->faces))) {
170                 fprintf(stderr, "append_polygons: failed to resize faces array to %d\n", newsz);
171                 return -1;
172         }
173         bnode->faces = tri;
174         tri += bnode->num_faces;
175         bnode->num_faces = newsz;
176
177         for(i=0; i<num_faces; i++) {
178                 *tri = *faces++;
179                 tri->mtl = mtl;
180
181                 for(j=0; j<3; j++) {
182                         cgm_vec3 *p = &tri->v[j].pos;
183                         if(p->x < bnode->aabb.vmin.x) bnode->aabb.vmin.x = p->x;
184                         if(p->x > bnode->aabb.vmax.x) bnode->aabb.vmax.x = p->x;
185                         if(p->y < bnode->aabb.vmin.y) bnode->aabb.vmin.y = p->y;
186                         if(p->y > bnode->aabb.vmax.y) bnode->aabb.vmax.y = p->y;
187                         if(p->z < bnode->aabb.vmin.z) bnode->aabb.vmin.z = p->z;
188                         if(p->z > bnode->aabb.vmax.z) bnode->aabb.vmax.z = p->z;
189                 }
190                 tri++;
191         }
192         return 0;
193 }