aboutsummaryrefslogtreecommitdiff
path: root/src/obj.c
blob: d832604022c3a75668d9f9f457582da5600a4f54 (plain)
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
/*
 * This file is part of mverse
 * Copyright (C) 2022  juanvalencia.xyz

 * mverse is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.

 * This program 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 General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

#include "obj.h"

struct Setv3 {
    float data[3];
};

struct Setv2 {
    float data[2];
};

struct Seti {
    int v, vn, vt;
};

static void readV3(char *line, struct Setv3 **v, int vIndex);
static void readV2(char *line, struct Setv2 **vn, int vtIndex);
static void readF(char *line, Mesh *mesh, int meshIndex, struct Setv3 *v, struct Setv2 *vt, struct Setv3 *vn);

static Material * readMtl(char *line, const char *path, int *size);
static unsigned int useMtl(char *line, Material *mtl, unsigned int size);

/* -------------------------------------------------------------------------- */

struct Seti * readIndices(char *line, int *nIndices);
static int readIndex(char *line, struct Seti *f);
static Vertex createVertex(struct Seti f, struct Setv3 *v, struct Setv2 *vt, struct Setv3 *vn);
static int vertexInVertices(Vertex vertex, Vertex *vertices, int nVertices);
static void vertexAdd(Mesh *mesh, Vertex vertex);
static int vertexGetIndex(Vertex *vertices,  Vertex vertex, int nVertices);
static void indexAdd(Mesh *mesh, int index);

/* -------------------------------------------------------------------------- */
static void getDir(char *filepath);
static void appendMtl(char *line, Material **mtl, int index);
static void readColor(char *line, float *k);


Obj
objCreate(const char *filename)
{
    Obj o;
    Mesh *mesh;
    Material *mtl;
    char lineBuffer[OBJ_LINE_MAX];
    FILE *fi;

    struct Setv3 *v, *vn;
    struct Setv2 *vt;
    int vSize, vtSize, vnSize;
    vSize = vtSize = vnSize = 0;

    fi = (!strcmp(filename, "-")) ? stdin : fopen(filename, "r");
    mesh = (Mesh *)calloc(1, sizeof(Mesh));

    // On error return NULL
    if (fi == NULL || mesh == NULL) {
        fprintf(stderr, "objCreateMesh() Error: %s\n", strerror(errno));
        fclose(fi);
        exit(1);
    }

    v   = (struct Setv3 *)calloc(1, vSize  * sizeof(struct Setv3));
    vt  = (struct Setv2 *)calloc(1, vtSize * sizeof(struct Setv2));
    vn  = (struct Setv3 *)calloc(1, vnSize * sizeof(struct Setv3));

    if (v == NULL) {
        fprintf(stderr, "objCreateMesh() Error: %s\n", strerror(errno));
        exit(1);
    }

    int n;
    char key[500];
    int vIndex, vtIndex, vnIndex;
    int mtlSize, mtlIndex, meshIndex;
    vIndex = vtIndex = vnIndex = meshIndex = mtlSize = 0;

    while (fgets(lineBuffer, OBJ_LINE_MAX, fi)) {

        sscanf(lineBuffer, "%s%n", key, &n);

        if      (!strcmp("f" , key))  readF (lineBuffer + n, mesh, meshIndex, v, vt, vn);
        else if (!strcmp("vt", key))  readV2(lineBuffer + n, &vt, vtIndex++);
        else if (!strcmp("vn", key))  readV3(lineBuffer + n, &vn, vnIndex++);
        else if (!strcmp("v" , key))  readV3(lineBuffer + n, &v,  vIndex++);

        else if (!strcmp("mtllib", key)) mtl = readMtl(lineBuffer + n, filename, &mtlSize);
        else if (!strcmp("usemtl", key) && mtlSize > 0) {
            mtlIndex = useMtl (lineBuffer + n, mtl, mtlSize);
            mesh = (Mesh *)realloc(mesh, (++meshIndex + 1) * sizeof(Mesh));
            memset(mesh + meshIndex, 0, sizeof(Mesh));
            mesh[meshIndex].material = mtl[mtlIndex];
            mesh[meshIndex].indexSize = 0;
        }
        key[0] = '\0';
    }

    o.mesh = mesh;
    o.size = meshIndex + 1;
    for (int i = 1; i < o.size; i++) {
        o.mesh[i].vertices = o.mesh[0].vertices;
        o.mesh[i].vertexSize = o.mesh[0].vertexSize;
    }

    free(v);
    free(vt);
    free(vn);
    fclose(fi);

    return o;
}

void
readF(char *line, Mesh *mesh, int meshIndex, struct Setv3 *v, struct Setv2 *vt, struct Setv3 *vn)
{
    Vertex vertexBuffer;
    struct Seti *f;
    int i, nIndices, vi;
    f = readIndices(line, &nIndices);

    for (i = 0; i < nIndices; i++) {
        vertexBuffer = createVertex(f[i], v, vt, vn);
        if (!vertexInVertices(vertexBuffer, mesh->vertices, mesh->vertexSize))
            vertexAdd(mesh, vertexBuffer); 
        vi = vertexGetIndex(mesh->vertices, vertexBuffer, mesh->vertexSize);
        indexAdd(mesh + meshIndex, vi);
    }
    free(f);
}

