started ropesim tool
[dosdemo] / tools / ropesim / 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, struct cmesh *cmsrc);
36
37 int cmesh_set_name(struct cmesh *cm, const char *name);
38 const char *cmesh_name(struct cmesh *cm);
39
40 int cmesh_has_attrib(struct cmesh *cm, int attr);
41 int cmesh_indexed(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(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(struct cmesh *cm, int attr, int idx);
52 int cmesh_attrib_count(struct cmesh *cm, int attr);
53 int cmesh_push_attrib(struct cmesh *cm, int attr, float *v);
54 int cmesh_push_attrib1f(struct cmesh *cm, int attr, float x);
55 int cmesh_push_attrib2f(struct cmesh *cm, int attr, float x, float y);
56 int cmesh_push_attrib3f(struct cmesh *cm, int attr, float x, float y, float z);
57 int cmesh_push_attrib4f(struct cmesh *cm, int attr, float x, float y, float z, float w);
58
59 /* indices can be 0, in which case only memory is allocated
60  * returns pointer to the index array
61  */
62 unsigned int *cmesh_set_index(struct cmesh *cm, int num, const unsigned int *indices);
63 unsigned int *cmesh_index(struct cmesh *cm);    /* invalidates IBO */
64 const unsigned int *cmesh_index_ro(struct cmesh *cm);   /* doesn't invalidate */
65 int cmesh_index_count(struct cmesh *cm);
66 int cmesh_push_index(struct cmesh *cm, unsigned int idx);
67
68 int cmesh_poly_count(struct cmesh *cm);
69
70 /* attr can be -1 to invalidate all attributes */
71 void cmesh_invalidate_vbo(struct cmesh *cm, int attr);
72 void cmesh_invalidate_ibo(struct cmesh *cm);
73
74 int cmesh_append(struct cmesh *cmdest, struct cmesh *cmsrc);
75
76 /* submeshes */
77 void cmesh_clear_submeshes(struct cmesh *cm);
78 /* a submesh is defined as a consecutive range of faces */
79 int cmesh_submesh(struct cmesh *cm, const char *name, int fstart, int fcount);
80 int cmesh_remove_submesh(struct cmesh *cm, int idx);
81 int cmesh_find_submesh(struct cmesh *cm, const char *name);
82 int cmesh_submesh_count(struct cmesh *cm);
83 int cmesh_clone_submesh(struct cmesh *cmdest, struct cmesh *cm, int subidx);
84
85 /* immediate-mode style mesh construction interface */
86 int cmesh_vertex(struct cmesh *cm, float x, float y, float z);
87 void cmesh_normal(struct cmesh *cm, float nx, float ny, float nz);
88 void cmesh_tangent(struct cmesh *cm, float tx, float ty, float tz);
89 void cmesh_texcoord(struct cmesh *cm, float u, float v, float w);
90 void cmesh_boneweights(struct cmesh *cm, float w1, float w2, float w3, float w4);
91 void cmesh_boneidx(struct cmesh *cm, int idx1, int idx2, int idx3, int idx4);
92
93 /* dir_xform can be null, in which case it's calculated from xform */
94 void cmesh_apply_xform(struct cmesh *cm, float *xform, float *dir_xform);
95
96 void cmesh_flip(struct cmesh *cm);      /* flip faces (winding) and normals */
97 void cmesh_flip_faces(struct cmesh *cm);
98 void cmesh_flip_normals(struct cmesh *cm);
99
100 int cmesh_explode(struct cmesh *cm);    /* undo all vertex sharing */
101
102 /* this is only guaranteed to work on an exploded mesh */
103 void cmesh_calc_face_normals(struct cmesh *cm);
104
105 void cmesh_draw(struct cmesh *cm);
106 void cmesh_draw_range(struct cmesh *cm, int start, int count);
107 void cmesh_draw_submesh(struct cmesh *cm, int subidx);  /* XXX only for indexed meshes currently */
108 void cmesh_draw_wire(struct cmesh *cm, float linesz);
109 void cmesh_draw_vertices(struct cmesh *cm, float ptsz);
110 void cmesh_draw_normals(struct cmesh *cm, float len);
111 void cmesh_draw_tangents(struct cmesh *cm, float len);
112
113 /* get the bounding box in local space. The result will be cached and subsequent
114  * calls will return the same box. The cache gets invalidated by any functions that
115  * can affect the vertex data
116  */
117 void cmesh_aabbox(struct cmesh *cm, cgm_vec3 *vmin, cgm_vec3 *vmax);
118
119 /* get the bounding sphere in local space. The result will be cached ... see above */
120 float cmesh_bsphere(struct cmesh *cm, cgm_vec3 *center, float *rad);
121
122 /* texture coordinate manipulation */
123 void cmesh_texcoord_apply_xform(struct cmesh *cm, float *xform);
124 void cmesh_texcoord_gen_plane(struct cmesh *cm, cgm_vec3 *norm, cgm_vec3 *tang);
125 void cmesh_texcoord_gen_box(struct cmesh *cm);
126 void cmesh_texcoord_gen_cylinder(struct cmesh *cm);
127
128 /* FILE I/O */
129 int cmesh_load(struct cmesh *cm, const char *fname);
130
131 int cmesh_dump(struct cmesh *cm, const char *fname);
132 int cmesh_dump_file(struct cmesh *cm, FILE *fp);
133 int cmesh_dump_obj(struct cmesh *cm, const char *fname);
134 int cmesh_dump_obj_file(struct cmesh *cm, FILE *fp, int voffs);
135
136
137
138 #endif  /* CMESH_H_ */