public void LoadDataFromBitmapFile(string path) { try { using (var bmpTemp = new Bitmap(path)) { LockBitmap lockBitmap = new LockBitmap(new Bitmap(bmpTemp), false); int width = lockBitmap.Width, height = lockBitmap.Height; Data = new RenderData(width, height); for (int i = 0; i < width * height; i++) { Data.TextureMap[i] = lockBitmap.Data[i * 4 + 0]; Data.HeightMap[i] = lockBitmap.Data[i * 4 + 1]; } } Console.WriteLine("LoadData: " + path); } catch (Exception e) { Console.WriteLine("LoadData: " + path + " Failed " + e.Message); } }
//Rendering the part of image from heightmap (elevate and apply textures & shadows) private void elevate(RenderData input, LockBitmap resultLB, float start, float end) { byte[] resultRGB = resultLB.Data; int widthSrc = input.Width, heightSrc = input.Height, widthDst = resultLB.Width, heightDst = resultLB.Height; for (int ix = (int)(widthSrc * start); ix < (int)(widthSrc * end); ix++) //Upwards { for (int iy = (heightSrc - 1); iy >= 0; iy -= 1) //Downwards { //get positions int offSrc = (ix + iy * widthSrc); int offDst = (ix + iy / 2 * widthDst) * 4; //height > 0 if (input.HeightMap[offSrc] > 0) { //get colorList & find color pos byte[] refColor = TexturePack.Textures[input.TextureMap[offSrc]].Data; int colorSize = refColor[0] - refColor[1]; int colorStart = 0; int colorListPos = -1; int colorPos = 0; while (colorStart < input.HeightMap[offSrc]) { colorListPos++; if (colorListPos >= colorSize) { colorListPos = 0; } colorStart += refColor[2 + colorListPos * 5]; } colorPos = refColor[2 + colorListPos * 5] - (colorStart - input.HeightMap[offSrc]); int iz = input.HeightMap[offSrc]; while (iz > 0) //Downwards { //Repeat until color is changed | ground reached while (iz > 0 && colorPos > 0) { //save if ((iy + heightExcess) - iz >= 0) { //get position on z axe int offDstZ = offDst - (widthDst * iz * 4) + widthDst * heightExcess * 4; //pos + curent height //pixel not yet drawn if (resultRGB[offDstZ + 3] == 0) { //draw pixel float shadow = 1f; if (iz < input.ShadowMap[offSrc] + 1) { shadow = 0.75f; } //resultRGB[offDstZ + 0] = (byte)(255 * shadow);//b //resultRGB[offDstZ + 1] = (byte)(255 * (iz % 5) * shadow);//g //resultRGB[offDstZ + 2] = (byte)(0 * shadow);//r //resultRGB[offDstZ + 3] = (byte)(255);//a resultRGB[offDstZ + 0] = (byte)(refColor[5 + colorListPos * 5] * shadow); //b resultRGB[offDstZ + 1] = (byte)(refColor[4 + colorListPos * 5] * shadow); //g resultRGB[offDstZ + 2] = (byte)(refColor[3 + colorListPos * 5] * shadow); //r resultRGB[offDstZ + 3] = (byte)(refColor[6 + colorListPos * 5]); //a } else { iz = 0; break; } } //get next z & color pos iz--; colorPos--; } //get next color colorListPos--; if (colorListPos < 0) { colorListPos = colorSize - 1; } colorPos = refColor[2 + colorListPos * 5]; } } } } }