PolyMesh.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. /*
  2. * PolyMesh.cpp
  3. * TAU
  4. *
  5. * Created by Ivan Safrin on 3/18/08.
  6. * Copyright 2008 __MyCompanyName__. All rights reserved.
  7. *
  8. */
  9. #include "PolyMesh.h"
  10. namespace Polycode {
  11. Mesh::Mesh(string fileName) {
  12. for(int i=0; i < 16; i++) {
  13. arrayDirtyMap[i] = false;
  14. renderDataArrays[i] = NULL;
  15. }
  16. meshType = TRI_MESH;
  17. numUVs = 1;
  18. meshHasVertexBuffer = false;
  19. loadMesh(fileName);
  20. vertexBuffer = NULL;
  21. }
  22. Mesh::Mesh(int meshType) {
  23. for(int i=0; i < 16; i++) {
  24. arrayDirtyMap[i] = false;
  25. renderDataArrays[i] = NULL;
  26. }
  27. this->meshType = meshType;
  28. meshHasVertexBuffer = false;
  29. numUVs = 1;
  30. vertexBuffer = NULL;
  31. }
  32. Mesh::~Mesh() {
  33. Logger::log("deleting mesh...\n");
  34. for(int i=0; i < vertices.size(); i++) {
  35. delete vertices[i];
  36. }
  37. vertices.clear();
  38. for(int i=0; i < polygons.size(); i++) {
  39. // delete polygons[i];
  40. }
  41. polygons.clear();
  42. }
  43. VertexBuffer *Mesh::getVertexBuffer() {
  44. return vertexBuffer;
  45. }
  46. void Mesh::setVertexBuffer(VertexBuffer *buffer) {
  47. vertexBuffer = buffer;
  48. meshHasVertexBuffer = true;
  49. }
  50. float Mesh::getRadius() {
  51. float hRad = 0;
  52. float len;
  53. for(int i=0; i < vertices.size(); i++) {
  54. len = vertices[i]->length();
  55. if(len > hRad)
  56. hRad = len;
  57. }
  58. return hRad;
  59. }
  60. void Mesh::saveToFile(OSFILE *outFile) {
  61. unsigned int numVertices = vertices.size();
  62. OSBasics::write(&numUVs, sizeof(unsigned int), 1, outFile);
  63. OSBasics::write(&numVertices, sizeof(unsigned int), 1, outFile);
  64. Vector3_struct pos;
  65. Vector3_struct nor;
  66. for(int i=0; i < vertices.size(); i++) {
  67. pos.x = vertices[i]->x;
  68. pos.y = vertices[i]->y;
  69. pos.z = vertices[i]->z;
  70. nor.x = vertices[i]->normal->x;
  71. nor.y = vertices[i]->normal->y;
  72. nor.z = vertices[i]->normal->z;
  73. OSBasics::write(&pos, sizeof(Vector3_struct), 1, outFile);
  74. OSBasics::write(&nor, sizeof(Vector3_struct), 1, outFile);
  75. unsigned int hasWeights = 0;
  76. OSBasics::write(&hasWeights, sizeof(unsigned int), 1, outFile);
  77. }
  78. unsigned int numFaces = polygons.size();
  79. OSBasics::write(&numFaces, sizeof(unsigned int), 1, outFile);
  80. Face_struct poly;
  81. for(int i=0; i < polygons.size(); i++) {
  82. poly.nx = polygons[i]->getFaceNormal().x;
  83. poly.ny = polygons[i]->getFaceNormal().y;
  84. poly.nz = polygons[i]->getFaceNormal().z;
  85. poly.v1 = getVertexIndex(polygons[i]->getVertex(0));
  86. poly.v2 = getVertexIndex(polygons[i]->getVertex(1));
  87. poly.v3 = getVertexIndex(polygons[i]->getVertex(2));
  88. poly.uvs[0].x = polygons[i]->getTexCoord(0)->x;
  89. poly.uvs[0].y = polygons[i]->getTexCoord(0)->y;
  90. poly.uvs[1].x = polygons[i]->getTexCoord(1)->x;
  91. poly.uvs[1].y = polygons[i]->getTexCoord(1)->y;
  92. poly.uvs[2].x = polygons[i]->getTexCoord(2)->x;
  93. poly.uvs[2].y = polygons[i]->getTexCoord(2)->y;
  94. OSBasics::write(&poly, sizeof(Face_struct), 1, outFile);
  95. if(numUVs == 2) {
  96. Vector2_struct uv2s[3];
  97. uv2s[0].x = polygons[i]->getTexCoord2(0)->x;
  98. uv2s[0].y = polygons[i]->getTexCoord2(0)->y;
  99. uv2s[1].x = polygons[i]->getTexCoord2(1)->x;
  100. uv2s[1].y = polygons[i]->getTexCoord2(1)->y;
  101. uv2s[2].x = polygons[i]->getTexCoord2(2)->x;
  102. uv2s[2].y = polygons[i]->getTexCoord2(2)->y;
  103. OSBasics::write(uv2s, sizeof(Vector2_struct), 3, outFile);
  104. }
  105. }
  106. }
  107. int Mesh::getVertexIndex(Vertex *vertex) {
  108. for(int i=0; i < vertices.size(); i++) {
  109. if(vertex == vertices[i])
  110. return i;
  111. }
  112. return 0;
  113. }
  114. void Mesh::loadFromFile(OSFILE *inFile) {
  115. int i;
  116. Vertex *newVertex;
  117. Polygon *polygon;
  118. unsigned int numVertices, numFaces, hasWeights, numWeights, numUVs;
  119. unsigned int boneID;
  120. float boneWeight;
  121. OSBasics::read(&numUVs, sizeof(unsigned int), 1, inFile);
  122. OSBasics::read(&numVertices, sizeof(unsigned int), 1, inFile);
  123. Vector3_struct pos;
  124. Vector3_struct nor;
  125. Face_struct face;
  126. for(i=0; i<numVertices; i++) {
  127. OSBasics::read(&pos, sizeof(Vector3_struct), 1, inFile);
  128. OSBasics::read(&nor, sizeof(Vector3_struct), 1, inFile);
  129. newVertex = new Vertex(pos.x, pos.y, pos.z, nor.x, nor.y,nor.z);
  130. OSBasics::read(&hasWeights, sizeof(unsigned int), 1, inFile);
  131. if(hasWeights == 1) {
  132. OSBasics::read(&numWeights, sizeof(unsigned int), 1, inFile);
  133. for(int j=0; j < numWeights; j++) {
  134. OSBasics::read(&boneID, sizeof(unsigned int), 1, inFile);
  135. OSBasics::read(&boneWeight, sizeof(float), 1, inFile);
  136. newVertex->addBoneAssignment(boneID, boneWeight);
  137. }
  138. newVertex->normalizeWeights();
  139. }
  140. vertices.push_back(newVertex);
  141. }
  142. OSBasics::read(&numFaces, sizeof(unsigned int), 1, inFile);
  143. for(i=0; i<numFaces; i++) {
  144. OSBasics::read(&face, sizeof(Face_struct), 1, inFile);
  145. polygon = new Polygon();
  146. polygon->addVertex(vertices[face.v1]);
  147. polygon->addVertex(vertices[face.v2]);
  148. polygon->addVertex(vertices[face.v3]);
  149. polygon->setNormal(Vector3(face.nx,face.ny,face.nz));
  150. vertices[face.v1]->setTexCoord(face.uvs[0].x,face.uvs[0].y);
  151. vertices[face.v2]->setTexCoord(face.uvs[1].x,face.uvs[1].y);
  152. vertices[face.v3]->setTexCoord(face.uvs[2].x,face.uvs[2].y);
  153. polygon->addTexCoord(face.uvs[0].x,face.uvs[0].y);
  154. polygon->addTexCoord(face.uvs[1].x,face.uvs[1].y);
  155. polygon->addTexCoord(face.uvs[2].x,face.uvs[2].y);
  156. polygon->setUseFaceUV(true);
  157. if(numUVs == 2) {
  158. Vector2_struct uv2s[3];
  159. OSBasics::read(uv2s, sizeof(Vector2_struct), 3, inFile);
  160. polygon->addTexCoord2(uv2s[0].x,uv2s[0].y);
  161. polygon->addTexCoord2(uv2s[1].x,uv2s[1].y);
  162. polygon->addTexCoord2(uv2s[2].x,uv2s[2].y);
  163. }
  164. polygons.push_back(polygon);
  165. }
  166. this->numUVs = numUVs;
  167. }
  168. void Mesh::loadMesh(string fileName) {
  169. OSFILE *inFile = OSBasics::open(fileName.c_str(), "rb");
  170. if(!inFile) {
  171. Logger::log("Error opening mesh file %s", fileName.c_str());
  172. }
  173. loadFromFile(inFile);
  174. OSBasics::close(inFile);
  175. arrayDirtyMap[RenderDataArray::VERTEX_DATA_ARRAY] = true;
  176. arrayDirtyMap[RenderDataArray::COLOR_DATA_ARRAY] = true;
  177. arrayDirtyMap[RenderDataArray::TEXCOORD_DATA_ARRAY] = true;
  178. arrayDirtyMap[RenderDataArray::NORMAL_DATA_ARRAY] = true;
  179. }
  180. void Mesh::createPlane(float w, float h) {
  181. Polygon *imagePolygon = new Polygon();
  182. imagePolygon->addVertex(0,0,0,0,0);
  183. imagePolygon->addVertex(w,0,0, 1, 0);
  184. imagePolygon->addVertex(w,h,0, 1, 1);
  185. imagePolygon->addVertex(0,h,0,0,1);
  186. addPolygon(imagePolygon);
  187. for(int i=0; i < polygons.size(); i++) {
  188. for(int j=0; j < polygons[i]->getVertexCount(); j++) {
  189. polygons[i]->getVertex(j)->x = polygons[i]->getVertex(j)->x - (w/2.0f);
  190. polygons[i]->getVertex(j)->y = polygons[i]->getVertex(j)->y - (h/2.0f);
  191. }
  192. }
  193. calculateNormals();
  194. arrayDirtyMap[RenderDataArray::VERTEX_DATA_ARRAY] = true;
  195. arrayDirtyMap[RenderDataArray::COLOR_DATA_ARRAY] = true;
  196. arrayDirtyMap[RenderDataArray::TEXCOORD_DATA_ARRAY] = true;
  197. arrayDirtyMap[RenderDataArray::NORMAL_DATA_ARRAY] = true;
  198. }
  199. void Mesh::addVertex(Vertex* vertex) {
  200. vertices.push_back(vertex);
  201. arrayDirtyMap[RenderDataArray::VERTEX_DATA_ARRAY] = true;
  202. arrayDirtyMap[RenderDataArray::COLOR_DATA_ARRAY] = true;
  203. arrayDirtyMap[RenderDataArray::TEXCOORD_DATA_ARRAY] = true;
  204. arrayDirtyMap[RenderDataArray::NORMAL_DATA_ARRAY] = true;
  205. }
  206. unsigned int Mesh::getNumVertices() {
  207. return vertices.size();
  208. }
  209. Vertex *Mesh::getVertex(unsigned int index) {
  210. return vertices[index];
  211. }
  212. Vector3 Mesh::calculateBBox() {
  213. Vector3 retVec;
  214. for(int i=0; i < vertices.size(); i++) {
  215. retVec.x = max(retVec.x,fabsf(vertices[i]->x));
  216. retVec.y = max(retVec.y,fabsf(vertices[i]->y));
  217. retVec.z = max(retVec.z,fabsf(vertices[i]->z));
  218. }
  219. return retVec*2;
  220. }
  221. void Mesh::createSphere(float radius, float numRings, float numSegments) {
  222. float fDeltaRingAngle = (PI / numRings);
  223. float fDeltaSegAngle = (2 * PI / numSegments);
  224. for(int i=0; i < numRings; i++) {
  225. float r0 = radius * sinf (i * fDeltaRingAngle);
  226. float y0 = radius * cosf (i * fDeltaRingAngle);
  227. for(int j=0; j < numSegments; j++) {
  228. float x0 = r0 * sinf(j * fDeltaSegAngle);
  229. float z0 = r0 * cosf(j * fDeltaSegAngle);
  230. float tx0 = (float) j / (float) numSegments;
  231. float tx1 = (float) i / (float) numRings;
  232. Vector3 nor = Vector3(x0, y0, z0);
  233. nor.Normalize();
  234. }
  235. }
  236. useVertexNormals(true);
  237. arrayDirtyMap[RenderDataArray::VERTEX_DATA_ARRAY] = true;
  238. arrayDirtyMap[RenderDataArray::COLOR_DATA_ARRAY] = true;
  239. arrayDirtyMap[RenderDataArray::TEXCOORD_DATA_ARRAY] = true;
  240. arrayDirtyMap[RenderDataArray::NORMAL_DATA_ARRAY] = true;
  241. }
  242. void Mesh::createBox(float w, float d, float h) {
  243. Polygon *polygon = new Polygon();
  244. polygon->addVertex(w,0,h, 1, 1);
  245. polygon->addVertex(0,0,h, 1, 0);
  246. polygon->addVertex(0,0,0,0,0);
  247. polygon->addVertex(w,0,0,0,1);
  248. addPolygon(polygon);
  249. polygon = new Polygon();
  250. polygon->addVertex(w,d,h, 1, 1);
  251. polygon->addVertex(w,d,0, 1, 0);
  252. polygon->addVertex(0,d,0,0,0);
  253. polygon->addVertex(0,d,h,0,1);
  254. addPolygon(polygon);
  255. polygon = new Polygon();
  256. polygon->addVertex(0,d,0,0,1);
  257. polygon->addVertex(w,d,0, 1, 1);
  258. polygon->addVertex(w,0,0, 1, 0);
  259. polygon->addVertex(0,0,0,0,0);
  260. addPolygon(polygon);
  261. polygon = new Polygon();
  262. polygon->addVertex(0,0,h,0,0);
  263. polygon->addVertex(w,0,h, 1, 0);
  264. polygon->addVertex(w,d,h, 1, 1);
  265. polygon->addVertex(0,d,h,0,1);
  266. addPolygon(polygon);
  267. polygon = new Polygon();
  268. polygon->addVertex(0,0,h,0,1);
  269. polygon->addVertex(0,d,h, 1, 1);
  270. polygon->addVertex(0,d,0, 1, 0);
  271. polygon->addVertex(0,0,0,0,0);
  272. addPolygon(polygon);
  273. polygon = new Polygon();
  274. polygon->addVertex(w,0,h,0,1);
  275. polygon->addVertex(w,0,0, 1, 1);
  276. polygon->addVertex(w,d,0, 1, 0);
  277. polygon->addVertex(w,d,h,0,0);
  278. addPolygon(polygon);
  279. for(int i=0; i < polygons.size(); i++) {
  280. for(int j=0; j < polygons[i]->getVertexCount(); j++) {
  281. polygons[i]->getVertex(j)->x = polygons[i]->getVertex(j)->x - (w/2.0f);
  282. polygons[i]->getVertex(j)->y = polygons[i]->getVertex(j)->y - (d/2.0f);
  283. polygons[i]->getVertex(j)->z = polygons[i]->getVertex(j)->z - (h/2.0f);
  284. }
  285. }
  286. calculateNormals();
  287. arrayDirtyMap[RenderDataArray::VERTEX_DATA_ARRAY] = true;
  288. arrayDirtyMap[RenderDataArray::COLOR_DATA_ARRAY] = true;
  289. arrayDirtyMap[RenderDataArray::TEXCOORD_DATA_ARRAY] = true;
  290. arrayDirtyMap[RenderDataArray::NORMAL_DATA_ARRAY] = true;
  291. }
  292. void Mesh::useVertexNormals(bool val) {
  293. for(int i =0; i < polygons.size(); i++) {
  294. polygons[i]->useVertexNormals = val;
  295. }
  296. arrayDirtyMap[RenderDataArray::NORMAL_DATA_ARRAY] = true;
  297. }
  298. void Mesh::calculateNormals() {
  299. for(int i =0; i < polygons.size(); i++) {
  300. polygons[i]->calculateNormal();
  301. }
  302. arrayDirtyMap[RenderDataArray::NORMAL_DATA_ARRAY] = true;
  303. }
  304. int Mesh::getMeshType() {
  305. return meshType;
  306. }
  307. void Mesh::setMeshType(int newType) {
  308. meshType = newType;
  309. }
  310. void Mesh::addPolygon(Polygon *newPolygon) {
  311. polygons.push_back(newPolygon);
  312. arrayDirtyMap[RenderDataArray::VERTEX_DATA_ARRAY] = true;
  313. arrayDirtyMap[RenderDataArray::COLOR_DATA_ARRAY] = true;
  314. arrayDirtyMap[RenderDataArray::TEXCOORD_DATA_ARRAY] = true;
  315. arrayDirtyMap[RenderDataArray::NORMAL_DATA_ARRAY] = true;
  316. }
  317. unsigned int Mesh::getPolygonCount() {
  318. return polygons.size();
  319. }
  320. Polygon *Mesh::getPolygon(unsigned int index) {
  321. return polygons[index];
  322. }
  323. }