initial commit
[liquidmodel] / 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 enum {
23         CMESH_TEX_COLOR,
24         CMESH_TEX_SPECULAR,
25         CMESH_TEX_REFLECT,
26         CMESH_TEX_BUMP,
27         CMESH_TEX_LIGHT,
28
29         CMESH_NUM_TEX
30 };
31
32 struct cmesh_texture {
33         char *name;
34         unsigned int id;
35 };
36
37 struct cmesh_material {
38         char *name;
39         cgm_vec3 color, specular, emissive;
40         float alpha;
41         float roughness, ior;
42         struct cmesh_texture tex[CMESH_NUM_TEX];
43 };
44
45 /* global state */
46 void cmesh_set_attrib_sdrloc(int attr, int loc);
47 int cmesh_get_attrib_sdrloc(int attr);
48 void cmesh_clear_attrib_sdrloc(void);
49
50 /* cmesh_bind_sdrloc will bind default attribute names (if they exist in the
51  * shader program) to locations used by cmesh_draw:
52  *   attr_vertex, attr_normal, attr_tangent, attr_texcoord, attr_color,
53  *   attr_boneweights, attr_boneidx, attr_texcoord2
54  */
55 void cmesh_bind_sdrloc(unsigned int sdr);
56
57 /* mesh functions */
58 struct cmesh *cmesh_alloc(void);
59 void cmesh_free(struct cmesh *cm);
60
61 int cmesh_init(struct cmesh *cm);
62 void cmesh_destroy(struct cmesh *cm);
63
64 void cmesh_clear(struct cmesh *cm);
65 int cmesh_clone(struct cmesh *cmdest, struct cmesh *cmsrc);
66
67 int cmesh_set_name(struct cmesh *cm, const char *name);
68 const char *cmesh_name(struct cmesh *cm);
69
70 struct cmesh_material *cmesh_material(struct cmesh *cm);
71 struct cmesh_material *cmesh_submesh_material(struct cmesh *cm, int subidx);
72 int cmesh_load_textures(struct cmesh *cm);
73
74 int cmesh_has_attrib(struct cmesh *cm, int attr);
75 int cmesh_indexed(struct cmesh *cm);
76
77 /* vdata can be 0, in which case only memory is allocated
78  * returns pointer to the attribute array
79  */
80 float *cmesh_set_attrib(struct cmesh *cm, int attr, int nelem, unsigned int num,
81                 const float *vdata);
82 float *cmesh_attrib(struct cmesh *cm, int attr);                        /* invalidates VBO */
83 const float *cmesh_attrib_ro(struct cmesh *cm, int attr);       /* doesn't invalidate */
84 float *cmesh_attrib_at(struct cmesh *cm, int attr, int idx);
85 const float *cmesh_attrib_at_ro(struct cmesh *cm, int attr, int idx);
86 int cmesh_attrib_count(struct cmesh *cm, int attr);
87 int cmesh_push_attrib(struct cmesh *cm, int attr, float *v);
88 int cmesh_push_attrib1f(struct cmesh *cm, int attr, float x);
89 int cmesh_push_attrib2f(struct cmesh *cm, int attr, float x, float y);
90 int cmesh_push_attrib3f(struct cmesh *cm, int attr, float x, float y, float z);
91 int cmesh_push_attrib4f(struct cmesh *cm, int attr, float x, float y, float z, float w);
92
93 /* indices can be 0, in which case only memory is allocated
94  * returns pointer to the index array
95  */
96 unsigned int *cmesh_set_index(struct cmesh *cm, int num, const unsigned int *indices);
97 unsigned int *cmesh_index(struct cmesh *cm);    /* invalidates IBO */
98 const unsigned int *cmesh_index_ro(struct cmesh *cm);   /* doesn't invalidate */
99 int cmesh_index_count(struct cmesh *cm);
100 int cmesh_push_index(struct cmesh *cm, unsigned int idx);
101
102 int cmesh_poly_count(struct cmesh *cm);
103
104 /* attr can be -1 to invalidate all attributes */
105 void cmesh_invalidate_vbo(struct cmesh *cm, int attr);
106 void cmesh_invalidate_ibo(struct cmesh *cm);
107
108 int cmesh_append(struct cmesh *cmdest, struct cmesh *cmsrc);
109
110 /* submeshes */
111 void cmesh_clear_submeshes(struct cmesh *cm);
112 /* a submesh is defined as a consecutive range of faces */
113 int cmesh_submesh(struct cmesh *cm, const char *name, int fstart, int fcount);
114 int cmesh_remove_submesh(struct cmesh *cm, int idx);
115 int cmesh_find_submesh(struct cmesh *cm, const char *name);
116 int cmesh_submesh_count(struct cmesh *cm);
117 int cmesh_clone_submesh(struct cmesh *cmdest, struct cmesh *cm, int subidx);
118 const char *cmesh_submesh_name(struct cmesh *cm, int idx);
119
120 /* immediate-mode style mesh construction interface */
121 int cmesh_vertex(struct cmesh *cm, float x, float y, float z);
122 void cmesh_normal(struct cmesh *cm, float nx, float ny, float nz);
123 void cmesh_tangent(struct cmesh *cm, float tx, float ty, float tz);
124 void cmesh_texcoord(struct cmesh *cm, float u, float v, float w);
125 void cmesh_boneweights(struct cmesh *cm, float w1, float w2, float w3, float w4);
126 void cmesh_boneidx(struct cmesh *cm, int idx1, int idx2, int idx3, int idx4);
127
128 /* dir_xform can be null, in which case it's calculated from xform */
129 void cmesh_apply_xform(struct cmesh *cm, float *xform, float *dir_xform);
130
131 void cmesh_flip(struct cmesh *cm);      /* flip faces (winding) and normals */
132 void cmesh_flip_faces(struct cmesh *cm);
133 void cmesh_flip_normals(struct cmesh *cm);
134
135 int cmesh_explode(struct cmesh *cm);    /* undo all vertex sharing */
136
137 /* this is only guaranteed to work on an exploded mesh */
138 void cmesh_calc_face_normals(struct cmesh *cm);
139
140 void cmesh_draw(struct cmesh *cm);
141 void cmesh_draw_range(struct cmesh *cm, int start, int count);
142 void cmesh_draw_submesh(struct cmesh *cm, int subidx);  /* XXX only for indexed meshes currently */
143 void cmesh_draw_wire(struct cmesh *cm, float linesz);
144 void cmesh_draw_vertices(struct cmesh *cm, float ptsz);
145 void cmesh_draw_normals(struct cmesh *cm, float len);
146 void cmesh_draw_tangents(struct cmesh *cm, float len);
147
148 /* get the bounding box in local space. The result will be cached and subsequent
149  * calls will return the same box. The cache gets invalidated by any functions that
150  * can affect the vertex data
151  */
152 void cmesh_aabbox(struct cmesh *cm, cgm_vec3 *vmin, cgm_vec3 *vmax);
153
154 /* get the bounding sphere in local space. The result will be cached ... see above */
155 float cmesh_bsphere(struct cmesh *cm, cgm_vec3 *center, float *rad);
156 float cmesh_submesh_bsphere(struct cmesh *cm, int subidx, cgm_vec3 *center, float *rad);
157
158 /* texture coordinate manipulation */
159 void cmesh_texcoord_apply_xform(struct cmesh *cm, float *xform);
160 void cmesh_texcoord_gen_plane(struct cmesh *cm, cgm_vec3 *norm, cgm_vec3 *tang);
161 void cmesh_texcoord_gen_box(struct cmesh *cm);
162 void cmesh_texcoord_gen_cylinder(struct cmesh *cm);
163
164 /* FILE I/O */
165 int cmesh_load(struct cmesh *cm, const char *fname);
166
167 int cmesh_dump(struct cmesh *cm, const char *fname);
168 int cmesh_dump_file(struct cmesh *cm, FILE *fp);
169 int cmesh_dump_obj(struct cmesh *cm, const char *fname);
170 int cmesh_dump_obj_file(struct cmesh *cm, FILE *fp, int voffs);
171
172
173
174 #endif  /* CMESH_H_ */