PolyPolygon.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. * PolyPolygon.cpp
  3. * TAU
  4. *
  5. * Created by Ivan Safrin on 3/14/08.
  6. * Copyright 2008 __MyCompanyName__. All rights reserved.
  7. *
  8. */
  9. #include "PolyPolygon.h"
  10. namespace Polycode {
  11. Polygon::Polygon() : useVertexNormals(false), vertexCount(0), useFaceUV(false) {
  12. normal = new Vector3();
  13. hasSecUVs = false;
  14. useVertexNormals = true;
  15. for(int i=0; i < 3; i++) {
  16. Vector2 *newCoord = new Vector2(0,0);
  17. texCoords.push_back(newCoord);
  18. }
  19. for(int i=0; i < 3; i++) {
  20. Vector2 *newCoord2 = new Vector2(0,0);
  21. texCoords2.push_back(newCoord2);
  22. }
  23. }
  24. Polygon::~Polygon() {
  25. for(int i=0; i < texCoords.size(); i++) {
  26. delete texCoords[i];
  27. }
  28. texCoords.clear();
  29. for(int i=0; i < texCoords2.size(); i++) {
  30. delete texCoords2[i];
  31. }
  32. texCoords2.clear();
  33. for(int i=0; i < vertices.size(); i++) {
  34. delete vertices[i];
  35. }
  36. vertices.clear();
  37. }
  38. void Polygon::flipUVY() {
  39. for(int i=0; i < vertices.size(); i++) {
  40. Vector2 *coord = vertices[i]->getTexCoord();
  41. vertices[i]->setTexCoord(coord->x, 1-coord->y);
  42. }
  43. }
  44. void Polygon::setUseFaceUV(bool val) {
  45. useFaceUV = val;
  46. }
  47. bool Polygon::usesFaceUV() {
  48. return useFaceUV;
  49. }
  50. Vector2 *Polygon::getTexCoord(int index) {
  51. return texCoords[index];
  52. }
  53. Vector2 *Polygon::getTexCoord2(int index) {
  54. return texCoords2[index];
  55. }
  56. unsigned int Polygon::getVertexCount() {
  57. return vertices.size();
  58. }
  59. Vertex *Polygon::getVertex(unsigned int index) {
  60. return vertices[index];
  61. }
  62. Vector3 Polygon::getFaceNormal() {
  63. /*
  64. Vector3 fNormal;
  65. fNormal.x = (vertices[2]->z-vertices[1]->z)*(vertices[2]->y-vertices[1]->y)-(vertices[0]->y-vertices[1]->y)*(vertices[2]->z-vertices[1]->z);
  66. fNormal.y = (vertices[0]->x-vertices[1]->x)*(vertices[2]->z-vertices[1]->z)-(vertices[0]->z-vertices[1]->z)*(vertices[2]->x-vertices[1]->x);
  67. fNormal.z = (vertices[0]->y-vertices[1]->y)*(vertices[2]->x-vertices[1]->x)-(vertices[0]->x-vertices[1]->x)*(vertices[2]->y-vertices[1]->y);
  68. fNormal.Normalize();
  69. return fNormal;
  70. */
  71. return *normal;
  72. }
  73. Rectangle Polygon::getBounds2D() {
  74. Rectangle retBox;
  75. retBox.x = 1000000000;
  76. retBox.y = 1000000000;
  77. for(int i=0; i < vertices.size(); i++) {
  78. retBox.x = min(retBox.x,vertices[i]->x);
  79. retBox.y = min(retBox.y,vertices[i]->y);
  80. }
  81. for(int i=0; i < vertices.size(); i++) {
  82. retBox.w = max(retBox.w, vertices[i]->x - retBox.x);
  83. retBox.h = max(retBox.h, vertices[i]->y - retBox.y);
  84. }
  85. return retBox;
  86. }
  87. void Polygon::removeVertex(int index) {
  88. vertices.erase(vertices.begin() + index);
  89. }
  90. void Polygon::setNormal(Vector3 normal) {
  91. *this->normal = normal;
  92. }
  93. void Polygon::calculateNormal() {
  94. if(vertices.size() < 3)
  95. return;
  96. // normal->x = (vertices[2]->z-vertices[1]->z)*(vertices[2]->y-vertices[1]->y)-(vertices[0]->y-vertices[1]->y)*(vertices[2]->z-vertices[1]->z);
  97. // normal->y = (vertices[0]->x-vertices[1]->x)*(vertices[2]->z-vertices[1]->z)-(vertices[0]->z-vertices[1]->z)*(vertices[2]->x-vertices[1]->x);
  98. // normal->z = (vertices[0]->y-vertices[1]->y)*(vertices[2]->x-vertices[1]->x)-(vertices[0]->x-vertices[1]->x)*(vertices[2]->y-vertices[1]->y);
  99. *normal = (*vertices[0] - *vertices[1]).crossProduct((*vertices[1] - *vertices[2]));
  100. normal->Normalize();
  101. for(int i=0; i < vertices.size(); i++) {
  102. vertices[i]->normal->x = normal->x;
  103. vertices[i]->normal->y = normal->y;
  104. vertices[i]->normal->z = normal->z;
  105. }
  106. }
  107. Vertex *Polygon::addVertex(Number x, Number y, Number z) {
  108. Vertex *vertex = new Vertex(x,y,z);
  109. vertices.push_back(vertex);
  110. return vertex;
  111. }
  112. void Polygon::addTexCoord2(Number u, Number v) {
  113. // Vector2 *newCoord = new Vector2(u,v);
  114. // texCoords2.push_back(newCoord);
  115. hasSecUVs =true;
  116. }
  117. void Polygon::addTexCoord(Number u, Number v) {
  118. // Vector2 *newCoord = new Vector2(u,v);
  119. // texCoords.push_back(newCoord);
  120. }
  121. void Polygon::addVertex(Vertex *vertex) {
  122. vertices.push_back(vertex);
  123. }
  124. Vertex *Polygon::addVertex(Number x, Number y, Number z, Number u, Number v) {
  125. Vertex *vertex = new Vertex(x,y,z,u,v);
  126. vertices.push_back(vertex);
  127. return vertex;
  128. }
  129. }