-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathobjLib.c
131 lines (100 loc) · 2.4 KB
/
objLib.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
#include <stdio.h>
#include <malloc.h>
#include "objLib.h"
void readFaces(struct Face * f, char * line)
{
f->fL = malloc(sizeof(int) * 3);
sscanf(line, "f %d%*[^ ]%d%*[^ ]%d%*[^\n]", &f->fL[0], &f->fL[1], &f->fL[2]);
}
void WriteVertexLine(All * all, char * line)
{
if (all->vCurrent >= all->vSize)
{
all->vSize *= MULTIPLIER;
all->V = (struct Vertex *) realloc(all->V, sizeof(struct Vertex) * all->vSize);
}
sscanf(line, "v %lf %lf %lf", &all->V[all->vCurrent].x, &all->V[all->vCurrent].y, &all->V[all->vCurrent].z);
++all->vCurrent;
}
void WriteFaceLine(All * all, char * line)
{
if (all->fCurrent >= all->fSize)
{
all->fSize *= MULTIPLIER;
all->F = (struct Face *) realloc(all->F, sizeof(struct Face) * all->fSize);
}
readFaces(&all->F[all->fCurrent], line);
++all->fCurrent;
}
void CreateTriangles(All * all)
{
int i, j;
all->Triangles = malloc(sizeof(struct Vertex *) * all->fCurrent);
for (i = 0; i < all->fCurrent; i++)
{
all->Triangles[i] = malloc(sizeof(struct Vertex) * 3);
}
for (i = 0; i < all->fCurrent; i++)
{
for (j = 0; j < 3; j++)
{
saveVertexesToTriangles(&all->Triangles[i][j], all->F[i].fL[j] - OBJ_STARTS_COUNTING_FROM_ONE, all->V);
}
}
}
void saveVertexesToTriangles(struct Vertex * T, int vertexNumber, struct Vertex * V)
{
T->x = V[vertexNumber].x;
T->y = V[vertexNumber].y;
T->z = V[vertexNumber].z;
}
void DeleteAll(All * all)
{
free(all->V);
free(all->F);
free(all->Triangles);
free(all);
}
All * ParseObjFile(char * fileName)
{
char line[BUFFER_LENGTH + 1] = "";
FILE * file = fopen(fileName, "r");
All * all = malloc(sizeof(All));
all->vSize = START_VERTEX_COUNT;
all->vCurrent = 0;
all->fSize = START_FACE_COUNT;
all->fCurrent = 0;
all->V = malloc(sizeof(struct Vertex) * START_VERTEX_COUNT);
all->F = malloc(sizeof(struct Face) * START_FACE_COUNT);
if (file == NULL)
{
fprintf(stdout, "No file!\n");
}
else
{
while(!feof(file))
{
if (fgets(line, BUFFER_LENGTH, file))
{
switch (line[0])
{
case 'v':
if (line[1] == ' ')
WriteVertexLine(all, line);
break;
case 'f':
if (line[1] == ' ')
{
WriteFaceLine(all, line);
}
break;
default:
break;
}
}
}
fclose(file);
CreateTriangles(all);
}
return all;
}