/* CONSTRUCTOR */ /// <summary> /// Constructs a new sun object at a fixed distance from the primary camera. /// </summary> /// <param name="window">A reference to the Direct3D-capable window in which the sun will be rendered.</param> /// <param name="distance">The distance between the camera and the sun in world units.</param> public Sun(Window3D window, float distance) : base(window) { Distance = distance; VertexBuffer.DebugName = "Sun Vertex Buffer"; VertexLayout.DebugName = "Sun Vertex Layout"; }
/// <summary> /// Clears references to resource-containing objects that are held by this library class. /// </summary> public static void Dispose() { if (!IsDisposed) { Library = null; Window = null; IsDisposed = true; } }
/* CONSTRUCTOR & DESTRUCTOR METHODS */ /// <summary> /// Constructs a generic 2D entity that will be displayed as a flat, image-like rendering in the 3D rendering window. /// </summary> /// <param name="window">A reference to a Direct3D-capable rendering window in which this entity will be displayed.</param> protected Entity2D(Window3D window) : base() { Window = window; Window.OnClose += Dispose; Position = new RectangleF(0, 0, 100, 100); IsVisible = true; }
/* CONSTRUCTOR METHOD */ /// <summary> /// Constructs a 2D block of text that can displayed in a 3D rendering window. /// </summary> /// <param name="window">A reference to a Direct3D-capable rendering window.</param> public TextDX(Window3D window) : base(window) { ColorBrush = Tag(new SolidColorBrush(RenderTarget, Color4.Black)); Font = "Calibri"; Size = 16f; Style = FontStyle.Normal; Text = string.Empty; UpdateSettings = true; Weight = FontWeight.Normal; }
/* CONSTRUCTOR & DESTRUCTOR METHODS */ /// <summary> /// Constructs a generic 2D rectangular panel that will be displayed as a flat, image-like rendering in the 3D rendering window. /// </summary> /// <param name="window">A reference to a Direct3D-capable rendering window in which this entity will be displayed.</param> protected Panel(Window3D window) : base(window) { BackgroundBrush = Tag(new SolidColorBrush(RenderTarget, Color.SlateGray)); BorderBrush = Tag(new SolidColorBrush(RenderTarget, Color.SlateGray)); Rectangle = new RoundedRectangle() { RadiusX = 0f, RadiusY = 0f, Rect = new RectangleF(0, 0, 100, 100), }; }
/* CONSTRUCTOR & DESTRUCTOR METHODS */ /// <summary> /// Constructs a renderable 2D image from a stored image file. /// </summary> /// <param name="window">A reference to a Direct3D-capable rendering window.</param> /// <param name="fileName">An absolute or relative image file path.</param> public ImageDX(Window3D window, string fileName) : base(window) { using (SystemBitmap image = (SystemBitmap)SystemBitmap.FromFile(fileName)) { Height = image.Height; Width = image.Width; var szImage = new System.Drawing.Rectangle(0, 0, image.Width, image.Height); int byteSize = image.Height * image.Width * sizeof(int); using (DataStream bmpStream = new DataStream(byteSize, true, true)) { var sysPixelFormat = System.Drawing.Imaging.PixelFormat.Format32bppPArgb; BitmapData bmpData = image.LockBits(szImage, ImageLockMode.ReadWrite, sysPixelFormat); for (int y = 0; y < image.Height; y++) { int offset = y * bmpData.Stride; for (int x = 0; x < image.Width; x++) { // System bitmaps store pixel values as BGRA byte arrays byte b = Marshal.ReadByte(bmpData.Scan0, offset++); byte g = Marshal.ReadByte(bmpData.Scan0, offset++); byte r = Marshal.ReadByte(bmpData.Scan0, offset++); byte a = Marshal.ReadByte(bmpData.Scan0, offset++); // Convert BGRA to RGBA (stored as a 32 bit = 4 byte integer) int rgba = r | (g << 8) | (b << 16) | (a << 24); // Write the RGBA value to the data buffer bmpStream.Write(rgba); } } image.UnlockBits(bmpData); bmpStream.Position = 0; BitmapProperties bmpProps = new BitmapProperties(RenderTarget.PixelFormat); Bitmap = new Bitmap(RenderTarget, new Size2(image.Width, image.Height), bmpStream, bmpData.Stride, bmpProps); Crop = new RectangleF(0, 0, Width, Height); } } }
/* CONSTRUCTOR METHOD */ /// <summary> /// Constructs a shader program out of an HLSL source code file. /// </summary> /// <param name="window">A reference to the Direct3D-capable window in which this shader will be run.</param> /// <param name="fileName">The name of the HLSL source code file.</param> public Shader(Window3D window, string fileName) { Window = window; Window.OnClose += Dispose; if (!Path.IsPathRooted(fileName)) { string baseDir = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location); string shaderDir = Path.Combine(baseDir, "Shaders"); fileName = Path.Combine(shaderDir, Path.GetFileName(fileName)); } if (!File.Exists(fileName)) { throw new FileNotFoundException("Could not find the shader file {0}.", fileName); } EntryPoint = "Main"; IsDisposed = false; Name = Path.GetFileNameWithoutExtension(fileName); SourceLocation = fileName; DetermineShaderType(); Compile(); }
/* CONSTRUCTORS & DESTRUCTOR */ /// <summary> /// Constructs an blank texture object that has no effect on rendered objects. /// </summary> /// <param name="window">A reference to the Direct3D-capable window in which this texture will be rendered.</param> public Texture(Window3D window) : base() { Window = window; Window.OnClose += Dispose; _TSSD = new SamplerStateDescription() { AddressU = TextureAddressMode.Wrap, AddressV = TextureAddressMode.Wrap, AddressW = TextureAddressMode.Wrap, BorderColor = Color4.Black, ComparisonFunction = Comparison.Never, Filter = Filter.Anisotropic, MaximumAnisotropy = 16, MaximumLod = float.MaxValue, MinimumLod = 0f, MipLodBias = 0f, }; UpdateSettings = true; }
/// <summary> /// Releases all unmanaged resources held by the texture object and nullifies references to other objects. /// </summary> public override void Dispose() { if (!IsDisposed) { base.Dispose(); Window = null; } }
/// <summary> /// Constructs a 2D texture from a matrix of data. /// </summary> /// <param name="window">A reference to the Direct3D-capable window containing the rendering engine.</param> /// <param name="data">A matrix of single-precision data that will make up the texture on the GPU.</param> public Texture(Window3D window, float[,] data) : this(window) { int h = data.GetLength(0); int w = data.GetLength(1); int floatSize = SizeOf<float>(); ImageBuffer = Tag(new DataStream(floatSize * h * w, true, true)); foreach (float x in data) { ImageBuffer.Write(x); } Image = Tag(new Texture2D ( Device3D, CreateTextureDescription(h, w, Formats.Float), new DataRectangle(ImageBuffer.DataPointer, w * floatSize) )); SRV = Tag(new ShaderResourceView(Device3D, Image)); }
/// <summary> /// Constructs a texture from an image file that can be applied to rendered 3D objects. /// </summary> /// <param name="window">A reference to the Direct3D-capable window in which this texture will be rendered.</param> /// <param name="fileName">A path string pointing to an image file containing the texture that will be rendered.</param> public Texture(Window3D window, string fileName) : this(window) { Image = (Texture2D)Tag(Texture2D.FromFile(Device3D, fileName)); SRV = Tag(new ShaderResourceView(Device3D, Image)); }
/// <summary> /// Disposes of any unmanaged resources held by the entity object. This method should be overidden and called by any subclasses that /// hold additional unmanaged resources. /// </summary> public override void Dispose() { if (!IsDisposed) { base.Dispose(); IsVisible = false; Window = null; } }
/// <summary> /// Constructs a 2D texture from a matrix of 3D data vectors. /// </summary> /// <param name="window">A reference to the Direct3D-capable window containing the rendering engine.</param> /// <param name="data">A matrix of 3D data vectors that will make up the texture on the GPU.</param> public Texture(Window3D window, Vector3[,] data) : this(window) { int h = data.GetLength(0); int w = data.GetLength(1); int szVector = SizeOf<Vector4>(); ImageBuffer = Tag(new DataStream(szVector * h * w, true, true)); foreach (Vector3 x in data) { ImageBuffer.Write((Vector4)x); } Image = Tag(new Texture2D ( Device3D, CreateTextureDescription(h, w, Formats.Float4), new DataRectangle(ImageBuffer.DataPointer, w * szVector) )); SRV = Tag(new ShaderResourceView(Device3D, Image)); }
/// <summary> /// Clears references held by this library class to objects containing unmanaged resources. /// </summary> public static void Dispose() { if (!IsDisposed) { Compute = null; Domain = null; Geometry = null; Hull = null; Pixel = null; Vertex = null; Window = null; IsDisposed = true; } }
public override void Dispose() { if (!IsDisposed) { base.Dispose(); SwapSettings = null; Window = null; } }
/// <summary> /// Releases all unmanaged resources that are held by this object and deletes all references to other objects. /// </summary> public override void Dispose() { if (!IsDisposed) { base.Dispose(); ByteCode = null; SwapShader = null; Window = null; } }
public Mesh(Window3D window, Vertex[] vertices) : base(window) { Vertices = vertices; }
/// <summary> /// Constructs a blending control object that enables simple transparency by default. /// </summary> /// <param name="window">A reference to the Direct3D-capable window in which blending will occur.</param> public BlendSettings(Window3D window) : this() { Window = window; Window.OnClose += Dispose; }
/// <summary> /// Deactivates this vertex layout in the rendering pipeline, replacing it with whatver layout was active before. /// </summary> /// <param name="window"></param> public void Deactivate(Window3D window) { window.VertexLayout = SwapLayout; SwapLayout = null; }
/// <summary> /// Activates this vertex layout in the rendering pipeline, replacing whatever layout was active before. /// </summary> /// <param name="window"></param> public void Activate(Window3D window) { SwapLayout = window.VertexLayout; window.VertexLayout = this; }
/// <summary> /// Constructs a texture from a matrix of color data structures that can be applied to rendered 3D objects. /// </summary> /// <param name="window">A reference to the Direct3D-capable window in which this texture will be rendered.</param> /// <param name="image">A matrix of RGBA color vectors with 8 bits of data per channel.</param> public Texture(Window3D window, Color[,] image) : this(window) { int height = image.GetLength(0); int width = image.GetLength(1); int byteSize = SizeOf(image); ImageBuffer = Tag(new DataStream(byteSize, true, true)); foreach (Color color in image) { ImageBuffer.Write(color); } Image = Tag(new Texture2D ( Device3D, CreateTextureDescription(height, width, Formats.Byte4), new DataRectangle(ImageBuffer.DataPointer, width * sizeof(int)) )); SRV = Tag(new ShaderResourceView(Device3D, Image)); }
/// <summary> /// Compiles GPU shader programs and stores them in the appropriate static library. /// <para> </para> /// This function imports and compiles HLSL source code located within the "Shaders" directory found in the compiled <para/> /// program folder. Unfortunately, however, shader compilation depends on having a fully initialized rendering engine, <para/> /// which precludes being able to automate initialization within the static class here. This method is therefore called <para/> /// automatically during construction of the rendering window. Consequently, it should never be called manually. /// </summary> /// <param name="window">A reference to the Direct3D-capable window containing an active rendering engine.</param> internal static void Initialize(Window3D window) { Window = window; Window.OnClose += Dispose; string appPath = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location); string shaderPath = Path.Combine(appPath, "Shaders"); string[] csFiles = Directory.GetFiles(shaderPath, "*.cx", SearchOption.AllDirectories); string[] dsFiles = Directory.GetFiles(shaderPath, "*.ds", SearchOption.AllDirectories); string[] gsFiles = Directory.GetFiles(shaderPath, "*.gs", SearchOption.AllDirectories); string[] hsFiles = Directory.GetFiles(shaderPath, "*.hs", SearchOption.AllDirectories); string[] psFiles = Directory.GetFiles(shaderPath, "*.ps", SearchOption.AllDirectories); string[] vsFiles = Directory.GetFiles(shaderPath, "*.vs", SearchOption.AllDirectories); Shader cs, ds, gs, hs, ps, vs; foreach (var csFile in csFiles) { cs = new Shader(Window, csFile); Compute.Add(cs.Name, cs); } foreach (var dsFile in dsFiles) { ds = new Shader(Window, dsFile); Domain.Add(ds.Name, ds); } foreach (var gsFile in gsFiles) { gs = new Shader(Window, gsFile); Geometry.Add(gs.Name, gs); } foreach (var hsFile in hsFiles) { hs = new Shader(Window, hsFile); Hull.Add(hs.Name, hs); } foreach (var psFile in psFiles) { ps = new Shader(Window, psFile); Pixel.Add(ps.Name, ps); } foreach (var vsFile in vsFiles) { vs = new Shader(Window, vsFile); Vertex.Add(vs.Name, vs); } }
/// <summary> /// Constructs a renderable 2D image from a color structure array. /// </summary> /// <param name="window">A reference to the Direct3D-capable window in which this texture will be rendered.</param> /// <param name="image">An array of size [HEIGHT, WIDTH] where each element represents a color formatted from RGBA bytes.</param> public ImageDX(Window3D window, Color[,] image) : base(window) { Height = image.GetLength(0); Width = image.GetLength(1); int byteSize = (int)Height * (int)Width * sizeof(int); using (DataStream bmpStream = new DataStream(byteSize, true, true)) { foreach (Color color in image) { bmpStream.Write((int)color); } bmpStream.Position = 0; BitmapProperties bmpProps = new BitmapProperties(RenderTarget.PixelFormat); int stride = (int)Width * sizeof(int); Bitmap = new Bitmap(RenderTarget, new Size2((int)Width, (int)Height), bmpStream, stride, bmpProps); Crop = new RectangleF(0, 0, Width, Height); } }
/// <summary> /// Constructs a texture from a matrix of color data structures that can be applied to rendered 3D objects. /// </summary> /// <param name="window">A reference to the Direct3D-capable window in which this texture will be rendered.</param> /// <param name="image">A matrix of RGBA color vectors with 32 bits of data per channel.</param> public Texture(Window3D window, Color4[,] image) : this(window) { int[] szImage = image.Size(); int byteSize = SizeOf(image); ImageBuffer = Tag(new DataStream(byteSize, true, true)); foreach (Color4 color in image) { ImageBuffer.Write(color); } Image = Tag(new Texture2D ( Device3D, CreateTextureDescription(szImage[0], szImage[1], Formats.Float4), new DataRectangle(ImageBuffer.DataPointer, szImage[1] * SizeOf<Color4>()) )); SRV = Tag(new ShaderResourceView(Device3D, Image)); }
public Mesh(Window3D window) : base(window) { }
/// <summary> /// Imports files from the "Textures" folder located within the compiled program folder and stores them in a static library. /// <para> </para> /// This function must be manually called prior to using any texture files that are to be imported. Unfortunately, the <para/> /// texture creation process depends on having a fully intialized rendering engine, which precludes being able to automate <para/> /// it for this static library. This method is therefore called automatically during construction of the rendering window. <para/> /// Consequently, it should never be called manually. /// </summary> /// <param name="window">A reference to a Direct3D-capable window containing an active rendering engine.</param> internal static void Initialize(Window3D window) { Window = window; Window.OnClose += Dispose; string appPath = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location); string texPath = Path.Combine(appPath, "Textures"); if (!Directory.Exists(texPath)) { throw new FileNotFoundException("Could not find the texture file folder."); } string[] texFiles = Directory.GetFiles(texPath); foreach (string texFile in texFiles) Library.Add(Path.GetFileNameWithoutExtension(texFile), new Texture(Window, texFile)); }