void
readV3(char *line, struct Setv3 **v, int vIndex)
{
    int i, n;
    char *ptr;
    struct Setv3 *vptr;

    if (vIndex > 0)
        *v = (struct Setv3 *)realloc(*v, (vIndex + 1) * sizeof(struct Setv3));

    vptr = *v;
    for (i = 0, ptr = line, n = 0; i < 3; i++, ptr += n)
        sscanf(ptr, " %f%n", vptr[vIndex].data + i, &n);
}

void
readV2(char *line, struct Setv2 **v, int vIndex)
{
    int i, n;
    char *ptr;
    struct Setv2 *vptr;

    if (vIndex > 0)
        *v = (struct Setv2 *)realloc(*v, (vIndex + 1) * sizeof(struct Setv2));

    vptr = *v;
    for (i = 0, ptr = line, n = 0; i < 2; i++, ptr += n)
        sscanf(line, " %f%n", vptr[vIndex].data + i, &n);
}

struct Seti *
readIndices(char *line, int *nIndices)
{
    struct Seti *f;
    struct Seti *buffer;
    char *ptr;
    int bufferSize;
    int n;

    buffer = (struct Seti *)calloc(3, sizeof(struct Seti));
    for (ptr = line, bufferSize = 0; *ptr != '\n'; ptr += n, bufferSize++) {
        if (bufferSize + 1 > 3)
            buffer = (struct Seti *)realloc(buffer, (bufferSize + 1) * sizeof(struct Seti));

        n = readIndex(ptr, buffer + bufferSize);

        if (buffer[bufferSize].v  != -1) buffer[bufferSize].v--;
        if (buffer[bufferSize].vt != -1) buffer[bufferSize].vt--;
        if (buffer[bufferSize].vn != -1) buffer[bufferSize].vn--;

        if (n > 0) continue;

        memset(buffer, 0, (bufferSize + 1) * sizeof(struct Seti));
        free(buffer);
        fprintf(stderr, "readIndices() Error: bad format in line '%s'", line);
        exit(1);
    }

    *nIndices = (bufferSize - 2) * 3;
    f = (struct Seti *)calloc(nIndices[0], sizeof(struct Seti));

    int i, j, k;

    for (i = j = 0, k = 1; i < bufferSize - 2; i++) {

        f[3 * i] = (j == 0) ? buffer[0] : buffer[j];

        if (j + k < bufferSize) {
            f[3 * i + 1] = buffer[j + k];
        } else {
            f[3 * i + 1] = buffer[0];
            j = 0;
            k *= 2;
            f[3 * i + 2] = buffer[j + k];
            j += k;
            continue;
        }

        f[3 * i + 2] = (j + 2 * k < bufferSize) ? buffer[j + 2 * k] : buffer[0];

        if (j + 2 * k < bufferSize) {
            j += 2 * k;
        } else {
            j = 0;
            k *= 2; // 1 2 4
        }
    }

    free(buffer);
    return f;
}

int
readIndex(char *line, struct Seti *f)
{
    int n;
    f->v = f->vt = f->vn = -1;
    if ((sscanf(line, " %d/%d/%d%n", &f->v, &f->vn, &f->vt, &n) >= 3)) return n;

    f->v = f->vt = f->vn = -1;
    if ((sscanf(line, " %d//%d%n",   &f->v, &f->vn, &n)         >= 2)) return n;

    f->v = f->vt = f->vn = -1;
    if ((sscanf(line, " %d/%d%n",    &f->v, &f->vt, &n)         >= 2)) return n;

    f->v = f->vt = f->vn = -1;
    if ((sscanf(line, " %d%n",       &f->v, &n)                 >= 1)) return n;

    return 0;
}

Vertex
createVertex(struct Seti f, struct Setv3 *v, struct Setv2 *vt, struct Setv3 *vn)
{
    int i;
    Vertex out = {.position = {0, 0, 0}, .normal = {0, 0, 0}, .texCoords = {0, 0}};
    for (i = 0; i < 3; i++) {
        if (f.v != -1) out.position[i] = v[f.v].data[i];
        if (f.vn != -1) out.normal[i] = vn[f.vn].data[i];
        if (f.vt != -1 && i < 2) out.texCoords[i] = vt[f.vt].data[i];
    }
    return out;
}

int
vertexInVertices(Vertex vertex, Vertex *vertices, int nVertices)
{
    return vertexGetIndex(vertices, vertex, nVertices) + 1;
}

int
vertexGetIndex(Vertex *vertices, Vertex vertex, int nVertices) {
    int i, j, n;
    for (i = 0; i < nVertices; i++) {
        n = 0;
        for (j = 0; j < 3; j++) {
            if (vertices[i].position[j] == vertex.position[j]) n++;
            if (vertices[i].normal[j] == vertex.normal[j]) n++;
        }
        for (j = 0; j < 2; j++) {
            if (vertices[i].texCoords[j] == vertex.texCoords[j]) n++;
        }

        if (n == 8) return i;
    }
    return -1;
}

