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.

120 lines
3.5 KiB

5 years ago
5 years ago
5 years ago
  1. #include "model.h"
  2. #include "utils.h"
  3. Model::Model() {
  4. }
  5. void Model::Init(const char*modelPath) {
  6. struct FloatData {
  7. float v[3];
  8. };
  9. struct VertexDefine {
  10. int posIndex;
  11. int texcoordIndex;
  12. int normalIndex;
  13. };
  14. int nFileSize = 0;
  15. unsigned char*fileContent = LoadFileContent(modelPath, nFileSize);
  16. if (fileContent == nullptr) {
  17. return;
  18. }
  19. std::vector<FloatData> positions, texcoords, normals;
  20. std::vector<VertexDefine> vertexes;
  21. std::string temp;
  22. std::stringstream ssFileContent((char*)fileContent);
  23. char szOneLine[256];
  24. while (!ssFileContent.eof()) {
  25. memset(szOneLine, 0, 256);
  26. ssFileContent.getline(szOneLine, 256);
  27. if (strlen(szOneLine) > 0) {
  28. if (szOneLine[0] == 'v') {
  29. std::stringstream ssOneLine(szOneLine);
  30. if (szOneLine[1] == 't') {
  31. ssOneLine >> temp;
  32. FloatData floatData;
  33. ssOneLine >> floatData.v[0];
  34. ssOneLine >> floatData.v[1];
  35. texcoords.push_back(floatData);
  36. }
  37. else if (szOneLine[1] == 'n') {
  38. ssOneLine >> temp;
  39. FloatData floatData;
  40. ssOneLine >> floatData.v[0];
  41. ssOneLine >> floatData.v[1];
  42. ssOneLine >> floatData.v[2];
  43. normals.push_back(floatData);
  44. }
  45. else {
  46. ssOneLine >> temp;
  47. FloatData floatData;
  48. ssOneLine >> floatData.v[0];
  49. ssOneLine >> floatData.v[1];
  50. ssOneLine >> floatData.v[2];
  51. positions.push_back(floatData);
  52. }
  53. }
  54. else if (szOneLine[0] == 'f') {
  55. std::stringstream ssOneLine(szOneLine);
  56. ssOneLine >> temp;
  57. std::string vertexStr;
  58. for (int i = 0; i < 3; i++) {
  59. ssOneLine >> vertexStr;
  60. size_t pos = vertexStr.find_first_of('/');
  61. std::string posIndexStr = vertexStr.substr(0, pos);
  62. size_t pos2 = vertexStr.find_first_of('/', pos + 1);
  63. std::string texcoordIndexStr = vertexStr.substr(pos + 1, pos2 - 1 - pos);
  64. std::string normalIndexStr = vertexStr.substr(pos2 + 1, vertexStr.length() - 1 - pos2);
  65. VertexDefine vd;
  66. vd.posIndex = atoi(posIndexStr.c_str());
  67. vd.texcoordIndex = atoi(texcoordIndexStr.c_str());
  68. vd.normalIndex = atoi(normalIndexStr.c_str());
  69. vertexes.push_back(vd);
  70. }
  71. }
  72. }
  73. }
  74. delete fileContent;
  75. int vertexCount = (int)vertexes.size();
  76. mVertexBuffer = new VertexBuffer;
  77. mVertexBuffer->SetSize(vertexCount);
  78. for (int i = 0; i < vertexCount; ++i) {
  79. float *temp = positions[vertexes[i].posIndex - 1].v;
  80. mVertexBuffer->SetPosition(i, temp[0], temp[1], temp[2]);
  81. temp = texcoords[vertexes[i].texcoordIndex - 1].v;
  82. mVertexBuffer->SetTexcoord(i, temp[0], temp[1]);
  83. temp = normals[vertexes[i].normalIndex - 1].v;
  84. mVertexBuffer->SetNormal(i, temp[0], temp[1], temp[2]);
  85. }
  86. mShader = new Shader;
  87. mShader->Init("Res/model.vs", "Res/model.fs");
  88. mShader->SetVec4("U_LightPos", 0.0f, 1.0f, 1.0f, 0.0f);//���ù�Դλ��
  89. mShader->SetVec4("U_LightAmbient", 1.0f, 1.0f, 1.0f, 1.0f);//���û���������
  90. mShader->SetVec4("U_LightDiffuse", 1.0f, 1.0f, 1.0f, 1.0f);//����������������
  91. mShader->SetVec4("U_AmbientMaterial", 0.1f, 0.1f, 0.1f, 1.0f);//���û��������ʷ���
  92. mShader->SetVec4("U_DiffuseMaterial", 0.6f, 0.6f, 0.6f, 1.0f);//�������������ʷ���
  93. }
  94. void Model::Draw(glm::mat4 & viewMatrix, glm::mat4 projectionMatrix) {
  95. glEnable(GL_DEPTH_TEST);
  96. mVertexBuffer->Bind();
  97. mShader->Bind(glm::value_ptr(mModelMatrix), glm::value_ptr(viewMatrix), glm::value_ptr(projectionMatrix));
  98. glDrawArrays(GL_TRIANGLES, 0, mVertexBuffer->mVertexCount);
  99. mVertexBuffer->Unbind();
  100. }
  101. void Model::SetPosition(float x, float y, float z) {
  102. mModelMatrix = glm::translate(x, y, z);
  103. }