SculptMesh.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  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 OpenSim 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 const float pixScale = 0.00390625f; // 1.0 / 256
  44. private Bitmap ScaleImage(Bitmap srcImage, float scale)
  45. {
  46. int sourceWidth = srcImage.Width;
  47. int sourceHeight = srcImage.Height;
  48. int sourceX = 0;
  49. int sourceY = 0;
  50. int destX = 0;
  51. int destY = 0;
  52. int destWidth = (int)(srcImage.Width * scale);
  53. int destHeight = (int)(srcImage.Height * scale);
  54. Bitmap scaledImage = new Bitmap(destWidth, destHeight,
  55. PixelFormat.Format24bppRgb);
  56. scaledImage.SetResolution(srcImage.HorizontalResolution,
  57. srcImage.VerticalResolution);
  58. Graphics grPhoto = Graphics.FromImage(scaledImage);
  59. grPhoto.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Bilinear;
  60. grPhoto.DrawImage(srcImage,
  61. new Rectangle(destX, destY, destWidth, destHeight),
  62. new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight),
  63. GraphicsUnit.Pixel);
  64. grPhoto.Dispose();
  65. return scaledImage;
  66. }
  67. public SculptMesh SculptMeshFromFile(string fileName, SculptType sculptType, int lod, bool viewerMode)
  68. {
  69. Bitmap bitmap = (Bitmap)Bitmap.FromFile(fileName);
  70. SculptMesh sculptMesh = new SculptMesh(bitmap, sculptType, lod, viewerMode);
  71. bitmap.Dispose();
  72. return sculptMesh;
  73. }
  74. /// <summary>
  75. /// ** Experimental ** May disappear from future versions ** not recommeneded for use in applications
  76. /// Construct a sculpt mesh from a 2D array of floats
  77. /// </summary>
  78. /// <param name="zMap"></param>
  79. /// <param name="xBegin"></param>
  80. /// <param name="xEnd"></param>
  81. /// <param name="yBegin"></param>
  82. /// <param name="yEnd"></param>
  83. /// <param name="viewerMode"></param>
  84. public SculptMesh(float[,] zMap, float xBegin, float xEnd, float yBegin, float yEnd, bool viewerMode)
  85. {
  86. float xStep, yStep;
  87. float uStep, vStep;
  88. int numYElements = zMap.GetLength(0);
  89. int numXElements = zMap.GetLength(1);
  90. try
  91. {
  92. xStep = (xEnd - xBegin) / (float)(numXElements - 1);
  93. yStep = (yEnd - yBegin) / (float)(numYElements - 1);
  94. uStep = 1.0f / (numXElements - 1);
  95. vStep = 1.0f / (numYElements - 1);
  96. }
  97. catch (DivideByZeroException)
  98. {
  99. return;
  100. }
  101. coords = new List<Coord>();
  102. faces = new List<Face>();
  103. normals = new List<Coord>();
  104. uvs = new List<UVCoord>();
  105. viewerFaces = new List<ViewerFace>();
  106. int p1, p2, p3, p4;
  107. int x, y;
  108. int xStart = 0, yStart = 0;
  109. for (y = yStart; y < numYElements; y++)
  110. {
  111. int rowOffset = y * numXElements;
  112. for (x = xStart; x < numXElements; x++)
  113. {
  114. /*
  115. * p1-----p2
  116. * | \ f2 |
  117. * | \ |
  118. * | f1 \|
  119. * p3-----p4
  120. */
  121. p4 = rowOffset + x;
  122. p3 = p4 - 1;
  123. p2 = p4 - numXElements;
  124. p1 = p3 - numXElements;
  125. Coord c = new Coord(xBegin + x * xStep, yBegin + y * yStep, zMap[y, x]);
  126. this.coords.Add(c);
  127. if (viewerMode)
  128. {
  129. this.normals.Add(new Coord());
  130. this.uvs.Add(new UVCoord(uStep * x, 1.0f - vStep * y));
  131. }
  132. if (y > 0 && x > 0)
  133. {
  134. Face f1, f2;
  135. //if (viewerMode)
  136. //{
  137. // f1 = new Face(p1, p3, p4, p1, p3, p4);
  138. // f1.uv1 = p1;
  139. // f1.uv2 = p3;
  140. // f1.uv3 = p4;
  141. // f2 = new Face(p1, p4, p2, p1, p4, p2);
  142. // f2.uv1 = p1;
  143. // f2.uv2 = p4;
  144. // f2.uv3 = p2;
  145. //}
  146. //else
  147. //{
  148. // f1 = new Face(p1, p3, p4);
  149. // f2 = new Face(p1, p4, p2);
  150. //}
  151. if (viewerMode)
  152. {
  153. f1 = new Face(p1, p4, p3, p1, p4, p3);
  154. f1.uv1 = p1;
  155. f1.uv2 = p4;
  156. f1.uv3 = p3;
  157. f2 = new Face(p1, p2, p4, p1, p2, p4);
  158. f2.uv1 = p1;
  159. f2.uv2 = p2;
  160. f2.uv3 = p4;
  161. }
  162. else
  163. {
  164. f1 = new Face(p1, p4, p3);
  165. f2 = new Face(p1, p2, p4);
  166. }
  167. this.faces.Add(f1);
  168. this.faces.Add(f2);
  169. }
  170. }
  171. }
  172. if (viewerMode)
  173. calcVertexNormals(SculptType.plane, numXElements, numYElements);
  174. }
  175. public SculptMesh(Bitmap sculptBitmap, SculptType sculptType, int lod, bool viewerMode)
  176. {
  177. coords = new List<Coord>();
  178. faces = new List<Face>();
  179. normals = new List<Coord>();
  180. uvs = new List<UVCoord>();
  181. //float sourceScaleFactor = (float)lod / (float)Math.Max(sculptBitmap.Width, sculptBitmap.Height);
  182. float sourceScaleFactor = (float)(lod) / (float)Math.Sqrt(sculptBitmap.Width * sculptBitmap.Height);
  183. bool scaleSourceImage = sourceScaleFactor < 1.0f ? true : false;
  184. Bitmap bitmap;
  185. if (scaleSourceImage)
  186. bitmap = ScaleImage(sculptBitmap, sourceScaleFactor);
  187. else
  188. bitmap = sculptBitmap;
  189. viewerFaces = new List<ViewerFace>();
  190. int width = bitmap.Width;
  191. int height = bitmap.Height;
  192. float widthUnit = 1.0f / width;
  193. float heightUnit = 1.0f / (height - 1);
  194. int p1, p2, p3, p4;
  195. Color color;
  196. float x, y, z;
  197. int imageX, imageY;
  198. if (sculptType == SculptType.sphere)
  199. { // average the top and bottom row pixel values so the resulting vertices appear to converge
  200. int lastRow = height - 1;
  201. int r1 = 0, g1 = 0, b1 = 0;
  202. int r2 = 0, g2 = 0, b2 = 0;
  203. for (imageX = 0; imageX < width; imageX++)
  204. {
  205. Color c1 = bitmap.GetPixel(imageX, 0);
  206. Color c2 = bitmap.GetPixel(imageX, lastRow);
  207. r1 += c1.R;
  208. g1 += c1.G;
  209. b1 += c1.B;
  210. r2 += c2.R;
  211. g2 += c2.G;
  212. b2 += c2.B;
  213. }
  214. Color newC1 = Color.FromArgb(r1 / width, g1 / width, b1 / width);
  215. Color newC2 = Color.FromArgb(r2 / width, g2 / width, b2 / width);
  216. for (imageX = 0; imageX < width; imageX++)
  217. {
  218. bitmap.SetPixel(imageX, 0, newC1);
  219. bitmap.SetPixel(imageX, lastRow, newC2);
  220. }
  221. }
  222. int pixelsAcross = sculptType == SculptType.plane ? width : width + 1;
  223. int pixelsDown = sculptType == SculptType.sphere || sculptType == SculptType.cylinder ? height + 1 : height;
  224. for (imageY = 0; imageY < pixelsDown; imageY++)
  225. {
  226. int rowOffset = imageY * width;
  227. for (imageX = 0; imageX < pixelsAcross; imageX++)
  228. {
  229. /*
  230. * p1-----p2
  231. * | \ f2 |
  232. * | \ |
  233. * | f1 \|
  234. * p3-----p4
  235. */
  236. if (imageX < width)
  237. {
  238. p4 = rowOffset + imageX;
  239. p3 = p4 - 1;
  240. }
  241. else
  242. {
  243. p4 = rowOffset; // wrap around to beginning
  244. p3 = rowOffset + imageX - 1;
  245. }
  246. p2 = p4 - width;
  247. p1 = p3 - width;
  248. color = bitmap.GetPixel(imageX == width ? 0 : imageX, imageY == height ? height - 1 : imageY);
  249. x = (color.R - 128) * pixScale;
  250. y = (color.G - 128) * pixScale;
  251. z = (color.B - 128) * pixScale;
  252. Coord c = new Coord(x, y, z);
  253. this.coords.Add(c);
  254. if (viewerMode)
  255. {
  256. this.normals.Add(new Coord());
  257. this.uvs.Add(new UVCoord(widthUnit * imageX, heightUnit * imageY));
  258. }
  259. if (imageY > 0 && imageX > 0)
  260. {
  261. Face f1, f2;
  262. if (viewerMode)
  263. {
  264. f1 = new Face(p1, p3, p4, p1, p3, p4);
  265. f1.uv1 = p1;
  266. f1.uv2 = p3;
  267. f1.uv3 = p4;
  268. f2 = new Face(p1, p4, p2, p1, p4, p2);
  269. f2.uv1 = p1;
  270. f2.uv2 = p4;
  271. f2.uv3 = p2;
  272. }
  273. else
  274. {
  275. f1 = new Face(p1, p3, p4);
  276. f2 = new Face(p1, p4, p2);
  277. }
  278. this.faces.Add(f1);
  279. this.faces.Add(f2);
  280. }
  281. }
  282. }
  283. if (scaleSourceImage)
  284. bitmap.Dispose();
  285. if (viewerMode)
  286. calcVertexNormals(sculptType, width, height);
  287. }
  288. private void calcVertexNormals(SculptType sculptType, int xSize, int ySize)
  289. { // compute vertex normals by summing all the surface normals of all the triangles sharing
  290. // each vertex and then normalizing
  291. int numFaces = this.faces.Count;
  292. for (int i = 0; i < numFaces; i++)
  293. {
  294. Face face = this.faces[i];
  295. Coord surfaceNormal = face.SurfaceNormal(this.coords);
  296. this.normals[face.v1] += surfaceNormal;
  297. this.normals[face.v2] += surfaceNormal;
  298. this.normals[face.v3] += surfaceNormal;
  299. }
  300. int numCoords = this.coords.Count;
  301. for (int i = 0; i < numCoords; i++)
  302. this.coords[i].Normalize();
  303. if (sculptType != SculptType.plane)
  304. { // blend the vertex normals at the cylinder seam
  305. int pixelsAcross = xSize + 1;
  306. for (int y = 0; y < ySize; y++)
  307. {
  308. int rowOffset = y * pixelsAcross;
  309. this.normals[rowOffset] = this.normals[rowOffset + xSize - 1] = (this.normals[rowOffset] + this.normals[rowOffset + xSize - 1]).Normalize();
  310. }
  311. }
  312. foreach (Face face in this.faces)
  313. {
  314. ViewerFace vf = new ViewerFace(0);
  315. vf.v1 = this.coords[face.v1];
  316. vf.v2 = this.coords[face.v2];
  317. vf.v3 = this.coords[face.v3];
  318. vf.n1 = this.normals[face.n1];
  319. vf.n2 = this.normals[face.n2];
  320. vf.n3 = this.normals[face.n3];
  321. vf.uv1 = this.uvs[face.uv1];
  322. vf.uv2 = this.uvs[face.uv2];
  323. vf.uv3 = this.uvs[face.uv3];
  324. this.viewerFaces.Add(vf);
  325. }
  326. }
  327. public void AddRot(Quat q)
  328. {
  329. int i;
  330. int numVerts = this.coords.Count;
  331. for (i = 0; i < numVerts; i++)
  332. this.coords[i] *= q;
  333. if (this.viewerFaces != null)
  334. {
  335. int numViewerFaces = this.viewerFaces.Count;
  336. for (i = 0; i < numViewerFaces; i++)
  337. {
  338. ViewerFace v = this.viewerFaces[i];
  339. v.v1 *= q;
  340. v.v2 *= q;
  341. v.v3 *= q;
  342. v.n1 *= q;
  343. v.n2 *= q;
  344. v.n3 *= q;
  345. this.viewerFaces[i] = v;
  346. }
  347. }
  348. }
  349. public void Scale(float x, float y, float z)
  350. {
  351. int i;
  352. int numVerts = this.coords.Count;
  353. //Coord vert;
  354. Coord m = new Coord(x, y, z);
  355. for (i = 0; i < numVerts; i++)
  356. this.coords[i] *= m;
  357. if (this.viewerFaces != null)
  358. {
  359. int numViewerFaces = this.viewerFaces.Count;
  360. for (i = 0; i < numViewerFaces; i++)
  361. {
  362. ViewerFace v = this.viewerFaces[i];
  363. v.v1 *= m;
  364. v.v2 *= m;
  365. v.v3 *= m;
  366. this.viewerFaces[i] = v;
  367. }
  368. }
  369. }
  370. public void DumpRaw(String path, String name, String title)
  371. {
  372. if (path == null)
  373. return;
  374. String fileName = name + "_" + title + ".raw";
  375. String completePath = Path.Combine(path, fileName);
  376. StreamWriter sw = new StreamWriter(completePath);
  377. for (int i = 0; i < this.faces.Count; i++)
  378. {
  379. string s = this.coords[this.faces[i].v1].ToString();
  380. s += " " + this.coords[this.faces[i].v2].ToString();
  381. s += " " + this.coords[this.faces[i].v3].ToString();
  382. sw.WriteLine(s);
  383. }
  384. sw.Close();
  385. }
  386. }
  387. }