public void DrawMinimap(int gx, int gy) { ///////////////////////////////// // Write minimapColor to texture ///////////////////////////////// #if false // BUGGY: assumes texture pitch = texture width, which DirectX does NOT garantee. // convert colors to bytes and write all at once (much faster than convert+writing one by one) int iByte = 0; for (int y = 0; y < RogueGame.MINITILE_SIZE * RogueGame.MAP_MAX_HEIGHT; y++) { for (int x = 0; x < RogueGame.MINITILE_SIZE * RogueGame.MAP_MAX_WIDTH; x++) { m_MinimapBytes[iByte++] = m_MinimapColors[x, y].B; m_MinimapBytes[iByte++] = m_MinimapColors[x, y].G; m_MinimapBytes[iByte++] = m_MinimapColors[x, y].R; m_MinimapBytes[iByte++] = m_MinimapColors[x, y].A; } } GraphicsStream gs = m_MinimapTexture.LockRectangle(0, LockFlags.None); gs.Write(m_MinimapBytes, 0, m_MinimapBytes.Length); m_MinimapTexture.UnlockRectangle(0); #endif #if true // correct way to do it, uses pitch. int pitch; // pitch = width of texture in memory, does not necessary equal to texture.Width. const int bytesPerPixel = 4; // A8R8G8B8 GraphicsStream gs = m_MinimapTexture.LockRectangle(0, LockFlags.None, out pitch); for (int y = 0; y < RogueGame.MINITILE_SIZE * RogueGame.MAP_MAX_HEIGHT; y++) { for (int x = 0; x < RogueGame.MINITILE_SIZE * RogueGame.MAP_MAX_WIDTH; x++) { Color color = m_MinimapColors[x, y]; gs.Position = (y * pitch) + (x * bytesPerPixel); gs.WriteByte(color.B); gs.WriteByte(color.G); gs.WriteByte(color.R); gs.WriteByte(color.A); } } m_MinimapTexture.UnlockRectangle(0); #endif /////////// // Add gfx. /////////// m_Gfxs.Add(new GfxSprite(m_Sprite, new SizeF(RogueGame.MINITILE_SIZE * RogueGame.MAP_MAX_WIDTH, RogueGame.MINITILE_SIZE * RogueGame.MAP_MAX_HEIGHT), RogueGame.MINITILE_SIZE * RogueGame.MAP_MAX_WIDTH, RogueGame.MINITILE_SIZE * RogueGame.MAP_MAX_HEIGHT, m_MinimapTexture, Color.White, gx, gy)); NeedRedraw = true; }
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; } }
public void toTextureArray(ref List <Texture> mTempAlphaTextures, int minXVert, int minZVert) { bool doBlendedFill = true; //lock in our alpha texture int slicePitch = (int)(BTerrainTexturing.getAlphaTextureWidth() * BTerrainTexturing.getAlphaTextureHeight()); int count = Math.Min(mTempAlphaTextures.Count, mLayers.Count); int width = (int)BTerrainTexturing.getAlphaTextureWidth(); int height = (int)BTerrainTexturing.getAlphaTextureHeight(); byte[] tempLargerImg = new byte[(width + 2) * (height + 2)]; byte[] bordered = new byte[width * height]; int i = 0; for (i = 0; i < count; i++) { if (mTempAlphaTextures[i] != null) { GraphicsStream texstream = mTempAlphaTextures[i].LockRectangle(0, LockFlags.None); if (i == 0) { for (int k = 0; k < slicePitch; k++) { texstream.WriteByte(255); } } else { if (doBlendedFill) { fillCreateLayer(mLayers[i].mAlphaLayer, mLayers[i].mActiveTextureIndex, mLayers[i].mLayerType, minXVert, minZVert, tempLargerImg, bordered); texstream.Write(bordered, 0, slicePitch); } else { texstream.Write(mLayers[i].mAlphaLayer, 0, slicePitch); } } mTempAlphaTextures[i].UnlockRectangle(0); texstream.Close(); } else { } } //we've got more layers than we've preallocated if (mTempAlphaTextures.Count < mLayers.Count) { int diff = mLayers.Count - mTempAlphaTextures.Count; for (int k = 0; k < diff; k++) { mTempAlphaTextures.Add(new Texture(BRenderDevice.getDevice(), (int)BTerrainTexturing.getAlphaTextureWidth(), (int)BTerrainTexturing.getAlphaTextureHeight(), 1, 0, Format.L8, Pool.Managed)); GraphicsStream texstream = mTempAlphaTextures[mTempAlphaTextures.Count - 1].LockRectangle(0, LockFlags.None); if (doBlendedFill) { fillCreateLayer(mLayers[i + k].mAlphaLayer, mLayers[i + k].mActiveTextureIndex, mLayers[i + k].mLayerType, minXVert, minZVert, tempLargerImg, bordered); texstream.Write(bordered, 0, slicePitch); } else { texstream.Write(mLayers[i + k].mAlphaLayer, 0, slicePitch); } mTempAlphaTextures[mTempAlphaTextures.Count - 1].UnlockRectangle(0); texstream.Close(); } } tempLargerImg = null; }
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); } }