Annotation of /trunk/DefaultRenderer/DefaultRenderer.cs
Parent Directory
|
Revision Log
Revision 59 - (view) (download)
| 1 : | albert | 4 | using System; |
| 2 : | using System.Collections.Generic; | ||
| 3 : | using System.Windows; | ||
| 4 : | using System.Windows.Controls; | ||
| 5 : | using System.Windows.Media; | ||
| 6 : | using System.Windows.Media.Media3D; | ||
| 7 : | using System.Windows.Threading; | ||
| 8 : | using OpenMetaverse; | ||
| 9 : | using OpenMetaverse.Rendering; | ||
| 10 : | using Xenki.Framework; | ||
| 11 : | using Quaternion=System.Windows.Media.Media3D.Quaternion; | ||
| 12 : | albert | 35 | using System.IO; |
| 13 : | using System.Windows.Media.Imaging; | ||
| 14 : | albert | 36 | using OpenMetaverse.Imaging; |
| 15 : | albert | 38 | using Material = System.Windows.Media.Media3D.Material; |
| 16 : | albert | 58 | using System.Runtime.InteropServices; |
| 17 : | albert | 4 | |
| 18 : | albert | 52 | |
| 19 : | albert | 4 | namespace Xenki.DefaultRenderer |
| 20 : | { | ||
| 21 : | albert | 58 | |
| 22 : | albert | 4 | class DefaultRenderer : IRenderer |
| 23 : | { | ||
| 24 : | private readonly Viewport3D m_viewport; | ||
| 25 : | private readonly PerspectiveCamera m_camera; | ||
| 26 : | albert | 15 | private CameraControl m_cameracontrol; |
| 27 : | albert | 4 | private IRendering m_mesher; |
| 28 : | albert | 53 | private DefaultTexture m_textureManager; |
| 29 : | albert | 50 | private DefaultDataControl m_primitiveData; |
| 30 : | albert | 40 | |
| 31 : | albert | 53 | //public ITextureManage TextureManager |
| 32 : | //{ | ||
| 33 : | // set { m_textureManager = value; } | ||
| 34 : | //} | ||
| 35 : | albert | 40 | |
| 36 : | albert | 4 | private bool Broken; |
| 37 : | |||
| 38 : | private readonly Point3D[,] m_heightmapPoints = new Point3D[256,256]; | ||
| 39 : | private double[,] m_heightmap = new double[256,256]; | ||
| 40 : | private ModelVisual3D m_terrain; | ||
| 41 : | |||
| 42 : | private readonly DiffuseMaterial m_terrainMaterial; | ||
| 43 : | albert | 56 | private string textureImageFolderPath = |
| 44 : | System.IO.Path.Combine(System.Windows.Forms.Application.LocalUserAppDataPath, "texture"); | ||
| 45 : | albert | 53 | private GridClient m_gridclient; |
| 46 : | |||
| 47 : | private GridClient gridClient; | ||
| 48 : | |||
| 49 : | albert | 54 | public GridClient Gridclient |
| 50 : | albert | 53 | { |
| 51 : | get { return gridClient; } | ||
| 52 : | set | ||
| 53 : | { | ||
| 54 : | gridClient = value; | ||
| 55 : | albert | 54 | |
| 56 : | m_textureManager = DefaultTexture.SingleTextureManager(); | ||
| 57 : | m_textureManager.GridClient = Gridclient; | ||
| 58 : | m_textureManager.OnDownloadFinished = OnTextureDownloaded; | ||
| 59 : | m_textureManager.StartDownload(); | ||
| 60 : | albert | 53 | } |
| 61 : | } | ||
| 62 : | |||
| 63 : | albert | 58 | |
| 64 : | albert | 4 | public DefaultRenderer(Viewport3D m_viewport) |
| 65 : | { | ||
| 66 : | albert | 58 | |
| 67 : | albert | 4 | // Viewport |
| 68 : | this.m_viewport = m_viewport; | ||
| 69 : | albert | 14 | // m_viewport.ClipToBounds = false; |
| 70 : | albert | 4 | m_viewport.IsHitTestVisible = true; |
| 71 : | albert | 54 | |
| 72 : | |||
| 73 : | albert | 50 | //setup data store control |
| 74 : | albert | 54 | m_primitiveData = DefaultDataControl.SingleSceneDataManager(); |
| 75 : | albert | 4 | |
| 76 : | albert | 53 | |
| 77 : | albert | 4 | // Camera Issues |
| 78 : | Point3D CameraPos = new Point3D(0,0,50); | ||
| 79 : | Point3D CameraLook = new Point3D(128,128,10); | ||
| 80 : | |||
| 81 : | m_camera = new PerspectiveCamera(new Point3D(0, 0, 0), new Vector3D(0, 0, 0), | ||
| 82 : | new Vector3D(0, 0, 1), 90); | ||
| 83 : | albert | 53 | |
| 84 : | albert | 4 | m_camera.Position = CameraPos; |
| 85 : | m_camera.LookDirection = new Vector3D(CameraLook.X - m_camera.Position.X, CameraLook.Y - m_camera.Position.Y, CameraLook.Z - m_camera.Position.Z); | ||
| 86 : | |||
| 87 : | m_camera.FarPlaneDistance = 1024; | ||
| 88 : | m_camera.NearPlaneDistance = 0.01; | ||
| 89 : | m_viewport.Camera = m_camera; | ||
| 90 : | |||
| 91 : | albert | 26 | // Setup Camera Control |
| 92 : | albert | 50 | m_cameracontrol = new CameraControl(m_camera,m_primitiveData); |
| 93 : | albert | 15 | m_cameracontrol.EventSource = m_viewport; |
| 94 : | albert | 50 | |
| 95 : | albert | 4 | GradientBrush terGrad = new RadialGradientBrush(Colors.ForestGreen, Colors.SandyBrown); |
| 96 : | m_terrainMaterial = new DiffuseMaterial( | ||
| 97 : | terGrad); | ||
| 98 : | |||
| 99 : | // Scene Lighting | ||
| 100 : | ModelVisual3D lights = new ModelVisual3D(); | ||
| 101 : | lights.Content = new DirectionalLight(Colors.White, new Vector3D(-20, -30, -10)); | ||
| 102 : | |||
| 103 : | ModelVisual3D point = new ModelVisual3D(); | ||
| 104 : | PointLight pointLight = new PointLight(Colors.WhiteSmoke, new Point3D(128, 128, 80)); | ||
| 105 : | point.Content = pointLight; | ||
| 106 : | |||
| 107 : | ModelVisual3D ambient = new ModelVisual3D(); | ||
| 108 : | AmbientLight ambientLight = new AmbientLight(Color.FromArgb(0xFF, 0x40, 0x40, 0x40)); | ||
| 109 : | ambient.Content = ambientLight; | ||
| 110 : | |||
| 111 : | m_viewport.Children.Add(lights); | ||
| 112 : | m_viewport.Children.Add(point); | ||
| 113 : | m_viewport.Children.Add(ambient); | ||
| 114 : | albert | 9 | |
| 115 : | albert | 4 | // Terrain Base |
| 116 : | for(int x=0;x<256;x++) | ||
| 117 : | { | ||
| 118 : | for(int y=0;y<256;y++) | ||
| 119 : | { | ||
| 120 : | albert | 53 | m_heightmap[x, y] = 10.0; |
| 121 : | albert | 4 | } |
| 122 : | } | ||
| 123 : | |||
| 124 : | m_terrain = CreateTerrain(m_heightmap); | ||
| 125 : | m_viewport.Children.Add(m_terrain); | ||
| 126 : | |||
| 127 : | // Water Plane | ||
| 128 : | SolidColorBrush brush = new SolidColorBrush(Color.FromArgb(128, 0, 128, 255)); | ||
| 129 : | DiffuseMaterial material = new DiffuseMaterial(brush); | ||
| 130 : | |||
| 131 : | //m_viewport.Children.Add(DefaultUtil.CreateCube(new Point3D(0,0,0), new Point3D(256,256,20), | ||
| 132 : | // material)); | ||
| 133 : | } | ||
| 134 : | albert | 9 | |
| 135 : | albert | 4 | public IRendering Mesher |
| 136 : | { | ||
| 137 : | set { m_mesher = value; } | ||
| 138 : | } | ||
| 139 : | albert | 53 | |
| 140 : | void OnTextureDownloaded(UUID downLoadedTextureID, bool success) | ||
| 141 : | { | ||
| 142 : | if (success) | ||
| 143 : | { | ||
| 144 : | UpdatePrimTexture(downLoadedTextureID); | ||
| 145 : | } | ||
| 146 : | } | ||
| 147 : | |||
| 148 : | albert | 43 | public void AddPrimitive(uint id, Primitive data) |
| 149 : | albert | 4 | { |
| 150 : | if(Broken) | ||
| 151 : | return; | ||
| 152 : | |||
| 153 : | m_viewport.Dispatcher.Invoke( | ||
| 154 : | DispatcherPriority.Normal, | ||
| 155 : | new System.Windows.Forms.MethodInvoker | ||
| 156 : | (delegate | ||
| 157 : | { | ||
| 158 : | albert | 56 | try |
| 159 : | albert | 4 | { |
| 160 : | albert | 56 | bool WaitParentIn = true; |
| 161 : | if (data.ParentID == 0) | ||
| 162 : | albert | 4 | { |
| 163 : | albert | 56 | WaitParentIn = false; |
| 164 : | } | ||
| 165 : | else | ||
| 166 : | { | ||
| 167 : | if (m_primitiveData.PrimModelsInScene.ContainsKey(data.ParentID)) | ||
| 168 : | albert | 48 | WaitParentIn = false; |
| 169 : | albert | 56 | } |
| 170 : | |||
| 171 : | if (WaitParentIn == false) | ||
| 172 : | { | ||
| 173 : | if (m_primitiveData.SceneContainVisual(data.LocalID)) | ||
| 174 : | albert | 48 | { |
| 175 : | albert | 56 | Visual3D v = m_primitiveData.GetVisualInScene(data.LocalID); |
| 176 : | m_viewport.Children.Remove(v); | ||
| 177 : | albert | 43 | |
| 178 : | albert | 56 | m_primitiveData.RemoveVisualInScene(data.LocalID); |
| 179 : | v = null; | ||
| 180 : | albert | 4 | } |
| 181 : | albert | 48 | |
| 182 : | albert | 56 | ModelVisual3D model = CreateModel(data); |
| 183 : | albert | 53 | |
| 184 : | albert | 56 | m_primitiveData.AddNewPrimitive(data.LocalID, model, data.ParentID); |
| 185 : | m_viewport.Children.Add(model); | ||
| 186 : | albert | 53 | |
| 187 : | albert | 56 | List<Primitive> waiting = m_primitiveData.GetModelsWaitingParent(data.LocalID); |
| 188 : | albert | 45 | |
| 189 : | albert | 56 | foreach (Primitive child in waiting) |
| 190 : | { | ||
| 191 : | AddPrimitive(child.LocalID, child); | ||
| 192 : | albert | 33 | } |
| 193 : | |||
| 194 : | albert | 56 | waiting.Clear(); |
| 195 : | waiting = null; | ||
| 196 : | model = null; | ||
| 197 : | albert | 4 | } |
| 198 : | albert | 56 | else |
| 199 : | m_primitiveData.AddWaitingParentPrimitive(id, data); | ||
| 200 : | |||
| 201 : | albert | 4 | } |
| 202 : | albert | 56 | catch (Exception e) |
| 203 : | { | ||
| 204 : | MessageBox.Show(e.ToString()); | ||
| 205 : | Broken = true; | ||
| 206 : | } | ||
| 207 : | |||
| 208 : | albert | 4 | } |
| 209 : | ) | ||
| 210 : | ); | ||
| 211 : | } | ||
| 212 : | |||
| 213 : | private ModelVisual3D CreateTerrain(double[,] map) | ||
| 214 : | { | ||
| 215 : | MeshGeometry3D mesh = new MeshGeometry3D(); | ||
| 216 : | |||
| 217 : | for (int y = 0; y < 256; y++) | ||
| 218 : | { | ||
| 219 : | for (int x = 0; x < 256; x++) | ||
| 220 : | { | ||
| 221 : | m_heightmapPoints[y, x] = new Point3D(x, y, map[x, y]); | ||
| 222 : | mesh.Positions.Add(m_heightmapPoints[y, x]); | ||
| 223 : | |||
| 224 : | mesh.TextureCoordinates.Add(new Point(x/255.0, y/255.0)); | ||
| 225 : | } | ||
| 226 : | } | ||
| 227 : | |||
| 228 : | for(int x=0;x<255;x++) | ||
| 229 : | { | ||
| 230 : | for(int y=0;y<255;y++) | ||
| 231 : | { | ||
| 232 : | int p0 = (x*256) + y; | ||
| 233 : | int p1 = (x*256) + (y + 1); | ||
| 234 : | int p2 = ((x + 1)*256) + y; | ||
| 235 : | int p3 = ((x + 1)*256) + (y + 1); | ||
| 236 : | |||
| 237 : | // Δ 013 |\ | ||
| 238 : | mesh.TriangleIndices.Add(p1); //#0 | ||
| 239 : | |||
| 240 : | mesh.TriangleIndices.Add(p3); //#1 | ||
| 241 : | |||
| 242 : | mesh.TriangleIndices.Add(p2); //#3 | ||
| 243 : | |||
| 244 : | // Δ 023 | ||
| 245 : | mesh.TriangleIndices.Add(p0); //#0 | ||
| 246 : | |||
| 247 : | mesh.TriangleIndices.Add(p1); //#2 | ||
| 248 : | |||
| 249 : | mesh.TriangleIndices.Add(p2); //#3 | ||
| 250 : | |||
| 251 : | } | ||
| 252 : | } | ||
| 253 : | |||
| 254 : | GeometryModel3D triangleModel = new GeometryModel3D( | ||
| 255 : | mesh, m_terrainMaterial); | ||
| 256 : | |||
| 257 : | ModelVisual3D model = new ModelVisual3D(); | ||
| 258 : | model.Content = triangleModel; | ||
| 259 : | |||
| 260 : | return model; | ||
| 261 : | } | ||
| 262 : | albert | 56 | GeometryModel3D triangleModel; |
| 263 : | SimpleMesh basic; | ||
| 264 : | MeshGeometry3D mesh; | ||
| 265 : | ModelVisual3D model; | ||
| 266 : | albert | 4 | |
| 267 : | albert | 43 | private ModelVisual3D CreateModel(Primitive data) |
| 268 : | albert | 4 | { |
| 269 : | albert | 56 | return CreateModel_Vertices(data); |
| 270 : | //return CreateModelFromFaced(data); | ||
| 271 : | } | ||
| 272 : | albert | 48 | |
| 273 : | albert | 56 | private ModelVisual3D CreateModel_Vertices(Primitive data) |
| 274 : | { | ||
| 275 : | albert | 58 | basic = m_mesher.GenerateSimpleMesh(data, DetailLevel.Medium); |
| 276 : | |||
| 277 : | albert | 56 | lock (m_primitiveData.PrimModelsInScene) |
| 278 : | albert | 50 | if (m_primitiveData.PrimModelsInScene.ContainsKey(data.ParentID)) |
| 279 : | albert | 49 | { |
| 280 : | albert | 56 | Primitive parent = m_primitiveData.PrimModelsInScene[data.ParentID]; |
| 281 : | albert | 58 | data.Position *= parent.Rotation; |
| 282 : | data.Position += parent.Position; | ||
| 283 : | albert | 59 | |
| 284 : | albert | 58 | } |
| 285 : | |||
| 286 : | albert | 57 | mesh = new MeshGeometry3D(); |
| 287 : | triangleModel = new GeometryModel3D(); | ||
| 288 : | albert | 44 | foreach (Vertex v in basic.Vertices) |
| 289 : | { | ||
| 290 : | albert | 57 | Vector3 pos = v.Position; |
| 291 : | albert | 4 | |
| 292 : | albert | 59 | pos *= data.Scale; |
| 293 : | pos *= data.Rotation;//there's something wrong about rotation | ||
| 294 : | albert | 58 | pos += data.Position; |
| 295 : | albert | 59 | |
| 296 : | albert | 57 | Point3D d = new Point3D(pos.X, pos.Y, pos.Z); |
| 297 : | albert | 44 | mesh.Positions.Add(d); |
| 298 : | albert | 50 | |
| 299 : | albert | 44 | // These normals dont appear to be working for some reason, |
| 300 : | // however the auto-generated normals appear adequate for now. | ||
| 301 : | albert | 50 | //Vector3D normal = new Vector3D(v.Normal.X, v.Normal.Y, v.Normal.Z); |
| 302 : | //mesh.Normals.Add(normal); | ||
| 303 : | double x = (v.Position.Length() == float.Parse("0")) ? 0 : (v.Position.X / v.Position.Length()); | ||
| 304 : | double y = (v.Position.Length() == float.Parse("0")) ? 0 : (v.Position.Y / v.Position.Length()); | ||
| 305 : | albert | 45 | |
| 306 : | albert | 50 | mesh.TextureCoordinates.Add(new Point(x, y)); |
| 307 : | albert | 44 | } |
| 308 : | albert | 4 | |
| 309 : | albert | 44 | foreach (ushort ind in basic.Indices) |
| 310 : | { | ||
| 311 : | mesh.TriangleIndices.Add(ind); | ||
| 312 : | } | ||
| 313 : | albert | 53 | |
| 314 : | triangleModel = PatchTexture(data, mesh, triangleModel); | ||
| 315 : | |||
| 316 : | albert | 56 | model = new ModelVisual3D(); |
| 317 : | albert | 53 | model.Content = triangleModel; |
| 318 : | albert | 56 | model.SetValue(dep, data.LocalID); |
| 319 : | |||
| 320 : | albert | 59 | m_primitiveData.AddNewPrimitivePrimData(data); |
| 321 : | albert | 58 | m_primitiveData.RemoveWaitingParentPrimitive(data.LocalID); |
| 322 : | |||
| 323 : | albert | 53 | return model; |
| 324 : | } | ||
| 325 : | albert | 56 | DependencyProperty dep = DependencyProperty.Register("ID", typeof(uint), typeof(Visual3D)); |
| 326 : | albert | 53 | |
| 327 : | private GeometryModel3D PatchTexture(Primitive data, MeshGeometry3D mesh, GeometryModel3D triangleModel) | ||
| 328 : | { | ||
| 329 : | albert | 50 | Color col = Colors.YellowGreen; |
| 330 : | albert | 44 | |
| 331 : | albert | 50 | if (data.Textures != null && |
| 332 : | data.Textures.DefaultTexture != null && | ||
| 333 : | data.Textures.DefaultTexture.TextureID != null) | ||
| 334 : | { | ||
| 335 : | Material m = m_primitiveData.GetMaterialFromCache(data.Textures.DefaultTexture.TextureID); | ||
| 336 : | if (m != null) | ||
| 337 : | { | ||
| 338 : | triangleModel = new GeometryModel3D(mesh, m); | ||
| 339 : | // | ||
| 340 : | m_primitiveData.RemoveUnAttachedTexture(data.LocalID); | ||
| 341 : | } | ||
| 342 : | else | ||
| 343 : | { | ||
| 344 : | string bitmapPath = System.IO.Path.Combine(textureImageFolderPath, data.Textures.DefaultTexture.TextureID.ToString() + ".jpg"); | ||
| 345 : | |||
| 346 : | if (File.Exists(bitmapPath)) | ||
| 347 : | { | ||
| 348 : | ImageSource imagesource = new BitmapImage(new Uri(bitmapPath)); | ||
| 349 : | ImageBrush brush = new ImageBrush(imagesource); | ||
| 350 : | m = new DiffuseMaterial(brush); | ||
| 351 : | |||
| 352 : | albert | 53 | triangleModel = new GeometryModel3D(mesh, m); |
| 353 : | albert | 50 | // |
| 354 : | m_primitiveData.RemoveUnAttachedTexture(data.LocalID); | ||
| 355 : | |||
| 356 : | if (m != null) | ||
| 357 : | m_primitiveData.AddNewMaterial2Cache(data.Textures.DefaultTexture.TextureID, m); | ||
| 358 : | |||
| 359 : | albert | 53 | imagesource = null; |
| 360 : | brush = null; | ||
| 361 : | m = null; | ||
| 362 : | albert | 50 | } |
| 363 : | else | ||
| 364 : | { | ||
| 365 : | col = Color.FromScRgb(data.Textures.DefaultTexture.RGBA.A, | ||
| 366 : | data.Textures.DefaultTexture.RGBA.R, | ||
| 367 : | data.Textures.DefaultTexture.RGBA.G, | ||
| 368 : | data.Textures.DefaultTexture.RGBA.B); | ||
| 369 : | |||
| 370 : | triangleModel = new GeometryModel3D( | ||
| 371 : | mesh, new DiffuseMaterial(new SolidColorBrush(col))); | ||
| 372 : | |||
| 373 : | albert | 55 | m_textureManager.RequestTexture(data.Textures.DefaultTexture.TextureID); |
| 374 : | albert | 50 | m_primitiveData.AddUnAttachedTexture(data.LocalID, data.Textures.DefaultTexture.TextureID); |
| 375 : | } | ||
| 376 : | } | ||
| 377 : | } | ||
| 378 : | else | ||
| 379 : | { | ||
| 380 : | col = Color.FromScRgb(data.Textures.DefaultTexture.RGBA.A, | ||
| 381 : | data.Textures.DefaultTexture.RGBA.R, | ||
| 382 : | data.Textures.DefaultTexture.RGBA.G, | ||
| 383 : | data.Textures.DefaultTexture.RGBA.B); | ||
| 384 : | |||
| 385 : | triangleModel = new GeometryModel3D( | ||
| 386 : | mesh, new DiffuseMaterial(new SolidColorBrush(col))); | ||
| 387 : | |||
| 388 : | //ADD the visual to the needUpdateTexute list.when the texture downloaded update it. | ||
| 389 : | m_primitiveData.AddUnAttachedTexture(data.LocalID, data.Textures.DefaultTexture.TextureID); | ||
| 390 : | } | ||
| 391 : | albert | 53 | return triangleModel; |
| 392 : | albert | 50 | } |
| 393 : | |||
| 394 : | albert | 52 | //private void SaveTexture2Image(ImageDownload image) |
| 395 : | //{ | ||
| 396 : | // ManagedImage managedimg; | ||
| 397 : | // Image img; | ||
| 398 : | // //byte[] bytes = GetTextureDataByTextureID(downLoadedTextureID); | ||
| 399 : | |||
| 400 : | // if (OpenJPEG.DecodeToImage(image.AssetData, out managedimg, out img)) | ||
| 401 : | // { | ||
| 402 : | // Bitmap bitmap = new Bitmap(img); | ||
| 403 : | // ImageSource source = new BitmapMetadataBlob( | ||
| 404 : | // bitmap.Save(Path.Combine(bmpFilePath, image.ID.ToString() + ".jpg"), System.Drawing.Imaging.ImageFormat.Jpeg); | ||
| 405 : | |||
| 406 : | // bitmap.Dispose(); | ||
| 407 : | // bitmap = null; | ||
| 408 : | // } | ||
| 409 : | |||
| 410 : | // img.Dispose(); | ||
| 411 : | // img = null; | ||
| 412 : | // managedimg = null; | ||
| 413 : | //} | ||
| 414 : | |||
| 415 : | albert | 50 | ModelVisual3D CreateModelFromFaced(Primitive data) |
| 416 : | { | ||
| 417 : | FacetedMesh faceMesh = m_mesher.GenerateFacetedMesh(data, DetailLevel.Medium); | ||
| 418 : | |||
| 419 : | lock (m_primitiveData.PrimModelsInScene) | ||
| 420 : | if (m_primitiveData.PrimModelsInScene.ContainsKey(data.ParentID)) | ||
| 421 : | { | ||
| 422 : | //OpenMetaverse.Quaternion rotate = m_primitiveData.GetParentPosition(data.ParentID); | ||
| 423 : | //if (m_primitiveData.PrimModelsInScene.ContainsKey(m_primitiveData.PrimModelsInScene[data.ParentID].ParentID)) | ||
| 424 : | // data.Position *= m_primitiveData.PrimModelsInScene[m_primitiveData.PrimModelsInScene[data.ParentID].ParentID].Rotation; | ||
| 425 : | //else | ||
| 426 : | //data.Position *= rotate;// m_primitiveData.PrimModelsInScene[data.ParentID].Rotation; | ||
| 427 : | data.Position += m_primitiveData.PrimModelsInScene[data.ParentID].Position; | ||
| 428 : | } | ||
| 429 : | |||
| 430 : | // Translation | ||
| 431 : | TranslateTransform3D tt = new TranslateTransform3D( | ||
| 432 : | new Vector3D(data.Position.X, data.Position.Y, data.Position.Z) | ||
| 433 : | ); | ||
| 434 : | |||
| 435 : | Quaternion rot = new Quaternion(data.Rotation.X, data.Rotation.Y, data.Rotation.Z, data.Rotation.W); | ||
| 436 : | |||
| 437 : | RotateTransform3D rt = new RotateTransform3D( | ||
| 438 : | new AxisAngleRotation3D(rot.Axis, rot.Angle) | ||
| 439 : | ); | ||
| 440 : | |||
| 441 : | // Scale | ||
| 442 : | ScaleTransform3D st = new ScaleTransform3D( | ||
| 443 : | new Vector3D(data.Scale.X, data.Scale.Y, data.Scale.Z)); | ||
| 444 : | |||
| 445 : | |||
| 446 : | ModelVisual3D model = new ModelVisual3D(); | ||
| 447 : | albert | 43 | GeometryModel3D triangleModel = new GeometryModel3D(); |
| 448 : | albert | 50 | MeshGeometry3D mesh; |
| 449 : | ImageSource imagesource; | ||
| 450 : | ImageBrush brush; | ||
| 451 : | ModelVisual3D visual; | ||
| 452 : | albert | 43 | // Draw the prim faces |
| 453 : | for (int j = 0; j < faceMesh.Faces.Count; j++) | ||
| 454 : | albert | 4 | { |
| 455 : | albert | 48 | mesh = new MeshGeometry3D(); |
| 456 : | albert | 43 | Face face = faceMesh.Faces[j]; |
| 457 : | foreach (Vertex v in face.Vertices) | ||
| 458 : | { | ||
| 459 : | Point3D d = new Point3D( | ||
| 460 : | v.Position.X, | ||
| 461 : | v.Position.Y, | ||
| 462 : | v.Position.Z); | ||
| 463 : | albert | 4 | |
| 464 : | albert | 43 | // Scale |
| 465 : | d = st.Transform(d); | ||
| 466 : | albert | 48 | //// Rotate |
| 467 : | albert | 43 | d = rt.Transform(d); |
| 468 : | albert | 48 | //// Move |
| 469 : | albert | 43 | d = tt.Transform(d); |
| 470 : | albert | 4 | |
| 471 : | albert | 43 | mesh.Positions.Add(d); |
| 472 : | // These normals dont appear to be working for some reason, | ||
| 473 : | // however the auto-generated normals appear adequate for now. | ||
| 474 : | albert | 50 | // mesh.Normals.Add(new Vector3D(v.Normal.X, v.Normal.Y, v.Normal.Z)); |
| 475 : | albert | 48 | double x = (v.Position.Length() == float.Parse("0")) ? 0 : (v.Position.X / v.Position.Length()); |
| 476 : | albert | 50 | double y = (v.Position.Length() == float.Parse("0")) ? 0 : (v.Position.Y / v.Position.Length()); |
| 477 : | albert | 48 | |
| 478 : | albert | 56 | mesh.TextureCoordinates.Add(new Point( x,y)); |
| 479 : | albert | 43 | } |
| 480 : | //mesh.TextureCoordinates.Add(new Point(0, 0)); | ||
| 481 : | //mesh.TextureCoordinates.Add(new Point(1, 0)); | ||
| 482 : | //mesh.TextureCoordinates.Add(new Point(0, 1)); | ||
| 483 : | //mesh.TextureCoordinates.Add(new Point(1, 1)); | ||
| 484 : | |||
| 485 : | foreach (ushort ind in face.Indices) | ||
| 486 : | albert | 35 | { |
| 487 : | albert | 43 | mesh.TriangleIndices.Add(ind); |
| 488 : | } | ||
| 489 : | Color col = Colors.YellowGreen; | ||
| 490 : | if (face.TextureFace.TextureID != null) | ||
| 491 : | { | ||
| 492 : | albert | 50 | visual = new ModelVisual3D(); |
| 493 : | afrisby | 25 | |
| 494 : | albert | 56 | Material m = m_primitiveData.GetMaterialFromCache(face.TextureFace.TextureID); |
| 495 : | albert | 50 | if (m != null) |
| 496 : | albert | 37 | { |
| 497 : | albert | 50 | triangleModel = new GeometryModel3D(mesh, m); |
| 498 : | // | ||
| 499 : | m_primitiveData.RemoveUnAttachedTexture(data.LocalID); | ||
| 500 : | } | ||
| 501 : | else | ||
| 502 : | { | ||
| 503 : | albert | 56 | string bitmapPath = System.IO.Path.Combine(textureImageFolderPath , face.TextureFace.TextureID.ToString() + ".jpg"); |
| 504 : | albert | 50 | if (File.Exists(bitmapPath) == false) |
| 505 : | { | ||
| 506 : | //ADD the visual to the needUpdateTexute list.when the texture downloaded update it. | ||
| 507 : | m_primitiveData.AddUnAttachedTexture(data.LocalID, face.TextureFace.TextureID); | ||
| 508 : | albert | 44 | |
| 509 : | albert | 50 | col = Color.FromScRgb(face.TextureFace.RGBA.A, |
| 510 : | face.TextureFace.RGBA.R, | ||
| 511 : | face.TextureFace.RGBA.G, | ||
| 512 : | face.TextureFace.RGBA.B); | ||
| 513 : | albert | 36 | |
| 514 : | albert | 50 | triangleModel = new GeometryModel3D( |
| 515 : | mesh, new DiffuseMaterial(new SolidColorBrush(col))); | ||
| 516 : | visual.Content = triangleModel; | ||
| 517 : | albert | 38 | |
| 518 : | albert | 50 | model.Children.Add(visual); |
| 519 : | albert | 44 | |
| 520 : | albert | 50 | continue; |
| 521 : | } | ||
| 522 : | albert | 35 | |
| 523 : | albert | 50 | imagesource = new BitmapImage(new Uri(bitmapPath)); |
| 524 : | brush = new ImageBrush(imagesource); | ||
| 525 : | m = new DiffuseMaterial(brush); | ||
| 526 : | albert | 48 | |
| 527 : | albert | 50 | triangleModel = new GeometryModel3D( |
| 528 : | mesh, m); | ||
| 529 : | albert | 48 | |
| 530 : | albert | 50 | // |
| 531 : | m_primitiveData.RemoveUnAttachedTexture(data.LocalID); | ||
| 532 : | albert | 35 | |
| 533 : | albert | 50 | if (m != null) |
| 534 : | m_primitiveData.AddNewMaterial2Cache(data.Textures.DefaultTexture.TextureID, m); | ||
| 535 : | albert | 43 | |
| 536 : | albert | 50 | } |
| 537 : | albert | 45 | |
| 538 : | albert | 50 | visual.Content = triangleModel; |
| 539 : | model.Children.Add(visual); | ||
| 540 : | albert | 43 | |
| 541 : | albert | 44 | } |
| 542 : | } | ||
| 543 : | albert | 56 | model.SetValue(dep, data.LocalID); |
| 544 : | albert | 50 | return model; |
| 545 : | albert | 43 | |
| 546 : | |||
| 547 : | albert | 4 | } |
| 548 : | |||
| 549 : | albert | 50 | //Dictionary<UUID, Material> m_materials = new Dictionary<UUID,Material>(); |
| 550 : | |||
| 551 : | albert | 43 | public void UpdatePrimTexture(UUID textureID) |
| 552 : | albert | 35 | { |
| 553 : | albert | 50 | List<Primitive> NeedUpdateVisuals = m_primitiveData.GetVisualByTextureID(textureID); |
| 554 : | albert | 37 | |
| 555 : | albert | 43 | foreach (Primitive visual in NeedUpdateVisuals) |
| 556 : | albert | 35 | { |
| 557 : | albert | 53 | //m_primitiveData.RemoveUnAttachedTexture(visual.LocalID); |
| 558 : | albert | 44 | RemovePrimitive(visual.LocalID); |
| 559 : | albert | 43 | AddPrimitive(visual.LocalID, visual); |
| 560 : | albert | 35 | } |
| 561 : | albert | 37 | |
| 562 : | albert | 53 | NeedUpdateVisuals.Clear(); |
| 563 : | NeedUpdateVisuals = null; | ||
| 564 : | |||
| 565 : | albert | 35 | } |
| 566 : | |||
| 567 : | albert | 4 | public void RemovePrimitive(uint id) |
| 568 : | { | ||
| 569 : | m_viewport.Dispatcher.Invoke( | ||
| 570 : | DispatcherPriority.Normal, | ||
| 571 : | new System.Windows.Forms.MethodInvoker | ||
| 572 : | (delegate | ||
| 573 : | { | ||
| 574 : | albert | 53 | |
| 575 : | albert | 4 | { |
| 576 : | albert | 53 | |
| 577 : | albert | 4 | // Remove from scene and models list |
| 578 : | albert | 56 | Visual3D v = m_primitiveData.GetVisualInScene(id); |
| 579 : | m_viewport.Children.Remove(v); | ||
| 580 : | v = null; | ||
| 581 : | |||
| 582 : | albert | 53 | m_primitiveData.RemoveVisualInScene(id); |
| 583 : | m_primitiveData.RemovePrimtivePrimData(id); | ||
| 584 : | albert | 4 | } |
| 585 : | } | ||
| 586 : | ) | ||
| 587 : | ); | ||
| 588 : | } | ||
| 589 : | |||
| 590 : | public void SetTerrain(int x, int y, double val) | ||
| 591 : | { | ||
| 592 : | m_heightmap[x, y] = val; | ||
| 593 : | } | ||
| 594 : | |||
| 595 : | public void SetTerrain(double[,] map) | ||
| 596 : | { | ||
| 597 : | m_heightmap = map; | ||
| 598 : | |||
| 599 : | m_viewport.Dispatcher.Invoke( | ||
| 600 : | DispatcherPriority.Normal, | ||
| 601 : | new System.Windows.Forms.MethodInvoker | ||
| 602 : | (delegate | ||
| 603 : | { | ||
| 604 : | m_viewport.Children.Remove(m_terrain); | ||
| 605 : | albert | 56 | |
| 606 : | m_terrain = null; | ||
| 607 : | |||
| 608 : | albert | 4 | m_terrain = CreateTerrain(map); |
| 609 : | m_viewport.Children.Add(m_terrain); | ||
| 610 : | } | ||
| 611 : | ) | ||
| 612 : | ); | ||
| 613 : | } | ||
| 614 : | } | ||
| 615 : | albert | 53 | |
| 616 : | albert | 4 | } |
| ViewVC Help | |
| Powered by ViewVC 1.0.0 |

