-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathobj.h
133 lines (110 loc) · 2.84 KB
/
obj.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#ifndef OBJ_H
#define OBJ_H
/*
* OBJ
* ===
*
* Reads [Wavefront .OBJ][wiki] 3D models.
*
* ## Notes:
*
* * *TODO:* Materials: `mtllib`, `usemtl` etc.
* * Faces containing more than 3 vertices are converted
* to triangle fans internally.
* * Faces can't belong to multiple groups.
*
* ## Links:
*
* * [Wavefront .obj file][wiki]
* * [Object Files (.obj)][bourke]
* * <https://www.cs.cmu.edu/~mbz/personal/graphics/obj.html>
*
* [wiki]: https://en.wikipedia.org/wiki/Wavefront_.obj_file
* [bourke]: http://paulbourke.net/dataformats/obj/
*/
typedef struct {
double x, y, z, w;
} ObjVert;
typedef struct {
double x, y, z;
} ObjNorm;
typedef struct {
double u, v, w;
} ObjUVW;
typedef struct {
int v, vt, vn;
} ObjFaceVert;
typedef struct {
/* OBJ supports more than 3 vertices per face;
I cater for that by just adding more triangles as a triangle strip.*/
ObjFaceVert verts[3];
int g, s;
} ObjFace;
typedef struct {
char *name;
} ObjGroup;
typedef struct {
unsigned int n, a;
int *idx;
} ObjLine;
typedef struct ObjMesh {
unsigned int nverts, averts;
ObjVert *verts; /* Vertices */
unsigned int nnorms, anorms;
ObjNorm *norms; /* Normals */
unsigned int ntexs, atexs;
ObjUVW *texs; /* Texture coordinates */
unsigned int npspaces, apspaces;
ObjUVW *pspaces; /* Parameter space vertices */
unsigned int nfaces, afaces;
ObjFace *faces; /* Faces */
unsigned int ngroups, agroups;
ObjGroup *groups; /* Groups */
unsigned int nlines, alines;
ObjLine *lines; /* Lines */
double xmin, xmax;
double ymin, ymax;
double zmin, zmax;
} ObjMesh;
typedef struct {
enum {mtl_none, mtl_rgb, mtl_spectral, mtl_xyz} type;
union {
struct { double r, g, b; } rgb;
struct { char *name; double factor; } spec;
struct { double x, y, z; } xyz;
};
} MtlColor;
typedef struct {
char *name;
MtlColor Ka;
MtlColor Kd;
MtlColor Ks;
MtlColor Tf;
double Tr;
int illum;
double Ns;
} Material;
typedef struct {
unsigned int n, a;
Material *mtls;
} MtlLibrary;
ObjMesh *obj_create();
ObjMesh *obj_load(const char *fname);
void obj_free(ObjMesh *obj);
int obj_save(ObjMesh *obj, const char *fname);
const char *obj_last_error();
/* Seek VEC_ADD_FUNCTION for the definition of these: */
ObjVert *obj_add_vert(ObjMesh *obj);
ObjNorm *obj_add_norm(ObjMesh *obj);
ObjUVW *obj_add_tex(ObjMesh *obj);
ObjUVW *obj_add_pspace(ObjMesh *obj);
ObjFace *obj_add_face(ObjMesh *obj);
ObjGroup *obj_add_group(ObjMesh *obj);
ObjLine *obj_add_line(ObjMesh *obj);
void obj_line_add_vtx(ObjLine *l, int i);
MtlLibrary *mtl_create();
void mtl_free(MtlLibrary *lib);
Material *mtl_add(MtlLibrary *lib, const char *name);
MtlLibrary *mtl_load(const char *fname);
int mtl_save(MtlLibrary *lib, const char *fname);
#endif