Vertex.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #include "Base.h"
  2. #include "Vertex.h"
  3. namespace gameplay
  4. {
  5. Vertex::Vertex(void)
  6. : hasNormal(false), hasTangent(false), hasBinormal(false), hasDiffuse(false), hasWeights(false)
  7. {
  8. for (unsigned int i = 0; i < MAX_UV_SETS; ++i)
  9. hasTexCoord[i] = false;
  10. }
  11. Vertex::~Vertex(void)
  12. {
  13. }
  14. unsigned int Vertex::byteSize() const
  15. {
  16. unsigned int count = POSITION_COUNT;
  17. if (hasNormal)
  18. count += NORMAL_COUNT;
  19. if (hasTangent)
  20. count += TANGENT_COUNT;
  21. if (hasBinormal)
  22. count += BINORMAL_COUNT;
  23. for (unsigned int i = 0; i < MAX_UV_SETS; ++i)
  24. {
  25. if (hasTexCoord[i])
  26. count += TEXCOORD_COUNT;
  27. }
  28. if (hasWeights)
  29. count += BLEND_WEIGHTS_COUNT + BLEND_INDICES_COUNT;
  30. if (hasDiffuse)
  31. count += DIFFUSE_COUNT;
  32. return count * sizeof(float);
  33. }
  34. void Vertex::writeBinary(FILE* file) const
  35. {
  36. writeVectorBinary(position, file);
  37. if (hasNormal)
  38. {
  39. writeVectorBinary(normal, file);
  40. }
  41. if (hasTangent)
  42. {
  43. writeVectorBinary(tangent, file);
  44. }
  45. if (hasBinormal)
  46. {
  47. writeVectorBinary(binormal, file);
  48. }
  49. for (unsigned int i = 0; i < MAX_UV_SETS; ++i)
  50. {
  51. if (hasTexCoord[i])
  52. {
  53. writeVectorBinary(texCoord[i], file);
  54. }
  55. }
  56. if (hasDiffuse)
  57. {
  58. writeVectorBinary(diffuse, file);
  59. }
  60. if (hasWeights)
  61. {
  62. writeVectorBinary(blendWeights, file);
  63. writeVectorBinary(blendIndices, file);
  64. }
  65. }
  66. void Vertex::writeText(FILE* file) const
  67. {
  68. write("// position\n", file);
  69. writeVectorText(position, file);
  70. if (hasNormal)
  71. {
  72. write("// normal\n", file);
  73. writeVectorText(normal, file);
  74. }
  75. if (hasTangent)
  76. {
  77. write("// tanget\n", file);
  78. writeVectorText(tangent, file);
  79. }
  80. if (hasBinormal)
  81. {
  82. write("// binormal\n", file);
  83. writeVectorText(binormal, file);
  84. }
  85. for (unsigned int i = 0; i < MAX_UV_SETS; ++i)
  86. {
  87. if (hasTexCoord[i])
  88. {
  89. write("// texCoord\n", file);
  90. writeVectorText(texCoord[i], file);
  91. }
  92. }
  93. if (hasDiffuse)
  94. {
  95. write("// diffuse\n", file);
  96. writeVectorText(diffuse, file);
  97. }
  98. if (hasWeights)
  99. {
  100. write("// blendWeights\n", file);
  101. writeVectorText(blendWeights, file);
  102. write("// blendIndices\n", file);
  103. writeVectorText(blendIndices, file);
  104. }
  105. }
  106. void Vertex::normalizeBlendWeight()
  107. {
  108. float total = blendWeights.x + blendWeights.y + blendWeights.z + blendWeights.w;
  109. if (total > 0.0f)
  110. {
  111. blendWeights.x = blendWeights.x / total;
  112. blendWeights.y = blendWeights.y / total;
  113. blendWeights.z = blendWeights.z / total;
  114. blendWeights.w = blendWeights.w / total;
  115. }
  116. }
  117. }