summaryrefslogtreecommitdiffstats
path: root/obj.h
blob: 6451b1866179821f9be4b4f8e7ea35d9fb7e6d6d (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
#ifndef _OBJLOADER_H_
#define _OBJLOADER_H_

extern char ObjLastFname[101];

typedef struct
{
    float x, y, z, w;
} ObjVertex;

typedef struct
{
    float x, y, z;
} ObjNormal;

typedef struct
{
    float s, t, r, q;
} ObjTexCoord;

typedef struct
{
    unsigned int Vertex[4];
    unsigned int Normal[4];
    unsigned int TexCoord[4];
} ObjFace;

typedef struct
{
	char *map_Ka;
	char *map_Kd;
	ObjVertex Tf;
	ObjVertex Ka;
	ObjVertex Kd;
	ObjVertex Ks;
	ObjVertex Ke;
	float Ns;
	float Ni;
	float d;
	float Tr;
	int illum;
} ObjMtl;

typedef struct
{
    char *objectName;
    char *groupName;
    char *usemtl;

    ObjMtl      *mtl;
    ObjVertex   *VertexArray;
    ObjNormal   *NormalArray;
    ObjTexCoord *TexCoordArray;
    ObjFace     *FaceArray;
    unsigned    nVertex, nNormal, nTexCoord, nFace;
    char *mtllib;
} ObjModel;

/* function prototypes */
ObjModel *ObjLoadModel(char *, size_t);

/* read a file into argument 2, and return amount of bytes read */
size_t ObjLoadFile(char *, char **);
void ObjList(ObjModel *);

/* once we load an .obj file, we can extract path up to last '/' to load a
 * mtl or texture that resides in the same folder */
char *ObjGetPath(const char *);

void ObjSubmitIndexedVertexArrayQuad(const ObjModel *);

void ObjSubmitGLCommandsQuad(const ObjModel *);
void ObjSubmitGLCommandsTriangle(const ObjModel *);
void ObjFree(ObjModel *);

static inline ObjNormal *ObjGetNormal(const ObjModel *model, const unsigned i,
                                      const unsigned j)
{
    return &(model->NormalArray[model->FaceArray[i].Normal[j] - 1]);
}

static inline ObjTexCoord *ObjGetTexCoord(const ObjModel *model, const unsigned i,
                                          const unsigned j)
{
    return &(model->TexCoordArray[model->FaceArray[i].TexCoord[j] - 1]);
}

static inline ObjVertex *ObjGetVertex(const ObjModel *model, const unsigned i,
                                      const unsigned j)
{
    return &(model->VertexArray[model->FaceArray[i].Vertex[j] - 1]);
}

#endif