labyrinth

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