SculptMesh.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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)(sourceWidth * scale);
  53. int destHeight = (int)(sourceHeight * 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. public SculptMesh(Bitmap sculptBitmap, SculptType sculptType, int lod, bool viewerMode)
  75. {
  76. coords = new List<Coord>();
  77. faces = new List<Face>();
  78. normals = new List<Coord>();
  79. uvs = new List<UVCoord>();
  80. float sourceScaleFactor = (float)lod / (float)Math.Max(sculptBitmap.Width, sculptBitmap.Height);
  81. bool scaleSourceImage = sourceScaleFactor < 1.0f ? true : false;
  82. Bitmap bitmap;
  83. if (scaleSourceImage)
  84. bitmap = ScaleImage(sculptBitmap, sourceScaleFactor);
  85. else
  86. bitmap = sculptBitmap;
  87. viewerFaces = new List<ViewerFace>();
  88. int width = bitmap.Width;
  89. int height = bitmap.Height;
  90. float widthUnit = 1.0f / width;
  91. float heightUnit = 1.0f / (height - 1);
  92. int p1, p2, p3, p4;
  93. Color color;
  94. float x, y, z;
  95. int imageX, imageY;
  96. if (sculptType == SculptType.sphere)
  97. { // average the top and bottom row pixel values so the resulting vertices appear to converge
  98. int lastRow = height - 1;
  99. int r1 = 0, g1 = 0, b1 = 0;
  100. int r2 = 0, g2 = 0, b2 = 0;
  101. for (imageX = 0; imageX < width; imageX++)
  102. {
  103. Color c1 = bitmap.GetPixel(imageX, 0);
  104. Color c2 = bitmap.GetPixel(imageX, lastRow);
  105. r1 += c1.R;
  106. g1 += c1.G;
  107. b1 += c1.B;
  108. r2 += c2.R;
  109. g2 += c2.G;
  110. b2 += c2.B;
  111. }
  112. Color newC1 = Color.FromArgb(r1 / width, g1 / width, b1 / width);
  113. Color newC2 = Color.FromArgb(r2 / width, g2 / width, b2 / width);
  114. for (imageX = 0; imageX < width; imageX++)
  115. {
  116. bitmap.SetPixel(imageX, 0, newC1);
  117. bitmap.SetPixel(imageX, lastRow, newC2);
  118. }
  119. }
  120. int pixelsAcross = sculptType == SculptType.plane ? width : width + 1;
  121. int pixelsDown = sculptType == SculptType.sphere || sculptType == SculptType.cylinder ? height + 1 : height;
  122. for (imageY = 0; imageY < pixelsDown; imageY++)
  123. {
  124. int rowOffset = imageY * width;
  125. for (imageX = 0; imageX < pixelsAcross; imageX++)
  126. {
  127. /*
  128. * p1-----p2
  129. * | \ f2 |
  130. * | \ |
  131. * | f1 \|
  132. * p3-----p4
  133. */
  134. if (imageX < width)
  135. {
  136. p4 = rowOffset + imageX;
  137. p3 = p4 - 1;
  138. }
  139. else
  140. {
  141. p4 = rowOffset; // wrap around to beginning
  142. p3 = rowOffset + imageX - 1;
  143. }
  144. p2 = p4 - width;
  145. p1 = p3 - width;
  146. color = bitmap.GetPixel(imageX == width ? 0 : imageX, imageY == height ? height - 1 : imageY);
  147. x = (color.R - 128) * pixScale;
  148. y = (color.G - 128) * pixScale;
  149. z = (color.B - 128) * pixScale;
  150. Coord c = new Coord(x, y, z);
  151. this.coords.Add(c);
  152. if (viewerMode)
  153. {
  154. this.normals.Add(new Coord());
  155. this.uvs.Add(new UVCoord(widthUnit * imageX, heightUnit * imageY));
  156. }
  157. if (imageY > 0 && imageX > 0)
  158. {
  159. Face f1, f2;
  160. if (viewerMode)
  161. {
  162. f1 = new Face(p1, p3, p4, p1, p3, p4);
  163. f1.uv1 = p1;
  164. f1.uv2 = p3;
  165. f1.uv3 = p4;
  166. f2 = new Face(p1, p4, p2, p1, p4, p2);
  167. f2.uv1 = p1;
  168. f2.uv2 = p4;
  169. f2.uv3 = p2;
  170. }
  171. else
  172. {
  173. f1 = new Face(p1, p3, p4);
  174. f2 = new Face(p1, p4, p2);
  175. }
  176. this.faces.Add(f1);
  177. this.faces.Add(f2);
  178. }
  179. }
  180. }
  181. if (scaleSourceImage)
  182. bitmap.Dispose();
  183. if (viewerMode)
  184. { // compute vertex normals by summing all the surface normals of all the triangles sharing
  185. // each vertex and then normalizing
  186. int numFaces = this.faces.Count;
  187. for (int i = 0; i < numFaces; i++)
  188. {
  189. Face face = this.faces[i];
  190. Coord surfaceNormal = face.SurfaceNormal(this.coords);
  191. this.normals[face.v1] += surfaceNormal;
  192. this.normals[face.v2] += surfaceNormal;
  193. this.normals[face.v3] += surfaceNormal;
  194. }
  195. int numCoords = this.coords.Count;
  196. for (int i = 0; i < numCoords; i++)
  197. this.coords[i].Normalize();
  198. if (sculptType != SculptType.plane)
  199. { // blend the vertex normals at the cylinder seam
  200. pixelsAcross = width + 1;
  201. for (imageY = 0; imageY < height; imageY++)
  202. {
  203. int rowOffset = imageY * pixelsAcross;
  204. this.normals[rowOffset] = this.normals[rowOffset + width - 1] = (this.normals[rowOffset] + this.normals[rowOffset + width - 1]).Normalize();
  205. }
  206. }
  207. foreach (Face face in this.faces)
  208. {
  209. ViewerFace vf = new ViewerFace(0);
  210. vf.v1 = this.coords[face.v1];
  211. vf.v2 = this.coords[face.v2];
  212. vf.v3 = this.coords[face.v3];
  213. vf.n1 = this.normals[face.n1];
  214. vf.n2 = this.normals[face.n2];
  215. vf.n3 = this.normals[face.n3];
  216. vf.uv1 = this.uvs[face.uv1];
  217. vf.uv2 = this.uvs[face.uv2];
  218. vf.uv3 = this.uvs[face.uv3];
  219. this.viewerFaces.Add(vf);
  220. }
  221. }
  222. }
  223. public void AddRot(Quat q)
  224. {
  225. int i;
  226. int numVerts = this.coords.Count;
  227. for (i = 0; i < numVerts; i++)
  228. this.coords[i] *= q;
  229. if (this.viewerFaces != null)
  230. {
  231. int numViewerFaces = this.viewerFaces.Count;
  232. for (i = 0; i < numViewerFaces; i++)
  233. {
  234. ViewerFace v = this.viewerFaces[i];
  235. v.v1 *= q;
  236. v.v2 *= q;
  237. v.v3 *= q;
  238. v.n1 *= q;
  239. v.n2 *= q;
  240. v.n3 *= q;
  241. this.viewerFaces[i] = v;
  242. }
  243. }
  244. }
  245. public void Scale(float x, float y, float z)
  246. {
  247. int i;
  248. int numVerts = this.coords.Count;
  249. //Coord vert;
  250. Coord m = new Coord(x, y, z);
  251. for (i = 0; i < numVerts; i++)
  252. this.coords[i] *= m;
  253. if (this.viewerFaces != null)
  254. {
  255. int numViewerFaces = this.viewerFaces.Count;
  256. for (i = 0; i < numViewerFaces; i++)
  257. {
  258. ViewerFace v = this.viewerFaces[i];
  259. v.v1 *= m;
  260. v.v2 *= m;
  261. v.v3 *= m;
  262. this.viewerFaces[i] = v;
  263. }
  264. }
  265. }
  266. public void DumpRaw(String path, String name, String title)
  267. {
  268. if (path == null)
  269. return;
  270. String fileName = name + "_" + title + ".raw";
  271. String completePath = Path.Combine(path, fileName);
  272. StreamWriter sw = new StreamWriter(completePath);
  273. for (int i = 0; i < this.faces.Count; i++)
  274. {
  275. string s = this.coords[this.faces[i].v1].ToString();
  276. s += " " + this.coords[this.faces[i].v2].ToString();
  277. s += " " + this.coords[this.faces[i].v3].ToString();
  278. sw.WriteLine(s);
  279. }
  280. sw.Close();
  281. }
  282. }
  283. }