You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

122 lines
3.4 KiB

4 years ago
4 years ago
  1. #include "model.h"
  2. #include "misc.h"
  3. #include <stdio.h>
  4. #include <string>
  5. #include <sstream>
  6. #include <vector>
  7. VertexData * LoadObjModel(const char * filePath, unsigned int ** indexes, int & vertexCount, int & indexCount)
  8. {
  9. char* fileContent = LoadFileContent(filePath);
  10. if (fileContent != nullptr) {
  11. struct VertexInfo {
  12. float v[3];
  13. };
  14. struct VertexDefine {
  15. int positionIndex;
  16. int texcoordIndex;
  17. int normalIndex;
  18. };
  19. std::vector<VertexInfo> position;
  20. std::vector<VertexInfo> texcoord;
  21. std::vector<VertexInfo> normal;
  22. std::vector<unsigned int> objIndexes; //�������� ��Ӧopengl��indexes
  23. std::vector<VertexDefine> vertices; //ÿһ���� ��Ӧopengl��vertex
  24. std::stringstream ssObjFile(fileContent);
  25. char szOneLine[256];
  26. std::string temp;
  27. while (!ssObjFile.eof()) {
  28. memset(szOneLine, 0, 256);
  29. ssObjFile.getline(szOneLine, 256);
  30. //printf("%s \n", szOneLine);
  31. if (strlen(szOneLine) > 0) {
  32. std::stringstream ssOneLine(szOneLine);
  33. if (szOneLine[0] == 'v') {
  34. if (szOneLine[1] == 't') {
  35. ssOneLine >> temp;
  36. VertexInfo vi;
  37. ssOneLine >> vi.v[0];
  38. ssOneLine >> vi.v[1];
  39. texcoord.push_back(vi);
  40. }
  41. else if (szOneLine[1] == 'n') {
  42. ssOneLine >> temp;
  43. VertexInfo vi;
  44. ssOneLine >> vi.v[0];
  45. ssOneLine >> vi.v[1];
  46. ssOneLine >> vi.v[2];
  47. normal.push_back(vi);
  48. }
  49. else {
  50. ssOneLine >> temp;
  51. VertexInfo vi;
  52. ssOneLine >> vi.v[0];
  53. ssOneLine >> vi.v[1];
  54. ssOneLine >> vi.v[2];
  55. position.push_back(vi);
  56. }
  57. }
  58. else if(szOneLine[0] == 'f'){
  59. ssOneLine >> temp;
  60. std::string vertexStr;
  61. for (int i = 0; i < 3; i++) {
  62. ssOneLine >> vertexStr;
  63. size_t pos = vertexStr.find_first_of('/');
  64. std::string positionIndexStr = vertexStr.substr(0, pos);
  65. size_t pos2 = vertexStr.find_first_of('/', pos + 1);
  66. std::string texcoordIndexStr = vertexStr.substr(pos + 1, pos2 - pos - 1);
  67. std::string normalIndexStr = vertexStr.substr(pos2 + 1, vertexStr.length() - pos2 - 1);
  68. VertexDefine vd;
  69. vd.positionIndex = atoi(positionIndexStr.c_str()) - 1;
  70. vd.texcoordIndex = atoi(texcoordIndexStr.c_str()) - 1;
  71. vd.normalIndex = atoi(normalIndexStr.c_str()) - 1;
  72. int nCurrentIndex = -1;
  73. size_t nCurrentVerticeCount = vertices.size();
  74. for (size_t j = 0; j < nCurrentVerticeCount; j++) {
  75. if (vertices[j].positionIndex == vd.positionIndex &&
  76. vertices[j].texcoordIndex == vd.texcoordIndex &&
  77. vertices[j].normalIndex == vd.normalIndex) {
  78. nCurrentIndex = j;
  79. break;
  80. }
  81. }
  82. if (nCurrentIndex == -1) {
  83. nCurrentIndex = vertices.size();
  84. vertices.push_back(vd);
  85. }
  86. objIndexes.push_back(nCurrentIndex);
  87. }
  88. }
  89. }
  90. }
  91. printf("face count %u\n", objIndexes.size() / 3);
  92. indexCount = (int)objIndexes.size();
  93. *indexes = new unsigned int[indexCount];
  94. for (int i = 0; i < indexCount; i++) {
  95. (*indexes)[i] = objIndexes[i];
  96. }
  97. vertexCount = (int)vertices.size();
  98. VertexData *vertexes = new VertexData[vertexCount];
  99. for (int i = 0; i < vertexCount; i++) {
  100. memcpy(vertexes[i].position, position[vertices[i].positionIndex].v, sizeof(float) * 3);
  101. memcpy(vertexes[i].texcoord, texcoord[vertices[i].texcoordIndex].v, sizeof(float) * 2);
  102. memcpy(vertexes[i].normal, normal[vertices[i].normalIndex].v, sizeof(float) * 3);
  103. }
  104. return vertexes;
  105. }
  106. return nullptr;
  107. }