public override void BindMaterial(DeviceContext context, ResourceProxy proxy) { using (DataStream stream = new DataStream(BufferSize, true, true)) { stream.Write<float>((float)Brightness); Material.CopyStream(context, constantBuffer, stream); } context.PixelShader.Set(pixelShader); context.PixelShader.SetSampler(0, sampler); context.PixelShader.SetConstantBuffer(2, constantBuffer); }
public Scene(Device device, DeviceContext context, RenderForm window, Size resolution) { Device = device; SetupTweakBars(window); CompileVertexShader(); LoadSceneAssets(); RenderDimensions = resolution; CreateCamera(resolution); CreateInput(window); window.MouseDown += MouseDown; window.MouseMove += MouseMove; window.MouseUp += MouseUp; proxy = new ResourceProxy(device); // TODO: create SkyGenerator instance here skyEnvMap = new GraphicsResource(device, new Size(2048, 1024), Format.R32G32B32A32_Float, true, true); }
public override void BindMaterial(DeviceContext context, ResourceProxy proxy) { using (DataStream stream = new DataStream(BufferSize, true, true)) { stream.Write<Vector4>(new Vector4(Diffuse, 1)); stream.Write<Vector4>(new Vector4(Specular, 1)); stream.Write<Vector4>(new Vector4((float)Shininess, (float)Shininess, (float)Shininess, (float)Shininess)); stream.Write<Vector4>(new Vector4((float)Brightness, (float)Brightness, (float)Brightness, (float)Brightness)); stream.Write<Boolean>(BumpMap != null); Material.CopyStream(context, constantBuffer, stream); } context.PixelShader.Set(pixelShader); context.PixelShader.SetSampler(0, sampler); context.PixelShader.SetConstantBuffer(2, constantBuffer); context.PixelShader.SetShaderResource(1, proxy[ColorMap]); context.PixelShader.SetShaderResource(2, proxy[BumpMap]); }
public void Render(DeviceContext context, Camera camera, Dictionary<String, Material> materials, ResourceProxy proxy) { Matrix modelToWorld = Matrix.Scaling(Scale) * Matrix.RotationYawPitchRoll(Rotation.X, Rotation.Y, Rotation.Z) * Matrix.Translation(Translation); { DataStream modelStream; context.MapSubresource(modelBuffer, MapMode.WriteDiscard, SharpDX.Direct3D11.MapFlags.None, out modelStream); modelStream.Write<Matrix>(Matrix.Transpose(modelToWorld)); context.UnmapSubresource(modelBuffer, 0); modelStream.Dispose(); } context.VertexShader.SetConstantBuffer(1, modelBuffer); context.PixelShader.SetConstantBuffer(1, modelBuffer); foreach (Mesh mesh in meshes) { if (!materials.ContainsKey(mesh.MeshName)) { //Console.WriteLine("WARNING: " + mesh.MeshName + " has no material! Skipping..."); continue; } Material material = materials[mesh.MeshName]; material.BindMaterial(context, proxy); mesh.Render(context); } }
/// <summary> /// Binds this material instance to a device context for rendering. A material must /// initialize its own pixel shader as well as its constant buffer, sampler(s), and /// shader resources (served by a ResourceProxy). /// </summary> /// <param name="context">The device context to bind the material to.</param> /// <param name="proxy">The resource proxy which provides resources.</param> public abstract void BindMaterial(DeviceContext context, ResourceProxy proxy);