labyrinth
view src/mesh.c @ 3:45b91185b298
android port
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Fri, 01 May 2015 04:36:50 +0300 |
parents | 8ba79034e8a6 |
children | c8826e5ebec1 |
line source
1 #include <stdlib.h>
2 #include "opengl.h"
3 #include "mesh.h"
4 #include "objfile.h"
6 /* use objfile to load mesh data (vertices, normals, and texcoords)
7 * from a wavefront|OBJ file.
8 */
9 struct mesh *load_mesh(const char *fname)
10 {
11 int i;
12 struct mesh *m;
13 struct objfile *obj;
15 if(!(obj = objf_load(fname))) {
16 return 0;
17 }
18 m = malloc(sizeof *m);
19 m->num_verts = objf_vertex_count(obj);
21 m->vert = malloc(m->num_verts * sizeof *m->vert);
22 m->norm = malloc(m->num_verts * sizeof *m->norm);
23 m->tc = malloc(m->num_verts * sizeof *m->tc);
25 for(i=0; i<m->num_verts; i++) {
26 float *v = objf_vertex(obj, i);
27 float *n = objf_normal(obj, i);
28 float *t = objf_texcoord(obj, i);
30 m->vert[i].x = v[0];
31 m->vert[i].y = v[1];
32 m->vert[i].z = v[2];
34 m->norm[i].x = n[0];
35 m->norm[i].y = n[1];
36 m->norm[i].z = n[2];
38 m->tc[i].x = t[0];
39 m->tc[i].y = t[1];
40 }
41 objf_free(obj);
42 return m;
43 }
45 void free_mesh(struct mesh *m)
46 {
47 if(m) {
48 free(m->vert);
49 free(m->norm);
50 free(m->tc);
51 free(m);
52 }
53 }
55 void render_mesh(struct mesh *m)
56 {
57 glEnableClientState(GL_VERTEX_ARRAY);
58 glEnableClientState(GL_NORMAL_ARRAY);
59 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
61 glVertexPointer(3, GL_FLOAT, 0, m->vert);
62 glNormalPointer(GL_FLOAT, 0, m->norm);
63 glTexCoordPointer(2, GL_FLOAT, 0, m->tc);
65 glDrawArrays(GL_TRIANGLES, 0, m->num_verts);
67 glDisableClientState(GL_VERTEX_ARRAY);
68 glDisableClientState(GL_NORMAL_ARRAY);
69 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
70 }