Пример #1
0
        internal TerrainLoop(GraphicsDevice gd, StuffKeeper sk, string gameRootDir)
        {
            mGD   = gd;
            mSK   = sk;
            mResX = gd.RendForm.ClientRectangle.Width;
            mResY = gd.RendForm.ClientRectangle.Height;

            mFontMats = new MatLib(gd, sk);

            mFontMats.CreateMaterial("Text");
            mFontMats.SetMaterialEffect("Text", "2D.fx");
            mFontMats.SetMaterialTechnique("Text", "Text");

            mFonts = sk.GetFontList();

            mST = new ScreenText(gd.GD, mFontMats, mFonts[0], 1000);

            mTextProj = Matrix.OrthoOffCenterLH(0, mResX, mResY, 0, 0.1f, 5f);

            string path = gameRootDir + "\\Levels\\Testa.Terrain";

            mTerrain = new Terrain(path);

            //build model for raycasts/collisions
            mTModel = mTerrain.ConstructModel();
            mTModel.FixBoxHeights();

//			Vector4	color	=Vector4.UnitY + (Vector4.UnitW * 0.15f);
            Vector4 color = Vector4.UnitX * 0.25f +
                            Vector4.UnitZ * 0.25f + (Vector4.UnitW * 0.55f);

            //string indicators for various statusy things
            mST.AddString(mFonts[0], "Stuffs", "PosStatus",
                          color, Vector2.UnitX * 20f + Vector2.UnitY * 610f, Vector2.One);
            mST.AddString(mFonts[0], "Stuffs", "ColStatus",
                          color, Vector2.UnitX * 20f + Vector2.UnitY * 630f, Vector2.One);
            mST.AddString(mFonts[0], "Thread Status...", "ThreadStatus",
                          color, Vector2.UnitX * 20f + Vector2.UnitY * 650f, Vector2.One);

            mTerMats = new MatLib(mGD, sk);

            Vector3 lightDir = Mathery.RandomDirection(mRand);

            Vector4 lightColor2 = Vector4.One * 0.4f;
            Vector4 lightColor3 = Vector4.One * 0.1f;

            lightColor2.W = lightColor3.W = 1f;

            mTerMats.CreateMaterial("Terrain");
            mTerMats.SetMaterialEffect("Terrain", "Terrain.fx");
            mTerMats.SetMaterialTechnique("Terrain", "TriTerrain");
            mTerMats.SetMaterialParameter("Terrain", "mLightColor0", Vector4.One);
            mTerMats.SetMaterialParameter("Terrain", "mLightColor1", lightColor2);
            mTerMats.SetMaterialParameter("Terrain", "mLightColor2", lightColor3);
            mTerMats.SetMaterialParameter("Terrain", "mLightDirection", lightDir);
            mTerMats.SetMaterialParameter("Terrain", "mSolidColour", Vector4.One);
            mTerMats.SetMaterialParameter("Terrain", "mSpecPower", 1);
            mTerMats.SetMaterialParameter("Terrain", "mSpecColor", Vector4.One);
            mTerMats.SetMaterialParameter("Terrain", "mWorld", Matrix.Identity);

            mTerMats.CreateMaterial("Sky");
            mTerMats.SetMaterialEffect("Sky", "Terrain.fx");
            mTerMats.SetMaterialTechnique("Sky", "SkyGradient");

            LoadShading(path);

            mTerMats.InitCelShading(1);
            mTerMats.GenerateCelTexturePreset(mGD.GD, mGD.GD.FeatureLevel == FeatureLevel.Level_9_3, false, 0);
            mTerMats.SetCelTexture(0);

            mSkyCube = PrimFactory.CreateCube(gd.GD, -5f);

            LoadTerrainTextureStuff(path);

            //debug draw
            mDebugMats = new MatLib(gd, sk);

            Vector4 redColor   = Vector4.One;
            Vector4 greenColor = Vector4.One;
            Vector4 blueColor  = Vector4.One;

            redColor.Y = redColor.Z = greenColor.X = greenColor.Z = blueColor.X = blueColor.Y = 0f;

            mDebugMats.CreateMaterial("DebugBoxes");
            mDebugMats.SetMaterialEffect("DebugBoxes", "Static.fx");
            mDebugMats.SetMaterialTechnique("DebugBoxes", "TriSolidSpec");
            mDebugMats.SetMaterialParameter("DebugBoxes", "mLightColor0", Vector4.One);
            mDebugMats.SetMaterialParameter("DebugBoxes", "mLightColor1", lightColor2);
            mDebugMats.SetMaterialParameter("DebugBoxes", "mLightColor2", lightColor3);
            mDebugMats.SetMaterialParameter("DebugBoxes", "mSolidColour", blueColor);
            mDebugMats.SetMaterialParameter("DebugBoxes", "mSpecPower", 1);
            mDebugMats.SetMaterialParameter("DebugBoxes", "mSpecColor", Vector4.One);

            mNumStreamThreads = 2;
            mGroundPos.Y      = 3000f;                  //start above
            mCellGridMax      = mTerrain.GetCellGridMax();
            mBoundary         = mTerrain.GetBoundary();

            Viewport vp = mGD.GetScreenViewPort();

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

            mGD.SetClip(0.1f, FogEnd);

            mDrawRays = new DrawRays(mGD, mSK);
            mOtherCam = new GameCamera(vp.Width, vp.Height, vp.Width / (float)vp.Height, 0.1f, FogEnd);
        }
