PrimitiveBaseShape.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  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.Reflection;
  29. using System.Xml.Serialization;
  30. using libsecondlife;
  31. using log4net;
  32. namespace OpenSim.Framework
  33. {
  34. public enum ProfileShape : byte
  35. {
  36. Circle = 0,
  37. Square = 1,
  38. IsometricTriangle = 2,
  39. EquilateralTriangle = 3,
  40. RightTriangle = 4,
  41. HalfCircle = 5
  42. }
  43. public enum HollowShape : byte
  44. {
  45. Same = 0,
  46. Circle = 16,
  47. Square = 32,
  48. Triangle = 48
  49. }
  50. public enum PCodeEnum : byte
  51. {
  52. Primitive = 9,
  53. Avatar = 47,
  54. Grass = 95,
  55. NewTree = 111,
  56. ParticleSystem = 143,
  57. Tree = 255
  58. }
  59. public enum Extrusion : byte
  60. {
  61. Straight = 16,
  62. Curve1 = 32,
  63. Curve2 = 48,
  64. Flexible = 128
  65. }
  66. [Serializable]
  67. public class PrimitiveBaseShape
  68. {
  69. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  70. private static readonly LLObject.TextureEntry m_defaultTexture;
  71. private byte[] m_textureEntry;
  72. public ushort PathBegin;
  73. public byte PathCurve;
  74. public ushort PathEnd;
  75. public sbyte PathRadiusOffset;
  76. public byte PathRevolutions;
  77. public byte PathScaleX;
  78. public byte PathScaleY;
  79. public byte PathShearX;
  80. public byte PathShearY;
  81. public sbyte PathSkew;
  82. public sbyte PathTaperX;
  83. public sbyte PathTaperY;
  84. public sbyte PathTwist;
  85. public sbyte PathTwistBegin;
  86. public byte PCode;
  87. public ushort ProfileBegin;
  88. public byte ProfileCurve
  89. {
  90. get { return (byte)((byte)HollowShape | (byte)ProfileShape); }
  91. set
  92. {
  93. // Handle hollow shape component
  94. byte hollowShapeByte = (byte)(value & 0xf0);
  95. if (!Enum.IsDefined(typeof(HollowShape), hollowShapeByte))
  96. {
  97. m_log.WarnFormat(
  98. "[SHAPE]: Attempt to set a ProfileCurve with a hollow shape value of {0}, which isn't a valid enum. Replacing with default shape.",
  99. hollowShapeByte);
  100. this.HollowShape = HollowShape.Same;
  101. }
  102. else
  103. {
  104. this.HollowShape = (HollowShape)hollowShapeByte;
  105. }
  106. // Handle profile shape component
  107. byte profileShapeByte = (byte)(value & 0xf);
  108. if (!Enum.IsDefined(typeof(ProfileShape), profileShapeByte))
  109. {
  110. m_log.WarnFormat(
  111. "[SHAPE]: Attempt to set a ProfileCurve with a profile shape value of {0}, which isn't a valid enum. Replacing with square.",
  112. profileShapeByte);
  113. this.ProfileShape = ProfileShape.Square;
  114. }
  115. else
  116. {
  117. this.ProfileShape = (ProfileShape)profileShapeByte;
  118. }
  119. }
  120. }
  121. public ushort ProfileEnd;
  122. public ushort ProfileHollow;
  123. public LLVector3 Scale;
  124. public byte State;
  125. // Sculpted
  126. [XmlIgnore] public LLUUID SculptTexture = LLUUID.Zero;
  127. [XmlIgnore] public byte SculptType = (byte)0;
  128. [XmlIgnore] public byte[] SculptData = new byte[0];
  129. // Flexi
  130. [XmlIgnore] public int FlexiSoftness = 0;
  131. [XmlIgnore] public float FlexiTension = 0f;
  132. [XmlIgnore] public float FlexiDrag = 0f;
  133. [XmlIgnore] public float FlexiGravity = 0f;
  134. [XmlIgnore] public float FlexiWind = 0f;
  135. [XmlIgnore] public float FlexiForceX = 0f;
  136. [XmlIgnore] public float FlexiForceY = 0f;
  137. [XmlIgnore] public float FlexiForceZ = 0f;
  138. //Bright n sparkly
  139. [XmlIgnore] public float LightColorR = 0f;
  140. [XmlIgnore] public float LightColorG = 0f;
  141. [XmlIgnore] public float LightColorB = 0f;
  142. [XmlIgnore] public float LightColorA = 1f;
  143. [XmlIgnore] public float LightRadius = 0f;
  144. [XmlIgnore] public float LightCutoff = 0f;
  145. [XmlIgnore] public float LightFalloff = 0f;
  146. [XmlIgnore] public float LightIntensity = 1f;
  147. [XmlIgnore] public bool FlexiEntry = false;
  148. [XmlIgnore] public bool LightEntry = false;
  149. [XmlIgnore] public bool SculptEntry = false;
  150. static PrimitiveBaseShape()
  151. {
  152. m_defaultTexture =
  153. new LLObject.TextureEntry(new LLUUID("89556747-24cb-43ed-920b-47caed15465f"));
  154. }
  155. public PrimitiveBaseShape()
  156. {
  157. PCode = (byte) PCodeEnum.Primitive;
  158. ExtraParams = new byte[1];
  159. Textures = m_defaultTexture;
  160. }
  161. [XmlIgnore]
  162. public LLObject.TextureEntry Textures
  163. {
  164. get { return new LLObject.TextureEntry(m_textureEntry, 0, m_textureEntry.Length); }
  165. set { m_textureEntry = value.ToBytes(); }
  166. }
  167. public byte[] TextureEntry
  168. {
  169. get { return m_textureEntry; }
  170. set { m_textureEntry = value; }
  171. }
  172. public ProfileShape ProfileShape;
  173. public HollowShape HollowShape;
  174. public static PrimitiveBaseShape Default
  175. {
  176. get
  177. {
  178. PrimitiveBaseShape boxShape = CreateBox();
  179. boxShape.SetScale(0.5f);
  180. return boxShape;
  181. }
  182. }
  183. public static PrimitiveBaseShape Create()
  184. {
  185. PrimitiveBaseShape shape = new PrimitiveBaseShape();
  186. return shape;
  187. }
  188. public static PrimitiveBaseShape CreateBox()
  189. {
  190. PrimitiveBaseShape shape = Create();
  191. shape.PathCurve = (byte) Extrusion.Straight;
  192. shape.ProfileShape = ProfileShape.Square;
  193. shape.PathScaleX = 100;
  194. shape.PathScaleY = 100;
  195. return shape;
  196. }
  197. public static PrimitiveBaseShape CreateCylinder()
  198. {
  199. PrimitiveBaseShape shape = Create();
  200. shape.PathCurve = (byte) Extrusion.Curve1;
  201. shape.ProfileShape = ProfileShape.Square;
  202. shape.PathScaleX = 100;
  203. shape.PathScaleY = 100;
  204. return shape;
  205. }
  206. public void SetScale(float side)
  207. {
  208. Scale = new LLVector3(side, side, side);
  209. }
  210. public void SetHeigth(float heigth)
  211. {
  212. Scale.Z = heigth;
  213. }
  214. public void SetRadius(float radius)
  215. {
  216. Scale.X = Scale.Y = radius * 2f;
  217. }
  218. // TODO: void returns need to change of course
  219. public virtual void GetMesh()
  220. {
  221. }
  222. public PrimitiveBaseShape Copy()
  223. {
  224. return (PrimitiveBaseShape) MemberwiseClone();
  225. }
  226. public static PrimitiveBaseShape CreateCylinder(float radius, float heigth)
  227. {
  228. PrimitiveBaseShape shape = CreateCylinder();
  229. shape.SetHeigth(heigth);
  230. shape.SetRadius(radius);
  231. return shape;
  232. }
  233. public void SetPathRange(LLVector3 pathRange)
  234. {
  235. PathBegin = LLObject.PackBeginCut(pathRange.X);
  236. PathEnd = LLObject.PackEndCut(pathRange.Y);
  237. }
  238. public void SetSculptData(byte sculptType, LLUUID SculptTextureUUID)
  239. {
  240. SculptType = sculptType;
  241. SculptTexture = SculptTextureUUID;
  242. }
  243. public void SetProfileRange(LLVector3 profileRange)
  244. {
  245. ProfileBegin = LLObject.PackBeginCut(profileRange.X);
  246. ProfileEnd = LLObject.PackEndCut(profileRange.Y);
  247. }
  248. public byte[] ExtraParams
  249. {
  250. get
  251. {
  252. return ExtraParamsToBytes();
  253. }
  254. set
  255. {
  256. ReadInExtraParamsBytes(value);
  257. }
  258. }
  259. public byte[] ExtraParamsToBytes()
  260. {
  261. ushort FlexiEP = 0x10;
  262. ushort LightEP = 0x20;
  263. ushort SculptEP = 0x30;
  264. int i = 0;
  265. uint TotalBytesLength = 1; // ExtraParamsNum
  266. uint ExtraParamsNum = 0;
  267. if (FlexiEntry)
  268. {
  269. ExtraParamsNum++;
  270. TotalBytesLength += 16;// data
  271. TotalBytesLength += 2 + 4; // type
  272. }
  273. if (LightEntry)
  274. {
  275. ExtraParamsNum++;
  276. TotalBytesLength += 16;// data
  277. TotalBytesLength += 2 + 4; // type
  278. }
  279. if (SculptEntry)
  280. {
  281. ExtraParamsNum++;
  282. TotalBytesLength += 17;// data
  283. TotalBytesLength += 2 + 4; // type
  284. }
  285. byte[] returnbytes = new byte[TotalBytesLength];
  286. // uint paramlength = ExtraParamsNum;
  287. // Stick in the number of parameters
  288. returnbytes[i++] = (byte)ExtraParamsNum;
  289. if (FlexiEntry)
  290. {
  291. byte[] FlexiData = GetFlexiBytes();
  292. returnbytes[i++] = (byte)(FlexiEP % 256);
  293. returnbytes[i++] = (byte)((FlexiEP >> 8) % 256);
  294. returnbytes[i++] = (byte)(FlexiData.Length % 256);
  295. returnbytes[i++] = (byte)((FlexiData.Length >> 8) % 256);
  296. returnbytes[i++] = (byte)((FlexiData.Length >> 16) % 256);
  297. returnbytes[i++] = (byte)((FlexiData.Length >> 24) % 256);
  298. Array.Copy(FlexiData, 0, returnbytes, i, FlexiData.Length);
  299. i += FlexiData.Length;
  300. }
  301. if (LightEntry)
  302. {
  303. byte[] LightData = GetLightBytes();
  304. returnbytes[i++] = (byte)(LightEP % 256);
  305. returnbytes[i++] = (byte)((LightEP >> 8) % 256);
  306. returnbytes[i++] = (byte)(LightData.Length % 256);
  307. returnbytes[i++] = (byte)((LightData.Length >> 8) % 256);
  308. returnbytes[i++] = (byte)((LightData.Length >> 16) % 256);
  309. returnbytes[i++] = (byte)((LightData.Length >> 24) % 256);
  310. Array.Copy(LightData, 0, returnbytes, i, LightData.Length);
  311. i += LightData.Length;
  312. }
  313. if (SculptEntry)
  314. {
  315. byte[] SculptData = GetSculptBytes();
  316. returnbytes[i++] = (byte)(SculptEP % 256);
  317. returnbytes[i++] = (byte)((SculptEP >> 8) % 256);
  318. returnbytes[i++] = (byte)(SculptData.Length % 256);
  319. returnbytes[i++] = (byte)((SculptData.Length >> 8) % 256);
  320. returnbytes[i++] = (byte)((SculptData.Length >> 16) % 256);
  321. returnbytes[i++] = (byte)((SculptData.Length >> 24) % 256);
  322. Array.Copy(SculptData, 0, returnbytes, i, SculptData.Length);
  323. i += SculptData.Length;
  324. }
  325. if (!FlexiEntry && !LightEntry && !SculptEntry)
  326. {
  327. byte[] returnbyte = new byte[1];
  328. returnbyte[0] = 0;
  329. return returnbyte;
  330. }
  331. return returnbytes;
  332. //m_log.Info("[EXTRAPARAMS]: Length = " + m_shape.ExtraParams.Length.ToString());
  333. }
  334. public void ReadInUpdateExtraParam(ushort type, bool inUse, byte[] data)
  335. {
  336. const ushort FlexiEP = 0x10;
  337. const ushort LightEP = 0x20;
  338. const ushort SculptEP = 0x30;
  339. switch (type)
  340. {
  341. case FlexiEP:
  342. if (!inUse)
  343. {
  344. FlexiEntry = false;
  345. return;
  346. }
  347. ReadFlexiData(data, 0);
  348. break;
  349. case LightEP:
  350. if (!inUse)
  351. {
  352. LightEntry = false;
  353. return;
  354. }
  355. ReadLightData(data, 0);
  356. break;
  357. case SculptEP:
  358. if (!inUse)
  359. {
  360. SculptEntry = false;
  361. return;
  362. }
  363. ReadSculptData(data, 0);
  364. break;
  365. }
  366. }
  367. public void ReadInExtraParamsBytes(byte[] data)
  368. {
  369. const ushort FlexiEP = 0x10;
  370. const ushort LightEP = 0x20;
  371. const ushort SculptEP = 0x30;
  372. bool lGotFlexi = false;
  373. bool lGotLight = false;
  374. bool lGotSculpt = false;
  375. int i = 0;
  376. byte extraParamCount = 0;
  377. if (data.Length > 0)
  378. {
  379. extraParamCount = data[i++];
  380. }
  381. for (int k = 0; k < extraParamCount; k++)
  382. {
  383. ushort epType = Helpers.BytesToUInt16(data, i);
  384. i += 2;
  385. // uint paramLength = Helpers.BytesToUIntBig(data, i);
  386. i += 4;
  387. switch (epType)
  388. {
  389. case FlexiEP:
  390. ReadFlexiData(data, i);
  391. i += 16;
  392. lGotFlexi = true;
  393. break;
  394. case LightEP:
  395. ReadLightData(data, i);
  396. i += 16;
  397. lGotLight = true;
  398. break;
  399. case SculptEP:
  400. ReadSculptData(data, i);
  401. i += 17;
  402. lGotSculpt = true;
  403. break;
  404. }
  405. }
  406. if (!lGotFlexi)
  407. FlexiEntry = false;
  408. if (!lGotLight)
  409. LightEntry = false;
  410. if (!lGotSculpt)
  411. SculptEntry = false;
  412. }
  413. public void ReadSculptData(byte[] data, int pos)
  414. {
  415. byte[] SculptTextureUUID = new byte[16];
  416. LLUUID SculptUUID = LLUUID.Zero;
  417. byte SculptTypel = data[16+pos];
  418. if (data.Length+pos >= 17)
  419. {
  420. SculptEntry = true;
  421. SculptTextureUUID = new byte[16];
  422. SculptTypel = data[16 + pos];
  423. Array.Copy(data, pos, SculptTextureUUID,0, 16);
  424. SculptUUID = new LLUUID(SculptTextureUUID, 0);
  425. }
  426. else
  427. {
  428. SculptEntry = false;
  429. SculptUUID = LLUUID.Zero;
  430. SculptTypel = 0x00;
  431. }
  432. if (SculptEntry)
  433. {
  434. if (SculptType != (byte)1 && SculptType != (byte)2 && SculptType != (byte)3 && SculptType != (byte)4)
  435. SculptType = 4;
  436. }
  437. SculptTexture = SculptUUID;
  438. SculptType = SculptTypel;
  439. //m_log.Info("[SCULPT]:" + SculptUUID.ToString());
  440. }
  441. public byte[] GetSculptBytes()
  442. {
  443. byte[] data = new byte[17];
  444. SculptTexture.GetBytes().CopyTo(data, 0);
  445. data[16] = (byte)SculptType;
  446. return data;
  447. }
  448. public void ReadFlexiData(byte[] data, int pos)
  449. {
  450. if (data.Length-pos >= 16)
  451. {
  452. FlexiEntry = true;
  453. FlexiSoftness = ((data[pos] & 0x80) >> 6) | ((data[pos + 1] & 0x80) >> 7);
  454. FlexiTension = (float)(data[pos++] & 0x7F) / 10.0f;
  455. FlexiDrag = (float)(data[pos++] & 0x7F) / 10.0f;
  456. FlexiGravity = (float)(data[pos++] / 10.0f) - 10.0f;
  457. FlexiWind = (float)data[pos++] / 10.0f;
  458. LLVector3 lForce = new LLVector3(data, pos);
  459. FlexiForceX = lForce.X;
  460. FlexiForceY = lForce.Y;
  461. FlexiForceZ = lForce.Z;
  462. }
  463. else
  464. {
  465. FlexiEntry = false;
  466. FlexiSoftness = 0;
  467. FlexiTension = 0.0f;
  468. FlexiDrag = 0.0f;
  469. FlexiGravity = 0.0f;
  470. FlexiWind = 0.0f;
  471. FlexiForceX = 0f;
  472. FlexiForceY = 0f;
  473. FlexiForceZ = 0f;
  474. }
  475. }
  476. public byte[] GetFlexiBytes()
  477. {
  478. byte[] data = new byte[16];
  479. int i = 0;
  480. // Softness is packed in the upper bits of tension and drag
  481. data[i] = (byte)((FlexiSoftness & 2) << 6);
  482. data[i + 1] = (byte)((FlexiSoftness & 1) << 7);
  483. data[i++] |= (byte)((byte)(FlexiTension * 10.01f) & 0x7F);
  484. data[i++] |= (byte)((byte)(FlexiDrag * 10.01f) & 0x7F);
  485. data[i++] = (byte)((FlexiGravity + 10.0f) * 10.01f);
  486. data[i++] = (byte)(FlexiWind * 10.01f);
  487. LLVector3 lForce = new LLVector3(FlexiForceX, FlexiForceY, FlexiForceZ);
  488. lForce.GetBytes().CopyTo(data, i);
  489. return data;
  490. }
  491. public void ReadLightData(byte[] data, int pos)
  492. {
  493. if (data.Length - pos >= 16)
  494. {
  495. LightEntry = true;
  496. LLColor lColor = new LLColor(data, pos, false);
  497. LightIntensity = lColor.A;
  498. LightColorA = 1f;
  499. LightColorR = lColor.R;
  500. LightColorG = lColor.G;
  501. LightColorB = lColor.B;
  502. LightRadius = Helpers.BytesToFloat(data, pos + 4);
  503. LightCutoff = Helpers.BytesToFloat(data, pos + 8);
  504. LightFalloff = Helpers.BytesToFloat(data, pos + 12);
  505. }
  506. else
  507. {
  508. LightEntry = false;
  509. LightColorA = 1f;
  510. LightColorR = 0f;
  511. LightColorG = 0f;
  512. LightColorB = 0f;
  513. LightRadius = 0f;
  514. LightCutoff = 0f;
  515. LightFalloff = 0f;
  516. LightIntensity = 0f;
  517. }
  518. }
  519. public byte[] GetLightBytes()
  520. {
  521. byte[] data = new byte[16];
  522. // Alpha channel in color is intensity
  523. LLColor tmpColor = new LLColor(LightColorR,LightColorG,LightColorB,LightIntensity);
  524. tmpColor.GetBytes().CopyTo(data, 0);
  525. Helpers.FloatToBytes(LightRadius).CopyTo(data, 4);
  526. Helpers.FloatToBytes(LightCutoff).CopyTo(data, 8);
  527. Helpers.FloatToBytes(LightFalloff).CopyTo(data, 12);
  528. return data;
  529. }
  530. }
  531. }