00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 using System;
00018 using System.Collections;
00019 using System.Collections.Generic;
00020 using System.Collections.Specialized;
00021 using System.Text;
00022 using System.IO;
00023 using System.Drawing;
00024 using System.Diagnostics;
00025 using System.Reflection;
00026 using Microsoft.DirectX;
00027 using Microsoft.DirectX.Direct3D;
00028 using Microsoft.Samples.DirectX.UtilityToolkit;
00029 using System.Xml;
00030 using System.Windows.Forms;
00031
00032 namespace DXGfxLib
00033 {
00038 public class DXGfxManager
00039 {
00046 public int height = 640;
00053 public int width = 480;
00054
00058 public Framework frameworkInUse;
00059
00063 public Device d3dDevice;
00064
00065 public SurfaceDescription backBufferDescription;
00066
00070 public EffectGroup defaultEffectGroup = null;
00071
00075 public string defaultEffectGroupBaseFile = "base.fx";
00076
00083 public bool cullNodes = true;
00084
00089 public ScriptManager scriptManager = null;
00090
00094 public DXGfxLib.Serialization.Manager serializationManager = null;
00095
00099 public string currentSceneName = "Default Scene";
00100
00108 private OutDoorScene outdoorScene = null;
00109
00113 public string sceneFile;
00114
00118 public string mediaFolderPath;
00119
00123 private string skyFile;
00124
00128 private string waterFile;
00129
00133 private string heigthMapFile;
00134
00135
00136 private ShaderFlags shaderFlags = ShaderFlags.NoPreShader;
00137
00138 public Avatar CurrentAvatar
00139 {
00140 get
00141 {
00142 if (outdoorScene == null)
00143 return null;
00144 return outdoorScene.CurrentAvatar;
00145 }
00146 set
00147 {
00148 if (outdoorScene == null)
00149 return;
00150 outdoorScene.CurrentAvatar = value;
00151 }
00152 }
00153
00154 public ThirdPersonCamera CurrentCamera
00155 {
00156 get
00157 {
00158 if (outdoorScene == null)
00159 return null;
00160 return outdoorScene.CurrentCamera;
00161 }
00162 set
00163 {
00164 if (outdoorScene == null)
00165 return;
00166 outdoorScene.CurrentCamera = value;
00167 }
00168 }
00169
00170 public Matrix MatView
00171 {
00172 get
00173 {
00174 if (outdoorScene == null)
00175 return Matrix.Identity;
00176 return outdoorScene.matView;
00177 }
00178 set
00179 {
00180 if (outdoorScene == null)
00181 return;
00182 outdoorScene.matView = value;
00183 }
00184 }
00185
00186 public Matrix MatProj
00187 {
00188 get
00189 {
00190 if (outdoorScene == null)
00191 return Matrix.Identity;
00192 return outdoorScene.matProj;
00193 }
00194 set
00195 {
00196 if (outdoorScene == null)
00197 return;
00198 outdoorScene.matProj = value;
00199 }
00200 }
00201
00202 public IInputHandler CurrentInputHandler
00203 {
00204 get
00205 {
00206 if (outdoorScene == null)
00207 return null;
00208 return outdoorScene.CurrentInputHandler;
00209 }
00210 set
00211 {
00212 if (outdoorScene == null)
00213 return;
00214 outdoorScene.CurrentInputHandler = value;
00215 }
00216 }
00217
00218 private static DXGfxManager globalInstance = null;
00219
00224 public DXGfxManager()
00225 {
00226 if (globalInstance == null)
00227 {
00228 globalInstance = this;
00229 }
00230 else
00231 {
00232 throw new Exception("This is inapropriate! Only one instance of DXGfxManager should exist per process! Use GetGlobalInstance() instead!");
00233 }
00234 }
00235
00240 public static DXGfxManager GetGlobalInstance()
00241 {
00242 if (globalInstance == null)
00243 {
00244 globalInstance = new DXGfxManager();
00245 }
00246
00247 return globalInstance;
00248 }
00249
00253 public void Init()
00254 {
00255 scriptManager = new ScriptManager();
00256
00257
00258
00259
00261
00262
00263 serializationManager = new DXGfxLib.Serialization.Manager();
00264 }
00265
00270 public void Reset()
00271 {
00272 if (scriptManager != null)
00273 {
00274 scriptManager.Dispose();
00275 scriptManager = null;
00276 }
00277 if (serializationManager != null)
00278 {
00279 serializationManager.Dispose();
00280 serializationManager = null;
00281 }
00282 }
00283
00290 public void Init(Framework framework)
00291 {
00292 frameworkInUse = framework;
00293
00294
00295
00296
00297 framework.Disposing += new EventHandler(OnDestroyDevice);
00298 framework.DeviceLost += new EventHandler(OnLostDevice);
00299 framework.DeviceCreated += new DeviceEventHandler(OnCreateDevice);
00300 framework.DeviceReset += new DeviceEventHandler(OnDeviceReset);
00301
00302 Init();
00303 }
00304
00312 public void OnDeviceReset(object sender, DeviceEventArgs e)
00313 {
00314 if (outdoorScene != null)
00315 {
00316 if (outdoorScene.CurrentCamera != null)
00317 {
00318
00319 outdoorScene.CurrentCamera.SetViewParameters(new Vector3(0.0f, 0.0f, -5.0f), Vector3.Empty);
00320
00321 float aspectRatio = (float)e.BackBufferDescription.Width / (float)e.BackBufferDescription.Height;
00322 outdoorScene.CurrentCamera.SetProjectionParameters((float)Math.PI / 4, aspectRatio, 0.1f, 1000.0f);
00323 outdoorScene.CurrentCamera.SetViewParameters(new Vector3(0.0f, 5.0f, -30.0f), new Vector3(0.0f, 0.0f, 0.0f));
00324 outdoorScene.CurrentCamera.IsPositionMovementEnabled = true;
00325 }
00326 }
00327
00328 d3dDevice = e.Device;
00329 backBufferDescription = e.BackBufferDescription;
00330 }
00331
00339 public void OnLostDevice(object sender, EventArgs e)
00340 {
00341 }
00342
00349 public void OnDestroyDevice(object sender, EventArgs e)
00350 {
00351 }
00352
00360 public void OnCreateDevice(object sender, DeviceEventArgs e)
00361 {
00362 d3dDevice = e.Device;
00363 backBufferDescription = e.BackBufferDescription;
00364
00365 defaultEffectGroup = new EffectGroup();
00366 defaultEffectGroup.Set(d3dDevice, defaultEffectGroupBaseFile);
00367 }
00368
00369 public void UpdateOutdoorScene(float elapsedTime, double appTime)
00370 {
00371 if (outdoorScene == null)
00372 {
00373 return;
00374 }
00375
00376 outdoorScene.Update(d3dDevice, appTime, elapsedTime);
00377 }
00378
00379 public void DrawOutdoorScene()
00380 {
00381
00382
00383 if (outdoorScene == null)
00384 {
00385 return;
00386 }
00387
00388
00389
00390
00391 d3dDevice.Clear(ClearFlags.ZBuffer | ClearFlags.Target, 0x00424B79, 1.0f, 0);
00392
00393
00394
00395
00396 outdoorScene.RenderScene(d3dDevice, outdoorScene.currentFrustrum);
00397
00398
00399
00400
00401
00402
00403 }
00404
00410 public FrameworkMesh FrameworkMeshFromFile(string fileName)
00411 {
00412 if (d3dDevice == null)
00413 {
00414 throw new Exception("d3dDevice is null, loading of a FrameworkMesh is impossible without a valid device!");
00415 }
00416 return new FrameworkMesh(d3dDevice, fileName);
00417 }
00418
00426 public OutDoorScene NewScene(string name, float size, int quadtreeLevels)
00427 {
00428 AABBox desiredBBox = new AABBox(new BBox(new Vector3(0.0f, 0.0f, 0.0f), size));
00429 outdoorScene = new OutDoorScene(desiredBBox, quadtreeLevels);
00430
00431 outdoorScene.sceneContent.Remove(currentSceneName);
00432 outdoorScene.sceneContent.Add(name, outdoorScene);
00433 currentSceneName = name;
00434
00435 outdoorScene.name = StringUtil.GetName(name);
00436
00437 return outdoorScene;
00438 }
00439
00440 public void SetScene(OutDoorScene outdoor)
00441 {
00442 this.outdoorScene = outdoor;
00443 }
00444
00445 public void CloseScene()
00446 {
00447 if (outdoorScene != null)
00448 {
00449 outdoorScene.Dispose();
00450 outdoorScene = null;
00451 }
00452
00453
00454 Reset();
00455 Init();
00456 }
00457
00462 public Hashtable GetSceneContent()
00463 {
00464 return outdoorScene.sceneContent;
00465 }
00466
00471 public void SetHeightMap(string fileName)
00472 {
00473 if (outdoorScene == null)
00474 {
00475 throw new NullReferenceException("The is no outdoorScene at this stage so this method can not be called!");
00476 }
00477
00478 outdoorScene.LoadTerrain(fileName);
00479
00480 heigthMapFile = fileName;
00481 }
00482
00489 public Water2 SetWater(string fileName)
00490 {
00491 if (outdoorScene == null)
00492 {
00493 throw new NullReferenceException("The is no outdoorScene at this stage so this method can not be called!");
00494 }
00495
00496 Water2 theWater = new Water2(1000);
00497 outdoorScene.SceneWater = theWater;
00498 theWater.name = "Water";
00499 theWater.fileName = fileName;
00500
00501 waterFile = fileName;
00502
00503 outdoorScene.sceneContent.Add(fileName, theWater);
00504
00505 return theWater;
00506 }
00507
00514 public SkyDome SetSky(string fileName)
00515 {
00516 if (outdoorScene == null)
00517 {
00518 throw new NullReferenceException("The is no outdoorScene at this stage so this method can not be called!");
00519 }
00520
00521 SkyDome theSky = new SkyDome();
00522 theSky.LoadFromFile(this.d3dDevice, fileName);
00523 outdoorScene.SceneSky = theSky;
00524 theSky.name = "theSky";
00525 theSky.fileName = fileName;
00526
00527 skyFile = fileName;
00528
00529 outdoorScene.sceneContent.Add(fileName, theSky);
00530
00531 return theSky;
00532 }
00533
00539 public SceneObject LoadObject(string fileName)
00540 {
00541 if (outdoorScene == null)
00542 {
00543 throw new NullReferenceException("The is no outdoorScene at this stage so this method can not be called!");
00544 }
00545
00546 MeshObject sceneObject = new MeshObject(d3dDevice, fileName);
00547 outdoorScene.Attach(sceneObject);
00548
00549 if (outdoorScene.sceneContent.Contains(fileName))
00550 {
00551
00552 sceneObject.name += "I";
00553 }
00554 outdoorScene.sceneContent.Add(sceneObject.name, sceneObject);
00555
00556 return sceneObject;
00557 }
00558
00567 public SceneObject LoadObject(Type desiredType, string fileName)
00568 {
00569 if (outdoorScene == null)
00570 {
00571 throw new NullReferenceException("The is no outdoorScene at this stage so this method can not be called!");
00572 }
00573
00574 ConstructorInfo ctor = desiredType.GetConstructor(new Type[] { });
00575 Object[] args = new Object[] { };
00576 Object obj = ctor.Invoke(args);
00577
00578 if (obj == null)
00579 {
00580 throw new Exception("Non creatable type !!");
00581 }
00582
00583 SceneObject sobj = obj as SceneObject;
00584
00585 if (sobj == null)
00586 {
00587 throw new Exception("Type doesn't derive from SceneObject this is wrong!!");
00588 }
00589
00590 if (File.Exists(fileName))
00591 {
00592 sobj.LoadFromFile(d3dDevice, fileName);
00593 }
00594 outdoorScene.Attach(sobj);
00595
00596 if (outdoorScene.sceneContent.Contains(fileName))
00597 {
00598
00599 sobj.name += "I";
00600 }
00601 outdoorScene.sceneContent.Add(sobj.name, sobj);
00602
00603 return sobj;
00604 }
00605
00606 public void LoadEffect(Device device, string file, ref Effect effOut)
00607 {
00608
00609 if (!File.Exists(file))
00610 {
00611 OpenFileDialog ofdlg = new OpenFileDialog();
00612 ofdlg.Title = file + " not found. Select it!";
00613
00614 if (ofdlg.ShowDialog() == DialogResult.OK)
00615 {
00616 file = ofdlg.FileName;
00617 }
00618 }
00619
00620 string effError;
00621
00622 effOut = ResourceCache.GetGlobalInstance().CreateEffectFromFile(device,
00623 file, null, null, shaderFlags, null, out effError);
00624
00625 if (!effError.Equals(""))
00626 {
00627 MessageBox.Show(effError);
00628 }
00629 }
00630
00631 public Effect LoadEffect(Device device, string file)
00632 {
00633 Effect newEff = null;
00634 LoadEffect(device, file, ref newEff);
00635
00636 return newEff;
00637 }
00643 public Object GetObj(string fileName)
00644 {
00645 if (outdoorScene == null)
00646 {
00647 throw new NullReferenceException("The is no outdoorScene at this stage so this method can not be called!");
00648 }
00649
00650 return outdoorScene.sceneContent[fileName];
00651 }
00652
00658 public SceneObject GetSceneObject(string fileName)
00659 {
00660 if (outdoorScene == null)
00661 {
00662 throw new NullReferenceException("The is no outdoorScene at this stage so this method can not be called!");
00663 }
00664
00665 SceneObject sceneObj = outdoorScene.sceneContent[fileName] as SceneObject;
00666 return sceneObj;
00667 }
00668
00673 public Scene GetScene()
00674 {
00675 if (outdoorScene == null)
00676 {
00677 throw new NullReferenceException("The is no outdoorScene at this stage so this method can not be called!");
00678 }
00679
00680 return outdoorScene;
00681 }
00682
00692 public void Save(string dstFullPathName)
00693 {
00694 if (outdoorScene == null)
00695 {
00696 throw new NullReferenceException("The is no outdoorScene at this stage so this method can not be called!");
00697 }
00698
00700
00701
00702
00703
00704
00705
00706
00707
00708
00709
00710
00711
00712
00713
00714
00715
00716
00717
00718
00719
00720
00721
00722
00723
00724
00725
00726
00727
00728
00729
00730
00731
00732
00733
00734
00735
00736
00737
00738
00739
00740
00741
00742
00743
00744
00745
00746
00747
00748
00749 DXGfxLib.Serialization.Manager.GetGlobalInstance().SaveScene(outdoorScene, dstFullPathName);
00750 }
00751
00757 public void Load(string fullPathName)
00758 {
00759 string mediaFolder = StringUtil.GetPath(fullPathName);
00760 mediaFolder += "Media";
00761
00762 Load(fullPathName, mediaFolder);
00763 }
00764
00772 public void Load(string fullPathName, string mediaFolder)
00773 {
00774
00775
00776
00777
00778
00779
00780
00781
00782
00783
00784
00785
00786
00787
00788
00789
00790
00791
00792
00793
00794
00795
00796
00797
00798
00799
00800
00801
00802
00803
00804
00805
00806
00807
00808
00809
00810
00811
00812
00813
00814
00815
00816
00817
00818
00819
00820
00821
00822
00823
00824
00825
00826
00827
00828
00829
00830
00831
00832
00833
00834
00835
00836
00837
00838
00839
00840
00841
00842
00843
00844
00845
00846
00847
00848
00849
00850
00851
00852
00853
00854
00855
00856
00857
00858
00859
00860
00861
00862
00863
00864
00865
00866
00867
00868
00869
00870
00871
00872
00873
00874
00875
00876
00877
00878
00879
00880
00881
00882
00883
00884
00885
00886
00887
00888
00889
00890
00891
00892
00893
00894
00895
00896
00897
00898 sceneFile = fullPathName;
00899 mediaFolderPath = mediaFolder;
00900
00901 outdoorScene = DXGfxLib.Serialization.Manager.GetGlobalInstance().LoadOutdoor(fullPathName);
00902
00903 string currentFolder = Directory.GetCurrentDirectory();
00904 Directory.SetCurrentDirectory(mediaFolder);
00905 if (outdoorScene.SceneTerrain != null)
00906 {
00907 if (File.Exists(outdoorScene.SceneTerrain.heightmapFile))
00908 {
00909 outdoorScene.SceneTerrain.LoadFromFile(DXGfxManager.GetGlobalInstance().d3dDevice, (OutDoorScene)DXGfxManager.GetGlobalInstance().GetScene(), outdoorScene.SceneTerrain.heightmapFile);
00910 }
00911 }
00912
00913 outdoorScene.GetPrepared();
00914
00915 Directory.SetCurrentDirectory(currentFolder);
00916 }
00917
00923 public void CallEvaluation(double appTime, float elapsedTime)
00924 {
00925 IDictionaryEnumerator sceneEnum = outdoorScene.sceneContent.GetEnumerator();
00926
00928 while (sceneEnum.MoveNext())
00929 {
00930 SceneObject sceneObj = sceneEnum.Value as SceneObject;
00931
00932 if (sceneObj != null)
00933 {
00934 sceneObj.Evaluate(appTime, elapsedTime);
00935 continue;
00936 }
00937
00938 Scene scene = sceneEnum.Value as Scene;
00939
00940 if (scene != null)
00941 {
00942 scene.Evaluate(appTime, elapsedTime);
00943 continue;
00944 }
00945 }
00946 }
00947 }
00948 }