/// <summary> /// Entry point for the application /// </summary> /// <param name="args"></param> public static void Main(string[] args) { // Create the message pool MessagePool pool = new MessagePool(); // Create the root actor Actor root = new Actor(pool); // Attach core systems root.AddComponent<UserInputHandler>(); root.AddComponent<Renderer>(); root.AddComponent<MaterialSystem>(); root.AddComponent<SceneManager>(); root.AddComponent<SceneLoader>(); root.AddComponent<Sleeper>().TargetFPS = 60.0f; // Attach exit listener bool exit = false; Listener<ExitMessage> exitlistener = root.AddComponent<Listener<ExitMessage>>() as Listener<ExitMessage>; exitlistener.OnMessageReceived += (msg) => exit = true; // Initialise root.Init(); // Send the initialise message InitialiseMessage initmsg = new InitialiseMessage(); pool.SendMessage(initmsg); // Load the scene if (!root.GetComponent<SceneLoader>().LoadSceneFromFile("scene.json")) { Console.WriteLine("Failed to load scene!"); Console.ReadKey(); return; } // Setup the frame message FrameMessage framemsg = new FrameMessage(); framemsg.FrameNumber = 0; framemsg.DeltaTime = 0.0f; // Setup the timer Stopwatch frametimer = new Stopwatch(); // Loop until done while (!exit) { // Send frame message frametimer.Start(); pool.SendMessage(framemsg); frametimer.Stop(); framemsg.DeltaTime = (float)frametimer.Elapsed.TotalSeconds; frametimer.Reset(); // Increase frame number framemsg.FrameNumber++; // Process windows events Application.DoEvents(); } // Send the shutdown message ShutdownMessage shutdownmsg = new ShutdownMessage(); pool.SendMessage(shutdownmsg); // Delete root actor and clean up root.Destroy(true); }
public void OnInitialise(InitialiseMessage msg) { // Initialise material system Console.WriteLine("Initialising material system..."); // Initialise maps materialmap = new Dictionary<string, Material>(); shadermap = new Dictionary<string, Shader>(); texturemap = new Dictionary<string, Texture2D>(); }
public void OnInitialise(InitialiseMessage msg) { // Initialise renderer Console.WriteLine("Initialising renderer..."); rendermsg = new RenderMessage(); updatemsg = new UpdateMessage(); // Create the window window = new RenderForm("Castle Renderer - 11030062"); window.Width = 1280; window.Height = 720; // Add form events window.FormClosed += window_FormClosed; // Defaults ClearColour = new Color4(1.0f, 0.0f, 0.0f, 1.0f); // Setup the device var description = new SwapChainDescription() { BufferCount = 1, Usage = Usage.RenderTargetOutput, OutputHandle = window.Handle, IsWindowed = true, ModeDescription = new ModeDescription(0, 0, new Rational(60, 1), Format.R8G8B8A8_UNorm), SampleDescription = new SampleDescription(1, 0), Flags = SwapChainFlags.AllowModeSwitch, SwapEffect = SwapEffect.Discard }; Device tmp; var result = Device.CreateWithSwapChain(DriverType.Hardware, DeviceCreationFlags.None, description, out tmp, out swapchain); if (result.IsFailure) { Console.WriteLine("Failed to create Direct3D11 device (" + result.Code.ToString() + ":" + result.Description + ")"); return; } Device = tmp; context = Device.ImmediateContext; using (var factory = swapchain.GetParent<Factory>()) factory.SetWindowAssociation(window.Handle, WindowAssociationFlags.IgnoreAltEnter); // Check AA stuff int q = Device.CheckMultisampleQualityLevels(Format.R8G8B8A8_UNorm, 8); // Setup the viewport viewport = new Viewport(0.0f, 0.0f, window.ClientSize.Width, window.ClientSize.Height); viewport.MinZ = 0.0f; viewport.MaxZ = 1.0f; context.Rasterizer.SetViewports(viewport); // Setup the backbuffer using (var resource = Resource.FromSwapChain<Texture2D>(swapchain, 0)) rtBackbuffer = new RenderTargetView(Device, resource); // Setup depth for backbuffer { Texture2DDescription texdesc = new Texture2DDescription() { ArraySize = 1, BindFlags = BindFlags.DepthStencil, CpuAccessFlags = CpuAccessFlags.None, Format = Format.D32_Float, Width = (int)viewport.Width, Height = (int)viewport.Height, MipLevels = 1, OptionFlags = ResourceOptionFlags.None, SampleDescription = new SampleDescription(1, 0), Usage = ResourceUsage.Default }; texDepthBuffer = new Texture2D(Device, texdesc); DepthStencilViewDescription viewdesc = new DepthStencilViewDescription() { ArraySize = 0, Format = Format.D32_Float, Dimension = DepthStencilViewDimension.Texture2D, MipSlice = 0, Flags = 0, FirstArraySlice = 0 }; vwDepthBuffer = new DepthStencilView(Device, texDepthBuffer, viewdesc); } // Setup states #region Depth States // Setup depth states { DepthStencilStateDescription desc = new DepthStencilStateDescription() { IsDepthEnabled = true, IsStencilEnabled = false, DepthWriteMask = DepthWriteMask.All, DepthComparison = Comparison.Less }; Depth_Enabled = DepthStencilState.FromDescription(Device, desc); } { DepthStencilStateDescription desc = new DepthStencilStateDescription() { IsDepthEnabled = false, IsStencilEnabled = false, DepthWriteMask = DepthWriteMask.Zero, DepthComparison = Comparison.Less }; Depth_Disabled = DepthStencilState.FromDescription(Device, desc); } { DepthStencilStateDescription desc = new DepthStencilStateDescription() { IsDepthEnabled = true, IsStencilEnabled = false, DepthWriteMask = DepthWriteMask.Zero, DepthComparison = Comparison.Less }; Depth_ReadOnly = DepthStencilState.FromDescription(Device, desc); } #endregion #region Sampler States Sampler_Clamp = SamplerState.FromDescription(Device, new SamplerDescription() { AddressU = TextureAddressMode.Clamp, AddressV = TextureAddressMode.Clamp, AddressW = TextureAddressMode.Clamp, Filter = Filter.Anisotropic, MinimumLod = 0.0f, MaximumLod = float.MaxValue, MaximumAnisotropy = 16 }); Sampler_Clamp_Point = SamplerState.FromDescription(Device, new SamplerDescription() { AddressU = TextureAddressMode.Clamp, AddressV = TextureAddressMode.Clamp, AddressW = TextureAddressMode.Clamp, Filter = Filter.MinMagMipPoint }); Sampler_Clamp_Linear = SamplerState.FromDescription(Device, new SamplerDescription() { AddressU = TextureAddressMode.Clamp, AddressV = TextureAddressMode.Clamp, AddressW = TextureAddressMode.Clamp, Filter = Filter.MinMagMipLinear }); Sampler_Wrap = SamplerState.FromDescription(Device, new SamplerDescription() { AddressU = TextureAddressMode.Wrap, AddressV = TextureAddressMode.Wrap, AddressW = TextureAddressMode.Wrap, Filter = Filter.Anisotropic, MinimumLod = 0.0f, MaximumLod = float.MaxValue, MaximumAnisotropy = 16 }); #endregion #region Rasterizer States Culling_Backface = RasterizerState.FromDescription(Device, new RasterizerStateDescription() { CullMode = CullMode.Back, DepthBias = 0, DepthBiasClamp = 0.0f, IsDepthClipEnabled = true, FillMode = FillMode.Solid, IsAntialiasedLineEnabled = false, IsFrontCounterclockwise = false, IsMultisampleEnabled = true, IsScissorEnabled = false, SlopeScaledDepthBias = 0.0f }); Culling_Frontface = RasterizerState.FromDescription(Device, new RasterizerStateDescription() { CullMode = CullMode.Front, DepthBias = 0, DepthBiasClamp = 0.0f, IsDepthClipEnabled = true, FillMode = FillMode.Solid, IsAntialiasedLineEnabled = false, IsFrontCounterclockwise = false, IsMultisampleEnabled = true, IsScissorEnabled = false, SlopeScaledDepthBias = 0.0f }); Culling_None = RasterizerState.FromDescription(Device, new RasterizerStateDescription() { CullMode = CullMode.None, DepthBias = 0, DepthBiasClamp = 0.0f, IsDepthClipEnabled = true, FillMode = FillMode.Solid, IsAntialiasedLineEnabled = false, IsFrontCounterclockwise = false, IsMultisampleEnabled = true, IsScissorEnabled = false, SlopeScaledDepthBias = 0.0f }); #endregion #region Blend States { BlendStateDescription desc = new BlendStateDescription(); desc.RenderTargets[0].BlendEnable = true; desc.RenderTargets[0].BlendOperation = BlendOperation.Add; desc.RenderTargets[0].SourceBlend = BlendOption.One; desc.RenderTargets[0].DestinationBlend = BlendOption.Zero; desc.RenderTargets[0].BlendOperationAlpha = BlendOperation.Add; desc.RenderTargets[0].SourceBlendAlpha = BlendOption.One; desc.RenderTargets[0].DestinationBlendAlpha = BlendOption.Zero; desc.RenderTargets[0].RenderTargetWriteMask = ColorWriteMaskFlags.All; Blend_Opaque = BlendState.FromDescription(Device, desc); } { BlendStateDescription desc = new BlendStateDescription(); desc.RenderTargets[0].BlendEnable = true; desc.RenderTargets[0].BlendOperation = BlendOperation.Add; desc.RenderTargets[0].SourceBlend = BlendOption.SourceAlpha; desc.RenderTargets[0].DestinationBlend = BlendOption.InverseSourceAlpha; desc.RenderTargets[0].BlendOperationAlpha = BlendOperation.Add; desc.RenderTargets[0].SourceBlendAlpha = BlendOption.One; desc.RenderTargets[0].DestinationBlendAlpha = BlendOption.Zero; desc.RenderTargets[0].RenderTargetWriteMask = ColorWriteMaskFlags.All; Blend_Alpha = BlendState.FromDescription(Device, desc); } { BlendStateDescription desc = new BlendStateDescription(); desc.RenderTargets[0].BlendEnable = true; desc.RenderTargets[0].BlendOperation = BlendOperation.Add; desc.RenderTargets[0].SourceBlend = BlendOption.One; desc.RenderTargets[0].DestinationBlend = BlendOption.One; desc.RenderTargets[0].BlendOperationAlpha = BlendOperation.Add; desc.RenderTargets[0].SourceBlendAlpha = BlendOption.One; desc.RenderTargets[0].DestinationBlendAlpha = BlendOption.Zero; desc.RenderTargets[0].RenderTargetWriteMask = ColorWriteMaskFlags.All; Blend_Add = BlendState.FromDescription(Device, desc); } { BlendStateDescription desc = new BlendStateDescription(); desc.RenderTargets[0].BlendEnable = true; desc.RenderTargets[0].BlendOperation = BlendOperation.Add; desc.RenderTargets[0].SourceBlend = BlendOption.DestinationColor; desc.RenderTargets[0].DestinationBlend = BlendOption.Zero; desc.RenderTargets[0].BlendOperationAlpha = BlendOperation.Add; desc.RenderTargets[0].SourceBlendAlpha = BlendOption.One; desc.RenderTargets[0].DestinationBlendAlpha = BlendOption.Zero; desc.RenderTargets[0].RenderTargetWriteMask = ColorWriteMaskFlags.All; Blend_Multiply = BlendState.FromDescription(Device, desc); } #endregion // Setup default states Depth = Depth_Enabled; Culling = Culling_Backface; Blend = Blend_Opaque; // Setup other objects shaderresourcemap = new Dictionary<Resource, ShaderResourceView>(); resourceviewslots = new ShaderResourceViewData[MaxPixelShaderResourceViewSlots]; // Send the window created message WindowCreatedMessage windowcreatedmsg = new WindowCreatedMessage(); windowcreatedmsg.Form = window; Owner.MessagePool.SendMessage(windowcreatedmsg); // Show the form window.Show(); }
public void OnInitialise(InitialiseMessage msg) { // Initialise scene manager Console.WriteLine("Initialising scene manager..."); // Initialise messages queuemsg = new PopulateRenderQueue(); queuemsg.SceneManager = this; cameramsg = new PopulateCameraList(); cameramsg.Cameras = new OrderedList<Camera>(new CameraComparer()); cameramsg.ShadowCasters = new HashSet<ShadowCaster>(); lightmsg = new PopulateLightList(); lightmsg.Lights = new OrderedList<Light>(new LightComparer()); psysmsg = new PopulateParticleSystemList(); psysmsg.ParticleSystems = new List<ParticleSystem>(); // Create render queue workitempool = new ResourcePool<RenderWorkItem>(); renderqueue = new OrderedList<RenderWorkItem>(new RenderWorkItemComparer()); effectqueue = new OrderedList<PostProcessEffect>(new PostProcessEffectComparer()); // Setup GBuffer Renderer renderer = Owner.GetComponent<Renderer>(); gbuffer = renderer.CreateRenderTarget(1, "GBuffer"); gbuffer.ClearColour = new Color4(0.0f, 0.0f, 0.0f, 0.0f); gbuffer.AddDepthComponent(); gbuffer_colour = gbuffer.AddTextureComponent(); gbuffer_normal = gbuffer.AddTextureComponent(SlimDX.DXGI.Format.R32G32B32A32_Float); gbuffer_position = gbuffer.AddTextureComponent(SlimDX.DXGI.Format.R32G32B32A32_Float); gbuffer_material = gbuffer.AddTextureComponent(SlimDX.DXGI.Format.R32G32B32A32_Float); gbuffer.Finish(); // Setup light accumulation buffer lightaccum = renderer.CreateRenderTarget(1, "LightAccum"); lightaccum.ClearColour = new Color4(1.0f, 0.0f, 0.0f, 0.0f); lightaccum_diffuse = lightaccum.AddTextureComponent(SlimDX.DXGI.Format.R32G32B32A32_Float); lightaccum_specular = lightaccum.AddTextureComponent(SlimDX.DXGI.Format.R32G32B32A32_Float); lightaccum.Finish(); // Setup particle accumulation buffer particleaccum = renderer.CreateRenderTarget(1, "ParticleAccum"); particleaccum.ClearColour = new Color4(0.0f, 0.0f, 0.0f, 0.0f); particleaccum_colour = particleaccum.AddTextureComponent(); particleaccum.Finish(); // Setup swap buffers swapA = renderer.CreateRenderTarget(1, "SwapA"); swapA_colour = swapA.AddTextureComponent(); swapA.Finish(); swapB = renderer.CreateRenderTarget(1, "SwapB"); swapB_colour = swapB.AddTextureComponent(); swapB.Finish(); // Setup materials MaterialSystem matsys = Owner.GetComponent<MaterialSystem>(); mat_blit = matsys.CreateMaterial("blit", "blit"); mat_blit.SetParameter("smpTexture", renderer.Sampler_Clamp); mat_blitlight = matsys.CreateMaterial("blit_light", "blit_light"); mat_blitlight.SetParameter("smpTexture", renderer.Sampler_Clamp); mat_blitlight.SetParameter("texColour", gbuffer.GetTexture(gbuffer_colour)); mat_blitlight.SetParameter("texDiffuseLight", lightaccum.GetTexture(lightaccum_diffuse)); mat_blitlight.SetParameter("texSpecularLight", lightaccum.GetTexture(lightaccum_specular)); // Setup lights mat_lights = new Dictionary<LightType, Material>(); mat_lights.Add(LightType.Ambient, matsys.CreateMaterial("light_ambient", "light_ambient")); mat_lights.Add(LightType.Directional, matsys.CreateMaterial("light_directional", "light_directional")); mat_lights.Add(LightType.Point, matsys.CreateMaterial("light_point", "light_point")); foreach (Material mat in mat_lights.Values) { mat.SetParameter("texNormal", gbuffer.GetTexture(gbuffer_normal)); mat.SetParameter("texPosition", gbuffer.GetTexture(gbuffer_position)); mat.SetParameter("texMaterial", gbuffer.GetTexture(gbuffer_material)); mat.SetParameter("smpTexture", renderer.Sampler_Clamp); } // Setup meshes mesh_fs = MeshBuilder.BuildFullscreenQuad(); mesh_skybox = MeshBuilder.BuildCube(); }