public BitmapSource Render(Camera camera) { // Setup for rendering. var rasterizerState = RasterizerState.New(_device, new RasterizerStateDescription { CullMode = CullMode.Back, FillMode = FillMode.Solid, IsMultisampleEnabled = true, IsFrontCounterClockwise = Options.TriangleWindingOrderReversed }); _device.SetRasterizerState(rasterizerState); _device.Clear(_renderTexture, ConversionUtility.ToSharpDXColor(Options.BackgroundColor)); _device.Clear(_depthStencilTexture, DepthStencilClearFlags.Depth, 1, 0); _device.SetRenderTargets(_depthStencilTexture, _renderTexture); _effect.LightingEnabled = Options.LightingEnabled; _effect.View = ConversionUtility.ToSharpDXMatrix(camera.GetViewMatrix()); _effect.Projection = ConversionUtility.ToSharpDXMatrix(camera.GetProjectionMatrix(_aspectRatio)); // Render scene. _device.SetBlendState(_device.BlendStates.Opaque); foreach (WarpMesh mesh in _meshes.Where(m => m.IsOpaque)) { mesh.Draw(_effect); } _device.SetBlendState(_device.BlendStates.AlphaBlend); foreach (WarpMesh mesh in _meshes.Where(m => !m.IsOpaque)) { mesh.Draw(_effect); } _device.SetBlendState(null); // Extract image from render target. _device.SetRenderTargets((RenderTargetView)null); _device.SetRasterizerState(null); rasterizerState.Dispose(); using (var memoryStream = new MemoryStream()) { _renderTexture.Save(memoryStream, ImageFileType.Png); var bi = new BitmapImage(); bi.BeginInit(); bi.CacheOption = BitmapCacheOption.OnLoad; bi.StreamSource = memoryStream; bi.EndInit(); return(bi); } }
private void Init(GraphicsDevice graphicsDevice) { _screenTriangle = SharpDX.Toolkit.Graphics.Buffer.Vertex.New(graphicsDevice, new[] { new Vector2(-1.0f, -1.0f), new Vector2(3.0f, -1.0f), new Vector2(-1.0f, 3.0f) }, SharpDX.Direct3D11.ResourceUsage.Immutable); _vertexInputLayout = VertexInputLayout.New( VertexBufferLayout.New(0, new VertexElement[]{new VertexElement("POSITION", 0, SharpDX.DXGI.Format.R32G32_Float, 0)}, 0)); var rasterizerStateDesc = SharpDX.Direct3D11.RasterizerStateDescription.Default(); rasterizerStateDesc.CullMode = SharpDX.Direct3D11.CullMode.None; _noneCullingState = RasterizerState.New(graphicsDevice, "CullModeNone", rasterizerStateDesc); initalised = true; }
public Canvas(int width, int height) { Width = width; Height = height; RenderTarget = RenderTarget2D.New( GraphicsDevice, width, height, Format.B8G8R8A8_UNorm); rasterizerState = RasterizerState.New(GraphicsDevice, new RasterizerStateDescription { CullMode = CullMode.None, FillMode = FillMode.Solid, IsAntialiasedLineEnabled = true, IsMultisampleEnabled = true }); effect = new BasicEffect(GraphicsDevice) { World = Matrix.Identity, View = Matrix.Identity, Projection = Matrix.OrthoOffCenterLH(0, Width, 0, Height, 1.0f, 1000.0f), VertexColorEnabled = true }; }
protected override void LoadContent() { base.LoadContent(); _spriteBatch = ToDisposeContent(new SpriteBatch(GraphicsDevice)); _vContent = new VisionContent(_graphicsDeviceManager.GraphicsDevice, Content); _arial16Font = Content.Load<SpriteFont>("Fonts/Arial16"); _basicEffect = new VBasicEffect(_graphicsDeviceManager.GraphicsDevice); _basicEffect.EnableDefaultLighting(); _ball = new SpherePrimitive<VertexPositionNormalTexture>(GraphicsDevice, (p, n, t, tx) => new VertexPositionNormalTexture(p, n, tx), 1); var x = _vContent.LoadEffect("effects/simpletextureeffect"); x.Texture = _vContent.Load<Texture2D>("terraintextures/sand"); _ballInstance = new VDrawableInstance(x, _ball, Matrix.Translation(10, 2, 10)); Sky = new SkySphere(_vContent, _vContent.Load<TextureCube>(@"Textures\clouds")); _movingShip = new MovingShip(new ShipModel(_vContent)); _water = WaterFactory.Create(_vContent); _water.ReflectedObjects.Add(_movingShip._shipModel); _water.ReflectedObjects.Add(_ballInstance); _water.ReflectedObjects.Add(Sky); _camera = new Camera( _vContent.ClientSize, new KeyboardManager(this), new MouseManager(this), null, //new PointerManager(this), new Vector3(0, 15, 0), new Vector3(-10, 15, 0)); _rasterizerState = RasterizerState.New(GraphicsDevice, new RasterizerStateDescription { FillMode = FillMode.Solid, CullMode = CullMode.Back, IsFrontCounterClockwise = false, DepthBias = 0, SlopeScaledDepthBias = 0.0f, DepthBiasClamp = 0.0f, IsDepthClipEnabled = true, IsScissorEnabled = false, IsMultisampleEnabled = false, IsAntialiasedLineEnabled = false }); _shadow = new ShadowMap(_vContent, 1024, 1024); //_shadow.ShadowCastingObjects.Add(_sailingShip); //_shadow.ShadowCastingObjects.Add(reimersTerrain); //_shadow.ShadowCastingObjects.Add(generatedTerrain); //_shadow.ShadowCastingObjects.Add(bridge); //_archipelag = new Archipelag(_vContent, _water, _shadow); _data.VContent = _vContent; _data.Camera = _camera; _data.Water = _water; _data.Shadow = _shadow; _q = new CxBillboard(_vContent, Matrix.Identity, _vContent.Load<Texture2D>("billboards/wheat_billboard"), 20, 10, 0.5f); _q.AddPositionsWithSameNormal(Vector3.Up, Vector3.Zero, Vector3.Left*10.5f, Vector3.Up, Vector3.Right*10.4f, Vector3.Up, Vector3.ForwardRH*3.45f, Vector3.Up, Vector3.BackwardRH*2.9f, Vector3.Up); _q.CreateVertices(); }
public VoxelRenderer(GraphicsDevice graphicsDevice, ContentManager contentManager) { _cubeVertexBuffer = Buffer.Vertex.New( graphicsDevice, new[] { // 3D coordinates UV Texture coordinates new CubeVertex() { Position = new Vector3(-1.0f, -1.0f, -1.0f), Normal = -Vector3.UnitZ, Texcoord = new Vector2(0.0f, 1.0f) }, // Front new CubeVertex() { Position = new Vector3(-1.0f, 1.0f, -1.0f), Normal = -Vector3.UnitZ, Texcoord = new Vector2(0.0f, 0.0f) }, new CubeVertex() { Position = new Vector3(1.0f, 1.0f, -1.0f ), Normal = -Vector3.UnitZ, Texcoord = new Vector2(1.0f, 0.0f) }, new CubeVertex() { Position = new Vector3(-1.0f, -1.0f, -1.0f), Normal = -Vector3.UnitZ, Texcoord = new Vector2(0.0f, 1.0f) }, new CubeVertex() { Position = new Vector3(1.0f, 1.0f, -1.0f ), Normal = -Vector3.UnitZ, Texcoord = new Vector2(1.0f, 0.0f) }, new CubeVertex() { Position = new Vector3(1.0f, -1.0f, -1.0f ), Normal = -Vector3.UnitZ, Texcoord = new Vector2(1.0f, 1.0f) }, new CubeVertex() { Position = new Vector3(-1.0f, -1.0f, 1.0f), Normal = Vector3.UnitZ, Texcoord = new Vector2(1.0f, 0.0f) }, // BACK new CubeVertex() { Position = new Vector3(1.0f, 1.0f, 1.0f ), Normal = Vector3.UnitZ, Texcoord = new Vector2(0.0f, 1.0f) }, new CubeVertex() { Position = new Vector3(-1.0f, 1.0f, 1.0f), Normal = Vector3.UnitZ, Texcoord = new Vector2(1.0f, 1.0f) }, new CubeVertex() { Position = new Vector3(-1.0f, -1.0f, 1.0f), Normal = Vector3.UnitZ, Texcoord = new Vector2(1.0f, 0.0f) }, new CubeVertex() { Position = new Vector3(1.0f, -1.0f, 1.0f ), Normal = Vector3.UnitZ, Texcoord = new Vector2(0.0f, 0.0f) }, new CubeVertex() { Position = new Vector3(1.0f, 1.0f, 1.0f ), Normal = Vector3.UnitZ, Texcoord = new Vector2(0.0f, 1.0f) }, new CubeVertex() { Position = new Vector3(-1.0f, 1.0f, -1.0f ), Normal = Vector3.UnitY, Texcoord = new Vector2(0.0f, 1.0f) }, // Top new CubeVertex() { Position = new Vector3(-1.0f, 1.0f, 1.0f ), Normal = Vector3.UnitY, Texcoord = new Vector2(0.0f, 0.0f) }, new CubeVertex() { Position = new Vector3(1.0f, 1.0f, 1.0f ), Normal = Vector3.UnitY, Texcoord = new Vector2(1.0f, 0.0f) }, new CubeVertex() { Position = new Vector3(-1.0f, 1.0f, -1.0f ), Normal = Vector3.UnitY, Texcoord = new Vector2(0.0f, 1.0f) }, new CubeVertex() { Position = new Vector3(1.0f, 1.0f, 1.0f ), Normal = Vector3.UnitY, Texcoord = new Vector2(1.0f, 0.0f) }, new CubeVertex() { Position = new Vector3(1.0f, 1.0f, -1.0f ), Normal = Vector3.UnitY, Texcoord = new Vector2(1.0f, 1.0f) }, new CubeVertex() { Position = new Vector3(-1.0f,-1.0f, -1.0f ), Normal = -Vector3.UnitY, Texcoord = new Vector2(1.0f, 0.0f) }, // Bottom new CubeVertex() { Position = new Vector3(1.0f,-1.0f, 1.0f ), Normal = -Vector3.UnitY, Texcoord = new Vector2(0.0f, 1.0f) }, new CubeVertex() { Position = new Vector3(-1.0f,-1.0f, 1.0f ), Normal = -Vector3.UnitY, Texcoord = new Vector2(1.0f, 1.0f) }, new CubeVertex() { Position = new Vector3(-1.0f,-1.0f, -1.0f ), Normal = -Vector3.UnitY, Texcoord = new Vector2(1.0f, 0.0f) }, new CubeVertex() { Position = new Vector3(1.0f,-1.0f, -1.0f ), Normal = -Vector3.UnitY, Texcoord = new Vector2(0.0f, 0.0f) }, new CubeVertex() { Position = new Vector3(1.0f,-1.0f, 1.0f ), Normal = -Vector3.UnitY, Texcoord = new Vector2(0.0f, 1.0f) }, new CubeVertex() { Position = new Vector3(-1.0f, -1.0f, -1.0f), Normal = -Vector3.UnitX, Texcoord = new Vector2(0.0f, 1.0f) }, // Left new CubeVertex() { Position = new Vector3(-1.0f, -1.0f, 1.0f), Normal = -Vector3.UnitX, Texcoord = new Vector2(0.0f, 0.0f) }, new CubeVertex() { Position = new Vector3(-1.0f, 1.0f, 1.0f), Normal = -Vector3.UnitX, Texcoord = new Vector2(1.0f, 0.0f) }, new CubeVertex() { Position = new Vector3(-1.0f, -1.0f, -1.0f), Normal = -Vector3.UnitX, Texcoord = new Vector2(0.0f, 1.0f) }, new CubeVertex() { Position = new Vector3(-1.0f, 1.0f, 1.0f), Normal = -Vector3.UnitX, Texcoord = new Vector2(1.0f, 0.0f) }, new CubeVertex() { Position = new Vector3(-1.0f, 1.0f, -1.0f), Normal = -Vector3.UnitX, Texcoord = new Vector2(1.0f, 1.0f) }, new CubeVertex() { Position = new Vector3(1.0f, -1.0f, -1.0f ), Normal = Vector3.UnitX, Texcoord = new Vector2(1.0f, 0.0f) }, // Right new CubeVertex() { Position = new Vector3(1.0f, 1.0f, 1.0f ), Normal = Vector3.UnitX, Texcoord = new Vector2(0.0f, 1.0f) }, new CubeVertex() { Position = new Vector3(1.0f, -1.0f, 1.0f ), Normal = Vector3.UnitX, Texcoord = new Vector2(1.0f, 1.0f) }, new CubeVertex() { Position = new Vector3(1.0f, -1.0f, -1.0f ), Normal = Vector3.UnitX, Texcoord = new Vector2(1.0f, 0.0f) }, new CubeVertex() { Position = new Vector3(1.0f, 1.0f, -1.0f ), Normal = Vector3.UnitX, Texcoord = new Vector2(0.0f, 0.0f) }, new CubeVertex() { Position = new Vector3(1.0f, 1.0f, 1.0f ), Normal = Vector3.UnitX, Texcoord = new Vector2(0.0f, 1.0f) } }, SharpDX.Direct3D11.ResourceUsage.Immutable); // Create an input layout from the vertices _vertexInputLayout = VertexInputLayout.New( VertexBufferLayout.New(0, new VertexElement[]{new VertexElement("POSITION_CUBE", 0, SharpDX.DXGI.Format.R32G32B32_Float, 0), new VertexElement("NORMAL", 0, SharpDX.DXGI.Format.R32G32B32_Float, sizeof(float) * 3), new VertexElement("TEXCOORD", 0, SharpDX.DXGI.Format.R32G32_Float, sizeof(float) * 6)}, 0), VertexBufferLayout.New(1, new VertexElement[]{new VertexElement("POSITION_INSTANCE", SharpDX.DXGI.Format.R32_SInt)}, 1)); // Create instance buffer for every VoxelInfo _voxelTypeRenderingData = new VoxelTypeInstanceData[TypeInformation.GetNumTypes()-1]; for (int i = 0; i < _voxelTypeRenderingData.Length; ++i) _voxelTypeRenderingData[i] = new VoxelTypeInstanceData(graphicsDevice, (VoxelType)(i+1)); LoadTextures(contentManager); // load shader EffectCompilerFlags compilerFlags = EffectCompilerFlags.None; #if DEBUG compilerFlags |= EffectCompilerFlags.Debug; #endif var voxelShaderCompileResult = EffectCompiler.CompileFromFile("Content/voxel.fx", compilerFlags); if (voxelShaderCompileResult.HasErrors) { System.Console.WriteLine(voxelShaderCompileResult.Logger.Messages); System.Diagnostics.Debugger.Break(); } _voxelEffect = new SharpDX.Toolkit.Graphics.Effect(graphicsDevice, voxelShaderCompileResult.EffectData); // setup states var rasterizerStateDesc = SharpDX.Direct3D11.RasterizerStateDescription.Default(); rasterizerStateDesc.CullMode = SharpDX.Direct3D11.CullMode.Back; _backfaceCullingState = RasterizerState.New(graphicsDevice, "CullModeBack", rasterizerStateDesc); rasterizerStateDesc.CullMode = SharpDX.Direct3D11.CullMode.None; _noneCullingState = RasterizerState.New(graphicsDevice, "CullModeNone", rasterizerStateDesc); var depthStencilStateDesc = SharpDX.Direct3D11.DepthStencilStateDescription.Default(); depthStencilStateDesc.IsDepthEnabled = true; _depthStencilStateState = DepthStencilState.New(graphicsDevice, "NormalZBufferUse", depthStencilStateDesc); var samplerStateDesc = SharpDX.Direct3D11.SamplerStateDescription.Default(); samplerStateDesc.AddressV = SharpDX.Direct3D11.TextureAddressMode.Mirror; samplerStateDesc.AddressU = SharpDX.Direct3D11.TextureAddressMode.Mirror; samplerStateDesc.Filter = SharpDX.Direct3D11.Filter.MinMagMipPoint; _pointSamplerState = SamplerState.New(graphicsDevice, "PointSampler", samplerStateDesc); _voxelEffect.Parameters["PointSampler"].SetResource(_pointSamplerState); var blendStateDesc = SharpDX.Direct3D11.BlendStateDescription.Default(); _blendStateOpaque = BlendState.New(graphicsDevice, "Opaque", blendStateDesc); blendStateDesc.RenderTarget[0].IsBlendEnabled = true; blendStateDesc.RenderTarget[0].SourceBlend = SharpDX.Direct3D11.BlendOption.SourceAlpha; blendStateDesc.RenderTarget[0].DestinationBlend = SharpDX.Direct3D11.BlendOption.InverseSourceAlpha; blendStateDesc.RenderTarget[0].BlendOperation = SharpDX.Direct3D11.BlendOperation.Add; _blendStateTransparent = BlendState.New(graphicsDevice, "AlphaBlend", blendStateDesc); // vertexbuffer for a single instance _singleInstanceBuffer = Buffer.Vertex.New<Int32>(graphicsDevice, 1, SharpDX.Direct3D11.ResourceUsage.Dynamic); }
void LoadContent() { var device = this.GraphicsDevice; m_basicEffect = ToDispose(new BasicEffect(GraphicsDevice)); m_basicEffect.TextureEnabled = true; m_basicEffect.Sampler = device.SamplerStates.PointClamp; LoadCube(); var rdesc = device.RasterizerStates.Default.Description; rdesc.DepthBias = -10; m_rasterizerState = RasterizerState.New(device, rdesc); }