obj_loader.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2014 Google Inc. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #include <string>
17 
18 #include "tango-gl/obj_loader.h"
19 
20 namespace tango_gl {
21 bool obj_loader::LoadOBJData(const char* path, std::vector<GLfloat>& vertices,
22  std::vector<GLushort>& indices) {
23  FILE *file = fopen(path, "r");
24  if (file == NULL) {
25  LOGE("Failed to open file: %s", path);
26  return false;
27  }
28 
29  while (1) {
30  char lineHeader[128];
31  int res = fscanf(file, "%s", lineHeader);
32  if (res == EOF) break;
33  if (strcmp(lineHeader, "v") == 0) {
34  GLfloat vertex[3];
35  int matches =
36  fscanf(file, "%f %f %f\n", &vertex[0], &vertex[1], &vertex[2]);
37  if (matches != 3) {
38  LOGE("Format of 'v float float float' required for each vertice line");
39  return false;
40  }
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]);
48  if (matches != 3) {
49  LOGE("Format of 'f int int int' required for each face line");
50  return false;
51  }
52  indices.push_back(vertexIndex[0] - 1);
53  indices.push_back(vertexIndex[1] - 1);
54  indices.push_back(vertexIndex[2] - 1);
55  }
56  else {
57  char comments_buffer[1000];
58  fgets(comments_buffer, 1000, file);
59  }
60  }
61  fclose(file);
62  return true;
63 }
64 
65 bool obj_loader::LoadOBJData(const char* path, std::vector<GLfloat>& vertices,
66  std::vector<GLfloat>& normals) {
67  std::vector<unsigned int> vertexIndices, normalIndices;
68  std::vector<GLfloat> temp_vertices, temp_normals;
69 
70  FILE* file = fopen(path, "r");
71  if (file == NULL) {
72  LOGE("Failed to open file: %s", path);
73  return false;
74  }
75 
76  while (1) {
77  char lineHeader[128];
78  int res = fscanf(file, "%s", lineHeader);
79  if (res == EOF) break;
80  if (strcmp(lineHeader, "v") == 0) {
81  GLfloat vertex[3];
82  int matches =
83  fscanf(file, "%f %f %f\n", &vertex[0], &vertex[1], &vertex[2]);
84  if (matches != 3) {
85  LOGE("Format of 'v float float float' required for each vertice line");
86  return false;
87  }
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) {
92  GLfloat normal[3];
93  int matches =
94  fscanf(file, "%f %f %f\n", &normal[0], &normal[1], &normal[2]);
95  if (matches != 3) {
96  LOGE("Format of 'vn float float float' required for each normal line");
97  return false;
98  }
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]);
108 
109  // .obj file is 1-indexed, so subtract 1 from all indices.
110  if (matches == 6) {
111  // If triangles provided.
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) {
119  // If quads provided.
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);
132  } else {
133  LOGE("Format of 'f int//int int//int int//int' required for each face");
134  return false;
135  }
136  } else {
137  char comments_buffer[1000];
138  fgets(comments_buffer, 1000, file);
139  }
140  }
141 
142  for (unsigned int i = 0; i < vertexIndices.size(); i++) {
143  unsigned int vertexIndex = vertexIndices[i];
144  unsigned int normalIndex = normalIndices[i];
145 
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]);
152  }
153  fclose(file);
154  return true;
155 }
156 } // namespace tango_gl
#define NULL
static const float vertices[]
Definition: quad.cpp:39
#define LOGE(...)
bool LoadOBJData(const char *path, std::vector< GLfloat > &vertices, std::vector< GLushort > &indices)
Definition: obj_loader.cpp:21
matches
res


rtabmap
Author(s): Mathieu Labbe
autogenerated on Mon Jan 23 2023 03:37:29