| 9 |
using OpenMetaverse.Rendering; |
using OpenMetaverse.Rendering; |
| 10 |
using Xenki.Framework; |
using Xenki.Framework; |
| 11 |
using Quaternion=System.Windows.Media.Media3D.Quaternion; |
using Quaternion=System.Windows.Media.Media3D.Quaternion; |
| 12 |
|
using System.IO; |
| 13 |
|
using System.Windows.Media.Imaging; |
| 14 |
|
using OpenMetaverse.Imaging; |
| 15 |
|
using Material = System.Windows.Media.Media3D.Material; |
| 16 |
|
|
| 17 |
|
|
| 18 |
namespace Xenki.DefaultRenderer |
namespace Xenki.DefaultRenderer |
| 19 |
{ |
{ |
| 21 |
{ |
{ |
| 22 |
private readonly Viewport3D m_viewport; |
private readonly Viewport3D m_viewport; |
| 23 |
private readonly PerspectiveCamera m_camera; |
private readonly PerspectiveCamera m_camera; |
| 24 |
private Trackball m_trackball; |
private CameraControl m_cameracontrol; |
| 25 |
private IRendering m_mesher; |
private IRendering m_mesher; |
| 26 |
|
private DefaultTexture m_textureManager; |
| 27 |
|
private DefaultDataControl m_primitiveData; |
| 28 |
|
|
| 29 |
|
//public ITextureManage TextureManager |
| 30 |
|
//{ |
| 31 |
|
// set { m_textureManager = value; } |
| 32 |
|
//} |
| 33 |
|
|
| 34 |
private bool Broken; |
private bool Broken; |
| 35 |
|
|
| 36 |
private readonly Point3D[,] m_heightmapPoints = new Point3D[256,256]; |
private readonly Point3D[,] m_heightmapPoints = new Point3D[256,256]; |
| 37 |
private double[,] m_heightmap = new double[256,256]; |
private double[,] m_heightmap = new double[256,256]; |
| 38 |
private ModelVisual3D m_terrain; |
private ModelVisual3D m_terrain; |
| 39 |
|
|
|
private readonly Dictionary<uint, Visual3D> m_models = new Dictionary<uint, Visual3D>(); |
|
|
private readonly Dictionary<uint, Vector3> m_parentPositions = new Dictionary<uint, Vector3>(); |
|
|
|
|
|
private readonly Material m_defaultMaterial; |
|
| 40 |
private readonly DiffuseMaterial m_terrainMaterial; |
private readonly DiffuseMaterial m_terrainMaterial; |
| 41 |
|
private string textureImageFolderPath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "texture"); |
| 42 |
|
private GridClient m_gridclient; |
| 43 |
|
|
| 44 |
|
private GridClient gridClient; |
| 45 |
|
|
| 46 |
|
public GridClient GridClient |
| 47 |
|
{ |
| 48 |
|
get { return gridClient; } |
| 49 |
|
set |
| 50 |
|
{ |
| 51 |
|
gridClient = value; |
| 52 |
|
} |
| 53 |
|
} |
| 54 |
|
|
| 55 |
public DefaultRenderer(Viewport3D m_viewport) |
public DefaultRenderer(Viewport3D m_viewport) |
| 56 |
{ |
{ |
| 57 |
// Viewport |
// Viewport |
| 58 |
this.m_viewport = m_viewport; |
this.m_viewport = m_viewport; |
| 59 |
m_viewport.ClipToBounds = false; |
// m_viewport.ClipToBounds = false; |
| 60 |
m_viewport.IsHitTestVisible = true; |
m_viewport.IsHitTestVisible = true; |
| 61 |
|
|
| 62 |
|
//setup data store control |
| 63 |
|
m_primitiveData = DefaultDataControl.SingleSceneDataManager(); |
| 64 |
|
m_textureManager = DefaultTexture.SingleTextureManager(); |
| 65 |
|
m_textureManager.GridClient = GridClient; |
| 66 |
|
m_textureManager.OnDownloadFinished = OnTextureDownloaded; |
| 67 |
|
m_textureManager.StartDownload(); |
| 68 |
|
|
| 69 |
|
|
| 70 |
// Camera Issues |
// Camera Issues |
| 71 |
Point3D CameraPos = new Point3D(0,0,50); |
Point3D CameraPos = new Point3D(0,0,50); |
| 72 |
Point3D CameraLook = new Point3D(128,128,10); |
Point3D CameraLook = new Point3D(128,128,10); |
| 81 |
m_camera.NearPlaneDistance = 0.01; |
m_camera.NearPlaneDistance = 0.01; |
| 82 |
m_viewport.Camera = m_camera; |
m_viewport.Camera = m_camera; |
| 83 |
|
|
| 84 |
// Setup Trackball |
// Setup Camera Control |
| 85 |
|
m_cameracontrol = new CameraControl(m_camera,m_primitiveData); |
| 86 |
m_trackball = new Trackball(m_camera); |
m_cameracontrol.EventSource = m_viewport; |
|
m_camera.Transform = m_trackball.Transform; |
|
|
|
|
|
m_trackball.EventSource = m_viewport; |
|
|
|
|
|
|
|
|
// Default Material Properties |
|
|
m_defaultMaterial = new DiffuseMaterial( |
|
|
new SolidColorBrush(Colors.DarkKhaki)); |
|
| 87 |
|
|
| 88 |
GradientBrush terGrad = new RadialGradientBrush(Colors.ForestGreen, Colors.SandyBrown); |
GradientBrush terGrad = new RadialGradientBrush(Colors.ForestGreen, Colors.SandyBrown); |
| 89 |
m_terrainMaterial = new DiffuseMaterial( |
m_terrainMaterial = new DiffuseMaterial( |
| 90 |
terGrad); |
terGrad); |
| 91 |
|
|
|
|
|
| 92 |
// Scene Lighting |
// Scene Lighting |
| 93 |
ModelVisual3D lights = new ModelVisual3D(); |
ModelVisual3D lights = new ModelVisual3D(); |
| 94 |
lights.Content = new DirectionalLight(Colors.White, new Vector3D(-20, -30, -10)); |
lights.Content = new DirectionalLight(Colors.White, new Vector3D(-20, -30, -10)); |
| 105 |
m_viewport.Children.Add(point); |
m_viewport.Children.Add(point); |
| 106 |
m_viewport.Children.Add(ambient); |
m_viewport.Children.Add(ambient); |
| 107 |
|
|
|
|
|
| 108 |
// Terrain Base |
// Terrain Base |
| 109 |
for(int x=0;x<256;x++) |
for(int x=0;x<256;x++) |
| 110 |
{ |
{ |
| 111 |
for(int y=0;y<256;y++) |
for(int y=0;y<256;y++) |
| 112 |
{ |
{ |
| 113 |
m_heightmap[x, y] = 30.0; |
m_heightmap[x, y] = 10.0; |
| 114 |
} |
} |
| 115 |
} |
} |
| 116 |
|
|
| 123 |
|
|
| 124 |
//m_viewport.Children.Add(DefaultUtil.CreateCube(new Point3D(0,0,0), new Point3D(256,256,20), |
//m_viewport.Children.Add(DefaultUtil.CreateCube(new Point3D(0,0,0), new Point3D(256,256,20), |
| 125 |
// material)); |
// material)); |
|
|
|
| 126 |
} |
} |
| 127 |
|
|
| 128 |
public IRendering Mesher |
public IRendering Mesher |
| 130 |
set { m_mesher = value; } |
set { m_mesher = value; } |
| 131 |
} |
} |
| 132 |
|
|
| 133 |
public void AddPrimitive(uint id, Primitive data) |
void OnTextureDownloaded(UUID downLoadedTextureID, bool success) |
| 134 |
{ |
{ |
| 135 |
lock(m_parentPositions) |
if (success) |
| 136 |
{ |
{ |
| 137 |
m_parentPositions[id] = data.Position; |
UpdatePrimTexture(downLoadedTextureID); |
| 138 |
|
} |
| 139 |
} |
} |
| 140 |
|
|
| 141 |
|
public void AddPrimitive(uint id, Primitive data) |
| 142 |
|
{ |
| 143 |
if(Broken) |
if(Broken) |
| 144 |
return; |
return; |
| 145 |
|
|
| 148 |
new System.Windows.Forms.MethodInvoker |
new System.Windows.Forms.MethodInvoker |
| 149 |
(delegate |
(delegate |
| 150 |
{ |
{ |
| 151 |
lock (m_models) |
// lock (m_primitiveData.PrimModelsInScene) |
| 152 |
{ |
{ |
| 153 |
try |
try |
| 154 |
{ |
{ |
| 155 |
if (m_models.ContainsKey(id)) |
bool WaitParentIn = true; |
| 156 |
|
if (data.ParentID == 0) |
| 157 |
{ |
{ |
| 158 |
m_viewport.Children.Remove(m_models[id]); |
//m_primitiveData.AddModelsParentPositions(id, data.Position); |
| 159 |
m_models.Remove(id); |
// test = true; |
| 160 |
|
WaitParentIn = false; |
| 161 |
} |
} |
| 162 |
|
else |
| 163 |
lock(m_parentPositions) |
{ |
| 164 |
|
if (m_primitiveData.PrimModelsInScene.ContainsKey(data.ParentID)) |
| 165 |
|
WaitParentIn = false; |
| 166 |
|
/* |
| 167 |
|
Vector3 vec = m_primitiveData.GetModelsParentPosition(data.ParentID); |
| 168 |
|
if (vec != new Vector3()) |
| 169 |
{ |
{ |
| 170 |
if(m_parentPositions.ContainsKey(data.ParentID)) |
if (test) |
| 171 |
{ |
{ |
| 172 |
data.Position += m_parentPositions[data.ParentID]; |
OpenMetaverse.Quaternion qqq = m_primitiveData.GetParentRotation(data.ParentID); |
| 173 |
|
data.Position *= qqq; |
| 174 |
} |
} |
| 175 |
|
test = false; |
| 176 |
|
data.Position += vec; |
| 177 |
|
//data.Position *= m_primitiveData.PrimModelsInScene[data.ParentID].Rotation ; |
| 178 |
|
m_primitiveData.AddModelsParentPositions(id, data.Position); |
| 179 |
|
|
| 180 |
|
WaitParentIn = false; |
| 181 |
|
} |
| 182 |
|
*/ |
| 183 |
|
} |
| 184 |
|
|
| 185 |
|
if (WaitParentIn == false) |
| 186 |
|
{ |
| 187 |
|
|
| 188 |
|
if (m_primitiveData.SceneContainVisual(data.LocalID)) |
| 189 |
|
{ |
| 190 |
|
m_viewport.Children.Remove(m_primitiveData.GetVisualInScene(data.LocalID)); |
| 191 |
|
m_primitiveData.RemoveVisualInScene(data.LocalID); |
| 192 |
} |
} |
| 193 |
|
|
|
// Add it to the scene appropriately. |
|
| 194 |
ModelVisual3D model = CreateModel(data); |
ModelVisual3D model = CreateModel(data); |
| 195 |
m_models.Add(id, model); |
|
| 196 |
|
m_primitiveData.AddNewPrimitive(data.LocalID, model, data.ParentID); |
| 197 |
|
m_primitiveData.AddNewPrimitivePrimData(data); |
| 198 |
|
m_primitiveData.RemoveWaitingParentPrimitive(data.LocalID); |
| 199 |
|
|
| 200 |
m_viewport.Children.Add(model); |
m_viewport.Children.Add(model); |
| 201 |
|
|
| 202 |
|
List<Primitive> waiting = m_primitiveData.GetModelsWaitingParent(data.LocalID); |
| 203 |
|
|
| 204 |
|
foreach (Primitive child in waiting) |
| 205 |
|
{ |
| 206 |
|
//Vector3 vec = m_primitiveData.GetModelsParentPosition(data.ParentID); |
| 207 |
|
|
| 208 |
|
//if (vec != new Vector3()) |
| 209 |
|
//{ |
| 210 |
|
// OpenMetaverse.Quaternion qqq = m_primitiveData.GetParentRotation(child.ParentID); |
| 211 |
|
// child.Position *= qqq; |
| 212 |
|
// child.Position += data.Position; |
| 213 |
|
// //data.Position *= m_primitiveData.PrimModelsInScene[data.ParentID].Rotation ; |
| 214 |
|
// m_primitiveData.AddModelsParentPositions(child.LocalID, child.Position); |
| 215 |
|
//} |
| 216 |
|
|
| 217 |
|
//ModelVisual3D childmodel = CreateModel(child); |
| 218 |
|
//m_viewport.Children.Add(childmodel); |
| 219 |
|
|
| 220 |
|
//m_primitiveData.AddNewPrimitive(child.LocalID, childmodel, child.ParentID); |
| 221 |
|
//m_primitiveData.AddNewPrimitivePrimData(child); |
| 222 |
|
//m_primitiveData.RemoveWaitingParentPrimitive(child.LocalID); |
| 223 |
|
|
| 224 |
|
AddPrimitive(child.LocalID, child); |
| 225 |
|
} |
| 226 |
|
waiting.Clear(); |
| 227 |
|
waiting = null; |
| 228 |
|
model = null; |
| 229 |
|
} |
| 230 |
|
else |
| 231 |
|
m_primitiveData.AddWaitingParentPrimitive(id, data); |
| 232 |
|
|
| 233 |
} |
} |
| 234 |
catch (Exception e) |
catch (Exception e) |
| 235 |
{ |
{ |
| 266 |
int p2 = ((x + 1)*256) + y; |
int p2 = ((x + 1)*256) + y; |
| 267 |
int p3 = ((x + 1)*256) + (y + 1); |
int p3 = ((x + 1)*256) + (y + 1); |
| 268 |
|
|
|
|
|
| 269 |
// Δ 013 |\ |
// Δ 013 |\ |
| 270 |
mesh.TriangleIndices.Add(p1); //#0 |
mesh.TriangleIndices.Add(p1); //#0 |
| 271 |
|
|
| 273 |
|
|
| 274 |
mesh.TriangleIndices.Add(p2); //#3 |
mesh.TriangleIndices.Add(p2); //#3 |
| 275 |
|
|
|
|
|
| 276 |
// Δ 023 |
// Δ 023 |
| 277 |
mesh.TriangleIndices.Add(p0); //#0 |
mesh.TriangleIndices.Add(p0); //#0 |
| 278 |
|
|
| 280 |
|
|
| 281 |
mesh.TriangleIndices.Add(p2); //#3 |
mesh.TriangleIndices.Add(p2); //#3 |
| 282 |
|
|
|
|
|
|
|
|
| 283 |
} |
} |
| 284 |
} |
} |
| 285 |
|
|
| 294 |
|
|
| 295 |
private ModelVisual3D CreateModel(Primitive data) |
private ModelVisual3D CreateModel(Primitive data) |
| 296 |
{ |
{ |
|
MeshGeometry3D mesh = new MeshGeometry3D(); |
|
| 297 |
SimpleMesh basic = m_mesher.GenerateSimpleMesh(data, DetailLevel.Medium); |
SimpleMesh basic = m_mesher.GenerateSimpleMesh(data, DetailLevel.Medium); |
| 298 |
|
MeshGeometry3D mesh = new MeshGeometry3D(); |
| 299 |
|
|
| 300 |
|
GeometryModel3D triangleModel = new GeometryModel3D(); |
| 301 |
|
|
| 302 |
|
lock(m_primitiveData.PrimModelsInScene) |
| 303 |
|
if (m_primitiveData.PrimModelsInScene.ContainsKey(data.ParentID)) |
| 304 |
|
{ |
| 305 |
|
//OpenMetaverse.Quaternion rotate = m_primitiveData.GetParentPosition(data.ParentID); |
| 306 |
|
//if (m_primitiveData.PrimModelsInScene.ContainsKey(m_primitiveData.PrimModelsInScene[data.ParentID].ParentID)) |
| 307 |
|
// data.Position *= m_primitiveData.PrimModelsInScene[m_primitiveData.PrimModelsInScene[data.ParentID].ParentID].Rotation; |
| 308 |
|
//else |
| 309 |
|
//data.Position *= m_primitiveData.PrimModelsInScene[data.ParentID].Rotation; |
| 310 |
|
data.Position += m_primitiveData.PrimModelsInScene[data.ParentID].Position; |
| 311 |
|
} |
| 312 |
|
|
| 313 |
// Translation |
// Translation |
| 314 |
TranslateTransform3D tt = new TranslateTransform3D( |
TranslateTransform3D tt = new TranslateTransform3D( |
| 340 |
d = tt.Transform(d); |
d = tt.Transform(d); |
| 341 |
|
|
| 342 |
mesh.Positions.Add(d); |
mesh.Positions.Add(d); |
| 343 |
|
|
| 344 |
// These normals dont appear to be working for some reason, |
// These normals dont appear to be working for some reason, |
| 345 |
// however the auto-generated normals appear adequate for now. |
// however the auto-generated normals appear adequate for now. |
| 346 |
// mesh.Normals.Add(new Vector3D(v.Normal.X, v.Normal.Y, v.Normal.Z)); |
//Vector3D normal = new Vector3D(v.Normal.X, v.Normal.Y, v.Normal.Z); |
| 347 |
mesh.TextureCoordinates.Add(new Point(v.TexCoord.X, v.TexCoord.Y)); |
//mesh.Normals.Add(normal); |
| 348 |
|
double x = (v.Position.Length() == float.Parse("0")) ? 0 : (v.Position.X / v.Position.Length()); |
| 349 |
|
double y = (v.Position.Length() == float.Parse("0")) ? 0 : (v.Position.Y / v.Position.Length()); |
| 350 |
|
|
| 351 |
|
mesh.TextureCoordinates.Add(new Point(x, y)); |
| 352 |
} |
} |
| 353 |
|
|
| 354 |
foreach (ushort ind in basic.Indices) |
foreach (ushort ind in basic.Indices) |
| 356 |
mesh.TriangleIndices.Add(ind); |
mesh.TriangleIndices.Add(ind); |
| 357 |
} |
} |
| 358 |
|
|
| 359 |
GeometryModel3D triangleModel; |
triangleModel = PatchTexture(data, mesh, triangleModel); |
| 360 |
|
|
| 361 |
|
ModelVisual3D model = new ModelVisual3D(); |
| 362 |
|
model.Content = triangleModel; |
| 363 |
|
|
| 364 |
Color[] colAry = new Color[5]; |
return model; |
| 365 |
colAry[0] = Colors.Violet; |
} |
| 366 |
colAry[1] = Colors.BlueViolet; |
|
| 367 |
colAry[2] = Colors.Navy; |
private GeometryModel3D PatchTexture(Primitive data, MeshGeometry3D mesh, GeometryModel3D triangleModel) |
| 368 |
colAry[3] = Colors.CornflowerBlue; |
{ |
| 369 |
colAry[4] = Colors.PowderBlue; |
Color col = Colors.YellowGreen; |
| 370 |
Random x = new Random(); |
|
| 371 |
Color col = colAry[x.Next(0, 5)]; |
if (data.Textures != null && |
| 372 |
|
data.Textures.DefaultTexture != null && |
| 373 |
|
data.Textures.DefaultTexture.TextureID != null) |
| 374 |
|
{ |
| 375 |
|
Material m = m_primitiveData.GetMaterialFromCache(data.Textures.DefaultTexture.TextureID); |
| 376 |
|
if (m != null) |
| 377 |
|
{ |
| 378 |
|
triangleModel = new GeometryModel3D(mesh, m); |
| 379 |
|
// |
| 380 |
|
m_primitiveData.RemoveUnAttachedTexture(data.LocalID); |
| 381 |
|
} |
| 382 |
|
else |
| 383 |
|
{ |
| 384 |
|
//ImageDownload image = m_textureManager.RetireveTextureData(data.Textures.DefaultTexture.TextureID); |
| 385 |
|
// if (OpenJPEG.DecodeToImage(image.AssetData, out managedimg, out img)) |
| 386 |
|
//{ |
| 387 |
|
// System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(img); |
| 388 |
|
// ImageSourceConverter converter = new ImageSourceConverter(); |
| 389 |
|
// ImageSource imagesource = (BitmapImage) converter.ConvertFrom(bitmap); |
| 390 |
|
// ImageBrush brush = new ImageBrush(imagesource); |
| 391 |
|
// m = new DiffuseMaterial(brush); |
| 392 |
|
// triangleModel = new GeometryModel3D( |
| 393 |
|
// mesh, m); |
| 394 |
|
// // |
| 395 |
|
// m_primitiveData.RemoveUnAttachedTexture(data.LocalID); |
| 396 |
|
|
| 397 |
|
// if (m != null) |
| 398 |
|
// m_primitiveData.AddNewMaterial2Cache(data.Textures.DefaultTexture.TextureID, m); |
| 399 |
|
//} |
| 400 |
|
string bitmapPath = System.IO.Path.Combine(textureImageFolderPath, data.Textures.DefaultTexture.TextureID.ToString() + ".jpg"); |
| 401 |
|
|
| 402 |
|
if (File.Exists(bitmapPath)) |
| 403 |
|
{ |
| 404 |
|
ImageSource imagesource = new BitmapImage(new Uri(bitmapPath)); |
| 405 |
|
ImageBrush brush = new ImageBrush(imagesource); |
| 406 |
|
m = new DiffuseMaterial(brush); |
| 407 |
|
|
| 408 |
|
triangleModel = new GeometryModel3D(mesh, m); |
| 409 |
|
// |
| 410 |
|
m_primitiveData.RemoveUnAttachedTexture(data.LocalID); |
| 411 |
|
|
| 412 |
|
if (m != null) |
| 413 |
|
m_primitiveData.AddNewMaterial2Cache(data.Textures.DefaultTexture.TextureID, m); |
| 414 |
|
|
| 415 |
|
imagesource = null; |
| 416 |
|
brush = null; |
| 417 |
|
m = null; |
| 418 |
|
} |
| 419 |
|
else |
| 420 |
|
{ |
| 421 |
|
col = Color.FromScRgb(data.Textures.DefaultTexture.RGBA.A, |
| 422 |
|
data.Textures.DefaultTexture.RGBA.R, |
| 423 |
|
data.Textures.DefaultTexture.RGBA.G, |
| 424 |
|
data.Textures.DefaultTexture.RGBA.B); |
| 425 |
|
|
| 426 |
triangleModel = new GeometryModel3D( |
triangleModel = new GeometryModel3D( |
| 427 |
mesh, new DiffuseMaterial(new SolidColorBrush(col))); |
mesh, new DiffuseMaterial(new SolidColorBrush(col))); |
| 428 |
|
|
| 429 |
//*/ |
m_primitiveData.AddUnAttachedTexture(data.LocalID, data.Textures.DefaultTexture.TextureID); |
| 430 |
/* |
} |
| 431 |
GradientBrush Grad = new LinearGradientBrush(Colors.Red, Colors.Blue, 90); |
} |
| 432 |
Material mat = new DiffuseMaterial(Grad); |
} |
| 433 |
|
else |
| 434 |
|
{ |
| 435 |
|
col = Color.FromScRgb(data.Textures.DefaultTexture.RGBA.A, |
| 436 |
|
data.Textures.DefaultTexture.RGBA.R, |
| 437 |
|
data.Textures.DefaultTexture.RGBA.G, |
| 438 |
|
data.Textures.DefaultTexture.RGBA.B); |
| 439 |
|
|
| 440 |
triangleModel = new GeometryModel3D( |
triangleModel = new GeometryModel3D( |
| 441 |
mesh, mat); |
mesh, new DiffuseMaterial(new SolidColorBrush(col))); |
| 442 |
*/ |
|
| 443 |
|
//ADD the visual to the needUpdateTexute list.when the texture downloaded update it. |
| 444 |
|
m_primitiveData.AddUnAttachedTexture(data.LocalID, data.Textures.DefaultTexture.TextureID); |
| 445 |
|
} |
| 446 |
|
return triangleModel; |
| 447 |
|
} |
| 448 |
|
|
| 449 |
|
//private void SaveTexture2Image(ImageDownload image) |
| 450 |
|
//{ |
| 451 |
|
// ManagedImage managedimg; |
| 452 |
|
// Image img; |
| 453 |
|
// //byte[] bytes = GetTextureDataByTextureID(downLoadedTextureID); |
| 454 |
|
|
| 455 |
|
// if (OpenJPEG.DecodeToImage(image.AssetData, out managedimg, out img)) |
| 456 |
|
// { |
| 457 |
|
// Bitmap bitmap = new Bitmap(img); |
| 458 |
|
// ImageSource source = new BitmapMetadataBlob( |
| 459 |
|
// bitmap.Save(Path.Combine(bmpFilePath, image.ID.ToString() + ".jpg"), System.Drawing.Imaging.ImageFormat.Jpeg); |
| 460 |
|
|
| 461 |
|
// bitmap.Dispose(); |
| 462 |
|
// bitmap = null; |
| 463 |
|
// } |
| 464 |
|
|
| 465 |
|
// img.Dispose(); |
| 466 |
|
// img = null; |
| 467 |
|
// managedimg = null; |
| 468 |
|
//} |
| 469 |
|
|
| 470 |
|
ModelVisual3D CreateModelFromFaced(Primitive data) |
| 471 |
|
{ |
| 472 |
|
|
| 473 |
|
FacetedMesh faceMesh = m_mesher.GenerateFacetedMesh(data, DetailLevel.Medium); |
| 474 |
|
|
| 475 |
|
lock (m_primitiveData.PrimModelsInScene) |
| 476 |
|
if (m_primitiveData.PrimModelsInScene.ContainsKey(data.ParentID)) |
| 477 |
|
{ |
| 478 |
|
//OpenMetaverse.Quaternion rotate = m_primitiveData.GetParentPosition(data.ParentID); |
| 479 |
|
//if (m_primitiveData.PrimModelsInScene.ContainsKey(m_primitiveData.PrimModelsInScene[data.ParentID].ParentID)) |
| 480 |
|
// data.Position *= m_primitiveData.PrimModelsInScene[m_primitiveData.PrimModelsInScene[data.ParentID].ParentID].Rotation; |
| 481 |
|
//else |
| 482 |
|
//data.Position *= rotate;// m_primitiveData.PrimModelsInScene[data.ParentID].Rotation; |
| 483 |
|
data.Position += m_primitiveData.PrimModelsInScene[data.ParentID].Position; |
| 484 |
|
} |
| 485 |
|
|
| 486 |
|
// Translation |
| 487 |
|
TranslateTransform3D tt = new TranslateTransform3D( |
| 488 |
|
new Vector3D(data.Position.X, data.Position.Y, data.Position.Z) |
| 489 |
|
); |
| 490 |
|
|
| 491 |
|
Quaternion rot = new Quaternion(data.Rotation.X, data.Rotation.Y, data.Rotation.Z, data.Rotation.W); |
| 492 |
|
|
| 493 |
|
RotateTransform3D rt = new RotateTransform3D( |
| 494 |
|
new AxisAngleRotation3D(rot.Axis, rot.Angle) |
| 495 |
|
); |
| 496 |
|
|
| 497 |
|
// Scale |
| 498 |
|
ScaleTransform3D st = new ScaleTransform3D( |
| 499 |
|
new Vector3D(data.Scale.X, data.Scale.Y, data.Scale.Z)); |
| 500 |
|
|
| 501 |
|
|
| 502 |
ModelVisual3D model = new ModelVisual3D(); |
ModelVisual3D model = new ModelVisual3D(); |
| 503 |
model.Content = triangleModel; |
GeometryModel3D triangleModel = new GeometryModel3D(); |
| 504 |
|
MeshGeometry3D mesh; |
| 505 |
|
ImageSource imagesource; |
| 506 |
|
ImageBrush brush; |
| 507 |
|
ModelVisual3D visual; |
| 508 |
|
// Draw the prim faces |
| 509 |
|
for (int j = 0; j < faceMesh.Faces.Count; j++) |
| 510 |
|
{ |
| 511 |
|
mesh = new MeshGeometry3D(); |
| 512 |
|
Face face = faceMesh.Faces[j]; |
| 513 |
|
foreach (Vertex v in face.Vertices) |
| 514 |
|
{ |
| 515 |
|
Point3D d = new Point3D( |
| 516 |
|
v.Position.X, |
| 517 |
|
v.Position.Y, |
| 518 |
|
v.Position.Z); |
| 519 |
|
|
| 520 |
|
// Scale |
| 521 |
|
d = st.Transform(d); |
| 522 |
|
//// Rotate |
| 523 |
|
d = rt.Transform(d); |
| 524 |
|
//// Move |
| 525 |
|
d = tt.Transform(d); |
| 526 |
|
|
| 527 |
|
mesh.Positions.Add(d); |
| 528 |
|
// These normals dont appear to be working for some reason, |
| 529 |
|
// however the auto-generated normals appear adequate for now. |
| 530 |
|
// mesh.Normals.Add(new Vector3D(v.Normal.X, v.Normal.Y, v.Normal.Z)); |
| 531 |
|
double x = (v.Position.Length() == float.Parse("0")) ? 0 : (v.Position.X / v.Position.Length()); |
| 532 |
|
double y = (v.Position.Length() == float.Parse("0")) ? 0 : (v.Position.Y / v.Position.Length()); |
| 533 |
|
|
| 534 |
|
//mesh.TextureCoordinates.Add(new Point( x,y)); |
| 535 |
|
} |
| 536 |
|
//mesh.TextureCoordinates.Add(new Point(0, 0)); |
| 537 |
|
//mesh.TextureCoordinates.Add(new Point(1, 0)); |
| 538 |
|
//mesh.TextureCoordinates.Add(new Point(0, 1)); |
| 539 |
|
//mesh.TextureCoordinates.Add(new Point(1, 1)); |
| 540 |
|
|
| 541 |
|
foreach (ushort ind in face.Indices) |
| 542 |
|
{ |
| 543 |
|
mesh.TriangleIndices.Add(ind); |
| 544 |
|
} |
| 545 |
|
Color col = Colors.YellowGreen; |
| 546 |
|
if (face.TextureFace.TextureID != null) |
| 547 |
|
{ |
| 548 |
|
visual = new ModelVisual3D(); |
| 549 |
|
|
| 550 |
|
Material m = m_primitiveData.GetMaterialFromCache(data.Textures.DefaultTexture.TextureID); |
| 551 |
|
if (m != null) |
| 552 |
|
{ |
| 553 |
|
triangleModel = new GeometryModel3D(mesh, m); |
| 554 |
|
// |
| 555 |
|
m_primitiveData.RemoveUnAttachedTexture(data.LocalID); |
| 556 |
|
} |
| 557 |
|
else |
| 558 |
|
{ |
| 559 |
|
string bitmapPath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "texture\\" + face.TextureFace.TextureID.ToString() + ".jpg"); |
| 560 |
|
if (File.Exists(bitmapPath) == false) |
| 561 |
|
{ |
| 562 |
|
//ADD the visual to the needUpdateTexute list.when the texture downloaded update it. |
| 563 |
|
m_primitiveData.AddUnAttachedTexture(data.LocalID, face.TextureFace.TextureID); |
| 564 |
|
|
| 565 |
|
col = Color.FromScRgb(face.TextureFace.RGBA.A, |
| 566 |
|
face.TextureFace.RGBA.R, |
| 567 |
|
face.TextureFace.RGBA.G, |
| 568 |
|
face.TextureFace.RGBA.B); |
| 569 |
|
|
| 570 |
|
triangleModel = new GeometryModel3D( |
| 571 |
|
mesh, new DiffuseMaterial(new SolidColorBrush(col))); |
| 572 |
|
visual.Content = triangleModel; |
| 573 |
|
|
| 574 |
|
model.Children.Add(visual); |
| 575 |
|
|
| 576 |
|
continue; |
| 577 |
|
} |
| 578 |
|
|
| 579 |
|
imagesource = new BitmapImage(new Uri(bitmapPath)); |
| 580 |
|
brush = new ImageBrush(imagesource); |
| 581 |
|
m = new DiffuseMaterial(brush); |
| 582 |
|
|
| 583 |
|
triangleModel = new GeometryModel3D( |
| 584 |
|
mesh, m); |
| 585 |
|
|
| 586 |
|
// |
| 587 |
|
m_primitiveData.RemoveUnAttachedTexture(data.LocalID); |
| 588 |
|
|
| 589 |
|
if (m != null) |
| 590 |
|
m_primitiveData.AddNewMaterial2Cache(data.Textures.DefaultTexture.TextureID, m); |
| 591 |
|
|
| 592 |
|
} |
| 593 |
|
|
| 594 |
|
visual.Content = triangleModel; |
| 595 |
|
model.Children.Add(visual); |
| 596 |
|
|
| 597 |
|
} |
| 598 |
|
} |
| 599 |
|
|
|
model.Transform = new TranslateTransform3D(0,0,0); |
|
| 600 |
return model; |
return model; |
| 601 |
|
|
| 602 |
|
|
| 603 |
|
} |
| 604 |
|
|
| 605 |
|
//Dictionary<UUID, Material> m_materials = new Dictionary<UUID,Material>(); |
| 606 |
|
|
| 607 |
|
public void UpdatePrimTexture(UUID textureID) |
| 608 |
|
{ |
| 609 |
|
List<Primitive> NeedUpdateVisuals = m_primitiveData.GetVisualByTextureID(textureID); |
| 610 |
|
|
| 611 |
|
foreach (Primitive visual in NeedUpdateVisuals) |
| 612 |
|
{ |
| 613 |
|
//m_primitiveData.RemoveUnAttachedTexture(visual.LocalID); |
| 614 |
|
RemovePrimitive(visual.LocalID); |
| 615 |
|
AddPrimitive(visual.LocalID, visual); |
| 616 |
|
} |
| 617 |
|
|
| 618 |
|
NeedUpdateVisuals.Clear(); |
| 619 |
|
NeedUpdateVisuals = null; |
| 620 |
|
|
| 621 |
} |
} |
| 622 |
|
|
| 623 |
public void RemovePrimitive(uint id) |
public void RemovePrimitive(uint id) |
| 627 |
new System.Windows.Forms.MethodInvoker |
new System.Windows.Forms.MethodInvoker |
| 628 |
(delegate |
(delegate |
| 629 |
{ |
{ |
| 630 |
if(m_models.ContainsKey(id)) |
|
| 631 |
{ |
{ |
| 632 |
|
|
| 633 |
// Remove from scene and models list |
// Remove from scene and models list |
| 634 |
m_viewport.Children.Remove(m_models[id]); |
m_viewport.Children.Remove(m_primitiveData.GetVisualInScene(id)); |
| 635 |
m_models.Remove(id); |
m_primitiveData.RemoveVisualInScene(id); |
| 636 |
|
m_primitiveData.RemovePrimtivePrimData(id); |
| 637 |
} |
} |
| 638 |
} |
} |
| 639 |
) |
) |
| 662 |
); |
); |
| 663 |
} |
} |
| 664 |
} |
} |
| 665 |
|
|
| 666 |
} |
} |