shell mesh
[meshfrac] / src / cmesh.h
1 #ifndef CMESH_H_
2 #define CMESH_H_
3
4 #include <stdio.h>
5 #include "cgmath/cgmath.h"
6
7 enum {
8         CMESH_ATTR_VERTEX,
9         CMESH_ATTR_NORMAL,
10         CMESH_ATTR_TANGENT,
11         CMESH_ATTR_TEXCOORD,
12         CMESH_ATTR_COLOR,
13         CMESH_ATTR_BONEWEIGHTS,
14         CMESH_ATTR_BONEIDX,
15         CMESH_ATTR_TEXCOORD2,
16
17         CMESH_NUM_ATTR
18 };
19
20 struct cmesh;
21
22 /* global state */
23 void cmesh_set_attrib_sdrloc(int attr, int loc);
24 int cmesh_get_attrib_sdrloc(int attr);
25 void cmesh_clear_attrib_sdrloc(void);
26
27 /* mesh functions */
28 struct cmesh *cmesh_alloc(void);
29 void cmesh_free(struct cmesh *cm);
30
31 int cmesh_init(struct cmesh *cm);
32 void cmesh_destroy(struct cmesh *cm);
33
34 void cmesh_clear(struct cmesh *cm);
35 int cmesh_clone(struct cmesh *cmdest, const struct cmesh *cmsrc);
36
37 int cmesh_set_name(struct cmesh *cm, const char *name);
38 const char *cmesh_name(const struct cmesh *cm);
39
40 int cmesh_has_attrib(const struct cmesh *cm, int attr);
41 int cmesh_indexed(const struct cmesh *cm);
42
43 /* vdata can be 0, in which case only memory is allocated
44  * returns pointer to the attribute array
45  */
46 float *cmesh_set_attrib(struct cmesh *cm, int attr, int nelem, unsigned int num,
47                 const float *vdata);
48 float *cmesh_attrib(struct cmesh *cm, int attr);                        /* invalidates VBO */
49 const float *cmesh_attrib_ro(const struct cmesh *cm, int attr); /* doesn't invalidate */
50 float *cmesh_attrib_at(struct cmesh *cm, int attr, int idx);
51 const float *cmesh_attrib_at_ro(const struct cmesh *cm, int attr, int idx);
52 int cmesh_attrib_count(const struct cmesh *cm, int attr);
53 int cmesh_attrib_nelem(const struct cmesh *cm, int attr);
54 int cmesh_push_attrib(struct cmesh *cm, int attr, float *v);
55 int cmesh_push_attrib1f(struct cmesh *cm, int attr, float x);
56 int cmesh_push_attrib2f(struct cmesh *cm, int attr, float x, float y);
57 int cmesh_push_attrib3f(struct cmesh *cm, int attr, float x, float y, float z);
58 int cmesh_push_attrib4f(struct cmesh *cm, int attr, float x, float y, float z, float w);
59
60 /* indices can be 0, in which case only memory is allocated
61  * returns pointer to the index array
62  */
63 unsigned int *cmesh_set_index(struct cmesh *cm, int num, const unsigned int *indices);
64 unsigned int *cmesh_index(struct cmesh *cm);    /* invalidates IBO */
65 const unsigned int *cmesh_index_ro(const struct cmesh *cm);     /* doesn't invalidate */
66 int cmesh_index_count(const struct cmesh *cm);
67 int cmesh_push_index(struct cmesh *cm, unsigned int idx);
68
69 int cmesh_poly_count(const struct cmesh *cm);
70
71 /* attr can be -1 to invalidate all attributes */
72 void cmesh_invalidate_vbo(struct cmesh *cm, int attr);
73 void cmesh_invalidate_ibo(struct cmesh *cm);
74
75 int cmesh_append(struct cmesh *cmdest, const struct cmesh *cmsrc);
76
77 /* submeshes */
78 void cmesh_clear_submeshes(struct cmesh *cm);
79 /* a submesh is defined as a consecutive range of faces */
80 int cmesh_submesh(struct cmesh *cm, const char *name, int fstart, int fcount);
81 int cmesh_remove_submesh(struct cmesh *cm, int idx);
82 int cmesh_find_submesh(const struct cmesh *cm, const char *name);
83 int cmesh_submesh_count(const struct cmesh *cm);
84 int cmesh_clone_submesh(struct cmesh *cmdest, const struct cmesh *cm, int subidx);
85
86 /* immediate-mode style mesh construction interface */
87 int cmesh_vertex(struct cmesh *cm, float x, float y, float z);
88 void cmesh_normal(struct cmesh *cm, float nx, float ny, float nz);
89 void cmesh_tangent(struct cmesh *cm, float tx, float ty, float tz);
90 void cmesh_texcoord(struct cmesh *cm, float u, float v, float w);
91 void cmesh_boneweights(struct cmesh *cm, float w1, float w2, float w3, float w4);
92 void cmesh_boneidx(struct cmesh *cm, int idx1, int idx2, int idx3, int idx4);
93
94 /* dir_xform can be null, in which case it's calculated from xform */
95 void cmesh_apply_xform(struct cmesh *cm, float *xform, float *dir_xform);
96
97 void cmesh_flip(struct cmesh *cm);      /* flip faces (winding) and normals */
98 void cmesh_flip_faces(struct cmesh *cm);
99 void cmesh_flip_normals(struct cmesh *cm);
100
101 int cmesh_explode(struct cmesh *cm);    /* undo all vertex sharing */
102
103 /* this is only guaranteed to work on an exploded mesh */
104 void cmesh_calc_face_normals(struct cmesh *cm);
105
106 void cmesh_draw(const struct cmesh *cm);
107 void cmesh_draw_range(const struct cmesh *cm, int start, int count);
108 void cmesh_draw_submesh(const struct cmesh *cm, int subidx);    /* XXX only for indexed meshes currently */
109 void cmesh_draw_wire(const struct cmesh *cm, float linesz);
110 void cmesh_draw_vertices(const struct cmesh *cm, float ptsz);
111 void cmesh_draw_normals(const struct cmesh *cm, float len);
112 void cmesh_draw_tangents(const struct cmesh *cm, float len);
113
114 /* get the bounding box in local space. The result will be cached and subsequent
115  * calls will return the same box. The cache gets invalidated by any functions that
116  * can affect the vertex data
117  */
118 void cmesh_aabbox(const struct cmesh *cm, cgm_vec3 *vmin, cgm_vec3 *vmax);
119
120 /* get the bounding sphere in local space. The result will be cached ... see above */
121 float cmesh_bsphere(const struct cmesh *cm, cgm_vec3 *center, float *rad);
122
123 /* texture coordinate manipulation */
124 void cmesh_texcoord_apply_xform(struct cmesh *cm, float *xform);
125 void cmesh_texcoord_gen_plane(struct cmesh *cm, cgm_vec3 *norm, cgm_vec3 *tang);
126 void cmesh_texcoord_gen_box(struct cmesh *cm);
127 void cmesh_texcoord_gen_cylinder(struct cmesh *cm);
128
129 /* FILE I/O */
130 int cmesh_load(struct cmesh *cm, const char *fname);
131
132 int cmesh_dump(const struct cmesh *cm, const char *fname);
133 int cmesh_dump_file(const struct cmesh *cm, FILE *fp);
134 int cmesh_dump_obj(const struct cmesh *cm, const char *fname);
135 int cmesh_dump_obj_file(const struct cmesh *cm, FILE *fp, int voffs);
136
137
138
139 #endif  /* CMESH_H_ */