| 23 |
private readonly PerspectiveCamera m_camera; |
private readonly PerspectiveCamera m_camera; |
| 24 |
private CameraControl m_cameracontrol; |
private CameraControl m_cameracontrol; |
| 25 |
private IRendering m_mesher; |
private IRendering m_mesher; |
| 26 |
private ITextureManage m_textureManager; |
private DefaultTexture m_textureManager; |
| 27 |
private DefaultDataControl m_primitiveData; |
private DefaultDataControl m_primitiveData; |
| 28 |
|
|
| 29 |
public ITextureManage TextureManager |
//public ITextureManage TextureManager |
| 30 |
{ |
//{ |
| 31 |
set { m_textureManager = value; } |
// set { m_textureManager = value; } |
| 32 |
} |
//} |
| 33 |
|
|
| 34 |
private bool Broken; |
private bool Broken; |
| 35 |
|
|
| 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>(); |
|
|
|
|
|
|
|
| 40 |
private readonly DiffuseMaterial m_terrainMaterial; |
private readonly DiffuseMaterial m_terrainMaterial; |
| 41 |
private string textureImageFolderPath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "texture"); |
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 |
{ |
{ |
| 61 |
|
|
| 62 |
//setup data store control |
//setup data store control |
| 63 |
m_primitiveData = DefaultDataControl.SingleSceneDataManager(); |
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); |
| 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 |
|
|
| 130 |
set { m_mesher = value; } |
set { m_mesher = value; } |
| 131 |
} |
} |
| 132 |
|
|
| 133 |
|
void OnTextureDownloaded(UUID downLoadedTextureID, bool success) |
| 134 |
|
{ |
| 135 |
|
if (success) |
| 136 |
|
{ |
| 137 |
|
UpdatePrimTexture(downLoadedTextureID); |
| 138 |
|
} |
| 139 |
|
} |
| 140 |
|
|
| 141 |
public void AddPrimitive(uint id, Primitive data) |
public void AddPrimitive(uint id, Primitive data) |
| 142 |
{ |
{ |
| 143 |
if(Broken) |
if(Broken) |
| 153 |
try |
try |
| 154 |
{ |
{ |
| 155 |
bool WaitParentIn = true; |
bool WaitParentIn = true; |
|
|
|
| 156 |
if (data.ParentID == 0) |
if (data.ParentID == 0) |
| 157 |
{ |
{ |
| 158 |
//m_primitiveData.AddModelsParentPositions(id, data.Position); |
//m_primitiveData.AddModelsParentPositions(id, data.Position); |
| 184 |
|
|
| 185 |
if (WaitParentIn == false) |
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 |
|
|
| 194 |
ModelVisual3D model = CreateModel(data); |
ModelVisual3D model = CreateModel(data); |
|
m_viewport.Children.Add(model); |
|
| 195 |
|
|
| 196 |
m_primitiveData.AddNewPrimitive(data.LocalID, model, data.ParentID); |
m_primitiveData.AddNewPrimitive(data.LocalID, model, data.ParentID); |
| 197 |
m_primitiveData.AddNewPrimitivePrimData(data); |
m_primitiveData.AddNewPrimitivePrimData(data); |
| 198 |
m_primitiveData.RemoveWaitingParentPrimitive(data.LocalID); |
m_primitiveData.RemoveWaitingParentPrimitive(data.LocalID); |
| 199 |
|
|
| 200 |
|
m_viewport.Children.Add(model); |
| 201 |
|
|
| 202 |
List<Primitive> waiting = m_primitiveData.GetModelsWaitingParent(data.LocalID); |
List<Primitive> waiting = m_primitiveData.GetModelsWaitingParent(data.LocalID); |
| 203 |
|
|
| 204 |
foreach (Primitive child in waiting) |
foreach (Primitive child in waiting) |
| 214 |
// m_primitiveData.AddModelsParentPositions(child.LocalID, child.Position); |
// m_primitiveData.AddModelsParentPositions(child.LocalID, child.Position); |
| 215 |
//} |
//} |
| 216 |
|
|
| 217 |
ModelVisual3D childmodel = CreateModel(child); |
//ModelVisual3D childmodel = CreateModel(child); |
| 218 |
m_viewport.Children.Add(childmodel); |
//m_viewport.Children.Add(childmodel); |
|
|
|
|
m_primitiveData.AddNewPrimitive(child.LocalID, childmodel, child.ParentID); |
|
|
m_primitiveData.AddNewPrimitivePrimData(child); |
|
|
m_primitiveData.RemoveWaitingParentPrimitive(child.LocalID); |
|
| 219 |
|
|
| 220 |
//this.AddPrimitive(child.LocalID, child); |
//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 |
else |
| 231 |
m_primitiveData.AddWaitingParentPrimitive(id, data); |
m_primitiveData.AddWaitingParentPrimitive(id, data); |
| 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 |
|
|
| 291 |
|
|
| 292 |
return model; |
return model; |
| 293 |
} |
} |
|
ManagedImage managedimg; |
|
|
System.Drawing.Image img; |
|
| 294 |
|
|
| 295 |
private ModelVisual3D CreateModel(Primitive data) |
private ModelVisual3D CreateModel(Primitive data) |
| 296 |
{ |
{ |
| 355 |
{ |
{ |
| 356 |
mesh.TriangleIndices.Add(ind); |
mesh.TriangleIndices.Add(ind); |
| 357 |
} |
} |
| 358 |
|
|
| 359 |
|
triangleModel = PatchTexture(data, mesh, triangleModel); |
| 360 |
|
|
| 361 |
|
ModelVisual3D model = new ModelVisual3D(); |
| 362 |
|
model.Content = triangleModel; |
| 363 |
|
|
| 364 |
|
return model; |
| 365 |
|
} |
| 366 |
|
|
| 367 |
|
private GeometryModel3D PatchTexture(Primitive data, MeshGeometry3D mesh, GeometryModel3D triangleModel) |
| 368 |
|
{ |
| 369 |
Color col = Colors.YellowGreen; |
Color col = Colors.YellowGreen; |
| 370 |
|
|
| 371 |
if (data.Textures != null && |
if (data.Textures != null && |
| 372 |
data.Textures.DefaultTexture != null && |
data.Textures.DefaultTexture != null && |
| 373 |
data.Textures.DefaultTexture.TextureID != null) |
data.Textures.DefaultTexture.TextureID != null) |
| 374 |
{ |
{ |
|
|
|
| 375 |
Material m = m_primitiveData.GetMaterialFromCache(data.Textures.DefaultTexture.TextureID); |
Material m = m_primitiveData.GetMaterialFromCache(data.Textures.DefaultTexture.TextureID); |
| 376 |
if (m != null) |
if (m != null) |
| 377 |
{ |
{ |
| 382 |
else |
else |
| 383 |
{ |
{ |
| 384 |
//ImageDownload image = m_textureManager.RetireveTextureData(data.Textures.DefaultTexture.TextureID); |
//ImageDownload image = m_textureManager.RetireveTextureData(data.Textures.DefaultTexture.TextureID); |
|
|
|
| 385 |
// if (OpenJPEG.DecodeToImage(image.AssetData, out managedimg, out img)) |
// if (OpenJPEG.DecodeToImage(image.AssetData, out managedimg, out img)) |
| 386 |
//{ |
//{ |
| 387 |
// System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(img); |
// System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(img); |
| 388 |
// ImageSourceConverter converter = new ImageSourceConverter(); |
// ImageSourceConverter converter = new ImageSourceConverter(); |
|
|
|
| 389 |
// ImageSource imagesource = (BitmapImage) converter.ConvertFrom(bitmap); |
// ImageSource imagesource = (BitmapImage) converter.ConvertFrom(bitmap); |
| 390 |
// ImageBrush brush = new ImageBrush(imagesource); |
// ImageBrush brush = new ImageBrush(imagesource); |
| 391 |
// m = new DiffuseMaterial(brush); |
// m = new DiffuseMaterial(brush); |
|
|
|
| 392 |
// triangleModel = new GeometryModel3D( |
// triangleModel = new GeometryModel3D( |
| 393 |
// mesh, m); |
// mesh, m); |
|
|
|
| 394 |
// // |
// // |
| 395 |
// m_primitiveData.RemoveUnAttachedTexture(data.LocalID); |
// m_primitiveData.RemoveUnAttachedTexture(data.LocalID); |
| 396 |
|
|
| 397 |
// if (m != null) |
// if (m != null) |
| 398 |
// m_primitiveData.AddNewMaterial2Cache(data.Textures.DefaultTexture.TextureID, m); |
// m_primitiveData.AddNewMaterial2Cache(data.Textures.DefaultTexture.TextureID, m); |
|
|
|
| 399 |
//} |
//} |
| 400 |
string bitmapPath = System.IO.Path.Combine(textureImageFolderPath, data.Textures.DefaultTexture.TextureID.ToString() + ".jpg"); |
string bitmapPath = System.IO.Path.Combine(textureImageFolderPath, data.Textures.DefaultTexture.TextureID.ToString() + ".jpg"); |
| 401 |
|
|
| 405 |
ImageBrush brush = new ImageBrush(imagesource); |
ImageBrush brush = new ImageBrush(imagesource); |
| 406 |
m = new DiffuseMaterial(brush); |
m = new DiffuseMaterial(brush); |
| 407 |
|
|
| 408 |
triangleModel = new GeometryModel3D( |
triangleModel = new GeometryModel3D(mesh, m); |
|
mesh, m); |
|
|
|
|
| 409 |
// |
// |
| 410 |
m_primitiveData.RemoveUnAttachedTexture(data.LocalID); |
m_primitiveData.RemoveUnAttachedTexture(data.LocalID); |
| 411 |
|
|
| 412 |
if (m != null) |
if (m != null) |
| 413 |
m_primitiveData.AddNewMaterial2Cache(data.Textures.DefaultTexture.TextureID, m); |
m_primitiveData.AddNewMaterial2Cache(data.Textures.DefaultTexture.TextureID, m); |
| 414 |
|
|
| 415 |
|
imagesource = null; |
| 416 |
|
brush = null; |
| 417 |
|
m = null; |
| 418 |
} |
} |
| 419 |
else |
else |
| 420 |
{ |
{ |
|
|
|
| 421 |
col = Color.FromScRgb(data.Textures.DefaultTexture.RGBA.A, |
col = Color.FromScRgb(data.Textures.DefaultTexture.RGBA.A, |
| 422 |
data.Textures.DefaultTexture.RGBA.R, |
data.Textures.DefaultTexture.RGBA.R, |
| 423 |
data.Textures.DefaultTexture.RGBA.G, |
data.Textures.DefaultTexture.RGBA.G, |
| 443 |
//ADD the visual to the needUpdateTexute list.when the texture downloaded update it. |
//ADD the visual to the needUpdateTexute list.when the texture downloaded update it. |
| 444 |
m_primitiveData.AddUnAttachedTexture(data.LocalID, data.Textures.DefaultTexture.TextureID); |
m_primitiveData.AddUnAttachedTexture(data.LocalID, data.Textures.DefaultTexture.TextureID); |
| 445 |
} |
} |
| 446 |
|
return triangleModel; |
|
ModelVisual3D model = new ModelVisual3D(); |
|
|
model.Content = triangleModel; |
|
|
|
|
|
return model; |
|
| 447 |
} |
} |
| 448 |
|
|
| 449 |
//private void SaveTexture2Image(ImageDownload image) |
//private void SaveTexture2Image(ImageDownload image) |
| 610 |
|
|
| 611 |
foreach (Primitive visual in NeedUpdateVisuals) |
foreach (Primitive visual in NeedUpdateVisuals) |
| 612 |
{ |
{ |
| 613 |
m_primitiveData.RemoveUnAttachedTexture(visual.LocalID); |
//m_primitiveData.RemoveUnAttachedTexture(visual.LocalID); |
| 614 |
RemovePrimitive(visual.LocalID); |
RemovePrimitive(visual.LocalID); |
| 615 |
AddPrimitive(visual.LocalID, visual); |
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_primitiveData.ModelsInScene.ContainsKey(id)) |
|
| 631 |
{ |
{ |
| 632 |
|
|
| 633 |
// Remove from scene and models list |
// Remove from scene and models list |
| 634 |
m_viewport.Children.Remove(m_primitiveData.ModelsInScene[id]); |
m_viewport.Children.Remove(m_primitiveData.GetVisualInScene(id)); |
| 635 |
|
m_primitiveData.RemoveVisualInScene(id); |
| 636 |
|
m_primitiveData.RemovePrimtivePrimData(id); |
| 637 |
} |
} |
| 638 |
} |
} |
| 639 |
) |
) |
| 662 |
); |
); |
| 663 |
} |
} |
| 664 |
} |
} |
| 665 |
//public struct FaceData |
|
|
//{ |
|
|
// public float[] Vertices; |
|
|
// public ushort[] Indices; |
|
|
// public float[] TexCoords; |
|
|
// public int TexturePointer; |
|
|
// public System.Drawing.Image Texture; |
|
|
// // TODO: Normals / binormals? |
|
|
//} |
|
| 666 |
} |
} |