00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 using System;
00018 using System.Collections.Generic;
00019 using System.Text;
00020 using System.IO;
00021 using Microsoft.DirectX;
00022 using Microsoft.DirectX.Direct3D;
00023 using Microsoft.Samples.DirectX.UtilityToolkit;
00024
00025 namespace DXGfxLib
00026 {
00030 public class SkyBox : MeshObject
00031 {
00032 protected const string effectFileName = "skyBox.fx";
00033
00034 protected string savedFileName;
00035
00036 protected float dim = 0.0f;
00037
00038 public SkyBox() : base()
00039 {
00040 dim = 160.0f;
00041
00042 worldAABBox = new AABBox(new BBox(new Vector3(0.0f, 0.0f, 0.0f), dim));
00043 }
00044
00045 public SkyBox(string file, float extent) : base()
00046 {
00047 if (DXGfxManager.GetGlobalInstance().d3dDevice == null)
00048 {
00049 throw new Exception("DXGfxManager.d3dDevice is null, creation of a skybox is impossible without a valid device!");
00050 }
00051 Init(DXGfxManager.GetGlobalInstance().d3dDevice, file, extent);
00052 }
00053
00061 public SkyBox(Device d3ddevice, string file, float extent) : base()
00062 {
00063 Init(d3ddevice, file, extent);
00064 }
00065
00066 public void Init(Device d3ddevice, string file, float extent)
00067 {
00068 dim = extent;
00069
00070 worldAABBox = new AABBox(new BBox(new Vector3(0.0f, 0.0f, 0.0f), dim));
00071 LoadFromFile(d3ddevice, file);
00072 }
00073
00077 public override void Dispose()
00078 {
00079 if (originalMesh != null)
00080 originalMesh.Dispose();
00081 }
00082
00089 public override void Update(Frustrum frustrum, double appTime, float elapsedTime)
00090 {
00091 base.Update(frustrum, appTime, elapsedTime);
00092
00093
00094
00095 Position = MathUtil.ExtractPosition(Matrix.Invert(frustrum.matView));
00096 }
00097
00103 public override void LoadFromFile(Device d3ddevice, string fileName)
00104 {
00105 ExtendedMaterial[] materials = null;
00106
00107 originalMesh = Mesh.FromFile(fileName, MeshFlags.Managed, d3ddevice, out materials);
00108
00109 if (meshTextures == null)
00110 {
00111
00112 meshTextures = new Texture[materials.Length];
00113 meshMaterials = new Material[materials.Length];
00114 for (int i = 0; i < materials.Length; i++)
00115 {
00116 meshMaterials[i] = materials[i].Material3D;
00117
00118 meshMaterials[i].Ambient = meshMaterials[i].Diffuse;
00119
00120
00121 meshTextures[i] = ResourceCache.GetGlobalInstance().CreateTextureFromFile(d3ddevice, materials[i].TextureFilename);
00122 }
00123 }
00124
00125 Matrix mat = Matrix.Identity;
00126 mat.Scale(dim, dim, dim);
00127 WorldMat = mat;
00128
00129 associatedEffectGroup.Set(d3ddevice, effectFileName);
00130 }
00131
00138 public override void AttachForRendering(Frustrum frustrum, List<IDrawable> objectsToBeRendered)
00139 {
00140 objectsToBeRendered.Add(this);
00141 }
00142
00147 public override void Draw(Device d3ddevice)
00148 {
00149 d3ddevice.SetTransform(TransformType.World, WorldMat);
00150
00151
00152
00153 for (int i = 0; i < meshMaterials.Length; i++)
00154 {
00155
00156 d3ddevice.Material = meshMaterials[i];
00157 d3ddevice.SetTexture(i, meshTextures[i]);
00158
00159
00160 originalMesh.DrawSubset(i);
00161 }
00162 }
00163 }
00164 }