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.

192 lines
5.1 KiB

5 years ago
  1. #include "model.h"
  2. #include "utils.h"
  3. Model::Model()
  4. {
  5. //��ʼ������
  6. memset(mAmbientMaterial, 0, sizeof(mAmbientMaterial));
  7. memset(mDiffuseMaterial, 0, sizeof(mDiffuseMaterial));
  8. memset(mSpecularMaterial, 0, sizeof(mSpecularMaterial));
  9. }
  10. void Model::Init(const char * modelPath)
  11. {
  12. struct FloatData {
  13. float v[3];
  14. };
  15. struct VertexDefine {
  16. int posIndex;
  17. int texcoordIndex;
  18. int normalIndex;
  19. };
  20. int nFileSize = 0;
  21. unsigned char* fileContent = LoadFileContent(modelPath, nFileSize);
  22. if (fileContent == nullptr) {
  23. return;
  24. }
  25. std::vector<FloatData> positions, texcoords, normals;
  26. std::vector<VertexDefine> vertexes;
  27. std::vector<int> indexes;
  28. std::string temp;
  29. //�洢�ļ����ݣ��ڴ���
  30. std::stringstream ssFileContent((char*)fileContent);
  31. //�洢������
  32. char szOneLine[256];
  33. //ʹ��������ȡ�ļ�
  34. while (!ssFileContent.eof()) {
  35. //��ȡһ��
  36. memset(szOneLine, 0, 256);
  37. ssFileContent.getline(szOneLine, 256);
  38. //�������
  39. if (strlen(szOneLine) > 0) {
  40. if (szOneLine[0] == 'v') {
  41. std::stringstream ssOneLine(szOneLine);
  42. if (szOneLine[1] == 't') {
  43. ssOneLine >> temp;//ͨ��������vt�� temp
  44. FloatData floatData;
  45. ssOneLine >> floatData.v[0];
  46. ssOneLine >> floatData.v[1];
  47. texcoords.push_back(floatData);
  48. printf("texcoord : %f,%f\n", floatData.v[0], floatData.v[1]);
  49. }
  50. else if (szOneLine[1] == 'n') {
  51. ssOneLine >> temp;
  52. FloatData floatData;
  53. ssOneLine >> floatData.v[0];
  54. ssOneLine >> floatData.v[1];
  55. ssOneLine >> floatData.v[2];
  56. normals.push_back(floatData);
  57. printf("normal : %f,%f,%f\n", floatData.v[0], floatData.v[1], floatData.v[2]);
  58. }
  59. else {
  60. ssOneLine >> temp;
  61. FloatData floatData;
  62. ssOneLine >> floatData.v[0];
  63. ssOneLine >> floatData.v[1];
  64. ssOneLine >> floatData.v[2];
  65. positions.push_back(floatData);
  66. printf("position : %f,%f,%f\n", floatData.v[0], floatData.v[1], floatData.v[2]);
  67. }
  68. }
  69. else if (szOneLine[0] == 'f') {
  70. std::stringstream ssOneLine(szOneLine);
  71. ssOneLine >> temp;
  72. std::string vertexStr;
  73. for (int i = 0; i < 3; i++) {
  74. ssOneLine >> vertexStr;
  75. //��ȡposition����
  76. size_t pos = vertexStr.find_first_of('/');
  77. std::string posIndexStr = vertexStr.substr(0, pos);
  78. //��ȡtexcoord����
  79. size_t pos2 = vertexStr.find_first_of('/', pos + 1);
  80. std::string texcoordIndexStr = vertexStr.substr(pos + 1, pos2 - 1 - pos);
  81. //��ȡnormal����
  82. std::string normalIndexStr = vertexStr.substr(pos2 + 1, vertexStr.length() - 1 - pos2);
  83. VertexDefine vd;
  84. vd.posIndex = atoi(posIndexStr.c_str());
  85. vd.texcoordIndex = atoi(texcoordIndexStr.c_str());
  86. vd.normalIndex = atoi(normalIndexStr.c_str());
  87. //ȥ���ظ�
  88. int nCurrentVertexIndex = -1;
  89. int nCurrentVertexCount = (int)vertexes.size();
  90. for (int j = 0; j < nCurrentVertexCount; j++) {
  91. if (vertexes[j].posIndex == vd.posIndex && vertexes[j].texcoordIndex == vd.texcoordIndex && vertexes[j].normalIndex == vd.normalIndex) {
  92. nCurrentVertexIndex = j;
  93. break;
  94. }
  95. }
  96. if (nCurrentVertexIndex == -1) {
  97. nCurrentVertexIndex = (int)vertexes.size();
  98. vertexes.push_back(vd);
  99. }
  100. indexes.push_back(nCurrentVertexIndex);
  101. }
  102. printf("draw command : %s\n", szOneLine);
  103. }
  104. }
  105. }
  106. //TODOΪɶҪ���ƣ�����
  107. mIndexCount = (int)indexes.size();
  108. mIndexes = new unsigned short[mIndexCount];
  109. for (int i = 0; i < mIndexCount; ++i) {
  110. mIndexes[i] = indexes[i];
  111. }
  112. int vertexCount = (int)vertexes.size();
  113. mVertexes = new VertexData[vertexCount];
  114. for (int i = 0; i < vertexCount; i++) {
  115. memcpy(mVertexes[i].position, positions[vertexes[i].posIndex - 1].v, sizeof(float) * 3);
  116. memcpy(mVertexes[i].texcoord, texcoords[vertexes[i].texcoordIndex - 1].v, sizeof(float) * 2);
  117. memcpy(mVertexes[i].normal, normals[vertexes[i].normalIndex - 1].v, sizeof(float) * 3);
  118. }
  119. delete fileContent;
  120. }
  121. void Model::Draw()
  122. {
  123. //��������
  124. glEnable(GL_LIGHTING);
  125. glMaterialfv(GL_FRONT, GL_AMBIENT, mAmbientMaterial);
  126. glMaterialfv(GL_FRONT, GL_DIFFUSE, mDiffuseMaterial);
  127. glMaterialfv(GL_FRONT, GL_SPECULAR, mSpecularMaterial);
  128. //��������
  129. glEnable(GL_TEXTURE_2D);
  130. //glMaterialf(GL_FRONT, GL_SHININESS, 64.0f);
  131. //glDisable(GL_TEXTURE_2D);
  132. glBindTexture(GL_TEXTURE_2D, mTexture);
  133. glEnable(GL_DEPTH_TEST);
  134. glPushMatrix();
  135. glTranslatef(0.0f, 0.0f, -5.0f);
  136. static float t = 0.0;
  137. glRotated(t, 1.0f, 1.0f, 1.0f);
  138. t += 0.05;
  139. glBegin(GL_TRIANGLES);
  140. for (int i = 0; i < mIndexCount; i++) {
  141. glTexCoord2fv(mVertexes[mIndexes[i]].texcoord);
  142. glNormal3fv(mVertexes[mIndexes[i]].normal);
  143. glVertex3fv(mVertexes[mIndexes[i]].position);
  144. }
  145. glEnd();
  146. glPopMatrix();
  147. }
  148. void Model::SetAmbientMaterial(float r, float g, float b, float a) {
  149. mAmbientMaterial[0] = r;
  150. mAmbientMaterial[1] = g;
  151. mAmbientMaterial[2] = b;
  152. mAmbientMaterial[3] = a;
  153. }
  154. void Model::SetDiffuseMaterial(float r, float g, float b, float a) {
  155. mDiffuseMaterial[0] = r;
  156. mDiffuseMaterial[1] = g;
  157. mDiffuseMaterial[2] = b;
  158. mDiffuseMaterial[3] = a;
  159. }
  160. void Model::SetSpecularMaterial(float r, float g, float b, float a) {
  161. mSpecularMaterial[0] = r;
  162. mSpecularMaterial[1] = g;
  163. mSpecularMaterial[2] = b;
  164. mSpecularMaterial[3] = a;
  165. }