PolyPolygon.cpp 3.7 KB

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