nuclear@3: #include nuclear@5: #include nuclear@3: #include "opengl.h" nuclear@0: #include "mesh.h" nuclear@0: #include "objfile.h" nuclear@0: nuclear@5: static struct vec3 def_verts[] = { nuclear@5: {-1, -1, 1}, {1, -1, 1}, {1, 1, 1}, {-1, -1, 1}, {1, 1, 1}, {-1, 1, 1}, nuclear@5: {1, -1, 1}, {1, -1, -1}, {1, 1, -1}, {1, -1, 1}, {1, 1, -1}, {1, 1, 1}, nuclear@5: {1, -1, -1}, {-1, -1, -1}, {-1, 1, -1}, {1, -1, -1}, {-1, 1, -1}, {1, 1, -1}, nuclear@5: {-1, -1, -1}, {-1, -1, 1}, {-1, 1, 1}, {-1, -1, -1}, {-1, 1, 1}, {-1, 1, -1} nuclear@5: }; nuclear@5: #define FACEATTR(x, y, z) {x, y, z}, {x, y, z}, {x, y, z}, {x, y, z}, {x, y, z}, {x, y, z} nuclear@5: static struct vec3 def_norm[] = { nuclear@5: FACEATTR(0, 0, 1), nuclear@5: FACEATTR(1, 0, 0), nuclear@5: FACEATTR(0, 0, -1), nuclear@5: FACEATTR(-1, 0, 0) nuclear@5: }; nuclear@5: static struct vec3 def_tc[] = { nuclear@5: {0, 0, 0}, {1, 0, 0}, {1, 1, 0}, {0, 0, 0}, {1, 1, 0}, {0, 1, 0}, nuclear@5: {0, 0, 0}, {1, 0, 0}, {1, 1, 0}, {0, 0, 0}, {1, 1, 0}, {0, 1, 0}, nuclear@5: {0, 0, 0}, {1, 0, 0}, {1, 1, 0}, {0, 0, 0}, {1, 1, 0}, {0, 1, 0}, nuclear@5: {0, 0, 0}, {1, 0, 0}, {1, 1, 0}, {0, 0, 0}, {1, 1, 0}, {0, 1, 0} nuclear@5: }; nuclear@5: nuclear@5: static struct mesh def_mesh = { nuclear@5: def_verts, def_norm, def_tc, nuclear@5: sizeof def_verts / sizeof *def_verts nuclear@5: }; nuclear@5: nuclear@0: /* use objfile to load mesh data (vertices, normals, and texcoords) nuclear@0: * from a wavefront|OBJ file. nuclear@0: */ nuclear@0: struct mesh *load_mesh(const char *fname) nuclear@0: { nuclear@0: int i; nuclear@0: struct mesh *m; nuclear@0: struct objfile *obj; nuclear@0: nuclear@0: if(!(obj = objf_load(fname))) { nuclear@5: return &def_mesh; nuclear@0: } nuclear@0: m = malloc(sizeof *m); nuclear@0: m->num_verts = objf_vertex_count(obj); nuclear@0: nuclear@0: m->vert = malloc(m->num_verts * sizeof *m->vert); nuclear@0: m->norm = malloc(m->num_verts * sizeof *m->norm); nuclear@0: m->tc = malloc(m->num_verts * sizeof *m->tc); nuclear@0: nuclear@0: for(i=0; inum_verts; i++) { nuclear@0: float *v = objf_vertex(obj, i); nuclear@0: float *n = objf_normal(obj, i); nuclear@0: float *t = objf_texcoord(obj, i); nuclear@0: nuclear@0: m->vert[i].x = v[0]; nuclear@0: m->vert[i].y = v[1]; nuclear@0: m->vert[i].z = v[2]; nuclear@0: nuclear@0: m->norm[i].x = n[0]; nuclear@0: m->norm[i].y = n[1]; nuclear@0: m->norm[i].z = n[2]; nuclear@0: nuclear@0: m->tc[i].x = t[0]; nuclear@0: m->tc[i].y = t[1]; nuclear@0: } nuclear@0: objf_free(obj); nuclear@0: return m; nuclear@0: } nuclear@0: nuclear@0: void free_mesh(struct mesh *m) nuclear@0: { nuclear@5: if(m && m != &def_mesh) { nuclear@0: free(m->vert); nuclear@0: free(m->norm); nuclear@0: free(m->tc); nuclear@0: free(m); nuclear@0: } nuclear@0: } nuclear@0: nuclear@0: void render_mesh(struct mesh *m) nuclear@0: { nuclear@0: glEnableClientState(GL_VERTEX_ARRAY); nuclear@0: glEnableClientState(GL_NORMAL_ARRAY); nuclear@0: glEnableClientState(GL_TEXTURE_COORD_ARRAY); nuclear@0: nuclear@0: glVertexPointer(3, GL_FLOAT, 0, m->vert); nuclear@0: glNormalPointer(GL_FLOAT, 0, m->norm); nuclear@0: glTexCoordPointer(2, GL_FLOAT, 0, m->tc); nuclear@0: nuclear@0: glDrawArrays(GL_TRIANGLES, 0, m->num_verts); nuclear@0: nuclear@0: glDisableClientState(GL_VERTEX_ARRAY); nuclear@0: glDisableClientState(GL_NORMAL_ARRAY); nuclear@0: glDisableClientState(GL_TEXTURE_COORD_ARRAY); nuclear@0: }