PolyMesh.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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 < polygons.size(); i++) {
  215. for(int j=0; j < polygons[i]->getVertexCount(); j++) {
  216. retVec.x = max(retVec.x,fabsf(polygons[i]->getVertex(j)->x));
  217. retVec.y = max(retVec.y,fabsf(polygons[i]->getVertex(j)->y));
  218. retVec.z = max(retVec.z,fabsf(polygons[i]->getVertex(j)->z));
  219. }
  220. }
  221. return retVec*2;
  222. }
  223. void Mesh::createSphere(float radius, float numRings, float numSegments) {
  224. float fDeltaRingAngle = (PI / numRings);
  225. float fDeltaSegAngle = (2 * PI / numSegments);
  226. for(int i=0; i < numRings; i++) {
  227. float r0 = radius * sinf (i * fDeltaRingAngle);
  228. float y0 = radius * cosf (i * fDeltaRingAngle);
  229. for(int j=0; j < numSegments; j++) {
  230. float x0 = r0 * sinf(j * fDeltaSegAngle);
  231. float z0 = r0 * cosf(j * fDeltaSegAngle);
  232. float tx0 = (float) j / (float) numSegments;
  233. float tx1 = (float) i / (float) numRings;
  234. Vector3 nor = Vector3(x0, y0, z0);
  235. nor.Normalize();
  236. }
  237. }
  238. useVertexNormals(true);
  239. arrayDirtyMap[RenderDataArray::VERTEX_DATA_ARRAY] = true;
  240. arrayDirtyMap[RenderDataArray::COLOR_DATA_ARRAY] = true;
  241. arrayDirtyMap[RenderDataArray::TEXCOORD_DATA_ARRAY] = true;
  242. arrayDirtyMap[RenderDataArray::NORMAL_DATA_ARRAY] = true;
  243. }
  244. void Mesh::createBox(float w, float d, float h) {
  245. Polygon *polygon = new Polygon();
  246. polygon->addVertex(w,0,h, 1, 1);
  247. polygon->addVertex(0,0,h, 1, 0);
  248. polygon->addVertex(0,0,0,0,0);
  249. polygon->addVertex(w,0,0,0,1);
  250. addPolygon(polygon);
  251. polygon = new Polygon();
  252. polygon->addVertex(w,d,h, 1, 1);
  253. polygon->addVertex(w,d,0, 1, 0);
  254. polygon->addVertex(0,d,0,0,0);
  255. polygon->addVertex(0,d,h,0,1);
  256. addPolygon(polygon);
  257. polygon = new Polygon();
  258. polygon->addVertex(0,d,0,0,1);
  259. polygon->addVertex(w,d,0, 1, 1);
  260. polygon->addVertex(w,0,0, 1, 0);
  261. polygon->addVertex(0,0,0,0,0);
  262. addPolygon(polygon);
  263. polygon = new Polygon();
  264. polygon->addVertex(0,0,h,0,0);
  265. polygon->addVertex(w,0,h, 1, 0);
  266. polygon->addVertex(w,d,h, 1, 1);
  267. polygon->addVertex(0,d,h,0,1);
  268. addPolygon(polygon);
  269. polygon = new Polygon();
  270. polygon->addVertex(0,0,h,0,1);
  271. polygon->addVertex(0,d,h, 1, 1);
  272. polygon->addVertex(0,d,0, 1, 0);
  273. polygon->addVertex(0,0,0,0,0);
  274. addPolygon(polygon);
  275. polygon = new Polygon();
  276. polygon->addVertex(w,0,h,0,1);
  277. polygon->addVertex(w,0,0, 1, 1);
  278. polygon->addVertex(w,d,0, 1, 0);
  279. polygon->addVertex(w,d,h,0,0);
  280. addPolygon(polygon);
  281. for(int i=0; i < polygons.size(); i++) {
  282. for(int j=0; j < polygons[i]->getVertexCount(); j++) {
  283. polygons[i]->getVertex(j)->x = polygons[i]->getVertex(j)->x - (w/2.0f);
  284. polygons[i]->getVertex(j)->y = polygons[i]->getVertex(j)->y - (d/2.0f);
  285. polygons[i]->getVertex(j)->z = polygons[i]->getVertex(j)->z - (h/2.0f);
  286. }
  287. }
  288. calculateNormals();
  289. arrayDirtyMap[RenderDataArray::VERTEX_DATA_ARRAY] = true;
  290. arrayDirtyMap[RenderDataArray::COLOR_DATA_ARRAY] = true;
  291. arrayDirtyMap[RenderDataArray::TEXCOORD_DATA_ARRAY] = true;
  292. arrayDirtyMap[RenderDataArray::NORMAL_DATA_ARRAY] = true;
  293. }
  294. void Mesh::useVertexNormals(bool val) {
  295. for(int i =0; i < polygons.size(); i++) {
  296. polygons[i]->useVertexNormals = val;
  297. }
  298. arrayDirtyMap[RenderDataArray::NORMAL_DATA_ARRAY] = true;
  299. }
  300. void Mesh::calculateNormals() {
  301. for(int i =0; i < polygons.size(); i++) {
  302. polygons[i]->calculateNormal();
  303. }
  304. arrayDirtyMap[RenderDataArray::NORMAL_DATA_ARRAY] = true;
  305. }
  306. int Mesh::getMeshType() {
  307. return meshType;
  308. }
  309. void Mesh::setMeshType(int newType) {
  310. meshType = newType;
  311. }
  312. void Mesh::addPolygon(Polygon *newPolygon) {
  313. polygons.push_back(newPolygon);
  314. arrayDirtyMap[RenderDataArray::VERTEX_DATA_ARRAY] = true;
  315. arrayDirtyMap[RenderDataArray::COLOR_DATA_ARRAY] = true;
  316. arrayDirtyMap[RenderDataArray::TEXCOORD_DATA_ARRAY] = true;
  317. arrayDirtyMap[RenderDataArray::NORMAL_DATA_ARRAY] = true;
  318. }
  319. unsigned int Mesh::getPolygonCount() {
  320. return polygons.size();
  321. }
  322. Polygon *Mesh::getPolygon(unsigned int index) {
  323. return polygons[index];
  324. }
  325. }