public void UpdateVolumeTexture(float[, ,] volumeData) { VolumeTexture tmp = _texture[0]; _texture[0] = _texture[1]; int temp = 0; int nz = volumeData.GetLength(0); if (texture != null) { texture.Dispose(); } texture = new VolumeTexture(DrawArgs.Device, _nx, _ny, nz, 1, Usage.None, Format.A8R8G8B8, Pool.Managed); DataBox db = texture.LockBox(0, LockFlags.None); BinaryWriter bw = new BinaryWriter(db.Data); bw.Seek(0, SeekOrigin.Begin); for (int l = 0; l < nz; l++) { for (int r = 0; r < _ny; r++) { for (int c = 0; c < _nx; c++) { temp = _dvr.ActualValueToTransferMapCoord(volumeData[l, r + _iy, c + _ix]); bw.Write(temp << 16); } } } texture.UnlockBox(0); _texture[1] = texture; tmp.Dispose(); GC.Collect(); }
public static void LoadSurfaceFromVolumeSlice(VolumeTexture volumeTex, int mip, int slice, Filter filter, Surface surface) { VolumeDescription vd = volumeTex.GetLevelDescription(mip); OpsFormatHelper formatHelp = OpsFormatHelper.FindByFormat(vd.Format); Texture sliceTex = new Texture(volumeTex.Device, vd.Width, vd.Height, 1, Usage.None, formatHelp.Format, Pool.SystemMemory); Box box = new Box(); box.Left = 0; box.Right = vd.Width; box.Top = 0; box.Bottom = vd.Height; box.Front = slice; box.Back = slice + 1; LockedBox volumeLB; GraphicsStream volumeData = volumeTex.LockBox(0, box, LockFlags.ReadOnly, out volumeLB); int slicePitch; GraphicsStream sliceData = sliceTex.LockRectangle(mip, LockFlags.None, out slicePitch); CopyTextureData(volumeData, vd.Width, vd.Height, formatHelp, volumeLB.RowPitch, sliceData, slicePitch); sliceTex.UnlockRectangle(0); volumeTex.UnlockBox(mip); SurfaceLoader.FromSurface(surface, sliceTex.GetSurfaceLevel(0), filter, 0); sliceTex.Dispose(); }
/// <summary> /// The device has been created. Resources that are not lost on /// Reset() can be created here -- resources in Pool.Managed, /// Pool.Scratch, or Pool.SystemMemory. Image surfaces created via /// CreateImageSurface are never lost and can be created here. Vertex /// shaders and pixel shaders can also be created here as they are not /// lost on Reset(). /// </summary> protected override void InitializeDeviceObjects() { // Initialize all of the fonts drawingFont.InitializeDeviceObjects(device); // Create a volume texture volume = new VolumeTexture(device, 16, 16, 16, 1, Format.A8R8G8B8, Pool.Managed); // Fill the volume texture int[,,] data = (int[, , ])volume.LockBox(typeof(int), 0, 0, 16, 16, 16); for (int w = 0; w < 16; w++) { for (int v = 0; v < 16; v++) { for (int u = 0; u < 16; u++) { float du = (u - 7.5f) / 7.5f; float dv = (v - 7.5f) / 7.5f; float dw = (w - 7.5f) / 7.5f; float fScale = (float)Math.Sqrt(du * du + dv * dv + dw * dw) / (float)Math.Sqrt(1.0f); if (fScale > 1.0f) { fScale = 0.0f; } else { fScale = 1.0f - fScale; } int r = (int)((w << 4) * fScale); int g = (int)((v << 4) * fScale); int b = (int)((u << 4) * fScale); data[w, v, u] = unchecked ((int)0xff000000 + (r << 16) + (g << 8) + (b)); } } } volume.UnlockBox(0); // Create a vertex buffer vertex = new VertexBuffer(typeof(VolumeVertex), 4, device, Usage.WriteOnly, VolumeVertex.Format, Pool.Managed); GraphicsStream vertStream = vertex.Lock(0, 0, 0); // Copy our vertices in vertStream.Write(vertices); vertex.Unlock(); }
private void GenerateTexture(double[,,] array) { int index = 0; byte[] buffer = new byte[4 * textureSizeX * textureSizeY * textureSizeZ]; for (int i = 0; i < textureSizeX; i++) { for (int j = 0; j < textureSizeY; j++) { for (int k = 0; k < textureSizeZ; k++) { float i1 = (float)(i * (float)(array.GetLength(0) - 1) / textureSizeX); float j1 = (float)(j * (float)(array.GetLength(1) - 1) / textureSizeY); float k1 = (float)(k * (float)(array.GetLength(2) - 1) / textureSizeZ); float value = MathHelper.GetValue(new Vector3(i1, j1, k1), array); byte[] color = RgbPalette.GetColorBytes(value, dataSource.Maximum, dataSource.Minimum, dataSource.MissingValue); if (value > dataSource.Maximum) { value = dataSource.Maximum; } if (value < dataSource.Minimum) { value = dataSource.Minimum; } buffer[index++] = color[3]; buffer[index++] = color[2]; buffer[index++] = color[1]; buffer[index++] = (byte)((value - dataSource.Minimum) / (dataSource.Maximum - dataSource.Minimum) * 255); } } } texture = new VolumeTexture(device, textureSizeX, textureSizeY, textureSizeZ, 1, Usage.Dynamic, Format.A8R8G8B8, Pool.Default); DataBox dbox = texture.LockBox(0, LockFlags.None); dbox.Data.WriteRange(buffer); texture.UnlockBox(0); }
private void GenerateTexture(double[, ,] array) { int index = 0; byte[] buffer = new byte[4 * textureSizeX * textureSizeY * textureSizeZ]; for (int i = 0; i < textureSizeX; i++) { for (int j = 0; j < textureSizeY; j++) { for (int k = 0; k < textureSizeZ; k++) { float i1 = (float)(i * (float)(array.GetLength(0) - 1) / textureSizeX); float j1 = (float)(j * (float)(array.GetLength(1) - 1) / textureSizeY); float k1 = (float)(k * (float)(array.GetLength(2) - 1) / textureSizeZ); float value = MathHelper.GetValue(new Vector3(i1, j1, k1), array); byte[] color = RgbPalette.GetColorBytes(value, dataSource.Maximum, dataSource.Minimum, dataSource.MissingValue); if (value > dataSource.Maximum) value = dataSource.Maximum; if (value < dataSource.Minimum) value = dataSource.Minimum; buffer[index++] = color[3]; buffer[index++] = color[2]; buffer[index++] = color[1]; buffer[index++] = (byte)((value - dataSource.Minimum) / (dataSource.Maximum - dataSource.Minimum) * 255); } } } texture = new VolumeTexture(device, textureSizeX, textureSizeY, textureSizeZ, 1, Usage.Dynamic, Format.A8R8G8B8, Pool.Default); DataBox dbox = texture.LockBox(0, LockFlags.None); dbox.Data.WriteRange(buffer); texture.UnlockBox(0); }
public static void LoadSurfaceFromVolumeSlice( VolumeTexture volumeTex, int mip, int slice, Filter filter, Surface surface) { VolumeDescription vd = volumeTex.GetLevelDescription(mip); OpsFormatHelper formatHelp = OpsFormatHelper.FindByFormat( vd.Format ); Texture sliceTex = new Texture(volumeTex.Device, vd.Width, vd.Height, 1, Usage.None, formatHelp.Format, Pool.SystemMemory); Box box = new Box(); box.Left = 0; box.Right = vd.Width; box.Top = 0; box.Bottom = vd.Height; box.Front = slice; box.Back = slice + 1; LockedBox volumeLB; GraphicsStream volumeData = volumeTex.LockBox(0, box, LockFlags.ReadOnly, out volumeLB); int slicePitch; GraphicsStream sliceData = sliceTex.LockRectangle(mip, LockFlags.None, out slicePitch); CopyTextureData(volumeData, vd.Width, vd.Height, formatHelp, volumeLB.RowPitch, sliceData, slicePitch); sliceTex.UnlockRectangle(0); volumeTex.UnlockBox(mip); SurfaceLoader.FromSurface(surface, sliceTex.GetSurfaceLevel(0), filter, 0); sliceTex.Dispose(); }