//---------------------------------- public void recalculateHeights() { //generate our heights representation from the terrain, //then add our jagged differences. int xRes = 256; float[] gpuHeightRepArray = new float[xRes * xRes]; for (uint i = 0; i < xRes * xRes; i++) { gpuHeightRepArray[i] = float.MinValue; } HeightsGen hg = new HeightsGen(); hg.computeHeightFieldDirectFloat((uint)xRes, (uint)xRes, true, -BMathLib.unitX, true, ref gpuHeightRepArray); hg.destroy(); hg = null; //resize our terrain input to our local input. float[] tmpHeightRes = ImageManipulation.resizeF32Img(gpuHeightRepArray, xRes, xRes, (int)mWidth - 1, (int)mHeight - 1, ImageManipulation.eFilterType.cFilter_Linear); // mHeights = null; // mHeights = ImageManipulation.resizeF32Img(gpuHeightRepArray, xRes, xRes, (int)mWidth , (int)mHeight, ImageManipulation.eFilterType.cFilter_Linear); //new float[mWidth * mHeight]; for (int x = 0; x < mWidth - 1; x++) { for (int z = 0; z < mHeight - 1; z++) { mHeights[x * mWidth + z] = tmpHeightRes[x * (mWidth - 1) + z]; //mColors[x * mWidth + z] = 0x440000FF; //(uint)Rand.mRandom.Next();//0x440000FF; } } //fill our edges with the previous height val. for (int q = 0; q < mWidth - 1; q++) { mHeights[(mWidth - 1) * mWidth + q] = mHeights[(mWidth - 2) * mWidth + q]; mHeights[q * mWidth + (mWidth - 1)] = mHeights[q * mWidth + (mWidth - 2)]; } //lets choose the max value between two samples.. int strideStep = (int)(xRes / (mWidth - 1)); for (int i = 0; i < mWidth; i++) { for (int j = 0; j < mHeight; j++) { int offX = (int)BMathLib.Clamp((i * strideStep), 0, xRes - 1); int offZ = (int)BMathLib.Clamp((j * strideStep), 0, xRes - 1); float highRes = gpuHeightRepArray[offX + xRes * offZ]; if (highRes > mHeights[i + mWidth * j]) { mHeights[i + mWidth * j] = highRes; } } } tmpHeightRes = null; gpuHeightRepArray = null; recalculateBBs(); recalculateVisuals(); }
public void recalculateHeights(bool recalcVis, bool doExtraRaycastCalc) { //generate our heights representation from the terrain, //then add our jagged differences. int xRes = 256; float[] gpuHeightRepArray = new float[xRes * xRes]; for (uint i = 0; i < xRes * xRes; i++) { gpuHeightRepArray[i] = float.MinValue; } HeightsGen hg = new HeightsGen(); hg.computeHeightFieldDirectFloat((uint)xRes, (uint)xRes, true, -BMathLib.unitX, true, ref gpuHeightRepArray); hg.destroy(); hg = null; //resize our terrain input to our local input. // mHeights = null; //mHeights = ImageManipulation.resizeF32Img(gpuHeightRepArray, xRes, xRes, (int)mWidth, (int)mHeight, ImageManipulation.eFilterType.cFilter_Linear); //new float[mWidth * mHeight]; float[] tmpHeightRes = ImageManipulation.resizeF32Img(gpuHeightRepArray, xRes, xRes, (int)mWidth - 1, (int)mHeight - 1, ImageManipulation.eFilterType.cFilter_Linear); for (int x = 0; x < mWidth - 1; x++) { for (int z = 0; z < mHeight - 1; z++) { mHeights[x * mWidth + z] = tmpHeightRes[x * (mWidth - 1) + z]; } } //fill our edges with the previous height val. for (int q = 0; q < mWidth - 1; q++) { mHeights[(mWidth - 1) * mWidth + q] = mHeights[(mWidth - 2) * mWidth + q]; mHeights[q * mWidth + (mWidth - 1)] = mHeights[q * mWidth + (mWidth - 2)]; } //Let's be douchebagish about this.. if (doExtraRaycastCalc) { // CLM [11.10.08] Disabled due to problems called by rays not being 100% accurate //{ // mWorkInc = 4; // ThreadPool.QueueUserWorkItem(new WaitCallback(calcHeightRaycastWorker), new heightGenWorkerData(0, 0, mWidth >> 1, mHeight >> 1)); // ThreadPool.QueueUserWorkItem(new WaitCallback(calcHeightRaycastWorker), new heightGenWorkerData(1 + (mWidth >> 1), 0, mWidth, mHeight >> 1)); // ThreadPool.QueueUserWorkItem(new WaitCallback(calcHeightRaycastWorker), new heightGenWorkerData(0, 1 + (mHeight >> 1), mWidth >> 1, mHeight)); // ThreadPool.QueueUserWorkItem(new WaitCallback(calcHeightRaycastWorker), new heightGenWorkerData(1 + (mWidth >> 1), 1 + (mHeight >> 1), mWidth, mHeight)); //} { mWorkInc = 1; ThreadPool.QueueUserWorkItem(new WaitCallback(calcHeightRaycastWorker), new heightGenWorkerData(0, 0, mWidth, mHeight)); } mWorkCompleted.WaitOne(); } tmpHeightRes = null; gpuHeightRepArray = null; recalculateBBs(); if (recalcVis) { recalculateVisuals(); } }