SculptMesh.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  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. // private Bitmap ScaleImage(Bitmap srcImage, float scale)
  49. // {
  50. // int sourceWidth = srcImage.Width;
  51. // int sourceHeight = srcImage.Height;
  52. // int sourceX = 0;
  53. // int sourceY = 0;
  54. // int destX = 0;
  55. // int destY = 0;
  56. // int destWidth = (int)(srcImage.Width * scale);
  57. // int destHeight = (int)(srcImage.Height * scale);
  58. // if (srcImage.PixelFormat == PixelFormat.Format32bppArgb)
  59. // for (int y = 0; y < srcImage.Height; y++)
  60. // for (int x = 0; x < srcImage.Width; x++)
  61. // {
  62. // Color c = srcImage.GetPixel(x, y);
  63. // srcImage.SetPixel(x, y, Color.FromArgb(255, c.R, c.G, c.B));
  64. // }
  65. // Bitmap scaledImage = new Bitmap(destWidth, destHeight,
  66. // PixelFormat.Format24bppRgb);
  67. // scaledImage.SetResolution(96.0f, 96.0f);
  68. // Graphics grPhoto = Graphics.FromImage(scaledImage);
  69. // grPhoto.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Low;
  70. // grPhoto.DrawImage(srcImage,
  71. // new Rectangle(destX, destY, destWidth, destHeight),
  72. // new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight),
  73. // GraphicsUnit.Pixel);
  74. // grPhoto.Dispose();
  75. // return scaledImage;
  76. // }
  77. public SculptMesh SculptMeshFromFile(string fileName, SculptType sculptType, int lod, bool viewerMode)
  78. {
  79. Bitmap bitmap = (Bitmap)Bitmap.FromFile(fileName);
  80. SculptMesh sculptMesh = new SculptMesh(bitmap, sculptType, lod, viewerMode);
  81. bitmap.Dispose();
  82. return sculptMesh;
  83. }
  84. public SculptMesh(string fileName, int sculptType, int lod, int viewerMode, int mirror, int invert)
  85. {
  86. Bitmap bitmap = (Bitmap)Bitmap.FromFile(fileName);
  87. _SculptMesh(bitmap, (SculptType)sculptType, lod, viewerMode != 0, mirror != 0, invert != 0);
  88. bitmap.Dispose();
  89. }
  90. #endif
  91. /// <summary>
  92. /// ** Experimental ** May disappear from future versions ** not recommeneded for use in applications
  93. /// Construct a sculpt mesh from a 2D array of floats
  94. /// </summary>
  95. /// <param name="zMap"></param>
  96. /// <param name="xBegin"></param>
  97. /// <param name="xEnd"></param>
  98. /// <param name="yBegin"></param>
  99. /// <param name="yEnd"></param>
  100. /// <param name="viewerMode"></param>
  101. public SculptMesh(float[,] zMap, float xBegin, float xEnd, float yBegin, float yEnd, bool viewerMode)
  102. {
  103. float xStep, yStep;
  104. float uStep, vStep;
  105. int numYElements = zMap.GetLength(0);
  106. int numXElements = zMap.GetLength(1);
  107. try
  108. {
  109. xStep = (xEnd - xBegin) / (float)(numXElements - 1);
  110. yStep = (yEnd - yBegin) / (float)(numYElements - 1);
  111. uStep = 1.0f / (numXElements - 1);
  112. vStep = 1.0f / (numYElements - 1);
  113. }
  114. catch (DivideByZeroException)
  115. {
  116. return;
  117. }
  118. coords = new List<Coord>();
  119. faces = new List<Face>();
  120. normals = new List<Coord>();
  121. uvs = new List<UVCoord>();
  122. viewerFaces = new List<ViewerFace>();
  123. int p1, p2, p3, p4;
  124. int x, y;
  125. int xStart = 0, yStart = 0;
  126. for (y = yStart; y < numYElements; y++)
  127. {
  128. int rowOffset = y * numXElements;
  129. for (x = xStart; x < numXElements; x++)
  130. {
  131. /*
  132. * p1-----p2
  133. * | \ f2 |
  134. * | \ |
  135. * | f1 \|
  136. * p3-----p4
  137. */
  138. p4 = rowOffset + x;
  139. p3 = p4 - 1;
  140. p2 = p4 - numXElements;
  141. p1 = p3 - numXElements;
  142. Coord c = new Coord(xBegin + x * xStep, yBegin + y * yStep, zMap[y, x]);
  143. this.coords.Add(c);
  144. if (viewerMode)
  145. {
  146. this.normals.Add(new Coord());
  147. this.uvs.Add(new UVCoord(uStep * x, 1.0f - vStep * y));
  148. }
  149. if (y > 0 && x > 0)
  150. {
  151. Face f1, f2;
  152. if (viewerMode)
  153. {
  154. f1 = new Face(p1, p4, p3, p1, p4, p3);
  155. f1.uv1 = p1;
  156. f1.uv2 = p4;
  157. f1.uv3 = p3;
  158. f2 = new Face(p1, p2, p4, p1, p2, p4);
  159. f2.uv1 = p1;
  160. f2.uv2 = p2;
  161. f2.uv3 = p4;
  162. }
  163. else
  164. {
  165. f1 = new Face(p1, p4, p3);
  166. f2 = new Face(p1, p2, p4);
  167. }
  168. this.faces.Add(f1);
  169. this.faces.Add(f2);
  170. }
  171. }
  172. }
  173. if (viewerMode)
  174. calcVertexNormals(SculptType.plane, numXElements, numYElements);
  175. }
  176. #if SYSTEM_DRAWING
  177. public SculptMesh(Bitmap sculptBitmap, SculptType sculptType, int lod, bool viewerMode)
  178. {
  179. _SculptMesh(sculptBitmap, sculptType, lod, viewerMode, false, false);
  180. }
  181. public SculptMesh(Bitmap sculptBitmap, SculptType sculptType, int lod, bool viewerMode, bool mirror, bool invert)
  182. {
  183. _SculptMesh(sculptBitmap, sculptType, lod, viewerMode, mirror, invert);
  184. }
  185. #endif
  186. public SculptMesh(List<List<Coord>> rows, SculptType sculptType, bool viewerMode, bool mirror, bool invert)
  187. {
  188. _SculptMesh(rows, sculptType, viewerMode, mirror, invert);
  189. }
  190. #if SYSTEM_DRAWING
  191. /// <summary>
  192. /// converts a bitmap to a list of lists of coords, while scaling the image.
  193. /// the scaling is done in floating point so as to allow for reduced vertex position
  194. /// quantization as the position will be averaged between pixel values. this routine will
  195. /// likely fail if the bitmap width and height are not powers of 2.
  196. /// </summary>
  197. /// <param name="bitmap"></param>
  198. /// <param name="scale"></param>
  199. /// <param name="mirror"></param>
  200. /// <returns></returns>
  201. private List<List<Coord>> bitmap2Coords(Bitmap bitmap, int scale, bool mirror)
  202. {
  203. int numRows = bitmap.Height / scale;
  204. int numCols = bitmap.Width / scale;
  205. List<List<Coord>> rows = new List<List<Coord>>(numRows);
  206. float pixScale = 1.0f / (scale * scale);
  207. pixScale /= 255;
  208. int imageX, imageY = 0;
  209. int rowNdx, colNdx;
  210. for (rowNdx = 0; rowNdx < numRows; rowNdx++)
  211. {
  212. List<Coord> row = new List<Coord>(numCols);
  213. for (colNdx = 0; colNdx < numCols; colNdx++)
  214. {
  215. imageX = colNdx * scale;
  216. int imageYStart = rowNdx * scale;
  217. int imageYEnd = imageYStart + scale;
  218. int imageXEnd = imageX + scale;
  219. float rSum = 0.0f;
  220. float gSum = 0.0f;
  221. float bSum = 0.0f;
  222. for (; imageX < imageXEnd; imageX++)
  223. {
  224. for (imageY = imageYStart; imageY < imageYEnd; imageY++)
  225. {
  226. Color c = bitmap.GetPixel(imageX, imageY);
  227. rSum += c.R;
  228. gSum += c.G;
  229. bSum += c.B;
  230. }
  231. }
  232. if (mirror)
  233. row.Add(new Coord(-(rSum * pixScale - 0.5f), gSum * pixScale - 0.5f, bSum * pixScale - 0.5f));
  234. else
  235. row.Add(new Coord(rSum * pixScale - 0.5f, gSum * pixScale - 0.5f, bSum * pixScale - 0.5f));
  236. }
  237. rows.Add(row);
  238. }
  239. return rows;
  240. }
  241. void _SculptMesh(Bitmap sculptBitmap, SculptType sculptType, int lod, bool viewerMode, bool mirror, bool invert)
  242. {
  243. coords = new List<Coord>();
  244. faces = new List<Face>();
  245. normals = new List<Coord>();
  246. uvs = new List<UVCoord>();
  247. sculptType = (SculptType)(((int)sculptType) & 0x07);
  248. if (mirror)
  249. if (sculptType == SculptType.plane)
  250. invert = !invert;
  251. float sourceScaleFactor = (float)(lod) / (float)Math.Sqrt(sculptBitmap.Width * sculptBitmap.Height);
  252. int scale = (int)(1.0f / sourceScaleFactor);
  253. if (scale < 1) scale = 1;
  254. _SculptMesh(bitmap2Coords(sculptBitmap, scale, mirror), sculptType, viewerMode, mirror, invert);
  255. }
  256. #endif
  257. void _SculptMesh(List<List<Coord>> rows, SculptType sculptType, bool viewerMode, bool mirror, bool invert)
  258. {
  259. coords = new List<Coord>();
  260. faces = new List<Face>();
  261. normals = new List<Coord>();
  262. uvs = new List<UVCoord>();
  263. sculptType = (SculptType)(((int)sculptType) & 0x07);
  264. if (mirror)
  265. if (sculptType == SculptType.plane)
  266. invert = !invert;
  267. viewerFaces = new List<ViewerFace>();
  268. int width = rows[0].Count;
  269. int p1, p2, p3, p4;
  270. int imageX, imageY;
  271. if (sculptType != SculptType.plane)
  272. {
  273. for (int rowNdx = 0; rowNdx < rows.Count; rowNdx++)
  274. rows[rowNdx].Add(rows[rowNdx][0]);
  275. }
  276. Coord topPole = rows[0][width / 2];
  277. Coord bottomPole = rows[rows.Count - 1][width / 2];
  278. if (sculptType == SculptType.sphere)
  279. {
  280. int count = rows[0].Count;
  281. List<Coord> topPoleRow = new List<Coord>(count);
  282. List<Coord> bottomPoleRow = new List<Coord>(count);
  283. for (int i = 0; i < count; i++)
  284. {
  285. topPoleRow.Add(topPole);
  286. bottomPoleRow.Add(bottomPole);
  287. }
  288. rows.Insert(0, topPoleRow);
  289. rows.Add(bottomPoleRow);
  290. }
  291. else if (sculptType == SculptType.torus)
  292. rows.Add(rows[0]);
  293. int coordsDown = rows.Count;
  294. int coordsAcross = rows[0].Count;
  295. float widthUnit = 1.0f / (coordsAcross - 1);
  296. float heightUnit = 1.0f / (coordsDown - 1);
  297. for (imageY = 0; imageY < coordsDown; imageY++)
  298. {
  299. int rowOffset = imageY * coordsAcross;
  300. for (imageX = 0; imageX < coordsAcross; imageX++)
  301. {
  302. /*
  303. * p1-----p2
  304. * | \ f2 |
  305. * | \ |
  306. * | f1 \|
  307. * p3-----p4
  308. */
  309. p4 = rowOffset + imageX;
  310. p3 = p4 - 1;
  311. p2 = p4 - coordsAcross;
  312. p1 = p3 - coordsAcross;
  313. this.coords.Add(rows[imageY][imageX]);
  314. if (viewerMode)
  315. {
  316. this.normals.Add(new Coord());
  317. this.uvs.Add(new UVCoord(widthUnit * imageX, heightUnit * imageY));
  318. }
  319. if (imageY > 0 && imageX > 0)
  320. {
  321. Face f1, f2;
  322. if (viewerMode)
  323. {
  324. if (invert)
  325. {
  326. f1 = new Face(p1, p4, p3, p1, p4, p3);
  327. f1.uv1 = p1;
  328. f1.uv2 = p4;
  329. f1.uv3 = p3;
  330. f2 = new Face(p1, p2, p4, p1, p2, p4);
  331. f2.uv1 = p1;
  332. f2.uv2 = p2;
  333. f2.uv3 = p4;
  334. }
  335. else
  336. {
  337. f1 = new Face(p1, p3, p4, p1, p3, p4);
  338. f1.uv1 = p1;
  339. f1.uv2 = p3;
  340. f1.uv3 = p4;
  341. f2 = new Face(p1, p4, p2, p1, p4, p2);
  342. f2.uv1 = p1;
  343. f2.uv2 = p4;
  344. f2.uv3 = p2;
  345. }
  346. }
  347. else
  348. {
  349. if (invert)
  350. {
  351. f1 = new Face(p1, p4, p3);
  352. f2 = new Face(p1, p2, p4);
  353. }
  354. else
  355. {
  356. f1 = new Face(p1, p3, p4);
  357. f2 = new Face(p1, p4, p2);
  358. }
  359. }
  360. this.faces.Add(f1);
  361. this.faces.Add(f2);
  362. }
  363. }
  364. }
  365. if (viewerMode)
  366. calcVertexNormals(sculptType, coordsAcross, coordsDown);
  367. }
  368. /// <summary>
  369. /// Duplicates a SculptMesh object. All object properties are copied by value, including lists.
  370. /// </summary>
  371. /// <returns></returns>
  372. public SculptMesh Copy()
  373. {
  374. return new SculptMesh(this);
  375. }
  376. public SculptMesh(SculptMesh sm)
  377. {
  378. coords = new List<Coord>(sm.coords);
  379. faces = new List<Face>(sm.faces);
  380. viewerFaces = new List<ViewerFace>(sm.viewerFaces);
  381. normals = new List<Coord>(sm.normals);
  382. uvs = new List<UVCoord>(sm.uvs);
  383. }
  384. private void calcVertexNormals(SculptType sculptType, int xSize, int ySize)
  385. { // compute vertex normals by summing all the surface normals of all the triangles sharing
  386. // each vertex and then normalizing
  387. int numFaces = this.faces.Count;
  388. for (int i = 0; i < numFaces; i++)
  389. {
  390. Face face = this.faces[i];
  391. Coord surfaceNormal = face.SurfaceNormal(this.coords);
  392. this.normals[face.n1] += surfaceNormal;
  393. this.normals[face.n2] += surfaceNormal;
  394. this.normals[face.n3] += surfaceNormal;
  395. }
  396. int numNormals = this.normals.Count;
  397. for (int i = 0; i < numNormals; i++)
  398. this.normals[i] = this.normals[i].Normalize();
  399. if (sculptType != SculptType.plane)
  400. { // blend the vertex normals at the cylinder seam
  401. for (int y = 0; y < ySize; y++)
  402. {
  403. int rowOffset = y * xSize;
  404. this.normals[rowOffset] = this.normals[rowOffset + xSize - 1] = (this.normals[rowOffset] + this.normals[rowOffset + xSize - 1]).Normalize();
  405. }
  406. }
  407. foreach (Face face in this.faces)
  408. {
  409. ViewerFace vf = new ViewerFace(0);
  410. vf.v1 = this.coords[face.v1];
  411. vf.v2 = this.coords[face.v2];
  412. vf.v3 = this.coords[face.v3];
  413. vf.coordIndex1 = face.v1;
  414. vf.coordIndex2 = face.v2;
  415. vf.coordIndex3 = face.v3;
  416. vf.n1 = this.normals[face.n1];
  417. vf.n2 = this.normals[face.n2];
  418. vf.n3 = this.normals[face.n3];
  419. vf.uv1 = this.uvs[face.uv1];
  420. vf.uv2 = this.uvs[face.uv2];
  421. vf.uv3 = this.uvs[face.uv3];
  422. this.viewerFaces.Add(vf);
  423. }
  424. }
  425. /// <summary>
  426. /// Adds a value to each XYZ vertex coordinate in the mesh
  427. /// </summary>
  428. /// <param name="x"></param>
  429. /// <param name="y"></param>
  430. /// <param name="z"></param>
  431. public void AddPos(float x, float y, float z)
  432. {
  433. int i;
  434. int numVerts = this.coords.Count;
  435. Coord vert;
  436. for (i = 0; i < numVerts; i++)
  437. {
  438. vert = this.coords[i];
  439. vert.X += x;
  440. vert.Y += y;
  441. vert.Z += z;
  442. this.coords[i] = vert;
  443. }
  444. if (this.viewerFaces != null)
  445. {
  446. int numViewerFaces = this.viewerFaces.Count;
  447. for (i = 0; i < numViewerFaces; i++)
  448. {
  449. ViewerFace v = this.viewerFaces[i];
  450. v.AddPos(x, y, z);
  451. this.viewerFaces[i] = v;
  452. }
  453. }
  454. }
  455. /// <summary>
  456. /// Rotates the mesh
  457. /// </summary>
  458. /// <param name="q"></param>
  459. public void AddRot(Quat q)
  460. {
  461. int i;
  462. int numVerts = this.coords.Count;
  463. for (i = 0; i < numVerts; i++)
  464. this.coords[i] *= q;
  465. int numNormals = this.normals.Count;
  466. for (i = 0; i < numNormals; i++)
  467. this.normals[i] *= q;
  468. if (this.viewerFaces != null)
  469. {
  470. int numViewerFaces = this.viewerFaces.Count;
  471. for (i = 0; i < numViewerFaces; i++)
  472. {
  473. ViewerFace v = this.viewerFaces[i];
  474. v.v1 *= q;
  475. v.v2 *= q;
  476. v.v3 *= q;
  477. v.n1 *= q;
  478. v.n2 *= q;
  479. v.n3 *= q;
  480. this.viewerFaces[i] = v;
  481. }
  482. }
  483. }
  484. public void Scale(float x, float y, float z)
  485. {
  486. int i;
  487. int numVerts = this.coords.Count;
  488. Coord m = new Coord(x, y, z);
  489. for (i = 0; i < numVerts; i++)
  490. this.coords[i] *= m;
  491. if (this.viewerFaces != null)
  492. {
  493. int numViewerFaces = this.viewerFaces.Count;
  494. for (i = 0; i < numViewerFaces; i++)
  495. {
  496. ViewerFace v = this.viewerFaces[i];
  497. v.v1 *= m;
  498. v.v2 *= m;
  499. v.v3 *= m;
  500. this.viewerFaces[i] = v;
  501. }
  502. }
  503. }
  504. public void DumpRaw(String path, String name, String title)
  505. {
  506. if (path == null)
  507. return;
  508. String fileName = name + "_" + title + ".raw";
  509. String completePath = System.IO.Path.Combine(path, fileName);
  510. StreamWriter sw = new StreamWriter(completePath);
  511. for (int i = 0; i < this.faces.Count; i++)
  512. {
  513. string s = this.coords[this.faces[i].v1].ToString();
  514. s += " " + this.coords[this.faces[i].v2].ToString();
  515. s += " " + this.coords[this.faces[i].v3].ToString();
  516. sw.WriteLine(s);
  517. }
  518. sw.Close();
  519. }
  520. }
  521. }