public Texture GenerateTexture(Size size, DataArea area, Device gDevice, GDALReader dataSrc) { Texture texture = new Texture(gDevice, size.Width, size.Height, 0, Usage.None, Format.A8R8G8B8, Pool.Managed); lock (this) { // find bands to use int r, g, b; dataSrc.Info.GetRGABands(out r, out g, out b); Size nativeSz = dataSrc.Info.Resolution; Band rBand = dataSrc.GetRasterBand(r + 1); Band gBand = dataSrc.GetRasterBand(g + 1); Band bBand = dataSrc.GetRasterBand(b + 1); // Note: This *should* work rData = new byte[nativeSz.Width * nativeSz.Height]; gData = new byte[nativeSz.Width * nativeSz.Height]; bData = new byte[nativeSz.Width * nativeSz.Height]; rBand.ReadRaster(0, 0, nativeSz.Width, nativeSz.Height, rData, nativeSz.Width, nativeSz.Height, 0, 0); gBand.ReadRaster(0, 0, nativeSz.Width, nativeSz.Height, gData, nativeSz.Width, nativeSz.Height, 0, 0); bBand.ReadRaster(0, 0, nativeSz.Width, nativeSz.Height, bData, nativeSz.Width, nativeSz.Height, 0, 0); ftScaleTemp = new SizeF(1, 1);// (float)area.Area.Width / area.DataSize.Width, (float)area.Area.Height / area.DataSize.Height); ftScaleTemp = new SizeF(ftScaleTemp.Width * area.Area.Width, ftScaleTemp.Height * area.Area.Height); ftShiftTemp = new Size(area.Area.Location); ftNativeSz = nativeSz; if (area.Data is byte[]) TextureLoader.FillTexture(texture, FillTextureByte); /*else if (area.Data is float[]) TextureLoader.FillTexture(texture, FillTextureFloat);*/ } return texture; }
public static Bitmap SampleRGBDiffuseMap(GDALReader dataSrc, Size size) { // find bands to use int r, g, b; dataSrc.Info.GetRGABands(out r, out g, out b); Size nativeSz = dataSrc.Info.Resolution; Band rBand = dataSrc.GetRasterBand(r + 1); Band gBand = dataSrc.GetRasterBand(g + 1); Band bBand = dataSrc.GetRasterBand(b + 1); // Note: This *should* work byte[] rData = new byte[size.Width * size.Height]; byte[] gData = new byte[size.Width * size.Height]; byte[] bData = new byte[size.Width * size.Height]; rBand.ReadRaster(0, 0, nativeSz.Width, nativeSz.Height, rData, size.Width, size.Height, 0, 0); gBand.ReadRaster(0, 0, nativeSz.Width, nativeSz.Height, gData, size.Width, size.Height, 0, 0); bBand.ReadRaster(0, 0, nativeSz.Width, nativeSz.Height, bData, size.Width, size.Height, 0, 0); // combine channel samples into image Bitmap bitmap = new Bitmap(size.Width, size.Height, PixelFormat.Format24bppRgb); BitmapData data = bitmap.LockBits(new Rectangle(new Point(), size), ImageLockMode.WriteOnly, PixelFormat.Format24bppRgb); unsafe { // write data to each pixel Bitmap24bitRGBPixel* pixels = (Bitmap24bitRGBPixel*)data.Scan0; for (int i = 0; i < rData.Length; i++) { pixels->R = bData[i]; pixels->G = gData[i]; pixels->B = rData[i]; pixels++; } } bitmap.UnlockBits(data); return bitmap; }
public Texture GenerateTexture(Size size, DataArea area, Device gDevice, GDALReader dataSrc) { Texture texture = new Texture(gDevice, size.Width, size.Height, 0, Usage.None, Format.A8R8G8B8, Pool.Managed); lock (this) { // find bands to use int r, g, b; dataSrc.Info.GetRGABands(out r, out g, out b); Size nativeSz = dataSrc.Info.Resolution; Band rBand = dataSrc.GetRasterBand(r + 1); Band gBand = dataSrc.GetRasterBand(g + 1); Band bBand = dataSrc.GetRasterBand(b + 1); rData = new byte[nativeSz.Width * nativeSz.Height]; gData = new byte[nativeSz.Width * nativeSz.Height]; bData = new byte[nativeSz.Width * nativeSz.Height]; rBand.ReadRaster(0, 0, nativeSz.Width, nativeSz.Height, rData, nativeSz.Width, nativeSz.Height, 0, 0); gBand.ReadRaster(0, 0, nativeSz.Width, nativeSz.Height, gData, nativeSz.Width, nativeSz.Height, 0, 0); bBand.ReadRaster(0, 0, nativeSz.Width, nativeSz.Height, bData, nativeSz.Width, nativeSz.Height, 0, 0); /*ftScaleTemp = new SizeF(1, 1);// (float)area.Area.Width / area.DataSize.Width, (float)area.Area.Height / area.DataSize.Height); ftScaleTemp = new SizeF(ftScaleTemp.Width * area.Area.Width, ftScaleTemp.Height * area.Area.Height); ftShiftTemp = new Size(area.Area.Location); ftNativeSz = nativeSz;*/ ftScaleTemp = new SizeF((float)area.Area.Width / size.Width, (float)area.Area.Height / size.Height); ftShiftTemp = new Size(area.Area.Location); if (area.Data is byte[]) { // to heights //TextureLoader.FillTexture(texture, FillTextureByteToHeights); // generate normal acc ftTexSz = size; //texStream = texture.LockRectangle(0, LockFlags.None); /*Texture normalTex = new Texture(gDevice, size.Width, size.Height, 0, Usage.None, Format.A8R8G8B8, Pool.Managed); TextureLoader.FillTexture(normalTex, FillTextureByteToNormalAcc);*/ heights = new float[size.Width * size.Height]; int index = 0; //SizeF nativeScale = new SizeF((float)nativeSz.Width / size.Width, (float)nativeSz.Height / size.Height); for (int y = 0; y < size.Height; y++) { int yNative = (int)(ftShiftTemp.Height + (y * ftScaleTemp.Width)); for (int x = 0; x < size.Width; x++) { int xNative = (int)(ftShiftTemp.Width + (x * ftScaleTemp.Width)); int indexNative = (yNative * nativeSz.Width) + xNative; float rValue = rData[indexNative] / 255f; float gValue = gData[indexNative] / 255f; float bValue = bData[indexNative] / 255f; heights[index++] = (rValue + gValue + bValue) / 3f; } } /*texture.UnlockRectangle(0); texture.Dispose();*/ // calculate normals Vector3[] normals; GenerateMap(heights, size, out normals); // write to texture texStream = texture.LockRectangle(0, LockFlags.None); foreach (Vector3 normal in normals) { texStream.WriteByte((byte)(((normal.Z / 2) + 0.5f) * 255)); texStream.WriteByte((byte)(((-normal.Y / 2) + 0.5f) * 255)); texStream.WriteByte((byte)(((normal.X / 2) + 0.5f) * 255)); texStream.WriteByte(255); } /*foreach (float height in heights) { texStream.WriteByte(0); texStream.WriteByte((byte)(height * 255)); texStream.WriteByte(0); texStream.WriteByte(255); }*/ texture.UnlockRectangle(0); } return texture; } }