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.

149 lines
4.5 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
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_LightAmbient", 1.0f, 1.0f, 1.0f, 1.0f);//���û���������
  89. mShader->SetVec4("U_LightPos", 0.0f, 1.0f, 2.0f, 0.0f);//���ù�Դλ��
  90. mShader->SetVec4("U_LightDiffuse", 1.0f, 1.0f, 1.0f, 1.0f);//����������������
  91. mShader->SetVec4("U_LightSpecular", 1.0f, 1.0f, 1.0f, 1.0f);//���þ��淴��������
  92. mShader->SetVec4("U_CameraPos", 0.0f, 0.0f, 0.0f, 1.0f);//���������
  93. mShader->SetVec4("U_LightOpt", 32.0f, 0.0f, 0.0f, 1.0f);//TODOû�����ף�ֻ֪�������ݼ���
  94. SetAmbientMaterial(0.1f, 0.1f, 0.1f, 1.0f);//���û��������ʷ���ϵ��
  95. SetDiffuseMaterial(0.6f, 0.6f, 0.6f, 1.0f);//�������������ʷ���ϵ��
  96. SetSpecularMaterial(1.0f, 1.0f, 1.0f, 1.0f);//���þ��淴�����ʷ���ϵ��
  97. }
  98. void Model::Draw(glm::mat4 & viewMatrix, glm::mat4 projectionMatrix, float x, float y, float z) {
  99. mShader->SetVec4("U_CameraPos", x, y, z, 1.0);
  100. glEnable(GL_DEPTH_TEST);
  101. mVertexBuffer->Bind();
  102. glm::mat4 it = glm::inverseTranspose(mModelMatrix);
  103. mShader->Bind(glm::value_ptr(mModelMatrix), glm::value_ptr(viewMatrix), glm::value_ptr(projectionMatrix));
  104. glUniformMatrix4fv(glGetUniformLocation(mShader->mProgram, "IT_ModelMatrix"), 1, GL_FALSE, glm::value_ptr(it));
  105. glDrawArrays(GL_TRIANGLES, 0, mVertexBuffer->mVertexCount);
  106. mVertexBuffer->Unbind();
  107. }
  108. void Model::SetPosition(float x, float y, float z) {
  109. mModelMatrix = glm::translate(x, y, z);
  110. }
  111. void Model::SetAmbientMaterial(float r, float g, float b, float a) {
  112. mShader->SetVec4("U_AmbientMaterial", r, g, b, a);
  113. }
  114. void Model::SetDiffuseMaterial(float r, float g, float b, float a) {
  115. mShader->SetVec4("U_DiffuseMaterial", r, g, b, a);
  116. }
  117. void Model::SetSpecularMaterial(float r, float g, float b, float a) {
  118. mShader->SetVec4("U_SpecularMaterial", r, g, b, a);
  119. }
  120. void Model::SetTexture(const char* imagePath) {
  121. mShader->SetTexture("U_Texture", imagePath);
  122. }
  123. void Model::SetTexture(GLuint texture)
  124. {
  125. mShader->SetTexture("U_Texture", texture);
  126. }