labyrinth

annotate src/mesh.c @ 0:8ba79034e8a6

labyrinth example initial commit
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 15 Jan 2015 14:59:38 +0200
parents
children 45b91185b298
rev   line source
nuclear@0 1 #ifdef __APPLE__
nuclear@0 2 #include <GLUT/glut.h>
nuclear@0 3 #elif defined(WIN32)
nuclear@0 4 #include <glut.h>
nuclear@0 5 #else
nuclear@0 6 #include <GL/glut.h>
nuclear@0 7 #endif
nuclear@0 8
nuclear@0 9 #include "mesh.h"
nuclear@0 10 #include "objfile.h"
nuclear@0 11
nuclear@0 12 /* use objfile to load mesh data (vertices, normals, and texcoords)
nuclear@0 13 * from a wavefront|OBJ file.
nuclear@0 14 */
nuclear@0 15 struct mesh *load_mesh(const char *fname)
nuclear@0 16 {
nuclear@0 17 int i;
nuclear@0 18 struct mesh *m;
nuclear@0 19 struct objfile *obj;
nuclear@0 20
nuclear@0 21 if(!(obj = objf_load(fname))) {
nuclear@0 22 return 0;
nuclear@0 23 }
nuclear@0 24 m = malloc(sizeof *m);
nuclear@0 25 m->num_verts = objf_vertex_count(obj);
nuclear@0 26
nuclear@0 27 m->vert = malloc(m->num_verts * sizeof *m->vert);
nuclear@0 28 m->norm = malloc(m->num_verts * sizeof *m->norm);
nuclear@0 29 m->tc = malloc(m->num_verts * sizeof *m->tc);
nuclear@0 30
nuclear@0 31 for(i=0; i<m->num_verts; i++) {
nuclear@0 32 float *v = objf_vertex(obj, i);
nuclear@0 33 float *n = objf_normal(obj, i);
nuclear@0 34 float *t = objf_texcoord(obj, i);
nuclear@0 35
nuclear@0 36 m->vert[i].x = v[0];
nuclear@0 37 m->vert[i].y = v[1];
nuclear@0 38 m->vert[i].z = v[2];
nuclear@0 39
nuclear@0 40 m->norm[i].x = n[0];
nuclear@0 41 m->norm[i].y = n[1];
nuclear@0 42 m->norm[i].z = n[2];
nuclear@0 43
nuclear@0 44 m->tc[i].x = t[0];
nuclear@0 45 m->tc[i].y = t[1];
nuclear@0 46 }
nuclear@0 47 objf_free(obj);
nuclear@0 48 return m;
nuclear@0 49 }
nuclear@0 50
nuclear@0 51 void free_mesh(struct mesh *m)
nuclear@0 52 {
nuclear@0 53 if(m) {
nuclear@0 54 free(m->vert);
nuclear@0 55 free(m->norm);
nuclear@0 56 free(m->tc);
nuclear@0 57 free(m);
nuclear@0 58 }
nuclear@0 59 }
nuclear@0 60
nuclear@0 61 void render_mesh(struct mesh *m)
nuclear@0 62 {
nuclear@0 63 glEnableClientState(GL_VERTEX_ARRAY);
nuclear@0 64 glEnableClientState(GL_NORMAL_ARRAY);
nuclear@0 65 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
nuclear@0 66
nuclear@0 67 glVertexPointer(3, GL_FLOAT, 0, m->vert);
nuclear@0 68 glNormalPointer(GL_FLOAT, 0, m->norm);
nuclear@0 69 glTexCoordPointer(2, GL_FLOAT, 0, m->tc);
nuclear@0 70
nuclear@0 71 glDrawArrays(GL_TRIANGLES, 0, m->num_verts);
nuclear@0 72
nuclear@0 73 glDisableClientState(GL_VERTEX_ARRAY);
nuclear@0 74 glDisableClientState(GL_NORMAL_ARRAY);
nuclear@0 75 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
nuclear@0 76 }