reemify program done

This commit is contained in:
MallocNull 2020-12-10 17:59:20 -06:00
parent 1041abc833
commit 63af9d68e0
2 changed files with 180 additions and 2 deletions

2
.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
.idea/
cmake-build-*/

View file

@ -32,8 +32,8 @@ void list_destroy(list_t* list) {
#define ISWP(X) swap(X, 4) #define ISWP(X) swap(X, 4)
void swap(uint8_t* in, int length) { void swap(uint8_t* in, int length) {
/*if(end_chk) if(end_chk)
return;*/ return;
uint8_t tmp[8] = { 0 }; uint8_t tmp[8] = { 0 };
for(int i = 0; i < length; ++i) for(int i = 0; i < length; ++i)
@ -41,7 +41,20 @@ void swap(uint8_t* in, int length) {
memcpy(in, tmp, length); memcpy(in, tmp, length);
} }
void write(FILE* fp, const uint8_t* data, int length) {
uint8_t tmp[8] = { 0 };
memcpy(tmp, data, length);
swap(tmp, length);
fwrite(tmp, 1, length, fp);
}
int main(int argc, char* argv[]) { int main(int argc, char* argv[]) {
if(sizeof(float) != 4) {
printf("Operating system must define floats as four bytes.");
return -1;
}
if(argc < 2) { if(argc < 2) {
printf("You must pass the OBJ file to be converted."); printf("You must pass the OBJ file to be converted.");
return -1; return -1;
@ -112,10 +125,173 @@ int main(int argc, char* argv[]) {
return -1; return -1;
} }
list_t* next = list_create();
int used = 0;
if(strncmp("v ", line, 2) == 0) {
used = 1;
++vertex_cnt;
sscanf(line, "%*s %f %f %f",
&next->data[0],
&next->data[1],
&next->data[2]
);
verts_at->next = next;
verts_at = next;
} else if(strncmp("vn ", line, 3) == 0) {
used = 1;
++normal_cnt;
sscanf(line, "%*s %f %f %f",
&next->data[0],
&next->data[1],
&next->data[2]
);
normals_at->next = next;
normals_at = next;
} else if(strncmp("vt ", line, 3) == 0) {
used = 1;
++tex_cnt;
sscanf(line, "%*s %f %f",
&next->data[0],
&next->data[1]
);
texs_at->next = next;
texs_at = next;
} else if(strncmp("f ", line, 2) == 0) {
used = 1;
++face_cnt;
if(vertex_cnt > 0 && tex_cnt > 0 && normal_cnt > 0) {
sscanf(line, "%*s %u/%u/%u %u/%u/%u %u/%u/%u",
&next->faces[0][0], &next->faces[0][1], &next->faces[0][2],
&next->faces[1][0], &next->faces[1][1], &next->faces[1][2],
&next->faces[2][0], &next->faces[2][1], &next->faces[2][2]
);
} else if(vertex_cnt > 0 && tex_cnt > 0 && normal_cnt == 0) {
sscanf(line, "%*s %u/%u %u/%u %u/%u",
&next->faces[0][0], &next->faces[0][1],
&next->faces[1][0], &next->faces[1][1],
&next->faces[2][0], &next->faces[2][1]
);
} else if(vertex_cnt > 0 && tex_cnt == 0 && normal_cnt > 0) {
sscanf(line, "%*s %u//%u %u//%u %u//%u",
&next->faces[0][0], &next->faces[0][2],
&next->faces[1][0], &next->faces[1][2],
&next->faces[2][0], &next->faces[2][2]
);
} else if(vertex_cnt > 0 && tex_cnt == 0 && normal_cnt == 0) {
sscanf(line, "%*s %u %u %u",
&next->faces[0][0],
&next->faces[1][0],
&next->faces[2][0]
);
} else {
printf("OBJ file must at least include vertices!");
return -1;
}
faces_at->next = next;
faces_at = next;
}
if(!used)
free(next);
} }
fclose(fpi); fclose(fpi);
uint8_t magic[] = {0xDE, 0xAF, 0xB0, 0x0B};
fwrite(magic, 1, 4, fpo);
write(fpo, (uint8_t*)&vertex_cnt, 4);
write(fpo, (uint8_t*)&tex_cnt, 4);
write(fpo, (uint8_t*)&normal_cnt, 4);
write(fpo, (uint8_t*)&face_cnt, 4);
list_t* at = verts->next;
while(at != NULL) {
write(fpo, (uint8_t*)&(at->data[0]), 4);
write(fpo, (uint8_t*)&(at->data[1]), 4);
write(fpo, (uint8_t*)&(at->data[2]), 4);
at = at->next;
}
at = texs->next;
while(at != NULL) {
write(fpo, (uint8_t*)&(at->data[0]), 4);
write(fpo, (uint8_t*)&(at->data[1]), 4);
at = at->next;
}
at = normals->next;
while(at != NULL) {
write(fpo, (uint8_t*)&(at->data[0]), 4);
write(fpo, (uint8_t*)&(at->data[1]), 4);
write(fpo, (uint8_t*)&(at->data[2]), 4);
at = at->next;
}
int face_length = 4;
if(face_cnt <= 0xFFFF)
face_length = 2;
at = faces->next;
while(at != NULL) {
if(tex_cnt > 0 && normal_cnt > 0) {
write(fpo, (uint8_t*)&(at->faces[0][0]), face_length);
write(fpo, (uint8_t*)&(at->faces[0][1]), face_length);
write(fpo, (uint8_t*)&(at->faces[0][2]), face_length);
write(fpo, (uint8_t*)&(at->faces[1][0]), face_length);
write(fpo, (uint8_t*)&(at->faces[1][1]), face_length);
write(fpo, (uint8_t*)&(at->faces[1][2]), face_length);
write(fpo, (uint8_t*)&(at->faces[2][0]), face_length);
write(fpo, (uint8_t*)&(at->faces[2][1]), face_length);
write(fpo, (uint8_t*)&(at->faces[2][2]), face_length);
} else if(tex_cnt > 0 && normal_cnt == 0) {
write(fpo, (uint8_t*)&(at->faces[0][0]), face_length);
write(fpo, (uint8_t*)&(at->faces[0][1]), face_length);
write(fpo, (uint8_t*)&(at->faces[1][0]), face_length);
write(fpo, (uint8_t*)&(at->faces[1][1]), face_length);
write(fpo, (uint8_t*)&(at->faces[2][0]), face_length);
write(fpo, (uint8_t*)&(at->faces[2][1]), face_length);
} else if(tex_cnt == 0 && normal_cnt > 0) {
write(fpo, (uint8_t*)&(at->faces[0][0]), face_length);
write(fpo, (uint8_t*)&(at->faces[0][2]), face_length);
write(fpo, (uint8_t*)&(at->faces[1][0]), face_length);
write(fpo, (uint8_t*)&(at->faces[1][2]), face_length);
write(fpo, (uint8_t*)&(at->faces[2][0]), face_length);
write(fpo, (uint8_t*)&(at->faces[2][2]), face_length);
} else {
write(fpo, (uint8_t*)&(at->faces[0][0]), face_length);
write(fpo, (uint8_t*)&(at->faces[1][0]), face_length);
write(fpo, (uint8_t*)&(at->faces[2][0]), face_length);
}
at = at->next;
}
list_destroy(verts);
list_destroy(normals);
list_destroy(texs);
list_destroy(faces);
fclose(fpo); fclose(fpo);
free(out); free(out);