SculptMesh.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  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.Drawing;
  30. using System.Drawing.Imaging;
  31. using System.Text;
  32. using OpenJPEGNet;
  33. using Image = System.Drawing.Image;
  34. namespace OpenSim.Region.Physics.Meshing
  35. {
  36. // This functionality based on the XNA SculptPreview by John Hurliman.
  37. public class SculptMesh : Mesh
  38. {
  39. Image idata = null;
  40. Bitmap bLOD = null;
  41. Bitmap bBitmap = null;
  42. Vertex northpole = new Vertex(0, 0, 0);
  43. Vertex southpole = new Vertex(0, 0, 0);
  44. private int lod = 32;
  45. private const float RANGE = 128.0f;
  46. public SculptMesh(byte[] jpegData, float _lod)
  47. {
  48. if (_lod == 2f || _lod == 4f || _lod == 8f || _lod == 16f || _lod == 32f || _lod == 64f)
  49. lod = (int)_lod;
  50. try
  51. {
  52. idata = OpenJPEG.DecodeToImage(jpegData);
  53. //int i = 0;
  54. //i = i / i;
  55. }
  56. catch (Exception)
  57. {
  58. System.Console.WriteLine("[PHYSICS]: Unable to generate a Sculpty physics proxy. Sculpty texture decode failed!");
  59. return;
  60. }
  61. if (idata != null)
  62. {
  63. bBitmap = new Bitmap(idata);
  64. if (bBitmap.Width == bBitmap.Height)
  65. {
  66. DoLOD();
  67. LoadPoles();
  68. processSculptTexture();
  69. bLOD.Dispose();
  70. bBitmap.Dispose();
  71. idata.Dispose();
  72. }
  73. }
  74. }
  75. private Vertex ColorToVertex(Color input)
  76. {
  77. return new Vertex(
  78. ((float)input.R - 128) / RANGE,
  79. ((float)input.G - 128) / RANGE,
  80. ((float)input.B - 128) / RANGE);
  81. }
  82. private void LoadPoles()
  83. {
  84. northpole = new Vertex(0, 0, 0);
  85. for (int x = 0; x < bLOD.Width; x++)
  86. {
  87. northpole += ColorToVertex(GetPixel(0, 0));
  88. }
  89. northpole /= bLOD.Width;
  90. southpole = new Vertex(0, 0, 0);
  91. for (int x = 0; x < bLOD.Width; x++)
  92. {
  93. //System.Console.WriteLine("Height: " + bLOD.Height.ToString());
  94. southpole += ColorToVertex(GetPixel(bLOD.Height - 1, (bLOD.Height - 1)));
  95. }
  96. southpole /= bBitmap.Width;
  97. }
  98. private Color GetPixel(int x, int y)
  99. {
  100. return bLOD.GetPixel(x, y);
  101. }
  102. public int LOD
  103. {
  104. get
  105. {
  106. return (int)Math.Log(Scale, 2);
  107. }
  108. set
  109. {
  110. int power = value;
  111. if (power == 0)
  112. power = 6;
  113. if (power < 2)
  114. power = 2;
  115. if (power > 9)
  116. power = 9;
  117. int t = (int)Math.Pow(2, power);
  118. if (t != Scale)
  119. {
  120. lod = t;
  121. }
  122. }
  123. }
  124. public int Scale
  125. {
  126. get
  127. {
  128. return lod;
  129. }
  130. }
  131. private void DoLOD()
  132. {
  133. int x_max = Math.Min(Scale, bBitmap.Width);
  134. int y_max = Math.Min(Scale, bBitmap.Height);
  135. if (bBitmap.Width == x_max && bBitmap.Height == y_max)
  136. bLOD = bBitmap;
  137. else if (bLOD == null || x_max != bLOD.Width || y_max != bLOD.Height)//don't resize if you don't need to.
  138. {
  139. System.Drawing.Bitmap tile = new System.Drawing.Bitmap(bBitmap.Width * 2, bBitmap.Height, PixelFormat.Format24bppRgb);
  140. System.Drawing.Bitmap tile_LOD = new System.Drawing.Bitmap(x_max * 2, y_max, PixelFormat.Format24bppRgb);
  141. bLOD = new System.Drawing.Bitmap(x_max, y_max, PixelFormat.Format24bppRgb);
  142. bLOD.SetResolution(bBitmap.HorizontalResolution, bBitmap.VerticalResolution);
  143. System.Drawing.Graphics grPhoto = System.Drawing.Graphics.FromImage(tile);
  144. grPhoto.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;
  145. grPhoto.DrawImage(bBitmap,
  146. new System.Drawing.Rectangle(0, 0, bBitmap.Width / 2, bBitmap.Height),
  147. new System.Drawing.Rectangle(bBitmap.Width / 2, 0, bBitmap.Width / 2, bBitmap.Height),
  148. System.Drawing.GraphicsUnit.Pixel);
  149. grPhoto.DrawImage(bBitmap,
  150. new System.Drawing.Rectangle((3 * bBitmap.Width) / 2, 0, bBitmap.Width / 2, bBitmap.Height),
  151. new System.Drawing.Rectangle(0, 0, bBitmap.Width / 2, bBitmap.Height),
  152. System.Drawing.GraphicsUnit.Pixel);
  153. grPhoto.DrawImage(bBitmap,
  154. new System.Drawing.Rectangle(bBitmap.Width / 2, 0, bBitmap.Width, bBitmap.Height),
  155. new System.Drawing.Rectangle(0, 0, bBitmap.Width, bBitmap.Height),
  156. System.Drawing.GraphicsUnit.Pixel);
  157. grPhoto = System.Drawing.Graphics.FromImage(tile_LOD);
  158. grPhoto.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBilinear;
  159. grPhoto.DrawImage(tile,
  160. new System.Drawing.Rectangle(0, 0, tile_LOD.Width, tile_LOD.Height),
  161. new System.Drawing.Rectangle(0, 0, tile.Width, tile.Height),
  162. System.Drawing.GraphicsUnit.Pixel);
  163. grPhoto = System.Drawing.Graphics.FromImage(bLOD);
  164. grPhoto.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;
  165. grPhoto.DrawImage(tile_LOD,
  166. new System.Drawing.Rectangle(0, 0, bLOD.Width, bLOD.Height),
  167. new System.Drawing.Rectangle(tile_LOD.Width / 4, 0, tile_LOD.Width / 2, tile_LOD.Height),
  168. System.Drawing.GraphicsUnit.Pixel);
  169. grPhoto.Dispose();
  170. tile_LOD.Dispose();
  171. tile.Dispose();
  172. }
  173. }
  174. public void clearStuff()
  175. {
  176. this.triangles.Clear();
  177. this.vertices.Clear();
  178. normals = new float[0];
  179. }
  180. public void processSculptTexture()
  181. {
  182. int x_max = Math.Min(Scale, bBitmap.Width);
  183. int y_max = Math.Min(Scale, bBitmap.Height);
  184. int COLUMNS = x_max + 1;
  185. Vertex[] sVertices = new Vertex[COLUMNS * y_max];
  186. //float[] indices = new float[COLUMNS * (y_max - 1) * 6];
  187. for (int y = 0; y < y_max; y++)
  188. {
  189. for (int x = 0; x < x_max; x++)
  190. {
  191. // Create the vertex
  192. Vertex v1 = new Vertex(0,0,0);
  193. // Create a vertex position from the RGB channels in the current pixel
  194. int ypos = y * bLOD.Width;
  195. if (y == 0)
  196. {
  197. v1 = northpole;
  198. }
  199. else if (y == y_max - 1)
  200. {
  201. v1 = southpole;
  202. }
  203. else
  204. {
  205. v1 = ColorToVertex(GetPixel(x, y));
  206. }
  207. // Add the vertex for use later
  208. if (!vertices.Contains(v1))
  209. Add(v1);
  210. sVertices[y * COLUMNS + x] = v1;
  211. //System.Console.WriteLine("adding: " + v1.ToString());
  212. }
  213. //Vertex tempVertex = vertices[y * COLUMNS];
  214. // sVertices[y * COLUMNS + x_max] = tempVertex;
  215. }
  216. // Create the Triangles
  217. //int i = 0;
  218. for (int y = 0; y < y_max - 1; y++)
  219. {
  220. int x;
  221. for (x = 0; x < x_max; x++)
  222. {
  223. Vertex vt11 = sVertices[(y * COLUMNS + x)];
  224. Vertex vt12 = sVertices[(y * COLUMNS + (x + 1))];
  225. Vertex vt13 = sVertices[((y + 1) * COLUMNS + (x + 1))];
  226. if (vt11 != null && vt12 != null && vt13 != null)
  227. {
  228. if (vt11 != vt12 && vt11 != vt13 && vt12 != vt13)
  229. {
  230. Triangle tri1 = new Triangle(vt11, vt12, vt13);
  231. //indices[i++] = (ushort)(y * COLUMNS + x);
  232. //indices[i++] = (ushort)(y * COLUMNS + (x + 1));
  233. //indices[i++] = (ushort)((y + 1) * COLUMNS + (x + 1));
  234. Add(tri1);
  235. }
  236. }
  237. Vertex vt21 = sVertices[(y * COLUMNS + x)];
  238. Vertex vt22 = sVertices[((y + 1) * COLUMNS + (x + 1))];
  239. Vertex vt23 = sVertices[((y + 1) * COLUMNS + x)];
  240. if (vt21 != null && vt22 != null && vt23 != null)
  241. {
  242. if (vt21.Equals(vt22, 0.022f) || vt21.Equals(vt23, 0.022f) || vt22.Equals(vt23, 0.022f))
  243. {
  244. }
  245. else
  246. {
  247. Triangle tri2 = new Triangle(vt21, vt22, vt23);
  248. //indices[i++] = (ushort)(y * COLUMNS + x);
  249. //indices[i++] = (ushort)((y + 1) * COLUMNS + (x + 1));
  250. //indices[i++] = (ushort)((y + 1) * COLUMNS + x);
  251. Add(tri2);
  252. }
  253. }
  254. }
  255. //Vertex vt31 = sVertices[(y * x_max + x)];
  256. //Vertex vt32 = sVertices[(y * x_max + 0)];
  257. //Vertex vt33 = sVertices[((y + 1) * x_max + 0)];
  258. //if (vt31 != null && vt32 != null && vt33 != null)
  259. //{
  260. //if (vt31.Equals(vt32, 0.022f) || vt31.Equals(vt33, 0.022f) || vt32.Equals(vt33, 0.022f))
  261. //{
  262. //}
  263. //else
  264. //{
  265. //Triangle tri3 = new Triangle(vt31, vt32, vt33);
  266. // Wrap the last cell in the row around
  267. //indices[i++] = (ushort)(y * x_max + x); //a
  268. //indices[i++] = (ushort)(y * x_max + 0); //b
  269. //indices[i++] = (ushort)((y + 1) * x_max + 0); //c
  270. //Add(tri3);
  271. // }
  272. //}
  273. //Vertex vt41 = sVertices[(y * x_max + x)];
  274. //Vertex vt42 = sVertices[((y + 1) * x_max + 0)];
  275. //Vertex vt43 = sVertices[((y + 1) * x_max + x)];
  276. //if (vt41 != null && vt42 != null && vt43 != null)
  277. //{
  278. //if (vt41.Equals(vt42, 0.022f) || vt31.Equals(vt43, 0.022f) || vt32.Equals(vt43, 0.022f))
  279. //{
  280. //}
  281. // else
  282. // {
  283. //Triangle tri4 = new Triangle(vt41, vt42, vt43);
  284. //indices[i++] = (ushort)(y * x_max + x); //a
  285. //indices[i++] = (ushort)((y + 1) * x_max + 0); //b
  286. //indices[i++] = (ushort)((y + 1) * x_max + x); //c
  287. //Add(tri4);
  288. //}
  289. //}
  290. }
  291. }
  292. }
  293. }