private void RenderPostProcessEffects(UniformBlock ubo, IEnumerable <IPostProcessEffect> postProcessEffects) { // POST PROCESS EFFECTs if (EnablePostProcessEffects) { gl.DepthTest(false); gl.DepthWrite(false); gl.Blend(false); gl.DrawLinesOnly(false); foreach (var pe in postProcessEffects) { if (pe.IsEnabled == false) { continue; } pe.BeforeBindCallBack(); gBuffer.BindForPostProcessEffects(pe); pe.Shader.Bind(); Factory.QuadMesh.Draw(); } gBuffer.Unbind(); } }
private void RenderDebugBounds(UniformBlock ubo, CameraData camera) { if (Factory.GetShader("internal/debugDrawBounds.shader").Bind()) { gl.DepthTest(false); gl.DepthWrite(false); gl.Blend(false); gl.DrawLinesOnly(true, false); var camPos = camera.ViewPointPosition; for (int i = 0; i < toRenderDefferredCount; i++) { var renderable = toRenderDefferred[i]; var bounds = renderable.GetFloatingOriginSpaceBounds(camPos); var modelMat = Matrix4.CreateScale(bounds.Extents) * Matrix4.CreateTranslation(bounds.Center); var modelViewMat = modelMat * camera.GetRotationMatrix(); ubo.model.modelMatrix = modelMat; ubo.model.modelViewMatrix = modelViewMat; ubo.model.modelViewProjectionMatrix = modelViewMat * camera.GetProjectionMatrix(); ubo.modelUBO.UploadToGPU(); Factory.SkyBoxMesh.Draw(false); } } }
internal void UploadUBOdata(UniformBlock ubo, int lightIndex) { ubo.light.color = this.color; if (type == LightType.Directional) { ubo.light.position = Vector3.Zero; } else { ubo.light.position = this.gameObject.transform.position; } if (type == LightType.Point) { ubo.light.direction = Vector3.Zero; } else { ubo.light.direction = this.gameObject.transform.forward; } ubo.light.spotExponent = this.spotExponent; ubo.light.spotCutOff = this.spotCutOff; ubo.light.hasShadows = hasShadows ? 1 : 0; ubo.light.lightIndex = lightIndex; ubo.lightUBO.UploadData(); }
override internal void UploadUBOandDraw(Camera camera, UniformBlock ubo) { var modelMat = this.gameObject.transform.localToWorldMatrix; var modelViewMat = modelMat * camera.GetViewMat(); ubo.model.modelMatrix = modelMat; ubo.model.modelViewMatrix = modelViewMat; ubo.model.modelViewProjectionMatrix = modelViewMat * camera.GetProjectionMat(); ubo.modelUBO.UploadData(); mesh.Draw(); }
internal void UploadDataToUBO(UniformBlock ubo) { ubo.engine.viewMatrix = GetViewMat(); ubo.engine.projectionMatrix = GetProjectionMat(); ubo.engine.viewProjectionMatrix = ubo.engine.viewMatrix * ubo.engine.projectionMatrix; ubo.engine.cameraPosition = this.gameObject.transform.position; ubo.engine.screenSize = this.screenSize; ubo.engine.nearClipPlane = this.nearClipPlane; ubo.engine.farClipPlane = this.farClipPlane; GL.Viewport(0, 0, pixelWidth, pixelHeight); ubo.engineUBO.UploadData(); }
private void RenderLights(UniformBlock ubo, CameraData camera, IList <ILight> allLights) { lock (allLights) { for (int lightIndex = 0; lightIndex < allLights.Count; lightIndex++) { var light = allLights[lightIndex]; if (light == null) { continue; } var shadowMap = light.ShadowMap; #region SHADOW MAAPING /* * if (shadowsEnabled && light.HasShadows) * { * //GL.Enable(EnableCap.CullFace); * //GL.CullFace(CullFaceMode.Back); * * shadowMap.FrameBufferForWriting(); * * GL.Enable(EnableCap.DepthTest); My.Check(); * GL.DepthMask(true); My.Check(); * * shadowMap.Clear(); * * shadowMap.shadowViewCamera.UploadDataToUBO(ubo); * * for (int i = 0; i < allRenderers.Count; i++) * { * var renderer = allRenderers[i]; * if (renderer == null) continue; * * //if (renderer.CanBeFrustumCulled == false || GeometryUtility.TestPlanesAABB(frustrumPlanes, renderer.bounds)) * { * renderer.Material.BeforeBindCallback(); * renderer.Material.Uniforms.SendAllUniformsTo(renderer.Material.DepthGrabShader.Uniforms); * renderer.Material.DepthGrabShader.Bind(); * renderer.UploadUBOandDraw(shadowMap.shadowViewCamera, ubo); * } * } * }*/ #endregion SHADOW MAAPING camera.UploadCameraDataToUBO(ubo); // bind camera view params // G BUFFER LIGHT PASS { gl.DepthWrite(false); gl.DepthTest(false); gl.DrawLinesOnly(false); light.UploadUBOdata(camera, ubo, lightIndex); var shader = Factory.GetShader("internal/deferred.oneLight.shader"); gBuffer.BindForLightPass(shader); if (lightIndex == 0) { GL.Clear(ClearBufferMask.ColorBufferBit); MyGL.Check(); } if (ShadowsEnabled && light.HasShadows) { shadowMap.BindUniforms(shader); } if (shader.Bind()) { //GL.Enable(EnableCap.Blend); //GL.BlendEquationSeparate(BlendEquationMode.FuncAdd, BlendEquationMode.FuncAdd); //GL.BlendFunc(BlendingFactorSrc.SrcColor, BlendingFactorDest.SrcColor); GL.BlendEquation(BlendEquationMode.FuncAdd); MyGL.Check(); GL.BlendFunc(BlendingFactorSrc.One, BlendingFactorDest.One); MyGL.Check(); Factory.QuadMesh.Draw(); GL.Disable(EnableCap.Blend); MyGL.Check(); } gBuffer.Unbind(); } } } }
private void RenderGBuffer(UniformBlock ubo, CameraData camera) { // G BUFFER GRAB PASS { gBuffer.BindAllFrameBuffersForDrawing(); // SKYBOX PASS if (Debug.GetCVar("rendering / debug / render white background")) { GL.ClearColor(System.Drawing.Color.White); MyGL.Check(); GL.DepthMask(true); MyGL.Check(); GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); MyGL.Check(); } else if (Debug.GetCVar("rendering / debug / render black background")) { GL.ClearColor(System.Drawing.Color.Black); MyGL.Check(); GL.DepthMask(true); MyGL.Check(); GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); MyGL.Check(); } else { GL.ClearColor(System.Drawing.Color.Black); MyGL.Check(); GL.DepthMask(true); MyGL.Check(); GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); MyGL.Check(); if (SkyboxCubeMap != null) { GL.Disable(EnableCap.DepthTest); MyGL.Check(); GL.DepthMask(false); MyGL.Check(); GL.PolygonMode(MaterialFace.FrontAndBack, PolygonMode.Fill); MyGL.Check(); var shader = Factory.GetShader("internal/deferred.skybox.shader"); shader.Uniforms.Set("skyboxCubeMap", SkyboxCubeMap); shader.Bind(); Factory.SkyBoxMesh.Draw(); } } SetPolygonMode(); // RENDER ALL OBJECTS { gl.DepthWrite(true); gl.DepthTest(true); gl.Blend(false); for (int i = 0; i < toRenderDefferredCount; i++) { var renderable = toRenderDefferred[i]; renderable.Material.BeforeBindCallback(); renderable.Material.Uniforms.SendAllUniformsTo(renderable.Material.RenderShader.Uniforms); renderable.Material.RenderShader.Bind(); renderable.UploadUBOandDraw(camera, ubo); } // GL.MultiDrawElementsIndirect } gBuffer.Unbind(); } }
public void RenderAll(Camera renderCamera, UniformBlock ubo, IList <ILight> allLights, IEnumerable <IPostProcessEffect> postProcessEffect) { gl.SetDefaults(); var camera = prepardWithCameraData; if (PauseRenderPrepare) { camera = renderCamera.GetDataCopy(); } camera.UploadCameraDataToUBO(ubo); // bind camera view params and matrices only once RenderGBuffer(ubo, camera); RenderLights(ubo, camera, allLights); // FORWARD RENDERING, TRANSPARENT OBJECTS { gl.DepthWrite(false); gl.DepthTest(true); gl.Blend(true); GL.BlendEquation(BlendEquationMode.FuncAdd); MyGL.Check(); GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha); MyGL.Check(); gl.DrawLinesOnly(DrawLines, false); for (int i = 0; i < toRenderTransparentCount; i++) { var renderable = toRenderTransparent[i]; renderable.Material.BeforeBindCallback(); renderable.Material.Uniforms.SendAllUniformsTo(renderable.Material.RenderShader.Uniforms); gBuffer.BindForTransparentPass(renderable.Material.RenderShader); renderable.Material.RenderShader.Bind(); renderable.UploadUBOandDraw(camera, ubo); } } RenderPostProcessEffects(ubo, postProcessEffect); // FINAL DRAW TO SCREEN { gl.DepthWrite(false); gl.DepthTest(false); gl.Blend(false); gl.DrawLinesOnly(false); GL.BindFramebuffer(FramebufferTarget.DrawFramebuffer, 0); MyGL.Check(); GL.Viewport(0, 0, camera.PixelWidth, camera.PixelHeight); MyGL.Check(); FinalDrawShader.Uniforms.Set("finalDrawTexture", gBuffer.FinalTextureToRead); if (FinalDrawShader.Bind()) { Factory.QuadMesh.Draw(); } } if (DebugBounds) { RenderDebugBounds(ubo, camera); } if (Debug.GetCVar("rendering / debug / draw normal buffer contents")) { gBuffer.DebugDrawNormal(); } if (Debug.GetCVar("rendering / debug / draw gbuffer contents")) { gBuffer.DebugDrawContents(); } //if (drawShadowMapContents) DebugDrawTexture(shadowMap.depthMap, new Vector4(0.5f, 0.5f, 1, 1), new Vector4(0.5f,0.5f,0,1), 1, 0); ErrorCode glError; while ((glError = GL.GetError()) != ErrorCode.NoError) { Log.Error("GL Error: " + glError); } }
void OnStart() { ubo = new UniformBlock(); //new PhysicsUsage.PhysicsManager(); stopwatchSinceStart.Restart(); renderManagerFront = new RenderManager(gameWindow.Width, gameWindow.Height); renderManagerBack = new RenderManager(gameWindow.Width, gameWindow.Height); /*Debug.CommonCVars.VSync().ToogledByKey(OpenTK.Input.Key.V).OnChanged += (cvar) => * { * if (cvar.Bool) VSync = VSyncMode.On; * else VSync = VSyncMode.Off; * }; * Debug.CommonCVars.VSync().InitializeWith(false);*/ Debug.GetCVar("rendering / fullscreen").OnChangedAndNow((cvar) => { if (cvar.Bool && WindowState != WindowState.Fullscreen) { WindowState = WindowState.Fullscreen; } else { WindowState = WindowState.Normal; } }); WindowTitle = defaultWindowTitle; foreach (StringName r in System.Enum.GetValues(typeof(StringName))) { if (r == StringName.Extensions) { break; } var str = GL.GetString(r); MyGL.Check(); Log.Info(r.ToString() + ": " + str); } // Other state //GL.Enable(EnableCap.Texture2D); My.Check(); //GL.Hint(HintTarget.PerspectiveCorrectionHint, HintMode.Nicest); My.Check(); //GL.Enable(EnableCap.Multisample); My.Check(); gameWindow.Resize += (sender, e) => OnResize(); OnResize(); gameWindow.Visible = true; StartOtherThreads(); while (ShouldContinueRunning) { MainLoop(); } gameWindow.Exit(); }
virtual internal void UploadUBOandDraw(Camera camera, UniformBlock ubo) { }