SculptMesh.cs 20 KB

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