2
0

PolyPolygon.cpp 3.5 KB

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