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