void UpdateTexture() { lock ( renderBufferLock ) { if (renderBuffer != null && renderBufferForSize == ViewSize && renderBuffer.Length == ViewSize.X * ViewSize.Y * 4) { try { HardwarePixelBuffer pixelBuffer = texture.GetBuffer(); pixelBuffer.Lock(HardwareBuffer.LockOptions.Discard); PixelBox pixelBox = pixelBuffer.GetCurrentLock(); unsafe { fixed(byte *ptr = renderBuffer) pixelBox.WriteDataUnmanaged((IntPtr)ptr, ViewSize.X, ViewSize.Y); } pixelBuffer.Unlock(); } catch (Exception ex) { Log.Error("WebBrowserControl: Caught exception in UpdateTexture(): " + ex.Message); } } } }
/// <summary> /// Bitmaps to texture. /// </summary> /// <param name="bmp">The BMP.</param> /// <param name="texture">The texture.</param> public unsafe static void BitmapToTexture(Bitmap bitmap, Texture texture) { var bmp = bitmap.Clone(new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height), System.Drawing.Imaging.PixelFormat.Format32bppArgb); var bitmapData = bmp.LockBits( new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadOnly, bmp.PixelFormat); HardwarePixelBuffer buffer = texture.GetBuffer(); buffer.Lock(Axiom.Graphics.BufferLocking.Discard); PixelBox pixelBox = buffer.CurrentLock; UInt32 *texData = (UInt32 *)pixelBox.Data; UInt32 *mapData = (UInt32 *)bitmapData.Scan0; var height = Math.Min(pixelBox.Height, bmp.Height); var width = Math.Min(pixelBox.Width, bmp.Width); for (var y = 0; y < height; ++y) { for (var x = 0; x < width; ++x) { texData[pixelBox.RowPitch * y + x] = mapData[bitmapData.Stride / 4 * y + x]; } } buffer.Unlock(); bmp.UnlockBits(bitmapData); }
/// <summary> /// /// </summary> /// <param name="c"></param> /// <param name="texId"></param> /// <param name="nx"></param> /// <param name="ny"></param> /// <param name="nz"></param> private unsafe void UpdateVolTextureData(Cell[,,] c, VolTextureId texId, int nx, int ny, int nz) { HardwarePixelBuffer buffer = _volTextures[(int)texId].GetBuffer(0, 0); buffer.Lock(HardwareBuffer.LockOptions.HBL_DISCARD); PixelBox pb = buffer.CurrentLock; uint x = 0; uint y = 0; uint z = 0; unsafe { byte *pbptr = (byte *)pb.data; for (z = pb.box.front; z < pb.box.back; z++) { for (y = pb.box.top; y < pb.box.bottom; y++) { for (x = pb.box.left; x < pb.box.right; x++) { PixelConverter.PackColour(0, 0, (uint)c[x, y, z].Dens, (uint)c[x, y, z].Light, PixelFormat.PF_BYTE_RGB, &pbptr[x]); } pbptr += pb.rowPitch; } pbptr += pb.SliceSkip; } } buffer.Unlock(); }
protected void Generate() { var julia = new Julia(this.globalReal, this.globalImag, this.globalTheta); float scale = 2.5f; float vcut = 29.0f; float vscale = 1.0f / vcut; HardwarePixelBuffer buffer = this.ptex.GetBuffer(0, 0); LogManager.Instance.Write("Volume Texture Sample [info]: HardwarePixelBuffer " + buffer.Width + "x" + buffer.Height); buffer.Lock(BufferLocking.Normal); PixelBox pb = buffer.CurrentLock; LogManager.Instance.Write("Volume Texture Sample [info]: PixelBox " + pb.Width + "x" + pb.Height + "x" + pb.Depth); unsafe { var pbptr = (BufferBase)pb.Data.Clone(); for (int z = pb.Front; z < pb.Back; z++) { for (int y = pb.Top; y < pb.Bottom; y++) { pbptr += pb.Left * sizeof(uint); for (int x = pb.Left; x < pb.Right; x++) { if (z == pb.Front || z == (pb.Back - 1) || y == pb.Top || y == (pb.Bottom - 1) || x == pb.Left || x == (pb.Right - 1)) { pbptr.ToUIntPointer()[0] = 0; } else { float val = julia.Eval(((float)x / pb.Width - 0.5f) * scale, ((float)y / pb.Height - 0.5f) * scale, ((float)z / pb.Depth - 0.5f) * scale); if (val > vcut) { val = vcut; } PixelConverter.PackColor((float)x / pb.Width, (float)y / pb.Height, (float)z / pb.Depth, (1.0f - (val * vscale)) * 0.7f, PixelFormat.A8R8G8B8, pbptr); } pbptr++; } pbptr += (pb.RowPitch - pb.Right) * sizeof(uint); } pbptr += pb.SliceSkip * sizeof(uint); } buffer.Unlock(); } }