Polygon.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /***************************************************************************
  2. Polygon.cpp - description
  3. -------------------
  4. begin : Wed Jan 26 2000
  5. copyright : (C) 2000 by Henrik Enqvist
  6. email : henqvist@excite.com
  7. ***************************************************************************/
  8. #include <cstdlib>
  9. #include <algorithm>
  10. #include "Private.h"
  11. #include "Polygon.h"
  12. #include "Shape3D.h"
  13. Polygon3D::Polygon3D(Shape3D* s, int size) {
  14. EmAssert(s != NULL, "Polygon3D::Polygon3D shape NULL in constructor");
  15. m_iProperties = 0;
  16. m_nmlSrc.x = 0;
  17. m_nmlSrc.y = 1;
  18. m_nmlSrc.z = 0;
  19. m_nmlTrans.x = 0;
  20. m_nmlTrans.y = 1;
  21. m_nmlTrans.z = 0;
  22. m_colFlatLight.r = 0;
  23. m_colFlatLight.g = 0;
  24. m_colFlatLight.b = 0;
  25. m_colFlatLight.a = 0;
  26. m_vIndex.reserve(size);
  27. m_vIndex.clear();
  28. p_Shape3D = s;
  29. }
  30. Polygon3D::~Polygon3D() {
  31. }
  32. void Polygon3D::copy(Polygon3D * poly) {
  33. if (poly == NULL) return;
  34. m_nmlSrc = poly->m_nmlSrc;
  35. m_nmlTrans = poly->m_nmlTrans;
  36. m_colFlatLight = poly->m_colFlatLight;
  37. m_vIndex.clear();
  38. vector<int>::iterator iter = poly->m_vIndex.begin();
  39. vector<int>::iterator end = poly->m_vIndex.end();
  40. for (; iter != end; ++iter) {
  41. m_vIndex.push_back(*iter);
  42. }
  43. }
  44. void Polygon3D::setProperty(int p) {
  45. m_iProperties |= p;
  46. }
  47. void Polygon3D::unsetProperty(int p) {
  48. m_iProperties -= (m_iProperties & p);
  49. }
  50. int Polygon3D::getProperties() {
  51. return m_iProperties;
  52. }
  53. void Polygon3D::setColor(float r, float g, float b, float a) {
  54. EmAssert(p_Shape3D != NULL, "Polygon3D::setColor shape NULL");
  55. vector<int>::iterator iter = m_vIndex.begin();
  56. vector<int>::iterator end = m_vIndex.end();
  57. for (; iter != end; ++iter) {
  58. p_Shape3D->setColor((*iter), r, g, b, a);
  59. }
  60. }
  61. void Polygon3D::decrement(int shindex) {
  62. if (shindex < 0) return;
  63. vector<int>::iterator iter = m_vIndex.begin();
  64. vector<int>::iterator end = m_vIndex.end();
  65. for (; iter != end; ++iter) {
  66. if ((*iter) > shindex) --(*iter);
  67. }
  68. }
  69. bool Polygon3D::connected(int shindexA, int shindexB) {
  70. if (this->includes(shindexA) == -1) return false;
  71. if (this->includes(shindexB) == -1) return false;
  72. if (this->includes(shindexA) == (this->includes(shindexB) + 1)) return true;
  73. if (this->includes(shindexB) == (this->includes(shindexA) + 1)) return true;
  74. if (this->includes(shindexA) == ((signed)m_vIndex.size() - 1) && this->includes(shindexB) == 0) return true;
  75. if (this->includes(shindexB) == ((signed)m_vIndex.size() - 1) && this->includes(shindexA) == 0) return true;
  76. return false;
  77. }
  78. int Polygon3D::getIndex(int polyindex) {
  79. if (polyindex < 0 || polyindex >= (signed)m_vIndex.size()) {
  80. return -1;
  81. }
  82. return m_vIndex[polyindex];
  83. }
  84. int Polygon3D::getIndexSize() {
  85. return m_vIndex.size();
  86. }
  87. Color * Polygon3D::getColor(int index) {
  88. EmAssert(p_Shape3D != NULL, "Polygon3D::getColor shape NULL");
  89. if (index < 0 || index >= (signed)m_vIndex.size()) {
  90. return NULL;
  91. }
  92. return p_Shape3D->getColor(m_vIndex[index]);
  93. }
  94. void Polygon3D::setColor(int index, float r, float g, float b, float a) {
  95. EmAssert(p_Shape3D != NULL, "Polygon3D::setColor shape NULL");
  96. if (index < 0 || index >= (signed)m_vIndex.size()) {
  97. return;
  98. }
  99. p_Shape3D->setColor(m_vIndex[index], r, g, b, a);
  100. }
  101. TexCoord * Polygon3D::getTexCoord(int index) {
  102. EmAssert(p_Shape3D != NULL, "Polygon3D::getTexCoord shape NULL");
  103. if (index < 0 || index >= (signed)m_vIndex.size()) {
  104. return NULL;
  105. }
  106. return p_Shape3D->getTexCoord(m_vIndex[index]);
  107. }
  108. void Polygon3D::add(int index) {
  109. EmAssert(p_Shape3D != NULL, "Polygon3D::add shape NULL");
  110. if (index < 0 || index >= (signed)p_Shape3D->m_vVtxSrc.size()) return;
  111. m_vIndex.push_back(index);
  112. if (m_vIndex.size() == 3) this->countNormal();
  113. }
  114. void Polygon3D::countNormal() {
  115. Vertex3D vtxA, vtxB;
  116. Vertex3D vtx0, vtx1, vtx2;
  117. if (m_vIndex.size() < 3 || p_Shape3D == NULL) return;
  118. vtx0 = p_Shape3D->m_vVtxSrc[m_vIndex[0]];
  119. vtx1 = p_Shape3D->m_vVtxSrc[m_vIndex[1]];
  120. vtx2 = p_Shape3D->m_vVtxSrc[m_vIndex[2]];
  121. vtxA.x = vtx2.x - vtx0.x;
  122. vtxA.y = vtx2.y - vtx0.y;
  123. vtxA.z = vtx2.z - vtx0.z;
  124. vtxB.x = vtx1.x - vtx0.x;
  125. vtxB.y = vtx1.y - vtx0.y;
  126. vtxB.z = vtx1.z - vtx0.z;
  127. EMath::crossProduct(vtxA, vtxB, m_nmlSrc);
  128. EMath::normalizeVector(m_nmlSrc);
  129. }
  130. int Polygon3D::includes(int index) {
  131. if (p_Shape3D == NULL) return -1;
  132. vector<int>::iterator iter = m_vIndex.begin();
  133. vector<int>::iterator end = m_vIndex.end();
  134. for (int a=0 ; iter != end; ++iter, ++a) {
  135. if ((*iter) == index) return a;
  136. }
  137. return -1;
  138. }
  139. void Polygon3D::moveUp(int polyindex) {
  140. if (polyindex <= 0 || polyindex > (signed)m_vIndex.size()) return;
  141. int tmp = m_vIndex[polyindex];
  142. m_vIndex[polyindex] = m_vIndex[polyindex-1];
  143. m_vIndex[polyindex-1] = tmp;
  144. }
  145. void Polygon3D::moveDown(int polyindex) {
  146. if (polyindex < 0 || polyindex > (signed)m_vIndex.size()-1) return;
  147. int tmp = m_vIndex[polyindex];
  148. m_vIndex[polyindex] = m_vIndex[polyindex+1];
  149. m_vIndex[polyindex+1] = tmp;
  150. }