Vertex.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #include "Base.h"
  2. #include "Vertex.h"
  3. namespace gameplay
  4. {
  5. Vertex::Vertex(void)
  6. : hasNormal(false), hasTangent(false), hasBinormal(false), hasTexCoord(false), hasColor(false), hasWeights(false)
  7. {
  8. }
  9. Vertex::~Vertex(void)
  10. {
  11. }
  12. unsigned int Vertex::byteSize() const
  13. {
  14. unsigned int count = 3;
  15. if (hasNormal)
  16. count += 3;
  17. if (hasTangent)
  18. count += 3;
  19. if (hasBinormal)
  20. count += 3;
  21. if (hasTexCoord)
  22. count += 2;
  23. if (hasWeights)
  24. count += 8;
  25. return count * sizeof(float);
  26. }
  27. void Vertex::writeBinary(FILE* file) const
  28. {
  29. writeVectorBinary(position, file);
  30. if (hasNormal)
  31. {
  32. writeVectorBinary(normal, file);
  33. }
  34. if (hasTangent)
  35. {
  36. writeVectorBinary(tangent, file);
  37. }
  38. if (hasBinormal)
  39. {
  40. writeVectorBinary(binormal, file);
  41. }
  42. if (hasTexCoord)
  43. {
  44. writeVectorBinary(texCoord, file);
  45. }
  46. // TODO add vertex color?
  47. //if (hasColor)
  48. //{
  49. // writeVectorBinary(color, file);
  50. //}
  51. if (hasWeights)
  52. {
  53. writeVectorBinary(blendWeights, file);
  54. writeVectorBinary(blendIndices, file);
  55. }
  56. }
  57. void Vertex::writeText(FILE* file) const
  58. {
  59. write("// position\n", file);
  60. writeVectorText(position, file);
  61. if (hasNormal)
  62. {
  63. write("// normal\n", file);
  64. writeVectorText(normal, file);
  65. }
  66. if (hasTangent)
  67. {
  68. write("// tanget\n", file);
  69. writeVectorText(tangent, file);
  70. }
  71. if (hasBinormal)
  72. {
  73. write("// binormal\n", file);
  74. writeVectorText(binormal, file);
  75. }
  76. if (hasTexCoord)
  77. {
  78. write("// texCoord\n", file);
  79. writeVectorText(texCoord, file);
  80. }
  81. if (hasWeights)
  82. {
  83. write("// blendWeights\n", file);
  84. writeVectorText(blendWeights, file);
  85. write("// blendIndices\n", file);
  86. writeVectorText(blendIndices, file);
  87. }
  88. }
  89. void Vertex::normalizeBlendWeight()
  90. {
  91. float total = blendWeights.x + blendWeights.y + blendWeights.z + blendWeights.w;
  92. if (total > 0.0f)
  93. {
  94. blendWeights.x = blendWeights.x / total;
  95. blendWeights.y = blendWeights.y / total;
  96. blendWeights.z = blendWeights.z / total;
  97. blendWeights.w = blendWeights.w / total;
  98. }
  99. }
  100. }