nuclear@0: #ifdef __APPLE__ nuclear@0: #include nuclear@0: #elif defined(WIN32) nuclear@0: #include nuclear@0: #else nuclear@0: #include nuclear@0: #endif nuclear@0: nuclear@0: #include "mesh.h" nuclear@0: #include "objfile.h" nuclear@0: 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@0: return 0; 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@0: if(m) { 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: }