poprawiona wydajność, zużycie RAMu

This commit is contained in:
2024-11-28 02:19:26 +01:00
parent bab9ab751c
commit 2f8f14d279
4 changed files with 79 additions and 122 deletions

View File

@@ -15,11 +15,15 @@ bool loadOBJ(const char* path, std::vector <glm::vec3>& out_vertices, std::vecto
return false;
}
int line = 0;
unsigned int line = 0;
unsigned int vs = 0;
unsigned int vts = 0;
unsigned int vns = 0;
unsigned int fs = 0;
char lineHeader[128];
while (1) {
char lineHeader[128];
// read the first word of the line
int res = fscanf(file, "%s", lineHeader);
if (res == EOF) break; // EOF = End Of File. Quit the loop.
@@ -30,23 +34,27 @@ bool loadOBJ(const char* path, std::vector <glm::vec3>& out_vertices, std::vecto
glm::vec3 vertex;
fscanf(file, "%f %f %f\n", &vertex.x, &vertex.y, &vertex.z);
temp_vertices.push_back(vertex);
vs++;
} else if (strcmp(lineHeader, "vt") == 0) {
glm::vec2 uv;
fscanf(file, "%f %f\n", &uv.x, &uv.y);
temp_uvs.push_back(uv);
vts++;
} else if (strcmp(lineHeader, "vn") == 0) {
glm::vec3 normal;
fscanf(file, "%f %f %f\n", &normal.x, &normal.y, &normal.z);
temp_normals.push_back(normal);
vns++;
} else if (strcmp(lineHeader, "f") == 0) {
unsigned int vertexIndex[3], uvIndex[3], normalIndex[3];
int matches = fscanf(file, "%d/%d/%d %d/%d/%d %d/%d/%d\n", &vertexIndex[0], &uvIndex[0], &normalIndex[0], &vertexIndex[1], &uvIndex[1], &normalIndex[1], &vertexIndex[2], &uvIndex[2], &normalIndex[2]);
fs++;
if (matches != 9){
printf("File can't be read by our simple parser. Try exporting with other options (%d matches on line %d)\n", matches, line);
@@ -64,24 +72,27 @@ bool loadOBJ(const char* path, std::vector <glm::vec3>& out_vertices, std::vecto
normalIndices.push_back(normalIndex[2]);
}
// For each vertex of each triangle
for (unsigned int i = 0; i < vertexIndices.size(); i++) {
unsigned int vertexIndex = vertexIndices[i];
unsigned int uvIndex = uvIndices[i];
unsigned int normalIndex = normalIndices[i];
glm::vec3 vertex1 = temp_vertices[vertexIndex - 1];
glm::vec2 vertex2 = temp_uvs[uvIndex - 1];
glm::vec3 vertex3 = temp_normals[normalIndex - 1];
out_vertices .push_back(vertex1);
out_uvs .push_back(vertex2);
out_normals .push_back(vertex3);
}
line++;
}
// For each vertex of each triangle
for (unsigned int i = 0; i < vertexIndices.size(); i++) {
unsigned int vertexIndex = vertexIndices[i];
unsigned int uvIndex = uvIndices[i];
unsigned int normalIndex = normalIndices[i];
glm::vec3 vertex1 = temp_vertices[vertexIndex - 1];
glm::vec2 vertex2 = temp_uvs[uvIndex - 1];
glm::vec3 vertex3 = temp_normals[normalIndex - 1];
out_vertices.push_back(vertex1);
out_uvs.push_back(vertex2);
out_normals.push_back(vertex3);
}
fclose(file);
printf("(loadOBJ.cpp) Loaded file with %d v, %d vt, %d vn, %d f. %d lines total.\n", vs, vts, vns, fs, line);
return true;
}