-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathObjFile.c
265 lines (237 loc) · 7.7 KB
/
ObjFile.c
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/*
* 3dObjLib: OBJ file generation
* Copyright (C) 2018 Christopher Bazley
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/* History:
CJB: 05-Aug-18: Copied this source file from SF3KtoObj.
CJB: 21-Apr-20: Fixed bad output in the form of "# 1 vertice".
CJB: 06-Apr-25: Dogfooding the _Optional qualifier.
CJB: 11-Apr-25: Allow null argument to get_colour and get_material.
CJB: 13-Apr-25: Rename output_primitives_get_(colour|material) types.
*/
/* ISO library header files */
#include <stddef.h>
#include <stdbool.h>
#include <stdio.h>
#include <limits.h>
/* Local header files */
#include "ObjFile.h"
#include "Coord.h"
#include "Vertex.h"
#include "Primitive.h"
#include "Internal/3dObjMisc.h"
static int convert_vnum(const VertexArray * const varray, int v,
const int vtotal, const int vobject,
const VertexStyle vstyle)
{
assert(varray != NULL);
assert(vtotal >= 0);
assert(vobject > 0);
const int id = vertex_array_get_id(varray, v);
if (vstyle == VertexStyle_Negative) {
assert(id <= vobject);
v = - (vobject - id);
} else {
assert(vstyle == VertexStyle_Positive);
v = 1 + vtotal + id;
}
return v;
}
bool output_vertices(FILE * const out, const int vobject,
const VertexArray * const varray,
const int rot)
{
assert(out != NULL);
assert(!ferror(out));
assert(vobject > 0);
if (fprintf(out, "\n# %d vertices\n", vobject) < 0) {
return false;
}
const int nvertices = vertex_array_get_num_vertices(varray);
for (int v = 0; v < nvertices; ++v) {
if ((v == rot) && fputs("# Following vertices rotate\n", out) == EOF) {
return false;
}
_Optional Coord (* const coords)[3] = vertex_array_get_coords(varray, v);
if (!coords) {
continue;
}
if (!vertex_array_is_used(varray, v)) {
DEBUGF("Omitting vertex %d {%"PCOORD",%"PCOORD",%"PCOORD"} "
"from the output\n",
v, (*coords)[0], (*coords)[1], (*coords)[2]);
continue;
}
if (fputs("v", out) == EOF) {
return false;
}
for (size_t dim = 0; dim < ARRAY_SIZE(*coords); ++dim) {
if (fprintf(out, " %f", (*coords)[dim]) < 0) {
return false;
}
}
if (fputs("\n", out) == EOF) {
return false;
}
}
return true;
}
static bool output_primitive(FILE * const out, const Primitive * const pp,
const int vtotal, const int vobject,
const VertexArray * const varray,
const VertexStyle vstyle,
const MeshStyle mstyle)
{
assert(out != NULL);
assert(!ferror(out));
assert(vtotal >= 0);
assert(vobject > 0);
const int nsides = primitive_get_num_sides(pp);
if ((nsides > 3) && (mstyle != MeshStyle_NoChange)) {
int s, v[3] = {0, 0, 0};
for (s = 0; s < 2; ++s) {
v[s] = convert_vnum(
varray, primitive_get_side(pp, s), vtotal, vobject, vstyle);
}
for (; s < nsides; ++s) {
int sindex;
if (mstyle == MeshStyle_TriangleFan) {
/* Count up from index 2...N-1, where N is the no. of sides */
sindex = s;
} else {
assert(mstyle == MeshStyle_TriangleStrip);
if (s % 2) {
/* Odd-numbered iterations count down from index N-1, N-2 .. */
sindex = primitive_get_num_sides(pp) - ((s-1)/2);
} else {
/* Even-numbered iterations count up from index 2,3,4 .. */
sindex = 1 + (s/2);
}
}
/* Replace the first or third vertex (always replace the third
when making triangle fans) */
const int vnext = convert_vnum(
varray, primitive_get_side(pp, sindex), vtotal,
vobject, vstyle);
if ((mstyle == MeshStyle_TriangleFan) || !(s % 2)) {
v[2] = vnext;
} else {
assert(mstyle == MeshStyle_TriangleStrip);
v[0] = vnext;
}
if (fputs("f", out) == EOF) {
return false;
}
for (size_t ts = 0; ts < ARRAY_SIZE(v); ++ts) {
if (fprintf(out, " %d", v[ts]) < 0) {
return false;
}
}
if (fputs("\n", out) == EOF) {
return false;
}
/* Keep the first or third vertex for the next iteration
(always keep the third when making triangle fans) */
v[1] = ((mstyle == MeshStyle_TriangleFan) || (s % 2)) ? v[2] : v[0];
} /* next side */
} else {
assert(nsides > 0);
const char *primitive_type;
switch (nsides) {
case 1:
primitive_type = "p";
break;
case 2:
primitive_type = "l";
break;
default:
primitive_type = "f";
break;
}
if (fputs(primitive_type, out) == EOF) {
return false;
}
for (int s = 0; s < nsides; ++s) {
const int v = convert_vnum(
varray, primitive_get_side(pp, s),
vtotal, vobject, vstyle);
if (fprintf(out, " %d", v) < 0) {
return false;
}
} /* next side */
if (fputs("\n", out) == EOF) {
return false;
}
}
return true;
}
bool output_primitives(FILE * const out, const char * const object_name,
const int vtotal, const int vobject,
const VertexArray * const varray,
Group const * const groups,
int const ngroups,
_Optional OutputPrimitivesGetColourFn *get_colour,
_Optional OutputPrimitivesGetMaterialFn *get_material,
_Optional void *arg, const VertexStyle vstyle,
const MeshStyle mstyle)
{
assert(out != NULL);
assert(!ferror(out));
assert(groups != NULL);
assert(vtotal >= 0);
assert(vobject > 0);
int last_colour = INT_MAX;
for (int g = 0; g < ngroups; ++g) {
const Group *const group = groups + g;
const int nprimitives = group_get_num_primitives(group);
if (nprimitives > 0) {
if (fprintf(out, "\n# %d primitives\n", nprimitives) < 0) {
return false;
}
if (fprintf(out, "g %s %s_%d\n", object_name, object_name, g) < 0) {
return false;
}
}
for (int p = 0; p < nprimitives; ++p) {
const _Optional Primitive * const pp = group_get_primitive(group, p);
if (!pp) {
return false;
}
int const colour = get_colour ?
get_colour(&*pp, arg) :
primitive_get_colour(&*pp);
if (last_colour != colour) {
char material[64];
int n = get_material ?
get_material(material, sizeof(material), colour, arg) :
snprintf(material, sizeof(material), "colour_%d", colour);
if (n < 0) {
return false;
}
n = fprintf(out, "usemtl %s\n", material);
if (n < 0) {
return false;
}
last_colour = colour;
}
if (!output_primitive(out, &*pp, vtotal, vobject, varray, vstyle, mstyle)) {
return false;
}
} /* next primitive */
} /* next group */
return true;
}