22 std::vector<GLushort>& indices) {
23 FILE *file = fopen(path,
"r");
25 LOGE(
"Failed to open file: %s", path);
31 int res = fscanf(file,
"%s", lineHeader);
32 if (res == EOF)
break;
33 if (strcmp(lineHeader,
"v") == 0) {
36 fscanf(file,
"%f %f %f\n", &vertex[0], &vertex[1], &vertex[2]);
38 LOGE(
"Format of 'v float float float' required for each vertice line");
41 vertices.push_back(vertex[0]);
42 vertices.push_back(vertex[1]);
43 vertices.push_back(vertex[2]);
44 }
else if (strcmp(lineHeader,
"f") == 0) {
45 GLushort vertexIndex[3];
46 int matches = fscanf(file,
"%hu %hu %hu\n", &vertexIndex[0],
47 &vertexIndex[1], &vertexIndex[2]);
49 LOGE(
"Format of 'f int int int' required for each face line");
52 indices.push_back(vertexIndex[0] - 1);
53 indices.push_back(vertexIndex[1] - 1);
54 indices.push_back(vertexIndex[2] - 1);
57 char comments_buffer[1000];
58 fgets(comments_buffer, 1000, file);
66 std::vector<GLfloat>& normals) {
67 std::vector<unsigned int> vertexIndices, normalIndices;
68 std::vector<GLfloat> temp_vertices, temp_normals;
70 FILE* file = fopen(path,
"r");
72 LOGE(
"Failed to open file: %s", path);
78 int res = fscanf(file,
"%s", lineHeader);
79 if (res == EOF)
break;
80 if (strcmp(lineHeader,
"v") == 0) {
83 fscanf(file,
"%f %f %f\n", &vertex[0], &vertex[1], &vertex[2]);
85 LOGE(
"Format of 'v float float float' required for each vertice line");
88 temp_vertices.push_back(vertex[0]);
89 temp_vertices.push_back(vertex[1]);
90 temp_vertices.push_back(vertex[2]);
91 }
else if (strcmp(lineHeader,
"vn") == 0) {
94 fscanf(file,
"%f %f %f\n", &normal[0], &normal[1], &normal[2]);
96 LOGE(
"Format of 'vn float float float' required for each normal line");
99 temp_normals.push_back(normal[0]);
100 temp_normals.push_back(normal[1]);
101 temp_normals.push_back(normal[2]);
102 }
else if (strcmp(lineHeader,
"f") == 0) {
103 GLushort vertexIndex[4];
104 GLushort normalIndex[4];
105 int matches = fscanf(file,
"%hu//%hu %hu//%hu %hu//%hu %hu//%hu\n",
106 &vertexIndex[0], &normalIndex[0], &vertexIndex[1], &normalIndex[1],
107 &vertexIndex[2], &normalIndex[2], &vertexIndex[3], &normalIndex[3]);
112 vertexIndices.push_back(vertexIndex[0] - 1);
113 vertexIndices.push_back(vertexIndex[1] - 1);
114 vertexIndices.push_back(vertexIndex[2] - 1);
115 normalIndices.push_back(normalIndex[0] - 1);
116 normalIndices.push_back(normalIndex[1] - 1);
117 normalIndices.push_back(normalIndex[2] - 1);
118 }
else if(matches == 8) {
120 vertexIndices.push_back(vertexIndex[0] - 1);
121 vertexIndices.push_back(vertexIndex[1] - 1);
122 vertexIndices.push_back(vertexIndex[2] - 1);
123 vertexIndices.push_back(vertexIndex[0] - 1);
124 vertexIndices.push_back(vertexIndex[2] - 1);
125 vertexIndices.push_back(vertexIndex[3] - 1);
126 normalIndices.push_back(normalIndex[0] - 1);
127 normalIndices.push_back(normalIndex[1] - 1);
128 normalIndices.push_back(normalIndex[2] - 1);
129 normalIndices.push_back(normalIndex[0] - 1);
130 normalIndices.push_back(normalIndex[2] - 1);
131 normalIndices.push_back(normalIndex[3] - 1);
133 LOGE(
"Format of 'f int//int int//int int//int' required for each face");
137 char comments_buffer[1000];
138 fgets(comments_buffer, 1000, file);
142 for (
unsigned int i = 0; i < vertexIndices.size(); i++) {
143 unsigned int vertexIndex = vertexIndices[i];
144 unsigned int normalIndex = normalIndices[i];
146 vertices.push_back(temp_vertices[vertexIndex * 3]);
147 vertices.push_back(temp_vertices[vertexIndex * 3 + 1]);
148 vertices.push_back(temp_vertices[vertexIndex * 3 + 2]);
149 normals.push_back(temp_normals[normalIndex * 3]);
150 normals.push_back(temp_normals[normalIndex * 3 + 1]);
151 normals.push_back(temp_normals[normalIndex * 3 + 2]);
static const float vertices[]
bool LoadOBJData(const char *path, std::vector< GLfloat > &vertices, std::vector< GLushort > &indices)