Annotation of /trunk/DefaultRenderer/DefaultPrimControl.cs
Parent Directory
|
Revision Log
Revision 63 - (view) (download)
| 1 : | albert | 60 | using System; |
| 2 : | using System.Collections.Generic; | ||
| 3 : | using System.Linq; | ||
| 4 : | using System.Text; | ||
| 5 : | using Xenki.Framework; | ||
| 6 : | using System.Windows.Media.Media3D; | ||
| 7 : | using OpenMetaverse; | ||
| 8 : | using OpenMetaverse.Rendering; | ||
| 9 : | using Quaternion = System.Windows.Media.Media3D.Quaternion; | ||
| 10 : | using Material = System.Windows.Media.Media3D.Material; | ||
| 11 : | |||
| 12 : | namespace Xenki.DefaultRenderer | ||
| 13 : | { | ||
| 14 : | public delegate void OnNewPrimitive(Visual3D visualPrim); | ||
| 15 : | |||
| 16 : | public class DefaultPrimControl | ||
| 17 : | { | ||
| 18 : | albert | 63 | //private GridClient m_Client; |
| 19 : | //private IRendering m_mesher; | ||
| 20 : | //public IRendering Mesher | ||
| 21 : | //{ | ||
| 22 : | // set { m_mesher = value; } | ||
| 23 : | //} | ||
| 24 : | //public GridClient Client | ||
| 25 : | //{ | ||
| 26 : | // get { return m_Client; } | ||
| 27 : | // set { if (value != null) InitializeClient(value); } | ||
| 28 : | //} | ||
| 29 : | albert | 60 | |
| 30 : | albert | 63 | //private void InitializeClient(GridClient value) |
| 31 : | //{ | ||
| 32 : | // m_Client = value; | ||
| 33 : | // m_Client.Objects.OnNewPrim += new ObjectManager.NewPrimCallback(Objects_OnNewPrim); | ||
| 34 : | //} | ||
| 35 : | albert | 60 | |
| 36 : | albert | 63 | //void Objects_OnNewPrim(Simulator simulator, Primitive prim, ulong regionHandle, ushort timeDilation) |
| 37 : | //{ | ||
| 38 : | // Visual3D visualPrim = CreateModel(prim); | ||
| 39 : | // bool waitParent = true; | ||
| 40 : | // lock (m_models) | ||
| 41 : | // { | ||
| 42 : | // if (m_models.ContainsKey(prim.LocalID)) | ||
| 43 : | // m_models.Remove(prim.LocalID); | ||
| 44 : | // m_models.Add(prim.LocalID, visualPrim); | ||
| 45 : | albert | 60 | |
| 46 : | albert | 63 | // if (prim.ParentID == 0) |
| 47 : | // waitParent = false; | ||
| 48 : | // else if (m_models.ContainsKey(prim.ParentID)) | ||
| 49 : | // waitParent = false; | ||
| 50 : | // } | ||
| 51 : | // lock (m_PrimModelsInScene) | ||
| 52 : | // { | ||
| 53 : | // if (false == m_PrimModelsInScene.ContainsKey(prim.LocalID)) | ||
| 54 : | // m_PrimModelsInScene.Add(prim.LocalID, prim); | ||
| 55 : | // } | ||
| 56 : | // if (waitParent == false)//notify Viewport to add visual | ||
| 57 : | // if (onNewPrimitive != null) | ||
| 58 : | // onNewPrimitive(visualPrim); | ||
| 59 : | //} | ||
| 60 : | albert | 60 | |
| 61 : | private Visual3D CreateModel(Primitive data) | ||
| 62 : | { | ||
| 63 : | Visual3D model = null; | ||
| 64 : | //SimpleMesh basic = m_mesher.GenerateSimpleMesh(data, DetailLevel.Medium); | ||
| 65 : | // lock (m_primitiveData.PrimModelsInScene) | ||
| 66 : | // if (m_primitiveData.PrimModelsInScene.ContainsKey(data.ParentID)) | ||
| 67 : | // { | ||
| 68 : | // Primitive parent = m_primitiveData.PrimModelsInScene[data.ParentID]; | ||
| 69 : | // data.Position *= parent.Rotation; | ||
| 70 : | // data.Position += parent.Position; | ||
| 71 : | // } | ||
| 72 : | |||
| 73 : | // mesh = new MeshGeometry3D(); | ||
| 74 : | // triangleModel = new GeometryModel3D(); | ||
| 75 : | // foreach (Vertex v in basic.Vertices) | ||
| 76 : | // { | ||
| 77 : | // Vector3 pos = v.Position; | ||
| 78 : | // pos *= data.Scale; | ||
| 79 : | // pos *= data.Rotation;//there's something wrong about rotation | ||
| 80 : | // pos += data.Position; | ||
| 81 : | // Point3D d = new Point3D(pos.X, pos.Y, pos.Z); | ||
| 82 : | // mesh.Positions.Add(d); | ||
| 83 : | // // These normals dont appear to be working for some reason, | ||
| 84 : | // // however the auto-generated normals appear adequate for now. | ||
| 85 : | // //Vector3D normal = new Vector3D(v.Normal.X, v.Normal.Y, v.Normal.Z); | ||
| 86 : | // //mesh.Normals.Add(normal); | ||
| 87 : | // double x = (v.Position.Length() == float.Parse("0")) ? 0 : (v.Position.X / v.Position.Length()); | ||
| 88 : | // double y = (v.Position.Length() == float.Parse("0")) ? 0 : (v.Position.Y / v.Position.Length()); | ||
| 89 : | |||
| 90 : | // mesh.TextureCoordinates.Add(new Point(x, y)); | ||
| 91 : | // } | ||
| 92 : | |||
| 93 : | // foreach (ushort ind in basic.Indices) | ||
| 94 : | // { | ||
| 95 : | // mesh.TriangleIndices.Add(ind); | ||
| 96 : | // } | ||
| 97 : | |||
| 98 : | // triangleModel = PatchTexture(data, mesh, triangleModel); | ||
| 99 : | |||
| 100 : | // model = new ModelVisual3D(); | ||
| 101 : | // model.Content = triangleModel; | ||
| 102 : | // model.SetValue(dep, data.LocalID); | ||
| 103 : | |||
| 104 : | // m_primitiveData.AddNewPrimitivePrimData(data); | ||
| 105 : | // m_primitiveData.RemoveWaitingParentPrimitive(data.LocalID); | ||
| 106 : | |||
| 107 : | return model; | ||
| 108 : | } | ||
| 109 : | |||
| 110 : | public event OnNewPrimitive onNewPrimitive; | ||
| 111 : | |||
| 112 : | //data | ||
| 113 : | private readonly Dictionary<uint, Visual3D> m_models = new Dictionary<uint, Visual3D>(); | ||
| 114 : | private readonly Dictionary<uint, Primitive> m_PrimModelsInScene = new Dictionary<uint, Primitive>(); | ||
| 115 : | |||
| 116 : | //key:children key value:parent | ||
| 117 : | private readonly Dictionary<uint, uint> primitiveParent = new Dictionary<uint, uint>(); | ||
| 118 : | |||
| 119 : | //private readonly Dictionary<uint, Vector3> m_parentPositions = new Dictionary<uint, Vector3>(); | ||
| 120 : | private readonly Dictionary<uint, Primitive> m_modelsWatingParent = new Dictionary<uint, Primitive>(); | ||
| 121 : | |||
| 122 : | private readonly Dictionary<uint, UUID> m_VisualNeedTextures = new Dictionary<uint, UUID>(); | ||
| 123 : | |||
| 124 : | private readonly Dictionary<UUID, Material> m_materials = new Dictionary<UUID, Material>(); | ||
| 125 : | |||
| 126 : | public Dictionary<uint, Visual3D> ModelsInScene | ||
| 127 : | { | ||
| 128 : | get { return m_models; } | ||
| 129 : | } | ||
| 130 : | public Dictionary<uint, Primitive> PrimModelsInScene | ||
| 131 : | { | ||
| 132 : | get { return m_PrimModelsInScene; } | ||
| 133 : | } | ||
| 134 : | //public Dictionary<uint, Vector3> ModelsParentPositions | ||
| 135 : | //{ | ||
| 136 : | // get { return m_parentPositions; } | ||
| 137 : | //} | ||
| 138 : | public Dictionary<uint, Primitive> ModelsWaitingParent | ||
| 139 : | { | ||
| 140 : | get { return m_modelsWatingParent; } | ||
| 141 : | } | ||
| 142 : | |||
| 143 : | public Dictionary<uint, UUID> UnAttachedTextures | ||
| 144 : | { | ||
| 145 : | get { return m_VisualNeedTextures; } | ||
| 146 : | } | ||
| 147 : | |||
| 148 : | private static readonly DefaultPrimControl singleton = new DefaultPrimControl(); | ||
| 149 : | |||
| 150 : | public static DefaultPrimControl SingleSceneDataManager() | ||
| 151 : | { | ||
| 152 : | return singleton; | ||
| 153 : | } | ||
| 154 : | private DefaultPrimControl(){ } | ||
| 155 : | |||
| 156 : | public void AddNewPrimitive(uint id,Visual3D visual,uint parentID)// Primitive prim) | ||
| 157 : | { | ||
| 158 : | lock (m_models) | ||
| 159 : | { | ||
| 160 : | m_models[id] = visual; | ||
| 161 : | primitiveParent[id] = parentID; | ||
| 162 : | } | ||
| 163 : | } | ||
| 164 : | public bool SceneContainVisual(uint primid) | ||
| 165 : | { | ||
| 166 : | bool b = false; | ||
| 167 : | lock (m_models) | ||
| 168 : | { | ||
| 169 : | b = m_models.ContainsKey(primid); | ||
| 170 : | } | ||
| 171 : | |||
| 172 : | return b; | ||
| 173 : | } | ||
| 174 : | public Visual3D GetVisualInScene(uint id) | ||
| 175 : | { | ||
| 176 : | Visual3D v = null; | ||
| 177 : | lock (m_models) | ||
| 178 : | if (m_models.ContainsKey(id)) | ||
| 179 : | v = m_models[id]; | ||
| 180 : | |||
| 181 : | return v; | ||
| 182 : | } | ||
| 183 : | |||
| 184 : | public void RemoveVisualInScene(uint id) | ||
| 185 : | { | ||
| 186 : | lock (m_models) | ||
| 187 : | { | ||
| 188 : | m_models[id] = null; | ||
| 189 : | m_models.Remove(id); | ||
| 190 : | } | ||
| 191 : | } | ||
| 192 : | |||
| 193 : | public void AddNewPrimitivePrimData(Primitive data) | ||
| 194 : | { | ||
| 195 : | lock (m_PrimModelsInScene) | ||
| 196 : | { | ||
| 197 : | m_PrimModelsInScene[data.LocalID] = data; | ||
| 198 : | } | ||
| 199 : | } | ||
| 200 : | public void RemovePrimtivePrimData(uint id) | ||
| 201 : | { | ||
| 202 : | lock (m_PrimModelsInScene) | ||
| 203 : | m_PrimModelsInScene.Remove(id); | ||
| 204 : | } | ||
| 205 : | |||
| 206 : | |||
| 207 : | public void AddWaitingParentPrimitive(uint id, Primitive data) | ||
| 208 : | { | ||
| 209 : | lock (m_modelsWatingParent) | ||
| 210 : | { | ||
| 211 : | if (m_modelsWatingParent.ContainsKey(id)) | ||
| 212 : | m_modelsWatingParent.Remove(id); | ||
| 213 : | |||
| 214 : | m_modelsWatingParent.Add(id, data); | ||
| 215 : | } | ||
| 216 : | } | ||
| 217 : | public void RemoveWaitingParentPrimitive(uint id) | ||
| 218 : | { | ||
| 219 : | lock (m_modelsWatingParent) | ||
| 220 : | { | ||
| 221 : | m_modelsWatingParent.Remove(id); | ||
| 222 : | } | ||
| 223 : | } | ||
| 224 : | public List<Primitive> GetModelsWaitingParent(uint parentID) | ||
| 225 : | { | ||
| 226 : | List<Primitive> list = new List<Primitive>(); | ||
| 227 : | lock (m_modelsWatingParent) | ||
| 228 : | { | ||
| 229 : | foreach (Primitive child in m_modelsWatingParent.Values) | ||
| 230 : | { | ||
| 231 : | if (child.ParentID == 0) | ||
| 232 : | throw new Exception("goooooooood"); | ||
| 233 : | if (child.ParentID == parentID) | ||
| 234 : | list.Add(child); | ||
| 235 : | } | ||
| 236 : | } | ||
| 237 : | //lock (primitiveParent) | ||
| 238 : | //{ | ||
| 239 : | // if (primitiveParent.ContainsValue(parentID)) | ||
| 240 : | // { | ||
| 241 : | |||
| 242 : | // } | ||
| 243 : | //} | ||
| 244 : | return list; | ||
| 245 : | } | ||
| 246 : | |||
| 247 : | private uint? FindKeyByValue(Visual3D value) | ||
| 248 : | { | ||
| 249 : | uint? returnValue=null; | ||
| 250 : | lock (m_models) | ||
| 251 : | { | ||
| 252 : | foreach (KeyValuePair<uint, Visual3D> pair in m_models) | ||
| 253 : | { | ||
| 254 : | if (pair.Value.Equals(value)) | ||
| 255 : | returnValue = pair.Key; | ||
| 256 : | } | ||
| 257 : | } | ||
| 258 : | return returnValue; | ||
| 259 : | } | ||
| 260 : | |||
| 261 : | public uint? GetKeyInSceneByValue(Visual3D visual) | ||
| 262 : | { | ||
| 263 : | return FindKeyByValue(visual); | ||
| 264 : | } | ||
| 265 : | |||
| 266 : | public List<Visual3D> GetChildrenByVisual3D(Visual3D parent) | ||
| 267 : | { | ||
| 268 : | uint? key = FindKeyByValue(parent); | ||
| 269 : | if (key.HasValue == false) | ||
| 270 : | return new List<Visual3D>(); | ||
| 271 : | |||
| 272 : | List<Visual3D> children = new List<Visual3D>(); | ||
| 273 : | foreach (KeyValuePair<uint, uint> child in primitiveParent) | ||
| 274 : | { | ||
| 275 : | if (child.Value == key.Value) | ||
| 276 : | children.Add(m_models[child.Key]); | ||
| 277 : | } | ||
| 278 : | return children; | ||
| 279 : | } | ||
| 280 : | |||
| 281 : | public void AddUnAttachedTexture( uint model,UUID textureID) | ||
| 282 : | { | ||
| 283 : | lock (m_VisualNeedTextures) | ||
| 284 : | { | ||
| 285 : | if (m_VisualNeedTextures.ContainsKey(model) == true) | ||
| 286 : | m_VisualNeedTextures.Remove(model); | ||
| 287 : | m_VisualNeedTextures.Add(model, textureID); | ||
| 288 : | } | ||
| 289 : | } | ||
| 290 : | public void RemoveUnAttachedTexture(uint model) | ||
| 291 : | { | ||
| 292 : | lock (m_VisualNeedTextures) | ||
| 293 : | { | ||
| 294 : | if(m_VisualNeedTextures.ContainsKey(model)) | ||
| 295 : | m_VisualNeedTextures.Remove(model); | ||
| 296 : | } | ||
| 297 : | } | ||
| 298 : | |||
| 299 : | public List<Primitive> GetVisualByTextureID(UUID textureID) | ||
| 300 : | { | ||
| 301 : | List<Primitive> list = new List<Primitive>(); | ||
| 302 : | lock (m_VisualNeedTextures) | ||
| 303 : | { | ||
| 304 : | foreach (KeyValuePair<uint,UUID> pair in m_VisualNeedTextures) | ||
| 305 : | { | ||
| 306 : | if (pair.Value == textureID) | ||
| 307 : | { | ||
| 308 : | if ( m_modelsWatingParent.ContainsKey(pair.Key) == true) | ||
| 309 : | continue; | ||
| 310 : | |||
| 311 : | lock (m_PrimModelsInScene) | ||
| 312 : | { | ||
| 313 : | if (m_PrimModelsInScene.ContainsKey(pair.Key) == false) | ||
| 314 : | continue; | ||
| 315 : | Primitive prim = m_PrimModelsInScene[pair.Key]; | ||
| 316 : | if (list.Contains(prim) == true) | ||
| 317 : | list.Remove(prim); | ||
| 318 : | list.Add(prim); | ||
| 319 : | } | ||
| 320 : | } | ||
| 321 : | } | ||
| 322 : | } | ||
| 323 : | |||
| 324 : | return list; | ||
| 325 : | } | ||
| 326 : | |||
| 327 : | //public void AddModelsParentPositions(uint id, Vector3 pos) | ||
| 328 : | //{ | ||
| 329 : | // lock (m_parentPositions) | ||
| 330 : | // { | ||
| 331 : | // if (m_parentPositions.ContainsKey(id)) | ||
| 332 : | // m_parentPositions[id] = pos; | ||
| 333 : | // else | ||
| 334 : | // m_parentPositions.Add(id, pos); | ||
| 335 : | // } | ||
| 336 : | //} | ||
| 337 : | //public Vector3 GetModelsParentPosition(uint id) | ||
| 338 : | //{ | ||
| 339 : | // Vector3 vec = new Vector3(); | ||
| 340 : | // lock (m_parentPositions) | ||
| 341 : | // { | ||
| 342 : | // if (m_parentPositions.ContainsKey(id)) | ||
| 343 : | // vec = m_parentPositions[id]; | ||
| 344 : | // } | ||
| 345 : | // return vec; | ||
| 346 : | //} | ||
| 347 : | |||
| 348 : | public Primitive GetParentRotation(uint parentid) | ||
| 349 : | { | ||
| 350 : | uint temp =parentid; | ||
| 351 : | lock(m_PrimModelsInScene) | ||
| 352 : | while (m_PrimModelsInScene.ContainsKey(parentid)) | ||
| 353 : | { | ||
| 354 : | temp = parentid; | ||
| 355 : | parentid = m_PrimModelsInScene[parentid].ParentID; | ||
| 356 : | } | ||
| 357 : | |||
| 358 : | return m_PrimModelsInScene[temp]; | ||
| 359 : | } | ||
| 360 : | |||
| 361 : | public void AddNewMaterial2Cache(UUID textureID, System.Windows.Media.Media3D.Material material) | ||
| 362 : | { | ||
| 363 : | lock (m_materials) | ||
| 364 : | { | ||
| 365 : | if (m_materials.ContainsKey(textureID)==false) | ||
| 366 : | { | ||
| 367 : | m_materials.Add(textureID, material); | ||
| 368 : | } | ||
| 369 : | } | ||
| 370 : | } | ||
| 371 : | public Material GetMaterialFromCache(UUID textureID) | ||
| 372 : | { | ||
| 373 : | Material m=null; | ||
| 374 : | lock (m_materials) | ||
| 375 : | if (m_materials.ContainsKey(textureID)) | ||
| 376 : | m = m_materials[textureID]; | ||
| 377 : | |||
| 378 : | |||
| 379 : | return m; | ||
| 380 : | } | ||
| 381 : | public void RemoveMaterialFromCache(UUID textureID) | ||
| 382 : | { | ||
| 383 : | lock (m_materials) | ||
| 384 : | { | ||
| 385 : | m_materials.Remove(textureID); | ||
| 386 : | } | ||
| 387 : | } | ||
| 388 : | } | ||
| 389 : | } |
| ViewVC Help | |
| Powered by ViewVC 1.0.0 |

