rev |
line source |
nuclear@3
|
1 #include <stdlib.h>
|
nuclear@5
|
2 #include <assert.h>
|
nuclear@3
|
3 #include "opengl.h"
|
nuclear@0
|
4 #include "mesh.h"
|
nuclear@0
|
5 #include "objfile.h"
|
nuclear@0
|
6
|
nuclear@5
|
7 static struct vec3 def_verts[] = {
|
nuclear@5
|
8 {-1, -1, 1}, {1, -1, 1}, {1, 1, 1}, {-1, -1, 1}, {1, 1, 1}, {-1, 1, 1},
|
nuclear@5
|
9 {1, -1, 1}, {1, -1, -1}, {1, 1, -1}, {1, -1, 1}, {1, 1, -1}, {1, 1, 1},
|
nuclear@5
|
10 {1, -1, -1}, {-1, -1, -1}, {-1, 1, -1}, {1, -1, -1}, {-1, 1, -1}, {1, 1, -1},
|
nuclear@5
|
11 {-1, -1, -1}, {-1, -1, 1}, {-1, 1, 1}, {-1, -1, -1}, {-1, 1, 1}, {-1, 1, -1}
|
nuclear@5
|
12 };
|
nuclear@5
|
13 #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
|
14 static struct vec3 def_norm[] = {
|
nuclear@5
|
15 FACEATTR(0, 0, 1),
|
nuclear@5
|
16 FACEATTR(1, 0, 0),
|
nuclear@5
|
17 FACEATTR(0, 0, -1),
|
nuclear@5
|
18 FACEATTR(-1, 0, 0)
|
nuclear@5
|
19 };
|
nuclear@5
|
20 static struct vec3 def_tc[] = {
|
nuclear@5
|
21 {0, 0, 0}, {1, 0, 0}, {1, 1, 0}, {0, 0, 0}, {1, 1, 0}, {0, 1, 0},
|
nuclear@5
|
22 {0, 0, 0}, {1, 0, 0}, {1, 1, 0}, {0, 0, 0}, {1, 1, 0}, {0, 1, 0},
|
nuclear@5
|
23 {0, 0, 0}, {1, 0, 0}, {1, 1, 0}, {0, 0, 0}, {1, 1, 0}, {0, 1, 0},
|
nuclear@5
|
24 {0, 0, 0}, {1, 0, 0}, {1, 1, 0}, {0, 0, 0}, {1, 1, 0}, {0, 1, 0}
|
nuclear@5
|
25 };
|
nuclear@5
|
26
|
nuclear@5
|
27 static struct mesh def_mesh = {
|
nuclear@5
|
28 def_verts, def_norm, def_tc,
|
nuclear@5
|
29 sizeof def_verts / sizeof *def_verts
|
nuclear@5
|
30 };
|
nuclear@5
|
31
|
nuclear@0
|
32 /* use objfile to load mesh data (vertices, normals, and texcoords)
|
nuclear@0
|
33 * from a wavefront|OBJ file.
|
nuclear@0
|
34 */
|
nuclear@0
|
35 struct mesh *load_mesh(const char *fname)
|
nuclear@0
|
36 {
|
nuclear@0
|
37 int i;
|
nuclear@0
|
38 struct mesh *m;
|
nuclear@0
|
39 struct objfile *obj;
|
nuclear@0
|
40
|
nuclear@0
|
41 if(!(obj = objf_load(fname))) {
|
nuclear@5
|
42 return &def_mesh;
|
nuclear@0
|
43 }
|
nuclear@0
|
44 m = malloc(sizeof *m);
|
nuclear@0
|
45 m->num_verts = objf_vertex_count(obj);
|
nuclear@0
|
46
|
nuclear@0
|
47 m->vert = malloc(m->num_verts * sizeof *m->vert);
|
nuclear@0
|
48 m->norm = malloc(m->num_verts * sizeof *m->norm);
|
nuclear@0
|
49 m->tc = malloc(m->num_verts * sizeof *m->tc);
|
nuclear@0
|
50
|
nuclear@0
|
51 for(i=0; i<m->num_verts; i++) {
|
nuclear@0
|
52 float *v = objf_vertex(obj, i);
|
nuclear@0
|
53 float *n = objf_normal(obj, i);
|
nuclear@0
|
54 float *t = objf_texcoord(obj, i);
|
nuclear@0
|
55
|
nuclear@0
|
56 m->vert[i].x = v[0];
|
nuclear@0
|
57 m->vert[i].y = v[1];
|
nuclear@0
|
58 m->vert[i].z = v[2];
|
nuclear@0
|
59
|
nuclear@0
|
60 m->norm[i].x = n[0];
|
nuclear@0
|
61 m->norm[i].y = n[1];
|
nuclear@0
|
62 m->norm[i].z = n[2];
|
nuclear@0
|
63
|
nuclear@0
|
64 m->tc[i].x = t[0];
|
nuclear@0
|
65 m->tc[i].y = t[1];
|
nuclear@0
|
66 }
|
nuclear@0
|
67 objf_free(obj);
|
nuclear@0
|
68 return m;
|
nuclear@0
|
69 }
|
nuclear@0
|
70
|
nuclear@0
|
71 void free_mesh(struct mesh *m)
|
nuclear@0
|
72 {
|
nuclear@5
|
73 if(m && m != &def_mesh) {
|
nuclear@0
|
74 free(m->vert);
|
nuclear@0
|
75 free(m->norm);
|
nuclear@0
|
76 free(m->tc);
|
nuclear@0
|
77 free(m);
|
nuclear@0
|
78 }
|
nuclear@0
|
79 }
|
nuclear@0
|
80
|
nuclear@0
|
81 void render_mesh(struct mesh *m)
|
nuclear@0
|
82 {
|
nuclear@0
|
83 glEnableClientState(GL_VERTEX_ARRAY);
|
nuclear@0
|
84 glEnableClientState(GL_NORMAL_ARRAY);
|
nuclear@0
|
85 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
|
nuclear@0
|
86
|
nuclear@0
|
87 glVertexPointer(3, GL_FLOAT, 0, m->vert);
|
nuclear@0
|
88 glNormalPointer(GL_FLOAT, 0, m->norm);
|
nuclear@0
|
89 glTexCoordPointer(2, GL_FLOAT, 0, m->tc);
|
nuclear@0
|
90
|
nuclear@0
|
91 glDrawArrays(GL_TRIANGLES, 0, m->num_verts);
|
nuclear@0
|
92
|
nuclear@0
|
93 glDisableClientState(GL_VERTEX_ARRAY);
|
nuclear@0
|
94 glDisableClientState(GL_NORMAL_ARRAY);
|
nuclear@0
|
95 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
|
nuclear@0
|
96 }
|