- Main Page
- Packages
- Classes
- Files

- File List
- File Members

# D:/guillaume/guillara/3D/DXGfx/DXGfxLib/SkyDome.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 {
    00027     public class SkyDome : MeshObject
    00028     {
    00029         private Texture[] skyboxTextures = null;
    00030         private Material[] skyBoxMaterials = null;
    00031         private Effect skyEffect = null;
    00032         private Texture baseTexture = null;
    00033         private Texture layerTexture = null;
    00034         private Texture gradientTexture = null;
    00035         private Texture noiseTexture = null;
    00036         private float movex = 0;
    00037         private float movey = 0;
    00038         private Frustrum frustrum = null;
    00039
    00040         public SkyDome()
    00041         {
    00042             Initialize();
    00043         }
    00044
    00045         public override void Initialize()
    00046         {
    00047             base.Initialize();
    00048         }
    00049
    00050         public override void GetPrepared()
    00051         {
    00052             base.GetPrepared();
    00053         }
    00054
    00061         public override void Update(Frustrum frustrum, double appTime, float elapsedTime)
    00062         {
    00063             base.Update(frustrum, appTime, elapsedTime);
    00064
    00065             // We are not going to change the orientation of the skybox but we want its center to
    00066             // be always at the camera position.
    00067             Position = MathUtil.ExtractPosition(Matrix.Invert(frustrum.matView));
    00068
    00069             movex = 0.03f * (float)Math.Sin(appTime);
    00070             movey = 0.03f * (float)Math.Cos(appTime);
    00071
    00072             this.frustrum = frustrum;
    00073         }
    00074
    00075         public void RenderSky(Device device)
    00076         {
    00077             device.Transform.World = worldMat;
    00078
    00079             if (skyEffect == null)
    00080             {
    00081                 DXGfxManager.GetGlobalInstance().LoadEffect(device, "SkyHLSL.fx", ref skyEffect);
    00082             }
    00083
    00084             skyEffect.SetValue("g_WorldViewProj", worldMat * frustrum.matView * frustrum.matProj);
    00085             skyEffect.SetValue("g_LayerTexture", layerTexture);
    00086             skyEffect.SetValue("g_GradientTexture", gradientTexture);
    00087             skyEffect.SetValue("g_NoiseTexture", noiseTexture);
    00088             skyEffect.SetValue("g_Movex", movex);
    00089             skyEffect.SetValue("g_Movey", movey);
    00090             skyEffect.SetValue("g_Movex2", movex);
    00091             skyEffect.SetValue("g_Movey2", movey);
    00092             skyEffect.Technique = "RenderSky";
    00093
    00094             device.VertexFormat = CustomVertex.PositionNormalTextured.Format;
    00095
    00096             device.RenderState.CullMode = Cull.None;
    00097
    00098             for (int i = 0; i < skyBoxMaterials.Length; i++)
    00099             {
    00100                 device.Material = skyBoxMaterials[i];
    00101                 skyEffect.SetValue("g_BaseTexture", skyboxTextures[i]);
    00102
    00103                 // Apply the technique contained in the effect
    00104                 int passes = skyEffect.Begin(0);
    00105                 for (int pass = 0; pass < passes; pass++)
    00106                 {
    00107                     skyEffect.BeginPass(pass);
    00108
    00109                     // Draw the mesh subset
    00110                     originalMesh.DrawSubset(i);
    00111
    00112                     skyEffect.EndPass();
    00113                 }
    00114                 skyEffect.End();
    00115             }
    00116         }
    00117
    00118         public override void LoadFromFile(Device d3ddevice, string fileName)
    00119         {
    00120             ExtendedMaterial[] materials = null;
    00121
    00122             string meshFile = Utility.FindMediaFile(fileName);
    00123             // Store the current folder, then switch to the folder where the media was found
    00124             string currentFolder = System.IO.Directory.GetCurrentDirectory();
    00125             System.IO.Directory.SetCurrentDirectory(new System.IO.FileInfo(fileName).Directory.FullName);
    00126
    00127             originalMesh = Mesh.FromFile(fileName, MeshFlags.Managed, d3ddevice, out materials);
    00128
    00129             if (skyboxTextures == null)
    00130             {
    00131                 // We need to extract the material properties and texture names
    00132                 skyboxTextures = new Texture[materials.Length];
    00133                 skyBoxMaterials = new Material[materials.Length];
    00134                 for (int i = 0; i < materials.Length; i++)
    00135                 {
    00136                     skyBoxMaterials[i] = materials[i].Material3D;
    00137                     // Set the ambient color for the material (D3DX does not do this)
    00138                     skyBoxMaterials[i].Ambient = skyBoxMaterials[i].Diffuse;
    00139
    00140                     // Create the texture
    00141                     if (System.IO.File.Exists(materials[i].TextureFilename))
    00142                     {
    00143                         skyboxTextures[i] = ResourceCache.GetGlobalInstance().CreateTextureFromFile(d3ddevice, materials[i].TextureFilename);
    00144                     }
    00145                     else
    00146                     {
    00147                         skyboxTextures[i] = ResourceCache.GetGlobalInstance().CreateTextureFromFile(d3ddevice, "skybox_front.jpg");
    00148                     }
    00149                 }
    00150             }
    00151
    00152             string textureFile = Utility.FindMediaFile("cloudBase.bmp");
    00153             // Store the current folder, then switch to the folder where the media was found
    00154             currentFolder = System.IO.Directory.GetCurrentDirectory();
    00155             System.IO.Directory.SetCurrentDirectory(new System.IO.FileInfo(textureFile).Directory.FullName);
    00156             // Now load the mesh hierarchy
    00157             baseTexture = ResourceCache.GetGlobalInstance().CreateTextureFromFile(d3ddevice, textureFile);
    00158
    00159
    00160             textureFile = Utility.FindMediaFile("cloudlayer.bmp");
    00161             // Store the current folder, then switch to the folder where the media was found
    00162             currentFolder = System.IO.Directory.GetCurrentDirectory();
    00163             System.IO.Directory.SetCurrentDirectory(new System.IO.FileInfo(textureFile).Directory.FullName);
    00164             // Now load the mesh hierarchy
    00165             layerTexture = ResourceCache.GetGlobalInstance().CreateTextureFromFile(d3ddevice, textureFile);
    00166
    00167
    00168             textureFile = Utility.FindMediaFile("gradient.bmp");
    00169             // Store the current folder, then switch to the folder where the media was found
    00170             currentFolder = System.IO.Directory.GetCurrentDirectory();
    00171             System.IO.Directory.SetCurrentDirectory(new System.IO.FileInfo(textureFile).Directory.FullName);
    00172             // Now load the mesh hierarchy
    00173             gradientTexture = ResourceCache.GetGlobalInstance().CreateTextureFromFile(d3ddevice, textureFile);
    00174
    00175
    00176             textureFile = Utility.FindMediaFile("noise.bmp");
    00177             // Store the current folder, then switch to the folder where the media was found
    00178             currentFolder = System.IO.Directory.GetCurrentDirectory();
    00179             System.IO.Directory.SetCurrentDirectory(new System.IO.FileInfo(textureFile).Directory.FullName);
    00180             // Now load the mesh hierarchy
    00181             noiseTexture = ResourceCache.GetGlobalInstance().CreateTextureFromFile(d3ddevice, textureFile);
    00182
    00183             System.IO.Directory.SetCurrentDirectory(currentFolder);
    00184
    00185             worldMat = Matrix.Scaling(10.0f, 6.0f, 10.0f);
    00186         }
    00187     }
    00188 }