static public void Dispose() { if (s_singleton == null) { return; } if (s_singleton.m_dbgModelEntity != null) { s_singleton.m_dbgModelEntity.Dispose(); } s_singleton = null; }
static public void Initialize() { s_singleton = new CullingSystem(); }
static void Main() { bool bStereoRendering = false; // change to 'false' due to non-stereo rendering for debug int multiThreadCount = 4; // 1 is single thread HmdDevice hmd = null; try { // init oculus rift hmd system HmdSystem.Initialize(); var hmdSys = HmdSystem.GetInstance(); hmd = hmdSys.DetectHmd(); } catch (Exception) { // failed to detect hmd hmd = null; } #if !DEBUG var configForm = new ConfigForm(hmd); Application.Run(configForm); if (configForm.HasResult()) { bStereoRendering = configForm.GetResult().UseHmd; } else { // cancel return; } #endif Size resolution = new Size(); if (!bStereoRendering) { //resolution.Width = 1920;// Full HD //resolution.Height = 1080; resolution.Width = 1280; resolution.Height = 720; } else { hmd.ResetPose(); resolution = hmd.Resolution; } var form = new MainForm(); form.ClientSize = resolution; // Create Device & SwapChain var desc = new SwapChainDescription() { BufferCount = 2, ModeDescription = new ModeDescription(resolution.Width, resolution.Height, new Rational(0, 1), DrawSystem.GetRenderTargetFormat()), IsWindowed = true, OutputHandle = form.GetRenderTarget().Handle, SampleDescription = new SampleDescription(1, 0), SwapEffect = SwapEffect.Sequential, Usage = Usage.RenderTargetOutput, Flags = SwapChainFlags.AllowModeSwitch, }; FeatureLevel[] levels = { FeatureLevel.Level_11_0 }; Device device; SwapChain swapChain; #if DEBUG Device.CreateWithSwapChain(DriverType.Hardware, DeviceCreationFlags.Debug, levels, desc, out device, out swapChain); #else Device.CreateWithSwapChain(DriverType.Hardware, DeviceCreationFlags.None, levels, desc, out device, out swapChain); #endif // Ignore all windows events var factory = swapChain.GetParent <Factory>(); factory.MakeWindowAssociation(form.Handle, WindowAssociationFlags.IgnoreAll); DrawSystem.Initialize(form.GetRenderTarget().Handle, device, swapChain, hmd, bStereoRendering, multiThreadCount); InputSystem.Initialize(form.GetRenderTarget()); EntitySystem.Initialize(); MapSystem.Initialize(); ChrSystem.Initialize(); CameraSystem.Initialize(); CullingSystem.Initialize(); GameSystem.Initialize(); GameSystem.GetInstance().Config.IsUseHmd = bStereoRendering; var scene = new Scene(device, swapChain, form.GetRenderTarget(), hmd, bStereoRendering, multiThreadCount); RenderLoop.Run(form, () => { scene.RenderFrame(); }); scene.Dispose(); // Release GameSystem.Dispose(); CullingSystem.Dispose(); CameraSystem.Dispose(); ChrSystem.Dispose(); MapSystem.Dispose(); EntitySystem.Dispose(); InputSystem.Dispose(); DrawSystem.Dispose(); device.Dispose(); swapChain.Dispose(); HmdSystem.Dispose(); }
/// <summary> /// Update component /// </summary> /// <param name="dT">spend time [sec]</param> public override void Update(double dT) { if (ModelContext.DrawModel == null) { return; } var drawSys = DrawSystem.GetInstance(); var cullingSys = CullingSystem.GetInstance(); var context = drawSys.GetDrawContext(); var dbg = DrawSystem.GetInstance().DebugCtrl; var layout = m_layoutC.Transform; CullingSystem.FrustumCullingResult result = cullingSys.CheckFrustumCulling(ModelContext.DrawModel.BoundingBox, layout); if (!result.IsVisible) { // out of view-volume } else { // Add command for draw mdoel foreach (var node in ModelContext.DrawModel.NodeList) { if (UpdateLine == GameEntityComponent.UpdateLines.PreDraw) { // use draw buffer drawSys.GetDrawBuffer().AppendStaticModel(layout, result.Z, ref node.Mesh, node.Material); } else if (UpdateLine == GameEntityComponent.UpdateLines.Draw) { Matrix[] boneMatrices = null; if (m_skeletonC != null) { // has skeleton boneMatrices = m_skeletonC.Skeleton.GetAllSkinningTransforms(); } MaterialBase material = node.Material; context.DrawModel(layout, Color4.White, node.Mesh, material, DrawSystem.RenderMode.Opaque, boneMatrices); } } } /* * // Add command for debug mdoel * if (dbg.IsEnableDrawTangentFrame && ModelContext.DebugModel != null) * { * foreach (var node in ModelContext.DebugModel.NodeList) * { * var command = DrawCommand.CreateDrawDebugCommand(node.Material, layout, node.Mesh); * drawSys.AddDrawCommand(command); * } * } */ /* * // Add command for draw shadow * if (ModelContext.EnableCastShadow) * { * foreach (var node in ModelContext.DrawModel.NodeList) * { * var command = DrawCommand.CreateDrawShadowCommand(layout, node.Mesh); * drawSys.AddDrawCommand(command); * } * * } */ // draw aabb if (dbg.IsEnableAabb) { if (m_dbgBoundingBoxModel == null) { // create model in first draw time var model = DrawModel.CreateBox("aabb", ModelContext.DrawModel.BoundingBox, Color.Pink); m_dbgBoundingBoxModel = model; } var mesh = m_dbgBoundingBoxModel.NodeList[0].Mesh; MaterialBase material = m_dbgBoundingBoxModel.NodeList[0].Material; context.DrawDebugModel(layout, mesh, DrawSystem.RenderMode.Transparency); } // draw bones for debug if (dbg.IsEnableDrawBone) { if (m_skeletonC != null) { m_skeletonC.Skeleton.DrawDebugModel(layout); } } }
public void RenderFrame() { double dt = m_fps.GetDeltaTime(); var drawSys = DrawSystem.GetInstance(); var cameraSys = CameraSystem.GetInstance(); var inputSys = InputSystem.GetInstance(); var entitySys = EntitySystem.GetInstance(); var mapSys = MapSystem.GetInstance(); var cullingSys = CullingSystem.GetInstance(); // update fps { double avgDT = m_fps.GetAverageDeltaTime(); string text = String.Format("FPS:{0:f2}, DeltaTime:{1:f2}ms", 1.0 / avgDT, avgDT * 1000.0f); m_numberEntity.SetNumber(1.0f / (float)avgDT); } if (m_multiThreadCount > 1) { Task.WaitAll(m_taskList.ToArray()); var tmpTaskResult = new List <CommandList>(m_taskResultList); inputSys.Update(dt); entitySys.UpdateComponents(GameEntityComponent.UpdateLines.Input, dt); entitySys.UpdateComponents(GameEntityComponent.UpdateLines.Behavior, dt); cameraSys.Update(dt); entitySys.UpdateComponents(GameEntityComponent.UpdateLines.PostBehavior, dt); DrawSystem.WorldData worldData; worldData.AmbientColor = new Color3(0.4f, 0.45f, 0.55f); worldData.FogColor = new Color3(0.3f, 0.5f, 0.8f); worldData.NearClip = 0.01f; worldData.FarClip = 100.0f; worldData.DirectionalLight = new DrawSystem.DirectionalLightData() { Direction = new Vector3(0.3f, -0.2f, 0.4f), Color = new Color3(0.6f, 0.6f, 0.5f), }; worldData.Camera = cameraSys.GetCameraData(); drawSys.BeginScene(worldData); var context = drawSys.GetDrawContext(); entitySys.UpdateComponents(GameEntityComponent.UpdateLines.Posing, dt); mapSys.Update(dt, context); cullingSys.UpdateFrustum(); entitySys.UpdateComponents(GameEntityComponent.UpdateLines.PreDraw, dt); drawSys.GetDrawBuffer().Process(drawSys.GetDrawContext()); entitySys.UpdateComponents(GameEntityComponent.UpdateLines.Draw, dt); // start command list generation for the next frame m_taskList.Clear(); m_taskResultList.Clear(); m_taskResultList.AddRange(Enumerable.Repeat <CommandList>(null, m_multiThreadCount)); m_accTime += dt; for (int threadIndex = 0; threadIndex < m_multiThreadCount; ++threadIndex) { int resultIndex = threadIndex; var subThreadContext = drawSys.GetSubThreadContext(threadIndex); m_taskList.Add(Task.Run(() => { // todo : do sub-thread task m_taskResultList[resultIndex] = subThreadContext.FinishCommandList(); })); } foreach (var result in tmpTaskResult) { context.ExecuteCommandList(result); } m_numberEntity.SetPose(ChrSystem.GetInstance().Player.FindComponent <LayoutComponent>().Transform); m_numberEntity.Draw(context); drawSys.EndScene(); } else { // not supported } m_fps.EndFrame(); m_fps.BeginFrame(); }