- Main Page
- Packages
- Classes
- Files

- File List
- File Members

# D:/guillaume/guillara/3D/DXGfx/DXGfxLib/SkyBox.cs

Go to the documentation of this file.

    00001 //BSD License
    00002 //DXGfx® - http://www.eteractions.com
    00003 //Copyright (c) 2005
    00004 //by Guillaume Randon
    00005 //Permission is hereby granted, free of charge, to any person obtaining a copy of this software
    00006 //and associated documentation files (the "Software"), to deal in the Software without restriction,
    00007 //including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
    00008 //and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
    00009 //subject to the following conditions:
    00010 //The above copyright notice and this permission notice shall be included in all copies or substantial
    00011 //portions of the Software.
    00012 //THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
    00013 //INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
    00014 //AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
    00015 //DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    00016 //OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
    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             // We are not going to change the orientation of the skybox but we want its center to
    00094             // be always at the camera position.
    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                 // We need to extract the material properties and texture names
    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                     // Set the ambient color for the material (D3DX does not do this)
    00118                     meshMaterials[i].Ambient = meshMaterials[i].Diffuse;
    00119
    00120                     // Create the texture
    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             // Meshes are divided into subsets, one for each material. Render them in
    00152             // a loop
    00153             for (int i = 0; i < meshMaterials.Length; i++)
    00154             {
    00155                 // Set the material and texture for this subset
    00156                 d3ddevice.Material = meshMaterials[i];
    00157                 d3ddevice.SetTexture(i, meshTextures[i]);
    00158
    00159                 // Draw the mesh subset
    00160                 originalMesh.DrawSubset(i);
    00161             }
    00162         }
    00163     }
    00164 }