00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #include <string>
00017
00018 #include "tango-gl/obj_loader.h"
00019
00020 namespace tango_gl {
00021 bool obj_loader::LoadOBJData(const char* path, std::vector<GLfloat>& vertices,
00022 std::vector<GLushort>& indices) {
00023 FILE *file = fopen(path, "r");
00024 if (file == NULL) {
00025 LOGE("Failed to open file: %s", path);
00026 return false;
00027 }
00028
00029 while (1) {
00030 char lineHeader[128];
00031 int res = fscanf(file, "%s", lineHeader);
00032 if (res == EOF) break;
00033 if (strcmp(lineHeader, "v") == 0) {
00034 GLfloat vertex[3];
00035 int matches =
00036 fscanf(file, "%f %f %f\n", &vertex[0], &vertex[1], &vertex[2]);
00037 if (matches != 3) {
00038 LOGE("Format of 'v float float float' required for each vertice line");
00039 return false;
00040 }
00041 vertices.push_back(vertex[0]);
00042 vertices.push_back(vertex[1]);
00043 vertices.push_back(vertex[2]);
00044 } else if (strcmp(lineHeader, "f") == 0) {
00045 GLushort vertexIndex[3];
00046 int matches = fscanf(file, "%hu %hu %hu\n", &vertexIndex[0],
00047 &vertexIndex[1], &vertexIndex[2]);
00048 if (matches != 3) {
00049 LOGE("Format of 'f int int int' required for each face line");
00050 return false;
00051 }
00052 indices.push_back(vertexIndex[0] - 1);
00053 indices.push_back(vertexIndex[1] - 1);
00054 indices.push_back(vertexIndex[2] - 1);
00055 }
00056 else {
00057 char comments_buffer[1000];
00058 fgets(comments_buffer, 1000, file);
00059 }
00060 }
00061 fclose(file);
00062 return true;
00063 }
00064
00065 bool obj_loader::LoadOBJData(const char* path, std::vector<GLfloat>& vertices,
00066 std::vector<GLfloat>& normals) {
00067 std::vector<unsigned int> vertexIndices, normalIndices;
00068 std::vector<GLfloat> temp_vertices, temp_normals;
00069
00070 FILE* file = fopen(path, "r");
00071 if (file == NULL) {
00072 LOGE("Failed to open file: %s", path);
00073 return false;
00074 }
00075
00076 while (1) {
00077 char lineHeader[128];
00078 int res = fscanf(file, "%s", lineHeader);
00079 if (res == EOF) break;
00080 if (strcmp(lineHeader, "v") == 0) {
00081 GLfloat vertex[3];
00082 int matches =
00083 fscanf(file, "%f %f %f\n", &vertex[0], &vertex[1], &vertex[2]);
00084 if (matches != 3) {
00085 LOGE("Format of 'v float float float' required for each vertice line");
00086 return false;
00087 }
00088 temp_vertices.push_back(vertex[0]);
00089 temp_vertices.push_back(vertex[1]);
00090 temp_vertices.push_back(vertex[2]);
00091 } else if (strcmp(lineHeader, "vn") == 0) {
00092 GLfloat normal[3];
00093 int matches =
00094 fscanf(file, "%f %f %f\n", &normal[0], &normal[1], &normal[2]);
00095 if (matches != 3) {
00096 LOGE("Format of 'vn float float float' required for each normal line");
00097 return false;
00098 }
00099 temp_normals.push_back(normal[0]);
00100 temp_normals.push_back(normal[1]);
00101 temp_normals.push_back(normal[2]);
00102 } else if (strcmp(lineHeader, "f") == 0) {
00103 GLushort vertexIndex[4];
00104 GLushort normalIndex[4];
00105 int matches = fscanf(file, "%hu//%hu %hu//%hu %hu//%hu %hu//%hu\n",
00106 &vertexIndex[0], &normalIndex[0], &vertexIndex[1], &normalIndex[1],
00107 &vertexIndex[2], &normalIndex[2], &vertexIndex[3], &normalIndex[3]);
00108
00109
00110 if (matches == 6) {
00111
00112 vertexIndices.push_back(vertexIndex[0] - 1);
00113 vertexIndices.push_back(vertexIndex[1] - 1);
00114 vertexIndices.push_back(vertexIndex[2] - 1);
00115 normalIndices.push_back(normalIndex[0] - 1);
00116 normalIndices.push_back(normalIndex[1] - 1);
00117 normalIndices.push_back(normalIndex[2] - 1);
00118 } else if(matches == 8) {
00119
00120 vertexIndices.push_back(vertexIndex[0] - 1);
00121 vertexIndices.push_back(vertexIndex[1] - 1);
00122 vertexIndices.push_back(vertexIndex[2] - 1);
00123 vertexIndices.push_back(vertexIndex[0] - 1);
00124 vertexIndices.push_back(vertexIndex[2] - 1);
00125 vertexIndices.push_back(vertexIndex[3] - 1);
00126 normalIndices.push_back(normalIndex[0] - 1);
00127 normalIndices.push_back(normalIndex[1] - 1);
00128 normalIndices.push_back(normalIndex[2] - 1);
00129 normalIndices.push_back(normalIndex[0] - 1);
00130 normalIndices.push_back(normalIndex[2] - 1);
00131 normalIndices.push_back(normalIndex[3] - 1);
00132 } else {
00133 LOGE("Format of 'f int//int int//int int//int' required for each face");
00134 return false;
00135 }
00136 } else {
00137 char comments_buffer[1000];
00138 fgets(comments_buffer, 1000, file);
00139 }
00140 }
00141
00142 for (unsigned int i = 0; i < vertexIndices.size(); i++) {
00143 unsigned int vertexIndex = vertexIndices[i];
00144 unsigned int normalIndex = normalIndices[i];
00145
00146 vertices.push_back(temp_vertices[vertexIndex * 3]);
00147 vertices.push_back(temp_vertices[vertexIndex * 3 + 1]);
00148 vertices.push_back(temp_vertices[vertexIndex * 3 + 2]);
00149 normals.push_back(temp_normals[normalIndex * 3]);
00150 normals.push_back(temp_normals[normalIndex * 3 + 1]);
00151 normals.push_back(temp_normals[normalIndex * 3 + 2]);
00152 }
00153 fclose(file);
00154 return true;
00155 }
00156 }