From: John Tsiombikas Date: Wed, 16 Jun 2021 21:14:44 +0000 (+0300) Subject: started the level loading routines X-Git-Url: http://git.mutantstargoat.com/user/nuclear/?p=cyberay;a=commitdiff_plain;h=721cbdcc5f52c2dda7eb03e645bb92521180a3fa started the level loading routines --- diff --git a/src/level.c b/src/level.c new file mode 100644 index 0000000..4d1e30e --- /dev/null +++ b/src/level.c @@ -0,0 +1,11 @@ +#include +#include "level.h" + + +int load_level(struct level *lvl, const char *fname) +{ +} + +int ray_level(cgm_ray *ray, struct level *lvl, float maxt, struct hitpoint *hit) +{ +} diff --git a/src/mesh.c b/src/mesh.c new file mode 100644 index 0000000..a84b347 --- /dev/null +++ b/src/mesh.c @@ -0,0 +1,103 @@ +#include +#include +#include "mesh.h" + +static char *cleanline(char *s) +{ + char *ptr; + + if((ptr = strchr(s, '#'))) *ptr = 0; + + while(*s && isspace(*s)) s++; + ptr = s + strlen(s) - 1; + while(ptr >= s && isspace(*s)) *ptr-- = 0; + + return *s ? s : 0; +} + +#define APPEND(prefix) \ + do { \ + if(prefix##count >= prefix##max) { \ + int newsz = prefix##max ? prefix##max * 2 : 8; \ + void *ptr = realloc(prefix##arr, newsz * sizeof(cgm_vec3)); \ + if(!ptr) { \ + fprintf(stderr, "load_mesh: failed to resize array to %d elements\n", newsz); \ + return -1; \ + } \ + prefix##arr = ptr; \ + prefix##max = newsz; \ + } \ + } while(0) + + +int load_mesh(struct mesh *m, const char *fname) +{ + int num, nline; + FILE *fp; + cgm_vec3 v; + cgm_vec3 *varr, *narr, *tarr; + int vcount, ncount, tcount, vmax, nmax, tmax; + char linebuf[256], *line; + + varr = narr = tarr = 0; + vcount = ncount = tcount = vmax = nmax = tmax = 0; + + if(!(fp = fopen(fname, "rb"))) { + fprintf(stderr, "load_mesh: failed to open: %s\n", fname); + return -1; + } + + nline = 0; + while(fgets(linebuf, sizeof linebuf, fp)) { + nline++; + if(!(line = cleanline(linebuf))) { + continue; + } + + switch(line[0]) { + case 'v': + v.y = v.z = 0; + if((num = sscanf(line + 1, "%f %f %f", &v.x, &v.y, &v.z)) < 2) { +verr: fprintf(stderr, "load_mesh: ignoring malformed attribute at %d: %s\n", nline, line); + continue; + } + if(isspace(line[1])) { + APPEND(v); + varr[vcount++] = v; + } else if(line[1] == 'n') { + APPEND(n); + narr[ncount++] = v; + } else if(line[1] == 't') { + APPEND(t); + tarr[ncount].x = v.x; + tarr[ncount++].y = v.y; + } else { + goto verr; + } + break; + + /* TODO cont */ + } + + } + + fclose(fp); + return 0; +} + +void draw_mesh(struct mesh *m) +{ + glEnableClientState(GL_VERTEX_ARRAY); + glEnableClientState(GL_NORMAL_ARRAY); + glEnableClientState(GL_TEXTURE_COORD_ARRAY); + + glVertexPointer(3, GL_FLOAT, sizeof *m->faces, &m->faces->v[0].pos.x); + glNormalPointer(GL_FLOAT, sizeof *m->faces, &m->faces->v[0].norm.x); + glTexCoordPointer(2, GL_FLOAT, sizeof *m->faces, &m->faces->v[0].tex.x); + + glDrawArrays(GL_TRIANGLES, 0, m->num_faces * 3); + + glDisableClientState(GL_VERTEX_ARRAY); + glDisableClientState(GL_NORMAL_ARRAY); + glDisableClientState(GL_TEXTURE_COORD_ARRAY); +} diff --git a/src/mesh.h b/src/mesh.h new file mode 100644 index 0000000..b97f9cd --- /dev/null +++ b/src/mesh.h @@ -0,0 +1,20 @@ +#ifndef MESH_H_ +#define MESH_H_ + +#include +#include +#include "geom.h" + +struct mesh { + struct triangle *faces; + int num_faces; + + struct aabox aabb; + + struct material mtl; +}; + +int load_mesh(struct mesh *m, const char *fname); +void draw_mesh(struct mesh *m); + +#endif /* MESH_H_ */ diff --git a/src/scene.h b/src/scene.h deleted file mode 100644 index 0b13c51..0000000 --- a/src/scene.h +++ /dev/null @@ -1,7 +0,0 @@ -#ifndef SCENE_H_ -#define SCENE_H_ - -struct scene { -}; - -#endif /* SCENE_H_ */