Пример #2
0
        internal void TBuild(int gridSize, int chunkSize, float medianHeight,
                             float variance, int polySize, int tilingIterations, float borderSize,
                             int smoothPasses, int seed, int erosionIterations,
                             float rainFall, float solubility, float evaporation, int threads)
        {
            mFracFact = new FractalFactory(variance, medianHeight, gridSize + 1, gridSize + 1);

            mNumStreamThreads = threads;

            float   [,] fract = mFracFact.CreateFractal(seed);

            for (int i = 0; i < smoothPasses; i++)
            {
                FractalFactory.SmoothPass(fract);
            }

            for (int i = 0; i < tilingIterations; i++)
            {
                float borderSlice = borderSize / tilingIterations;

                FractalFactory.MakeTiled(fract, borderSlice * (i + 1));
            }

            if (erosionIterations > 0)
            {
                int realIterations = FractalFactory.Erode(fract, mRand,
                                                          erosionIterations, rainFall, solubility, evaporation);

                //redo tiling if eroded
                for (int i = 0; i < tilingIterations; i++)
                {
                    float borderSlice = borderSize / tilingIterations;

                    FractalFactory.MakeTiled(fract, borderSlice * (i + 1));
                }
            }

            Vector3 [,] norms = mFracFact.BuildNormals(fract, polySize);

            if (mTModel != null)
            {
                mTModel.FreeAll();
            }
            mTModel = new TerrainModel(fract, polySize, gridSize);

            mCellGridMax = gridSize / chunkSize;

            List <HeightMap.TexData> tdata = new List <HeightMap.TexData>();

            float transHeight = 0f;

            if (mTerrain != null)
            {
                //grab a copy of the old texture data if any
                List <HeightMap.TexData> texOld = mTerrain.GetTextureData(out transHeight);

                //clone it because it is about to all get nuked
                foreach (HeightMap.TexData td in texOld)
                {
                    HeightMap.TexData td2 = new HeightMap.TexData(td);
                    tdata.Add(td2);
                }
                mTerrain.FreeAll();
            }
            mTerrain = new Terrain(fract, norms, polySize, chunkSize, mCellGridMax);

            mTerrain.SetTextureData(tdata, transHeight);

            mBoundary = chunkSize * polySize;

            WrapGridCoordinates();

            mTerrain.SetCellCoord(mGridCoordinate);

            mTerrain.BuildGrid(mGD, mChunkRange, mNumStreamThreads);

            mPos.Y = mTModel.GetHeight(mPos) + 200f;

            mTerrain.UpdatePosition(mPos, mTerMats);

            //clamp box heights
            mTModel.FixBoxHeights();

            //clear existing
            if (mQTreeBoxes != null)
            {
                mQTreeBoxes.Free();
            }

            //turn on to debug quadtree
            //careful about big map sizes
//			List<BoundingBox>	boxes	=mTModel.GetAllBoxes();
//			mQTreeBoxes	=PrimFactory.CreateCubes(mGD.GD, boxes);
        }