Exemple #1
0
        internal void ApplyShadingInfo(TerrainShading.ShadingInfo si)
        {
            mTerMats.SetMaterialParameter("Terrain", "mFogStart", si.mFogStart);
            mTerMats.SetMaterialParameter("Terrain", "mFogEnd", si.mFogEnd);
            mTerMats.SetMaterialParameter("Terrain", "mFogEnabled", si.mbFogEnabled? 1f : 0f);

            mTerMats.SetMaterialParameter("Sky", "mSkyGradient0", si.mSkyColor0.ToVector3());
            mTerMats.SetMaterialParameter("Sky", "mSkyGradient1", si.mSkyColor1.ToVector3());

            Viewport vp = mGD.GetScreenViewPort();

            mGD.GCam.Projection = Matrix.PerspectiveFovLH(
                MathUtil.DegreesToRadians(45f),
                vp.Width / (float)vp.Height, 0.1f, si.mFogEnd);

            mGD.SetClip(0.1f, si.mFogEnd);

            mChunkRange = si.mChunkRange;
        }
Exemple #2
0
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            GraphicsDevice gd = new GraphicsDevice("Terrain Editor",
                                                   FeatureLevel.Level_11_0, 0.1f, 3000f);

            //save form positions
            gd.RendForm.DataBindings.Add(new Binding("Location",
                                                     Settings.Default, "MainWindowPos", true,
                                                     DataSourceUpdateMode.OnPropertyChanged));

            gd.RendForm.Location = Settings.Default.MainWindowPos;

            //used to have a hard coded path here for #debug
            //but now can just use launch.json to provide it
            string rootDir = ".";

            StuffKeeper sk = new StuffKeeper();

            SharedForms.ShaderCompileHelper.mTitle = "Compiling Shaders...";

            sk.eCompileNeeded += SharedForms.ShaderCompileHelper.CompileNeededHandler;
            sk.eCompileDone   += SharedForms.ShaderCompileHelper.CompileDoneHandler;

            sk.Init(gd, rootDir);

            TerrainAtlas ta = new TerrainAtlas(gd, sk);

            ta.DataBindings.Add(new Binding("Location",
                                            Settings.Default, "AtlasFormPos", true,
                                            DataSourceUpdateMode.OnPropertyChanged));
            ta.DataBindings.Add(new Binding("Size",
                                            Settings.Default, "AtlasFormSize", true,
                                            DataSourceUpdateMode.OnPropertyChanged));

            ta.Location = Settings.Default.AtlasFormPos;
            ta.Size     = Settings.Default.AtlasFormSize;
            ta.Visible  = true;

            TerrainForm tf = new TerrainForm();

            tf.DataBindings.Add(new Binding("Location",
                                            Settings.Default, "TerrainFormPos", true,
                                            DataSourceUpdateMode.OnPropertyChanged));

            tf.Location = Settings.Default.TerrainFormPos;
            tf.Visible  = true;

            TerrainShading ts = new TerrainShading();

            ts.DataBindings.Add(new Binding("Location",
                                            Settings.Default, "ShadingFormPos", true,
                                            DataSourceUpdateMode.OnPropertyChanged));

            ts.Location = Settings.Default.ShadingFormPos;
            ts.Visible  = true;

            PlayerSteering pSteering    = SetUpSteering();
            Input          inp          = SetUpInput();
            Random         rand         = new Random();
            Vector3        pos          = Vector3.One * 5f;
            Vector3        lightDir     = -Vector3.UnitY;
            bool           bMouseLookOn = false;

            EventHandler actHandler = new EventHandler(
                delegate(object s, EventArgs ea)
                { inp.ClearInputs(); });

            EventHandler <EventArgs> deActHandler = new EventHandler <EventArgs>(
                delegate(object s, EventArgs ea)
            {
                gd.SetCapture(false);
                bMouseLookOn = false;
            });

            gd.RendForm.Activated      += actHandler;
            gd.RendForm.AppDeactivated += deActHandler;

            GameLoop gLoop = new GameLoop(gd, sk, rootDir);

            EventHandler buildHandler = new EventHandler(
                delegate(object s, EventArgs ea)
                { ListEventArgs <HeightMap.TexData> lea = ea as ListEventArgs <HeightMap.TexData>;
                  gLoop.Texture((TexAtlas)s, lea.mList, ta.GetTransitionHeight()); });

            EventHandler applyHandler = new EventHandler(
                delegate(object s, EventArgs ea)
            {
                TerrainShading.ShadingInfo si = s as TerrainShading.ShadingInfo;
                gLoop.ApplyShadingInfo(si);
            });

            ta.eReBuild += buildHandler;
            ts.eApply   += applyHandler;

            EventHandler tBuildHandler = new EventHandler(
                delegate(object s, EventArgs ea)
                { int gridSize, chunkSize, tilingIterations, threads;
                  int erosionIterations, polySize, smoothPasses, seed;
                  float medianHeight, variance, borderSize;
                  float rainFall, solubility, evaporation;
                  tf.GetBuildData(out gridSize, out chunkSize,
                                  out medianHeight, out variance, out polySize,
                                  out tilingIterations, out borderSize, out smoothPasses,
                                  out seed, out erosionIterations, out rainFall,
                                  out solubility, out evaporation, out threads);
                  gLoop.TBuild(gridSize, chunkSize, medianHeight, variance,
                               polySize, tilingIterations, borderSize,
                               smoothPasses, seed, erosionIterations,
                               rainFall, solubility, evaporation, threads); });
            EventHandler tLoadHandler = new EventHandler(
                delegate(object s, EventArgs ea)
            {
                gLoop.TLoad(s as string);
                ta.LoadAtlasInfo(s as string);
            });
            EventHandler tSaveHandler = new EventHandler(
                delegate(object s, EventArgs ea)
            {
                if (gLoop.TSave(s as string))
                {
                    ta.SaveAtlasInfo(s as string);
                    ts.SaveShadingInfo(s as string);
                }
            });

            tf.eBuild += tBuildHandler;
            tf.eLoad  += tLoadHandler;
            tf.eSave  += tSaveHandler;

            UpdateTimer time = new UpdateTimer(true, false);

            time.SetFixedTimeStepSeconds(1f / 60f);             //60fps update rate
            time.SetMaxDeltaSeconds(MaxTimeDelta);

            List <Input.InputAction> acts = new List <Input.InputAction>();

            RenderLoop.Run(gd.RendForm, () =>
            {
                if (!gd.RendForm.Focused)
                {
                    Thread.Sleep(33);
                }

                gd.CheckResize();

                if (bMouseLookOn && gd.RendForm.Focused)
                {
                    gd.ResetCursorPos();
                }

                //Clear views
                gd.ClearViews();

                time.Stamp();
                while (time.GetUpdateDeltaSeconds() > 0f)
                {
                    acts = UpdateInput(inp, gd,
                                       time.GetUpdateDeltaSeconds(), ref bMouseLookOn);
                    if (!gd.RendForm.Focused)
                    {
                        acts.Clear();
                        bMouseLookOn = false;
                        gd.SetCapture(false);
                        inp.UnMapAxisAction(Input.MoveAxis.MouseYAxis);
                        inp.UnMapAxisAction(Input.MoveAxis.MouseXAxis);
                    }
                    gLoop.Update(time, acts, pSteering);
                    time.UpdateDone();
                }

                gLoop.RenderUpdate(time.GetRenderUpdateDeltaMilliSeconds());

                gLoop.Render();

                gd.Present();

                acts.Clear();
            }, true);                   //true here is slow but needed for winforms events

            Settings.Default.Save();

            sk.eCompileNeeded -= SharedForms.ShaderCompileHelper.CompileNeededHandler;
            sk.eCompileDone   -= SharedForms.ShaderCompileHelper.CompileDoneHandler;

            gd.RendForm.Activated      -= actHandler;
            gd.RendForm.AppDeactivated -= deActHandler;
            ta.eReBuild -= buildHandler;
            tf.eBuild   -= tBuildHandler;
            ts.eApply   -= applyHandler;

            gLoop.FreeAll();
            inp.FreeAll();
            ta.FreeAll();
            sk.FreeAll();

            //Release all resources
            gd.ReleaseAll();
        }