Пример #1
0
 public void destroy()
 {
     if (mTempTargetSurfFLOAT != null)
     {
         mTempTargetSurfFLOAT.Dispose();
         mTempTargetSurfFLOAT = null;
     }
     if (mTempTargetTexFLOAT != null)
     {
         mTempTargetTexFLOAT.Dispose();
         mTempTargetTexFLOAT = null;
     }
     if (mDepthBufferC != null)
     {
         mDepthBufferC.Dispose();
         mDepthBufferC = null;
     }
     if (pDepthStencilSurf != null)
     {
         pDepthStencilSurf.Dispose();
         pDepthStencilSurf = null;
     }
     if (mOC != null)
     {
         mOC.Dispose();
         mOC = null;
     }
     if (mDepthArray != null)
     {
         for (int x = 0; x < mWidth; x++)
         {
             for (int y = 0; y < mHeight; y++)
             {
                 mDepthArray[x, y].mDepths.Clear();
                 mDepthArray[x, y] = null;
             }
         }
         mDepthArray = null;
     }
     ExportTo360.checkUnloadRender();
 }
Пример #2
0
            public void init(int width, int height)
            {
                ExportTo360.checkLoadRender();

                mWidth  = width;
                mHeight = height;

                loadShader();
                initTextures();

                mOC = new Query(BRenderDevice.getDevice(), QueryType.Occlusion);

                mDepthArray = new fragmentDepths[width, height];
                for (int x = 0; x < width; x++)
                {
                    for (int y = 0; y < height; y++)
                    {
                        mDepthArray[x, y]         = new fragmentDepths();
                        mDepthArray[x, y].mDepths = new List <float>();
                    }
                }
            }
Пример #3
0
        //-----------------------------------------------------------------------------------
        //-----------------------------------------------------------------------------------

        public void computeHeightFieldDirectFloat(uint width, uint height, bool includeSimMod, Vector3 upDir, bool onlyDoOneSample, ref float[] output)
        {
            copyTempD3DValues();
            TerrainGlobals.getTerrain().getQuadNodeRoot().clearVisibleDatHandle();
            TerrainGlobals.getVisual().destroyAllHandles();
            TerrainGlobals.getVisual().setHandleGenMode(true);
            GC.WaitForPendingFinalizers();

            ExportTo360.checkLoadRender();

            createTempTextures(width, height);

            calculateWorldBounds(includeSimMod);


            mHeightFieldAttributes = new BHeightFieldAttributes(width, height);

            Matrix       worldToView = Matrix.Identity;
            Matrix       viewToProj = Matrix.Identity;
            BBoundingBox worldBounds = new BBoundingBox();
            double       worldMinY = 0, worldMaxY = 0;

            computeTransforms(upDir, ref worldToView, ref viewToProj, ref worldBounds, ref worldMinY, ref worldMaxY);

            float mWorldRangeY = (float)(mWorldMax.Y - mWorldMin.Y);

            uint numVals = width * height;

            if (output == null || output.Length != numVals)
            {
                output = new float[numVals];
                for (uint i = 0; i < numVals; i++)
                {
                    output[i] = float.MinValue;
                }
            }

            uint   cNumSamples = (uint)(onlyDoOneSample?1:4);
            Random rand        = new Random();

            for (uint sampleIndex = 0; sampleIndex < cNumSamples; sampleIndex++)
            {
                float xOfs = sampleIndex != 0 ? (float)((rand.NextDouble() * 2) - 1) : 0.0f;
                float yOfs = sampleIndex != 0 ? (float)((rand.NextDouble() * 2) - 1) : 0.0f;

                Matrix projOfs = Matrix.Translation(xOfs / width, yOfs / height, 0.0f);

                generateHeightBuffer(ref pHighDepthSurf, width, height, true, worldToView, viewToProj * projOfs, includeSimMod);

                #region fill & lock

                //mWorldMin.Y += 1.0f;
                //mWorldMax.Y -= 1.0f;

                unsafe
                {
                    GraphicsStream texstreamHI = pHighDepths.LockRectangle(0, LockFlags.None);
                    float *        pSrcHigh    = (float *)texstreamHI.InternalDataPointer;

                    //copy the data out so we can screw with it.
                    for (uint c = 0; c < numVals; c++)
                    {
                        output[c] = (float)Math.Max(output[c], pSrcHigh[c]);
                    }
                    pHighDepths.UnlockRectangle(0);
                }
                //find the max & min depth for this slice


                #endregion
            }



            releaseTempTextures();
            restoreTempD3DValues();
            TerrainGlobals.getVisual().setHandleGenMode(false);
            ExportTo360.checkUnloadRender();


            //#region dumpHeightfiled
            //{
            //   FileStream s = File.Open("_outHiDirect.raw", FileMode.OpenOrCreate, FileAccess.Write);
            //   BinaryWriter f = new BinaryWriter(s);
            //   for (int i = 0; i < width * height; i++)
            //   {
            //      float kp = (output[i] - mWorldMin.Y) / mWorldRangeY;
            //      ushort op = (ushort)(kp * ushort.MaxValue);
            //      f.Write(op);
            //   }
            //   f.Close();
            //   s.Close();
            //}

            //#endregion
        }