void
vertexAdd(Mesh *mesh, Vertex vertex)
{
    Vertex *v;
    int i;
    unsigned int vSize;

    v = mesh->vertices;
    vSize = mesh->vertexSize;

    if (!vSize) v = (Vertex *)calloc(1, sizeof(Vertex));
    else        v = (Vertex *)realloc(v, (vSize + 1) * sizeof(Vertex));

    if (v == NULL) {
        fprintf(stderr, "vertexAdd() Error: %s\n", strerror(errno));
        exit(1);
    }

    for (i = 0; i < 3; i++) {
        v[vSize].position[i] = vertex.position[i];
        v[vSize].normal[i]   = vertex.normal[i];
        if (i < 2)
            v[vSize].texCoords[i]   = vertex.texCoords[i];
    }
    mesh->vertices = v;
    mesh->vertexSize++;
}

void
indexAdd(Mesh *mesh, int index)
{
    unsigned int *iv, iSize;

    iv = mesh->indices;
    iSize = mesh->indexSize;

    if (!iSize) iv = (unsigned int *)calloc(1, sizeof(int));
    else        iv = (unsigned int *)realloc(iv, (iSize + 1) * sizeof(int));

    if (iv == NULL) {
        fprintf(stderr, "vertexAdd() Error: %s\n", strerror(errno));
        exit(1);
    }

    iv[iSize] = index;
    mesh->indices = iv;
    mesh->indexSize++;
}

Material *
readMtl(char *line, const char *objFile, int *size)
{
    Material *out;
    char buffer[OBJ_LINE_MAX], mtlFilename[OBJ_LINE_MAX], key[OBJ_MAX_WORD];
    char path[OBJ_MAX_WORD], fileName[OBJ_MAX_WORD];
    FILE *fin;
    int i, n;

    strncpy(path, objFile, 512);
    getDir(path);
    sscanf(line, " ./%s", fileName);
    sprintf(mtlFilename, "%s/%s", path, fileName);

    fin = fopen(mtlFilename, "r");

    if (fin == NULL && errno == ENOENT) {
        perror("readMtl() Warning");
        if (size) *size = 0;
        return NULL;
    } else if (fin == NULL) {
        perror("readMtl() Error");
        exit(1);
    }

    i = 0;
    while(fgets(buffer, OBJ_LINE_MAX, fin)) {
        sscanf(buffer, "%s%n", key, &n);
        if  (!strcmp(key, "newmtl")) appendMtl(buffer + n, &out, i++);
        if (i > 0) {
            if      (!strcmp(key, "Ka"))     readColor(buffer + n, out[i - 1].ka);
            else if (!strcmp(key, "Kd"))     readColor(buffer + n, out[i - 1].kd);
            else if (!strcmp(key, "Ks"))     readColor(buffer + n, out[i - 1].ks);
            else if (!strcmp(key, "illum"))  sscanf(buffer + n, "%u", &out[i - 1].illum);
            else if (!strcmp(key, "Ns"))     sscanf(buffer + n, "%f", &out[i - 1].ns);
        }
        key[0] = '\0';
    }

    if (size) *size = i;
    fclose(fin);
    return out;
}

void
appendMtl(char *line, Material **mtl, int index)
{
    char name[OBJ_LINE_MAX];

    if (index == 0)
        *mtl = (Material *)calloc(1, sizeof(Material));
    else {
        *mtl = (Material *)realloc(*mtl, (index + 1) * sizeof(Material));
        memset(*mtl + index, 0, sizeof(Material));
    }

    sscanf(line, "%s", name);
    strncpy((*mtl + index)->name, name, OBJ_LINE_MAX);

    if (mtl == NULL) {
        perror("appendMtl() Error");
        exit(1);
    }
}

void
readColor(char *line, float *k)
{
    int ret;
    ret = sscanf(line, "%f %f %f", k, k + 1, k + 2);
    switch (ret) {
        case 1:
            k[1] = k[2] = k[0];
            break;
        case 3:
            break;
        default:
            fprintf(stderr, "readColor() Error: line '%s' invalid format", line - 2);
            exit(1);
            break;
    }
}

unsigned int
useMtl(char *line, Material *mtl, unsigned int size)
{
    int i;
    char name[OBJ_LINE_MAX];
    for (i = 0; i < size; i++) {
        sscanf(line, "%s", name);
        if (!strcmp(name, mtl[i].name))
            return i;
    }
    return 0;
}

void
getDir(char *filepath)
{
    int i;
    for (i = strlen(filepath) - 1; i >= 0; i--) {
        switch (filepath[i]) {
            case '/':
            case '\\':
                filepath[i] = '\0';
                return;
            default:
                break;
        }
    }
    strcpy(filepath, ".");
}
Feel free to download, copy and edit any repo