public void Render(RenderContext rc, string pipelineStage) { Matrix4x4 orthoProjection = Matrix4x4.CreateOrthographicOffCenter( 0f, rc.Viewport.Width, rc.Viewport.Height, 0f, -1.0f, 1.0f); Matrix4x4 proj = orthoProjection; _projectionMatrixBuffer.SetData(ref proj, 64); float width = _imageWidth; Matrix4x4 world = Matrix4x4.CreateScale(width) * Matrix4x4.CreateTranslation(rc.Viewport.Width - width - 20, 20, 0); _worldMatrixBuffer.SetData(ref world, 64); rc.VertexBuffer = _vertexBuffer; rc.IndexBuffer = _indexBuffer; _material.Apply(rc); rc.SetConstantBuffer(0, _worldMatrixBuffer); rc.SetConstantBuffer(1, _projectionMatrixBuffer); rc.SetTexture(2, SharedTextures.GetTextureBinding("ShadowMap")); rc.SetDepthStencilState(_depthDisabledState); rc.DrawIndexedPrimitives(6, 0); rc.SetDepthStencilState(rc.DefaultDepthStencilState); }
public void Render(RenderContext rc, string stage) { rc.VertexBuffer = _vb; rc.IndexBuffer = _ib; if (stage == "ShadowMap") { _shadowmapMaterial.Apply(rc); rc.SetConstantBuffer(0, _buffersDict["LightProjMatrix"]); rc.SetConstantBuffer(1, _buffersDict["LightViewMatrix"]); _worldProvider.SetData(_worldBuffer); rc.SetConstantBuffer(2, _worldBuffer); } else { _regularMaterial.Apply(rc); rc.SetConstantBuffer(0, _buffersDict["ProjectionMatrix"]); rc.SetConstantBuffer(1, _buffersDict["ViewMatrix"]); rc.SetConstantBuffer(2, _buffersDict["LightProjMatrix"]); rc.SetConstantBuffer(3, _buffersDict["LightViewMatrix"]); rc.SetConstantBuffer(4, _buffersDict["LightInfo"]); _worldProvider.SetData(_worldBuffer); rc.SetConstantBuffer(5, _worldBuffer); _inverseWorldProvider.SetData(_inverseTransposeWorldBuffer); rc.SetConstantBuffer(6, _inverseTransposeWorldBuffer); rc.SetTexture(7, _textureBinding); rc.SetSamplerState(8, rc.Anisox4Sampler); rc.SetTexture(9, SharedTextures.GetTextureBinding("ShadowMap_Preview")); rc.SetSamplerState(10, rc.PointSampler); } rc.DrawIndexedPrimitives(_elementCount, 0); }
/// <summary> /// Loads a texture from the specified relative filepath and maps it to the given identifier. /// </summary> /// <param name="filepath">The relative filepath of the texture.</param> /// <param name="identifier">The texture identifier.</param> /// <returns>Success if the texture was loaded and mapped to the identifier.</returns> public static RwResult LoadSharedTexture(string filepath, string identifier) { if (SharedTextures == null) { SharedTextures = new Dictionary <int, Bitmap>(); } string lowIdentifier = identifier.ToString(); if (TextureIdentifierMappings.ContainsKey(lowIdentifier)) { return(RwResult.DuplicateAssetIdentifier); } if (!File.Exists(filepath)) { return(RwResult.FileNotFound); } Bitmap loadedTexture = null; try { string extension = Path.GetExtension(filepath).ToLower(); if (NativeGdiFormats.Contains(extension)) { loadedTexture = (Bitmap)Bitmap.FromFile(filepath); } else if (extension == ".svg") { SvgDocument svg = SvgDocument.Open(filepath); loadedTexture = svg.Draw(); } } catch (OutOfMemoryException memEx) { return(RwResult.InvalidFileFormat); } int id = 0; if (FreeIds.Count > 0) { id = FreeIds[0]; FreeIds.RemoveAt(0); } else { id = SharedTextures.Count; } TextureIdentifierMappings.Add(lowIdentifier, id); SharedTextures.Add(id, loadedTexture); return(RwResult.Success); }
private void InitializeContextObjects(RenderContext rc) { _depthTexture = rc.ResourceFactory.CreateTexture( 1, DepthMapWidth, DepthMapHeight, PixelFormat.R16_UInt, DeviceTextureCreateOptions.DepthStencil); _depthTextureBinding = rc.ResourceFactory.CreateShaderTextureBinding(_depthTexture); _shadowMapFramebuffer = rc.ResourceFactory.CreateFramebuffer(); _shadowMapFramebuffer.DepthTexture = _depthTexture; SharedTextures.SetTextureBinding(_contextBindingName, _depthTextureBinding); }
public void Render(RenderContext rc, string pipelineStage) { rc.VertexBuffer = _vb; rc.IndexBuffer = _ib; Matrix4x4 worldData = Matrix4x4.CreateScale(Scale) * Matrix4x4.CreateFromQuaternion(Rotation) * Matrix4x4.CreateTranslation(Position); _worldBuffer.SetData(ref worldData, 64); Matrix4x4 inverseTransposeData = Utilities.CalculateInverseTranspose(worldData); _inverseTransposeWorldBuffer.SetData(ref inverseTransposeData, 64); if (pipelineStage == "ShadowMap") { _shadowPassMaterial.Apply(rc); rc.SetConstantBuffer(0, SharedDataProviders.LightProjMatrixBuffer); rc.SetConstantBuffer(1, SharedDataProviders.LightViewMatrixBuffer); rc.SetConstantBuffer(2, _worldBuffer); } else { Debug.Assert(pipelineStage == "Standard"); _regularPassMaterial.Apply(rc); rc.SetConstantBuffer(0, SharedDataProviders.ProjectionMatrixBuffer); rc.SetConstantBuffer(1, SharedDataProviders.ViewMatrixBuffer); rc.SetConstantBuffer(2, SharedDataProviders.LightProjMatrixBuffer); rc.SetConstantBuffer(3, SharedDataProviders.LightViewMatrixBuffer); rc.SetConstantBuffer(4, SharedDataProviders.LightInfoBuffer); rc.SetConstantBuffer(5, _worldBuffer); rc.SetConstantBuffer(6, _inverseTransposeWorldBuffer); rc.SetTexture(7, _surfaceTextureBinding); rc.SetSamplerState(8, rc.Anisox4Sampler); rc.SetTexture(9, SharedTextures.GetTextureBinding("ShadowMap")); rc.SetSamplerState(10, _shadowMapSampler); } rc.DrawIndexedPrimitives(_meshData.Indices.Length, 0); }
/// <summary> /// Creates a shared texture /// </summary> /// <param name="name">Name of the texture</param> /// <returns>Texture handle</returns> public static Texture2D CreateShared(string name) { if (SharedTextures == null) { SharedTextures = new Dictionary <string, Texture2D>(); } // Texture already exist, so return it if (SharedTextures.ContainsKey(name)) { return(SharedTextures[name]); } // Else create the texture SharedTextures[name] = new Texture2D(); return(SharedTextures[name]); }
/// <summary> /// Removes all shared textures /// </summary> public static void DeleteShared() { SharedTextures.Clear(); }