-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparsers.cpp
More file actions
137 lines (125 loc) · 3.62 KB
/
Copy pathparsers.cpp
File metadata and controls
137 lines (125 loc) · 3.62 KB
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
int ParseVertices(FILE* file, Vec3 * vertices) {
int count = 0;
char buffer[1024];
rewind(file);
while (fgets(buffer, sizeof buffer, file)) {
if (!(buffer[0] == 'v' && buffer[1] == ' ')) continue;
float x, y, z;
sscanf(buffer, "%*c %f %f %f", &x, &y, &z);
vertices[count] = Vec3 {x, y, z};
count++;
}
return count;
}
int ParseVerticeNormals(FILE* file, Vec3* vertice_normals) {
int count = 0;
char buffer[1024];
rewind(file);
while (fgets(buffer, sizeof buffer, file)) {
if (!(buffer[0] == 'v' && buffer[1] == 'n' && buffer[2] == ' ')) continue;
float x, y, z;
sscanf(buffer, "%*c%*c %f %f %f", &x, &y, &z);
vertice_normals[count] = Vec3 {x, y, z};
count++;
}
return count;
}
int ParseVerticeTextures(FILE* file, Vec3* vertice_textures) {
int count = 0;
char buffer[1024];
rewind(file);
while (fgets(buffer, sizeof buffer, file)) {
if (!(buffer[0] == 'v' && buffer[1] == 't' && buffer[2] == ' ')) continue;
float x, y, z;
sscanf(buffer, "%*c%*c %f %f %f", &x, &y, &z);
vertice_textures[count] = Vec3 {x, y, z};
count++;
}
return count;
}
int ParseFaces(FILE* file, Face* faces) {
int count = 0;
char buffer[1024];
rewind(file);
while (fgets(buffer, sizeof buffer, file)) {
if (!(buffer[0] == 'f' && buffer[1] == ' ')) continue;
int v1, v2, v3;
int t1, t2, t3;
sscanf (buffer, "%*c %d/%d/%*d %d/%d/%*d %d/%d/%*d", &v1, &t1, &v2, &t2, &v3, &t3);
faces[count] = Face {{v1, v2, v3}, {t1, t2, t3}};
count++;
}
return count;
}
void ParseObjFile(const char* file_name) {
FILE* file = fopen(file_name, "r");
if(!file) {
printf("ERROR OPENING FILE %s", file_name);
}
MODEL.vertices_size = ParseVertices(file, MODEL.vertices);
MODEL.vertice_normals_size = ParseVerticeNormals(file, MODEL.vertice_normals);
MODEL.vertice_textures_size = ParseVerticeTextures(file, MODEL.vertice_textures);
MODEL.faces_size = ParseFaces(file, MODEL.faces);
fclose(file);
}
void ParseHeader(FILE* file, Texture* texture) {
fseek(file, 12, SEEK_SET);
// make sure to check that padding does not fuck up
// this clever reading of w, h and bpp.
fread(texture, 5, 1, file);
}
void ParseTgaRle(FILE* file, Texture* texture) {
int bitmap_position = 0;
bool has_alpha = texture->bpp == 32;
bool has_color = texture->bpp != 8;
fseek(file, 18, SEEK_SET);
do {
unsigned char rep_count = fgetc(file);
if(rep_count == 0xFF) {
int x = 1234;
}
// raw packets
if(rep_count < 128) {
rep_count++;
for(int i = 0; i < rep_count; i++) {
BYTE b = fgetc(file);
BYTE g = 0;
BYTE r = 0;
BYTE a = 0;
if(has_color) {
g = fgetc(file);
r = fgetc(file);
}
if(has_alpha) {
a = fgetc(file);
}
texture->buffer[bitmap_position++] = {b, g, r, a};
}
} else {
rep_count -= 127;
BYTE b = fgetc(file);
BYTE g = 0;
BYTE r = 0;
BYTE a = 0;
if(has_color) {
g = fgetc(file);
r = fgetc(file);
}
if(has_alpha) {
a = fgetc(file);
}
for(int i = 0; i < rep_count; i++) {
texture->buffer[bitmap_position++] = {b, g, r, a};
}
}
}while(bitmap_position < texture->width * texture->height);
}
void ParseTgaImage(const char* file_name, Texture* texture) {
FILE* file = fopen(file_name, "rb");
ParseHeader(file, texture);
texture->buffer = (Color*) malloc(sizeof Color *
texture->width *
texture->height);
ParseTgaRle(file, texture);
fclose(file);
}