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

