public void Bind(MMaterial mat, int index) { mat.shader.SetVec3("pointLights[" + index + "].position", MTransform.GetVector3(transform.Position - Globals.GlobalOffset)); mat.shader.SetVec3("pointLights[" + index + "].ambient", Ambient); mat.shader.SetVec3("pointLights[" + index + "].diffuse", Diffuse); mat.shader.SetVec3("pointLights[" + index + "].specular", Specular); mat.shader.SetFloat("pointLights[" + index + "].constant", 1.0f); mat.shader.SetFloat("pointLights[" + index + "].linear", 0.19f); mat.shader.SetFloat("pointLights[" + index + "].quadratic", 0.032f); }
public override void Update() { Vector3 pos = MTransform.GetVector3(transform.Position - Globals.GlobalOffset); // Vector3 pos = new Vector3(0, 0, 1); AL.Listener(ALListener3f.Position, ref pos); Vector3 Vel = Vector3.Zero; AL.Listener(ALListener3f.Velocity, ref Vel); Vector3d up = new Vector3d(0, 1, 0); Vector3 vup = MTransform.GetVector3(up); AL.Listener(ALListenerfv.Orientation, ref pos, ref vup); }
/** * During the Rendering phase all matrices are pre-multiplied (to bring coordinates back to local 32bit space) * and all offsets and set relative to the camera, to aid with shadow calculations - * */ public void Render() { GL.BindFramebuffer(FramebufferTarget.Framebuffer, 0); Globals.ShaderOverride = null; Globals.DrawCalls = 0; Globals.Index = 0; Globals.GlobalOffset = Camera.transform.Position; Globals.GlobalOffsetCalc = Camera.transform.Position; Matrix4d offsetmat = Matrix4d.CreateTranslation(-Globals.GlobalOffset); Matrix4d lightmatrix = light.GetLightSpaceMatrix(); Matrix4d view = Camera.GetViewMatrix(); Matrix4d projection = Camera.GetProjection(true); Matrix4d viewproj = view * projection; // GL.Viewport(0, 0, MScreen.Width, MScreen.Height); //============================ // Globals.GlobalOffset = Vector3d.Zero; // GL.CullFace(CullFaceMode.Back); // reset viewport // 2. render scene as normal using the generated depth/shadow map // -------------------------------------------------------------- for (int i = 0; i < MaterialRoot.Modules.Count; i++) { MMaterial mat = (MMaterial)MaterialRoot.Modules[i]; if (mat.IsUsed == false) { continue; } mat.shader.Bind(); mat.shader.SetMat4("view", MTransform.GetFloatMatrix(view)); mat.shader.SetMat4("projection", MTransform.GetFloatMatrix(projection)); mat.shader.SetVec4("Tweak", new Vector4(Settings.Tweak1, Settings.Tweak2, Settings.Tweak3, Settings.Tweak4)); // set light uniforms Fog.Bind(mat.shader); //TODO: use nearest lights mat.shader.SetInt("NumLights", LightRoot.Modules.Count); //TODO sort lights closest to furthest (because there is max lights =10 in shader) for (int il = 0; il < LightRoot.Modules.Count; il++) { if (il >= MAXLIGHTS) { continue; } //pointLightPositions[0] MPointLight p = (MPointLight)LightRoot.Modules[il]; p.Bind(mat, il); } mat.shader.SetVec3("viewPos", MTransform.GetVector3(Camera.transform.Position - Globals.GlobalOffset)); mat.shader.SetVec3("sunPos", MTransform.GetVector3(light.transform.Position - Globals.GlobalOffset)); mat.shader.SetInt("Closeup", Closeup); light.Bind(mat); } /////////////////////// DEPTH LIGHT FOR SHADOWS /////////////////////// GL.Viewport(0, 0, MScreen.Width, MScreen.Height); // GL.PolygonMode(MaterialFace.FrontAndBack, PolygonMode.Fill); if (light.Shadows) { GL.Enable(EnableCap.CullFace); GL.CullFace(CullFaceMode.Back); //GL.PolygonMode(MaterialFace.FrontAndBack, PolygonMode.Fill); Globals.RenderPass = Globals.eRenderPass.ShadowDepth; //============================ //render depthmap from light using depth shader light.Render(lightmatrix, offsetmat); simpleDepthShader.Bind(); Globals.ShaderOverride = simpleDepthShader; GL.DepthFunc(DepthFunction.Less); GL.Enable(EnableCap.DepthTest); //Background.Render(lightmatrix, offsetmat); //GL.Clear(ClearBufferMask.DepthBufferBit); Background2.Render(lightmatrix, offsetmat); ModelRoot.Render(lightmatrix, offsetmat); Globals.RenderPass = Globals.eRenderPass.Normal; Globals.ShaderOverride = null; } //============================ ////////////////// NORMAL RENDER //////////////////// GL.BindFramebuffer(FramebufferTarget.Framebuffer, 0); //Helper.CheckGLError(this, "Render 1"); //set FBO Renderer.Render(viewproj, offsetmat); //Helper.CheckGLError(this, "Render 1"); //load the shadow map into texture slot 4 GL.ActiveTexture(TextureUnit.Texture0 + MShader.LOCATION_SHADOWMAP); GL.BindTexture(TextureTarget.Texture2D, light.depthMap); //////////////////////////////////////////////////////// // GL.PatchParameter(PatchParameterInt.PatchVertices, 16); RenderScene(ref view, ref offsetmat, ref projection, ref viewproj); //////////// OVERLAY LAYER //////////// //============================ GL.Disable(EnableCap.DepthTest); GL.Clear(ClearBufferMask.DepthBufferBit); if (Overlay.Modules.Count > 0) { projection = Camera.GetOverlayProjection(); view = Camera.GetOverlayViewMatrix(); viewproj = view * projection; //UpdateOverlay(); Overlay.Render(viewproj, Matrix4d.Identity); } if (Stereo == true) { GL.Viewport(0, 0, MScreen.Width / 2, MScreen.Height); Vector3d original = Camera.transform.Position; Camera.transform.Position -= Camera.transform.Right(); view = Camera.GetViewMatrix(); viewproj = view * projection; GL.Viewport(MScreen.Width / 2, 0, MScreen.Width / 2, MScreen.Height); RenderScene(ref view, ref offsetmat, ref projection, ref viewproj); Camera.transform.Position = original; GL.Viewport(0, 0, MScreen.Width, MScreen.Height); } Renderer.AfterRender(); // GL.Disable(EnableCap.DepthTest); // GL.Disable(EnableCap.DepthTest); // render Depth map to quad for visual debugging // --------------------------------------------- //GL.PolygonMode(MaterialFace.Front, PolygonMode.Fill); if (Settings.DebugDepth == true) { debugQuad.Bind(); debugQuad.material.shader.SetFloat("near_plane", light.NearPlane); debugQuad.material.shader.SetFloat("far_plane", light.FarPlane); GL.ActiveTexture(TextureUnit.Texture0); GL.BindTexture(TextureTarget.Texture2D, light.depthMap); GL.Clear(ClearBufferMask.DepthBufferBit); debugQuad.Render(lightmatrix, Matrix4d.Identity); } ///do not reset /// Globals.GlobalOffset = Vector3d.Zero; //projection = Camera.GetProjection(true); //GUIRoot.Render(Matrix4d.Identity, Matrix4d.Identity); ErrorCode err = GL.GetError(); if (err != ErrorCode.NoError) { //Console.WriteLine("MScene Render:" + err); } }