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