Пример #4
0
        public void computeHeightField(uint width, uint height, bool includeSimMod, Vector3 upDir, uint cNumSamples)
        {
            copyTempD3DValues();
            TerrainGlobals.getTerrain().getQuadNodeRoot().clearVisibleDatHandle();
            TerrainGlobals.getVisual().destroyAllHandles();
            TerrainGlobals.getVisual().setHandleGenMode(true);
            GC.WaitForPendingFinalizers();

            ExportTo360.checkLoadRender();

            createTempTextures(width, height);

            calculateWorldBounds(includeSimMod);


            mHeightFieldAttributes = new BHeightFieldAttributes(width, height);

            Matrix       worldToView = Matrix.Identity;
            Matrix       viewToProj = Matrix.Identity;
            BBoundingBox worldBounds = new BBoundingBox();
            double       worldMinY = 0, worldMaxY = 0;

            computeTransforms(upDir, ref worldToView, ref viewToProj, ref worldBounds, ref worldMinY, ref worldMaxY);

            Random rand = new Random();

            double minDepth = float.MaxValue;
            double maxDepth = float.MinValue;



            cNumSamples = (uint)BMathLib.Clamp(cNumSamples, 1, 32);

            for (uint sampleIndex = 0; sampleIndex < cNumSamples; sampleIndex++)
            {
                float xOfs = sampleIndex != 0 ? (float)((rand.NextDouble() * 2) - 1) : 0.0f;
                float yOfs = sampleIndex != 0 ? (float)((rand.NextDouble() * 2) - 1) : 0.0f;

                Matrix projOfs = Matrix.Translation(xOfs / width, yOfs / height, 0.0f);

                generateDepthBuffer(ref pHighDepthSurf, width, height, false, worldToView, viewToProj * projOfs, includeSimMod);
                generateDepthBuffer(ref pLowDepthSurf, width, height, true, worldToView, viewToProj * projOfs, includeSimMod);

                fillHeightFieldTexture(width, height, mHeightFieldAttributes, pLowDepths, pHighDepths, ref minDepth, ref maxDepth);
            }

            viewToProj = viewToProj * Matrix.Translation(0.0f, 0.0f, (float)(-minDepth)) * Matrix.Scaling(1.0f, 1.0f, (float)(1.0f / (maxDepth - minDepth)));
            double newWorldMinY = worldMinY + (worldMaxY - worldMinY) * minDepth;

            worldMaxY = worldMinY + (worldMaxY - worldMinY) * maxDepth;
            worldMinY = newWorldMinY;
            minDepth  = 0.0f;
            maxDepth  = 1.0f;

            mHeightFieldAttributes.mWidth  = width;
            mHeightFieldAttributes.mHeight = height;
            mHeightFieldAttributes.mBounds = worldBounds;

            mHeightFieldAttributes.mWorldMinY   = (float)worldMinY;
            mHeightFieldAttributes.mWorldMaxY   = (float)worldMaxY;
            mHeightFieldAttributes.mWorldRangeY = (float)(worldMaxY - worldMinY);


            float minZ = 0;
            float maxZ = 1;
            float x    = 0;
            float y    = 0;

            Matrix cMTProjToScreen = BMathLib.matrixFrom16floats(
                width * .5f, 0.0f, 0.0f, 0.0f,
                0.0f, height * -.5f, 0.0f, 0.0f,
                0.0f, 0.0f, maxZ - minZ, 0.0f,
                x + width * .5f, y + height * .5f, minZ, 1.0f);

            mHeightFieldAttributes.mWorldToNormZ = worldToView * viewToProj * cMTProjToScreen;


            Matrix screenToProj    = Matrix.Invert(cMTProjToScreen);
            Matrix projToView      = Matrix.Invert(viewToProj);
            Matrix cMTScreenToView = Matrix.Multiply(screenToProj, projToView);

            mHeightFieldAttributes.mNormZToWorld = cMTScreenToView * Matrix.Invert(worldToView);

            releaseTempTextures();
            restoreTempD3DValues();
            TerrainGlobals.getVisual().setHandleGenMode(false);
            ExportTo360.checkUnloadRender();

            //copy our pixels from the previous point
            {
                for (int q = 0; q < width; q++)
                {
                    mHeightFieldAttributes.mpTexelsLO[(width - 1) * width + q] = mHeightFieldAttributes.mpTexelsLO[(width - 2) * width + q];
                    mHeightFieldAttributes.mpTexelsLO[q * width + (width - 1)] = mHeightFieldAttributes.mpTexelsLO[q * width + (width - 2)];
                    mHeightFieldAttributes.mpTexelsLO[q]         = mHeightFieldAttributes.mpTexelsLO[width + q];
                    mHeightFieldAttributes.mpTexelsLO[q * width] = mHeightFieldAttributes.mpTexelsLO[q * width + (1)];

                    mHeightFieldAttributes.mpTexelsHI[(width - 1) * width + q] = mHeightFieldAttributes.mpTexelsHI[(width - 2) * width + q];
                    mHeightFieldAttributes.mpTexelsHI[q * width + (width - 1)] = mHeightFieldAttributes.mpTexelsHI[q * width + (width - 2)];
                    mHeightFieldAttributes.mpTexelsHI[q]         = mHeightFieldAttributes.mpTexelsHI[width + q];
                    mHeightFieldAttributes.mpTexelsHI[q * width] = mHeightFieldAttributes.mpTexelsHI[q * width + (1)];
                }
            }

            #region dumpHeightfiled
            //{
            //   FileStream s = File.Open(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + "\\out.raw", FileMode.OpenOrCreate, FileAccess.Write);
            //   BinaryWriter f = new BinaryWriter(s);
            //   for (int i = 0; i < width * height; i++)
            //   {
            //      f.Write(mHeightFieldAttributes.mpTexelsLO[i]);
            //      f.Write(mHeightFieldAttributes.mpTexelsHI[i]);
            //   }
            //   f.Close();
            //   s.Close();
            //}

            #endregion
        }