1
0

SculptMesh.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
  1. /*
  2. * Copyright (c) Contributors
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSimulator Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. // to build without references to System.Drawing, comment this out
  28. #define SYSTEM_DRAWING
  29. using System;
  30. using System.Collections.Generic;
  31. using System.Text;
  32. using System.IO;
  33. #if SYSTEM_DRAWING
  34. using System.Drawing;
  35. using System.Drawing.Imaging;
  36. #endif
  37. namespace PrimMesher
  38. {
  39. public class SculptMesh
  40. {
  41. public List<Coord> coords;
  42. public List<Face> faces;
  43. public List<ViewerFace> viewerFaces;
  44. public List<Coord> normals;
  45. public List<UVCoord> uvs;
  46. public enum SculptType { sphere = 1, torus = 2, plane = 3, cylinder = 4 };
  47. #if SYSTEM_DRAWING
  48. public SculptMesh SculptMeshFromFile(string fileName, SculptType sculptType, int lod, bool viewerMode)
  49. {
  50. Bitmap bitmap = (Bitmap)Bitmap.FromFile(fileName);
  51. SculptMesh sculptMesh = new SculptMesh(bitmap, sculptType, lod, viewerMode);
  52. bitmap.Dispose();
  53. return sculptMesh;
  54. }
  55. public SculptMesh(string fileName, int sculptType, int lod, int viewerMode, int mirror, int invert)
  56. {
  57. Bitmap bitmap = (Bitmap)Bitmap.FromFile(fileName);
  58. _SculptMesh(bitmap, (SculptType)sculptType, lod, viewerMode != 0, mirror != 0, invert != 0);
  59. bitmap.Dispose();
  60. }
  61. #endif
  62. /// <summary>
  63. /// ** Experimental ** May disappear from future versions ** not recommeneded for use in applications
  64. /// Construct a sculpt mesh from a 2D array of floats
  65. /// </summary>
  66. /// <param name="zMap"></param>
  67. /// <param name="xBegin"></param>
  68. /// <param name="xEnd"></param>
  69. /// <param name="yBegin"></param>
  70. /// <param name="yEnd"></param>
  71. /// <param name="viewerMode"></param>
  72. public SculptMesh(float[,] zMap, float xBegin, float xEnd, float yBegin, float yEnd, bool viewerMode)
  73. {
  74. float xStep, yStep;
  75. float uStep, vStep;
  76. int numYElements = zMap.GetLength(0);
  77. int numXElements = zMap.GetLength(1);
  78. try
  79. {
  80. xStep = (xEnd - xBegin) / (float)(numXElements - 1);
  81. yStep = (yEnd - yBegin) / (float)(numYElements - 1);
  82. uStep = 1.0f / (numXElements - 1);
  83. vStep = 1.0f / (numYElements - 1);
  84. }
  85. catch (DivideByZeroException)
  86. {
  87. return;
  88. }
  89. coords = new List<Coord>();
  90. faces = new List<Face>();
  91. normals = new List<Coord>();
  92. uvs = new List<UVCoord>();
  93. viewerFaces = new List<ViewerFace>();
  94. int p1, p2, p3, p4;
  95. int x, y;
  96. int xStart = 0, yStart = 0;
  97. for (y = yStart; y < numYElements; y++)
  98. {
  99. int rowOffset = y * numXElements;
  100. for (x = xStart; x < numXElements; x++)
  101. {
  102. /*
  103. * p1-----p2
  104. * | \ f2 |
  105. * | \ |
  106. * | f1 \|
  107. * p3-----p4
  108. */
  109. p4 = rowOffset + x;
  110. p3 = p4 - 1;
  111. p2 = p4 - numXElements;
  112. p1 = p3 - numXElements;
  113. Coord c = new Coord(xBegin + x * xStep, yBegin + y * yStep, zMap[y, x]);
  114. this.coords.Add(c);
  115. if (viewerMode)
  116. {
  117. this.normals.Add(new Coord());
  118. this.uvs.Add(new UVCoord(uStep * x, 1.0f - vStep * y));
  119. }
  120. if (y > 0 && x > 0)
  121. {
  122. Face f1, f2;
  123. if (viewerMode)
  124. {
  125. f1 = new Face(p1, p4, p3, p1, p4, p3);
  126. f1.uv1 = p1;
  127. f1.uv2 = p4;
  128. f1.uv3 = p3;
  129. f2 = new Face(p1, p2, p4, p1, p2, p4);
  130. f2.uv1 = p1;
  131. f2.uv2 = p2;
  132. f2.uv3 = p4;
  133. }
  134. else
  135. {
  136. f1 = new Face(p1, p4, p3);
  137. f2 = new Face(p1, p2, p4);
  138. }
  139. this.faces.Add(f1);
  140. this.faces.Add(f2);
  141. }
  142. }
  143. }
  144. if (viewerMode)
  145. calcVertexNormals(SculptType.plane, numXElements, numYElements);
  146. }
  147. #if SYSTEM_DRAWING
  148. public SculptMesh(Bitmap sculptBitmap, SculptType sculptType, int lod, bool viewerMode)
  149. {
  150. _SculptMesh(sculptBitmap, sculptType, lod, viewerMode, false, false);
  151. }
  152. public SculptMesh(Bitmap sculptBitmap, SculptType sculptType, int lod, bool viewerMode, bool mirror, bool invert)
  153. {
  154. _SculptMesh(sculptBitmap, sculptType, lod, viewerMode, mirror, invert);
  155. }
  156. #endif
  157. public SculptMesh(List<List<Coord>> rows, SculptType sculptType, bool viewerMode, bool mirror, bool invert)
  158. {
  159. _SculptMesh(rows, sculptType, viewerMode, mirror, invert);
  160. }
  161. #if SYSTEM_DRAWING
  162. /// <summary>
  163. /// converts a bitmap to a list of lists of coords, while scaling the image.
  164. /// the scaling is done in floating point so as to allow for reduced vertex position
  165. /// quantization as the position will be averaged between pixel values. this routine will
  166. /// likely fail if the bitmap width and height are not powers of 2.
  167. /// </summary>
  168. /// <param name="bitmap"></param>
  169. /// <param name="scale"></param>
  170. /// <param name="mirror"></param>
  171. /// <returns></returns>
  172. private List<List<Coord>> bitmap2Coords(Bitmap bitmap, int scale, bool mirror)
  173. {
  174. int numRows = bitmap.Height / scale;
  175. int numCols = bitmap.Width / scale;
  176. List<List<Coord>> rows = new List<List<Coord>>(numRows);
  177. float pixScale = 1.0f / (scale * scale);
  178. pixScale /= 255;
  179. int imageX, imageY = 0;
  180. int rowNdx, colNdx;
  181. for (rowNdx = 0; rowNdx < numRows; rowNdx++)
  182. {
  183. List<Coord> row = new List<Coord>(numCols);
  184. for (colNdx = 0; colNdx < numCols; colNdx++)
  185. {
  186. imageX = colNdx * scale;
  187. int imageYStart = rowNdx * scale;
  188. int imageYEnd = imageYStart + scale;
  189. int imageXEnd = imageX + scale;
  190. float rSum = 0.0f;
  191. float gSum = 0.0f;
  192. float bSum = 0.0f;
  193. for (; imageX < imageXEnd; imageX++)
  194. {
  195. for (imageY = imageYStart; imageY < imageYEnd; imageY++)
  196. {
  197. Color c = bitmap.GetPixel(imageX, imageY);
  198. if (c.A != 255)
  199. {
  200. bitmap.SetPixel(imageX, imageY, Color.FromArgb(255, c.R, c.G, c.B));
  201. c = bitmap.GetPixel(imageX, imageY);
  202. }
  203. rSum += c.R;
  204. gSum += c.G;
  205. bSum += c.B;
  206. }
  207. }
  208. if (mirror)
  209. row.Add(new Coord(-(rSum * pixScale - 0.5f), gSum * pixScale - 0.5f, bSum * pixScale - 0.5f));
  210. else
  211. row.Add(new Coord(rSum * pixScale - 0.5f, gSum * pixScale - 0.5f, bSum * pixScale - 0.5f));
  212. }
  213. rows.Add(row);
  214. }
  215. return rows;
  216. }
  217. private List<List<Coord>> bitmap2CoordsSampled(Bitmap bitmap, int scale, bool mirror)
  218. {
  219. int numRows = bitmap.Height / scale;
  220. int numCols = bitmap.Width / scale;
  221. List<List<Coord>> rows = new List<List<Coord>>(numRows);
  222. float pixScale = 1.0f / 256.0f;
  223. int imageX, imageY = 0;
  224. int rowNdx, colNdx;
  225. for (rowNdx = 0; rowNdx <= numRows; rowNdx++)
  226. {
  227. List<Coord> row = new List<Coord>(numCols);
  228. imageY = rowNdx * scale;
  229. if (rowNdx == numRows) imageY--;
  230. for (colNdx = 0; colNdx <= numCols; colNdx++)
  231. {
  232. imageX = colNdx * scale;
  233. if (colNdx == numCols) imageX--;
  234. Color c = bitmap.GetPixel(imageX, imageY);
  235. if (c.A != 255)
  236. {
  237. bitmap.SetPixel(imageX, imageY, Color.FromArgb(255, c.R, c.G, c.B));
  238. c = bitmap.GetPixel(imageX, imageY);
  239. }
  240. if (mirror)
  241. row.Add(new Coord(-(c.R * pixScale - 0.5f), c.G * pixScale - 0.5f, c.B * pixScale - 0.5f));
  242. else
  243. row.Add(new Coord(c.R * pixScale - 0.5f, c.G * pixScale - 0.5f, c.B * pixScale - 0.5f));
  244. }
  245. rows.Add(row);
  246. }
  247. return rows;
  248. }
  249. void _SculptMesh(Bitmap sculptBitmap, SculptType sculptType, int lod, bool viewerMode, bool mirror, bool invert)
  250. {
  251. _SculptMesh(new SculptMap(sculptBitmap, lod).ToRows(mirror), sculptType, viewerMode, mirror, invert);
  252. }
  253. #endif
  254. void _SculptMesh(List<List<Coord>> rows, SculptType sculptType, bool viewerMode, bool mirror, bool invert)
  255. {
  256. coords = new List<Coord>();
  257. faces = new List<Face>();
  258. normals = new List<Coord>();
  259. uvs = new List<UVCoord>();
  260. sculptType = (SculptType)(((int)sculptType) & 0x07);
  261. if (mirror)
  262. invert = !invert;
  263. viewerFaces = new List<ViewerFace>();
  264. int width = rows[0].Count;
  265. int p1, p2, p3, p4;
  266. int imageX, imageY;
  267. if (sculptType != SculptType.plane)
  268. {
  269. if (rows.Count % 2 == 0)
  270. {
  271. for (int rowNdx = 0; rowNdx < rows.Count; rowNdx++)
  272. rows[rowNdx].Add(rows[rowNdx][0]);
  273. }
  274. else
  275. {
  276. int lastIndex = rows[0].Count - 1;
  277. for (int i = 0; i < rows.Count; i++)
  278. rows[i][0] = rows[i][lastIndex];
  279. }
  280. }
  281. Coord topPole = rows[0][width / 2];
  282. Coord bottomPole = rows[rows.Count - 1][width / 2];
  283. if (sculptType == SculptType.sphere)
  284. {
  285. if (rows.Count % 2 == 0)
  286. {
  287. int count = rows[0].Count;
  288. List<Coord> topPoleRow = new List<Coord>(count);
  289. List<Coord> bottomPoleRow = new List<Coord>(count);
  290. for (int i = 0; i < count; i++)
  291. {
  292. topPoleRow.Add(topPole);
  293. bottomPoleRow.Add(bottomPole);
  294. }
  295. rows.Insert(0, topPoleRow);
  296. rows.Add(bottomPoleRow);
  297. }
  298. else
  299. {
  300. int count = rows[0].Count;
  301. List<Coord> topPoleRow = rows[0];
  302. List<Coord> bottomPoleRow = rows[rows.Count - 1];
  303. for (int i = 0; i < count; i++)
  304. {
  305. topPoleRow[i] = topPole;
  306. bottomPoleRow[i] = bottomPole;
  307. }
  308. }
  309. }
  310. if (sculptType == SculptType.torus)
  311. rows.Add(rows[0]);
  312. int coordsDown = rows.Count;
  313. int coordsAcross = rows[0].Count;
  314. int lastColumn = coordsAcross - 1;
  315. float widthUnit = 1.0f / (coordsAcross - 1);
  316. float heightUnit = 1.0f / (coordsDown - 1);
  317. for (imageY = 0; imageY < coordsDown; imageY++)
  318. {
  319. int rowOffset = imageY * coordsAcross;
  320. for (imageX = 0; imageX < coordsAcross; imageX++)
  321. {
  322. /*
  323. * p1-----p2
  324. * | \ f2 |
  325. * | \ |
  326. * | f1 \|
  327. * p3-----p4
  328. */
  329. p4 = rowOffset + imageX;
  330. p3 = p4 - 1;
  331. p2 = p4 - coordsAcross;
  332. p1 = p3 - coordsAcross;
  333. this.coords.Add(rows[imageY][imageX]);
  334. if (viewerMode)
  335. {
  336. this.normals.Add(new Coord());
  337. this.uvs.Add(new UVCoord(widthUnit * imageX, heightUnit * imageY));
  338. }
  339. if (imageY > 0 && imageX > 0)
  340. {
  341. Face f1, f2;
  342. if (viewerMode)
  343. {
  344. if (invert)
  345. {
  346. f1 = new Face(p1, p4, p3, p1, p4, p3);
  347. f1.uv1 = p1;
  348. f1.uv2 = p4;
  349. f1.uv3 = p3;
  350. f2 = new Face(p1, p2, p4, p1, p2, p4);
  351. f2.uv1 = p1;
  352. f2.uv2 = p2;
  353. f2.uv3 = p4;
  354. }
  355. else
  356. {
  357. f1 = new Face(p1, p3, p4, p1, p3, p4);
  358. f1.uv1 = p1;
  359. f1.uv2 = p3;
  360. f1.uv3 = p4;
  361. f2 = new Face(p1, p4, p2, p1, p4, p2);
  362. f2.uv1 = p1;
  363. f2.uv2 = p4;
  364. f2.uv3 = p2;
  365. }
  366. }
  367. else
  368. {
  369. if (invert)
  370. {
  371. f1 = new Face(p1, p4, p3);
  372. f2 = new Face(p1, p2, p4);
  373. }
  374. else
  375. {
  376. f1 = new Face(p1, p3, p4);
  377. f2 = new Face(p1, p4, p2);
  378. }
  379. }
  380. this.faces.Add(f1);
  381. this.faces.Add(f2);
  382. }
  383. }
  384. }
  385. if (viewerMode)
  386. calcVertexNormals(sculptType, coordsAcross, coordsDown);
  387. }
  388. /// <summary>
  389. /// Duplicates a SculptMesh object. All object properties are copied by value, including lists.
  390. /// </summary>
  391. /// <returns></returns>
  392. public SculptMesh Copy()
  393. {
  394. return new SculptMesh(this);
  395. }
  396. public SculptMesh(SculptMesh sm)
  397. {
  398. coords = new List<Coord>(sm.coords);
  399. faces = new List<Face>(sm.faces);
  400. viewerFaces = new List<ViewerFace>(sm.viewerFaces);
  401. normals = new List<Coord>(sm.normals);
  402. uvs = new List<UVCoord>(sm.uvs);
  403. }
  404. private void calcVertexNormals(SculptType sculptType, int xSize, int ySize)
  405. { // compute vertex normals by summing all the surface normals of all the triangles sharing
  406. // each vertex and then normalizing
  407. int numFaces = this.faces.Count;
  408. for (int i = 0; i < numFaces; i++)
  409. {
  410. Face face = this.faces[i];
  411. Coord surfaceNormal = face.SurfaceNormal(this.coords);
  412. this.normals[face.n1] += surfaceNormal;
  413. this.normals[face.n2] += surfaceNormal;
  414. this.normals[face.n3] += surfaceNormal;
  415. }
  416. int numNormals = this.normals.Count;
  417. for (int i = 0; i < numNormals; i++)
  418. this.normals[i] = this.normals[i].Normalize();
  419. if (sculptType != SculptType.plane)
  420. { // blend the vertex normals at the cylinder seam
  421. for (int y = 0; y < ySize; y++)
  422. {
  423. int rowOffset = y * xSize;
  424. this.normals[rowOffset] = this.normals[rowOffset + xSize - 1] = (this.normals[rowOffset] + this.normals[rowOffset + xSize - 1]).Normalize();
  425. }
  426. }
  427. foreach (Face face in this.faces)
  428. {
  429. ViewerFace vf = new ViewerFace(0);
  430. vf.v1 = this.coords[face.v1];
  431. vf.v2 = this.coords[face.v2];
  432. vf.v3 = this.coords[face.v3];
  433. vf.coordIndex1 = face.v1;
  434. vf.coordIndex2 = face.v2;
  435. vf.coordIndex3 = face.v3;
  436. vf.n1 = this.normals[face.n1];
  437. vf.n2 = this.normals[face.n2];
  438. vf.n3 = this.normals[face.n3];
  439. vf.uv1 = this.uvs[face.uv1];
  440. vf.uv2 = this.uvs[face.uv2];
  441. vf.uv3 = this.uvs[face.uv3];
  442. this.viewerFaces.Add(vf);
  443. }
  444. }
  445. /// <summary>
  446. /// Adds a value to each XYZ vertex coordinate in the mesh
  447. /// </summary>
  448. /// <param name="x"></param>
  449. /// <param name="y"></param>
  450. /// <param name="z"></param>
  451. public void AddPos(float x, float y, float z)
  452. {
  453. int i;
  454. int numVerts = this.coords.Count;
  455. Coord vert;
  456. for (i = 0; i < numVerts; i++)
  457. {
  458. vert = this.coords[i];
  459. vert.X += x;
  460. vert.Y += y;
  461. vert.Z += z;
  462. this.coords[i] = vert;
  463. }
  464. if (this.viewerFaces != null)
  465. {
  466. int numViewerFaces = this.viewerFaces.Count;
  467. for (i = 0; i < numViewerFaces; i++)
  468. {
  469. ViewerFace v = this.viewerFaces[i];
  470. v.AddPos(x, y, z);
  471. this.viewerFaces[i] = v;
  472. }
  473. }
  474. }
  475. /// <summary>
  476. /// Rotates the mesh
  477. /// </summary>
  478. /// <param name="q"></param>
  479. public void AddRot(Quat q)
  480. {
  481. int i;
  482. int numVerts = this.coords.Count;
  483. for (i = 0; i < numVerts; i++)
  484. this.coords[i] *= q;
  485. int numNormals = this.normals.Count;
  486. for (i = 0; i < numNormals; i++)
  487. this.normals[i] *= q;
  488. if (this.viewerFaces != null)
  489. {
  490. int numViewerFaces = this.viewerFaces.Count;
  491. for (i = 0; i < numViewerFaces; i++)
  492. {
  493. ViewerFace v = this.viewerFaces[i];
  494. v.v1 *= q;
  495. v.v2 *= q;
  496. v.v3 *= q;
  497. v.n1 *= q;
  498. v.n2 *= q;
  499. v.n3 *= q;
  500. this.viewerFaces[i] = v;
  501. }
  502. }
  503. }
  504. public void Scale(float x, float y, float z)
  505. {
  506. int i;
  507. int numVerts = this.coords.Count;
  508. Coord m = new Coord(x, y, z);
  509. for (i = 0; i < numVerts; i++)
  510. this.coords[i] *= m;
  511. if (this.viewerFaces != null)
  512. {
  513. int numViewerFaces = this.viewerFaces.Count;
  514. for (i = 0; i < numViewerFaces; i++)
  515. {
  516. ViewerFace v = this.viewerFaces[i];
  517. v.v1 *= m;
  518. v.v2 *= m;
  519. v.v3 *= m;
  520. this.viewerFaces[i] = v;
  521. }
  522. }
  523. }
  524. public void DumpRaw(String path, String name, String title)
  525. {
  526. if (path == null)
  527. return;
  528. String fileName = name + "_" + title + ".raw";
  529. String completePath = System.IO.Path.Combine(path, fileName);
  530. StreamWriter sw = new StreamWriter(completePath);
  531. for (int i = 0; i < this.faces.Count; i++)
  532. {
  533. string s = this.coords[this.faces[i].v1].ToString();
  534. s += " " + this.coords[this.faces[i].v2].ToString();
  535. s += " " + this.coords[this.faces[i].v3].ToString();
  536. sw.WriteLine(s);
  537. }
  538. sw.Close();
  539. }
  540. }
  541. }