/// <summary> /// Cargar todas la matrices generales que necesita el shader. /// </summary> public void SetShaderMatrix(Effect effect, TGCMatrix world) { var matWorldView = world.ToMatrix() * D3DDevice.Device.Transform.View; var matWorldViewProj = matWorldView * D3DDevice.Device.Transform.Projection; effect.SetValue("matWorld", world.ToMatrix()); effect.SetValue("matWorldView", matWorldView); effect.SetValue("matWorldViewProj", matWorldViewProj); effect.SetValue("matInverseTransposeWorld", TGCMatrix.TransposeMatrix(TGCMatrix.Invert(world)).ToMatrix()); }
public void efectoSombra(TGCVector3 lightDir, TGCVector3 lightPos, TGCMatrix lightView, TGCMatrix projMatrix) { efecto.SetValue("g_vLightPos", new Microsoft.DirectX.Vector4(lightPos.X, lightPos.Y, lightPos.Z, 1)); efecto.SetValue("g_vLightDir", new Microsoft.DirectX.Vector4(lightDir.X, lightDir.Y, lightDir.Z, 1)); efecto.SetValue("g_mProjLight", projMatrix.ToMatrix()); efecto.SetValue("g_mViewLightProj", (lightView * projMatrix).ToMatrix()); }
public void RenderShadowMap() { // Calculo la matriz de view de la luz shadowEffect.SetValue("g_vLightPos", new Vector4(g_LightPos.X, g_LightPos.Y, g_LightPos.Z, 1)); shadowEffect.SetValue("g_vLightDir", new Vector4(g_LightDir.X, g_LightDir.Y, g_LightDir.Z, 1)); skeletalShadowEffect.SetValue("g_vLightPos", new Vector4(g_LightPos.X, g_LightPos.Y, g_LightPos.Z, 1)); skeletalShadowEffect.SetValue("g_vLightDir", new Vector4(g_LightDir.X, g_LightDir.Y, g_LightDir.Z, 1)); g_LightView = TGCMatrix.LookAtLH(g_LightPos, g_LightPos + g_LightDir, new TGCVector3(0, 0, 1)); // inicializacion standard: shadowEffect.SetValue("g_mProjLight", g_mShadowProj.ToMatrix()); shadowEffect.SetValue("g_mViewLightProj", (g_LightView * g_mShadowProj).ToMatrix()); skeletalShadowEffect.SetValue("g_mProjLight", g_mShadowProj.ToMatrix()); skeletalShadowEffect.SetValue("g_mViewLightProj", (g_LightView * g_mShadowProj).ToMatrix()); // Primero genero el shadow map, para ello dibujo desde el pto de vista de luz // a una textura, con el VS y PS que generan un mapa de profundidades. var pOldRT = D3DDevice.Instance.Device.GetRenderTarget(0); var pShadowSurf = g_pShadowMap.GetSurfaceLevel(0); D3DDevice.Instance.Device.SetRenderTarget(0, pShadowSurf); var pOldDS = D3DDevice.Instance.Device.DepthStencilSurface; D3DDevice.Instance.Device.DepthStencilSurface = shadowDepthStencil; D3DDevice.Instance.Device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.Black, 1.0f, 0); D3DDevice.Instance.Device.BeginScene(); // Hago el render de la escena pp dicha shadowEffect.SetValue("g_txShadow", g_pShadowMap); skeletalShadowEffect.SetValue("g_txShadow", g_pShadowMap); // RENDER doingShadowRender = true; RenderScene(); // Termino D3DDevice.Instance.Device.EndScene(); //TextureLoader.Save("shadowmap.bmp", ImageFileFormat.Bmp, g_pShadowMap); // restuaro el render target y el stencil D3DDevice.Instance.Device.DepthStencilSurface = pOldDS; D3DDevice.Instance.Device.SetRenderTarget(0, pOldRT); }
public void RenderShadowMap() { var effect = TgcShaders.Instance.TgcMeshPointLightShader; // Calculo la matriz de view de la luz effect.SetValue("g_vLightPos", new Vector4(g_LightPos.X, g_LightPos.Y, g_LightPos.Z, 1)); effect.SetValue("g_vLightDir", new Vector4(g_LightDir.X, g_LightDir.Y, g_LightDir.Z, 1)); g_LightView = TGCMatrix.LookAtLH(g_LightPos, g_LightPos + g_LightDir, new TGCVector3(0, 0, 1)); // inicializacion standard: effect.SetValue("g_mProjLight", g_mShadowProj.ToMatrix()); effect.SetValue("g_mViewLightProj", (g_LightView * g_mShadowProj).ToMatrix()); // Primero genero el shadow map, para ello dibujo desde el pto de vista de luz // a una textura, con el VS y PS que generan un mapa de profundidades. var pOldRT = D3DDevice.Instance.Device.GetRenderTarget(0); var pShadowSurf = g_pShadowMap.GetSurfaceLevel(0); D3DDevice.Instance.Device.SetRenderTarget(0, pShadowSurf); var pOldDS = D3DDevice.Instance.Device.DepthStencilSurface; D3DDevice.Instance.Device.DepthStencilSurface = g_pDSShadow; D3DDevice.Instance.Device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.Black, 1.0f, 0); D3DDevice.Instance.Device.BeginScene(); // Hago el render de la escena pp dicha effect.SetValue("g_txShadow", g_pShadowMap); navePrincipal.ActionOnNave(m => m.Technique = "RenderShadow"); navePrincipal.Render(sol.Position, Camara.Position); if (spawnaearEnemigos) { enemigos.FindAll(enemigo => enemigo.EstaViva() && enemigo.EnemigoEstaAdelante()).ForEach(e => e.Scene.Meshes.ForEach(m => m.Technique = "RenderShadow")); enemigos.FindAll(enemigo => enemigo.EstaViva() && enemigo.EnemigoEstaAdelante()).ForEach(e => e.Render(sol.Position, Camara.Position)); } escenarios.ForEach(es => es.Scene.Meshes.ForEach(m => m.Technique = "RenderShadow")); escenarios.ForEach(es => es.torres.ForEach(t => t.Scene.Meshes.ForEach(m => m.Technique = "RenderShadow"))); escenarios.ForEach(es => es.Render(sol.Position, Camara.Position, ElapsedTime)); // Termino D3DDevice.Instance.Device.EndScene(); if (save) { TextureLoader.Save("shadowmap.jpg", ImageFileFormat.Jpg, g_pShadowMap); save = false; } // restuaro el render target y el stencil D3DDevice.Instance.Device.DepthStencilSurface = pOldDS; D3DDevice.Instance.Device.SetRenderTarget(0, pOldRT); }
private void RenderShaders()// x ahora solo shadowmap { // Calculo la matriz de view de la luz if (VariablesGlobales.DameLuz) { lightPos = VariablesGlobales.camara.GetPositionAtDistance(-10);//VariablesGlobales.xwing.GetPosition(); lightDir = VariablesGlobales.xwing.GetCoordenadaEsferica().GetXYZCoord(); shader.SetValue("g_vLightPos", new Vector4(lightPos.X, lightPos.Y, lightPos.Z, 1)); shader.SetValue("g_vLightDir", new Vector4(lightDir.X, lightDir.Y, lightDir.Z, 1)); lightView = TGCMatrix.LookAtLH(lightPos, lightPos + lightDir, new TGCVector3(0, 0, 1)); } else { lightPos.Y = VariablesGlobales.xwing.GetPosition().Y + 50; lightPos.Z = VariablesGlobales.xwing.GetPosition().Z; shader.SetValue("g_vLightPos", new Vector4(lightPos.X, lightPos.Y, lightPos.Z, 1)); lightDir = VariablesGlobales.xwing.GetPosition() - lightPos; lightDir.Normalize(); shader.SetValue("g_vLightDir", new Vector4(lightDir.X, lightDir.Y, lightDir.Z, 1)); lightView = TGCMatrix.LookAtLH(lightPos, lightPos + lightDir, new TGCVector3(0, 0, 1)); } //@@@@xq está el up vector en la z?? //@@@@@@@@Probar solo renderizar en el shadow al xwing, y dsps poner al piso como render scene // inicializacion standard: shader.SetValue("g_mProjLight", shadowProj.ToMatrix()); shader.SetValue("g_mViewLightProj", (lightView * shadowProj).ToMatrix()); // Primero genero el shadow map, para ello dibujo desde el pto de vista de luz // a una textura, con el VS y PS que generan un mapa de profundidades. var pShadowSurf = shadow_map.GetSurfaceLevel(0); D3DDevice.Instance.Device.SetRenderTarget(0, pShadowSurf); D3DDevice.Instance.Device.DepthStencilSurface = shadow_depth; D3DDevice.Instance.Device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.Black, 1.0f, 0); // Hago el render de la escena pp dicha shader.SetValue("g_txShadow", shadow_map);//deja la textura seteada pa q se use dsps en el render normal D3DDevice.Instance.Device.BeginScene(); //necesito un metodo q todos puedan pasar su mesh y q el postprocess les ponga la technique shaderManager.SetTechnique("RenderShadow", ShaderManager.MESH_TYPE.SHADOW); shaderManager.RenderMesh(ShaderManager.MESH_TYPE.SHADOW); // Termino D3DDevice.Instance.Device.EndScene(); //TextureLoader.Save(variablesGlobales.shadersDir+"shadowmap.bmp", ImageFileFormat.Bmp, shadow_map); }
public void RenderShadowMap() { //Doy posicion a la luz // Calculo la matriz de view de la luz effect.SetValue("g_vLightPos", new Vector4(g_LightPos.X, g_LightPos.Y, g_LightPos.Z, 1)); effect.SetValue("g_vLightDir", new Vector4(g_LightDir.X, g_LightDir.Y, g_LightDir.Z, 1)); g_LightView = TGCMatrix.LookAtLH(g_LightPos, g_LightPos + g_LightDir, new TGCVector3(0, 0, 1)); // inicializacion standard: effect.SetValue("g_mProjLight", g_mShadowProj.ToMatrix()); effect.SetValue("g_mViewLightProj", (g_LightView * g_mShadowProj).ToMatrix()); // Primero genero el shadow map, para ello dibujo desde el pto de vista de luz // a una textura, con el VS y PS que generan un mapa de profundidades. var pOldRT = D3DDevice.Instance.Device.GetRenderTarget(0); var pShadowSurf = g_pShadowMap.GetSurfaceLevel(0); D3DDevice.Instance.Device.SetRenderTarget(0, pShadowSurf); var pOldDS = D3DDevice.Instance.Device.DepthStencilSurface; D3DDevice.Instance.Device.DepthStencilSurface = g_pDSShadow; D3DDevice.Instance.Device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.White, 1.0f, 0); D3DDevice.Instance.Device.BeginScene(); // Hago el render de la escena pp dicha // solo los objetos que proyectan sombras: //Renderizar terreno terrain.executeRender(effect); // dibujo el bosque foreach (var instance in bosque) { instance.UpdateMeshTransform(); instance.Render(); } // el tanque // Seteo la tecnica: estoy generando la sombra o estoy dibujando la escena mesh.Technique = "RenderShadow"; mesh.Render(); // Termino D3DDevice.Instance.Device.EndScene(); //TextureLoader.Save("shadowmap.bmp", ImageFileFormat.Bmp, g_pShadowMap); // restuaro el render target y el stencil D3DDevice.Instance.Device.DepthStencilSurface = pOldDS; D3DDevice.Instance.Device.SetRenderTarget(0, pOldRT); effect.SetValue("g_txShadow", g_pShadowMap); }
public void RenderShadowMap() { // Calculo la matriz de view de la luz effect.SetValue("g_vLightPos", new TGCVector4(g_LightPos.X, g_LightPos.Y, g_LightPos.Z, 1)); effect.SetValue("g_vLightDir", new TGCVector4(g_LightDir.X, g_LightDir.Y, g_LightDir.Z, 1)); g_LightView = TGCMatrix.LookAtLH(g_LightPos, g_LightPos + g_LightDir, new TGCVector3(0, 0, 1)); arrow.PStart = g_LightPos; arrow.PEnd = g_LightPos + g_LightDir * 20f; arrow.updateValues(); // inicializacion standard: effect.SetValue("g_mProjLight", g_mShadowProj.ToMatrix()); effect.SetValue("g_mViewLightProj", (g_LightView * g_mShadowProj).ToMatrix()); //frustumShadow.updateVolume(TGCMatrix.FromMatrix(D3DDevice.Instance.Device.Transform.View), // TGCMatrix.FromMatrix(D3DDevice.Instance.Device.Transform.Projection)); // Primero genero el shadow map, para ello dibujo desde el pto de vista de luz // a una textura, con el VS y PS que generan un mapa de profundidades. var pOldRT = D3DDevice.Instance.Device.GetRenderTarget(0); var pShadowSurf = g_pShadowMap.GetSurfaceLevel(0); D3DDevice.Instance.Device.SetRenderTarget(0, pShadowSurf); var pOldDS = D3DDevice.Instance.Device.DepthStencilSurface; D3DDevice.Instance.Device.DepthStencilSurface = g_pDSShadow; D3DDevice.Instance.Device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.Gray, 1.0f, 0); //D3DDevice.Instance.Device.BeginScene(); // Hago el render de la escena pp dicha effect.SetValue("g_txShadow", g_pShadowMap); //RenderScene(true,g_LightView, g_mShadowProj); RenderScene2(true); //Termino //D3DDevice.Instance.Device.EndScene(); TextureLoader.Save("shadowmap.bmp", ImageFileFormat.Bmp, g_pShadowMap); // restuaro el render target y el stencil D3DDevice.Instance.Device.DepthStencilSurface = pOldDS; D3DDevice.Instance.Device.SetRenderTarget(0, pOldRT); }
public override void Render() { ClearTextures(); var device = D3DDevice.Instance.Device; // guardo el Render target anterior y seteo la textura como render target var pOldRT = device.GetRenderTarget(0); var pSurf = g_pVel1.GetSurfaceLevel(0); device.SetRenderTarget(0, pSurf); // hago lo mismo con el depthbuffer, necesito el que no tiene multisampling var pOldDS = device.DepthStencilSurface; device.DepthStencilSurface = g_pDepthStencil; // 1 - Genero un mapa de velocidad effect.Technique = "VelocityMap"; // necesito mandarle la matrix de view proj anterior effect.SetValue("matWorldViewProjAnt", antMatWorldView.ToMatrix() * device.Transform.Projection); device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.Black, 1.0f, 0); device.BeginScene(); renderScene("VelocityMap"); device.EndScene(); device.Present(); pSurf.Dispose(); // 2- Genero la imagen pp dicha effect.Technique = "DefaultTechnique"; pSurf = g_pRenderTarget.GetSurfaceLevel(0); device.SetRenderTarget(0, pSurf); device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.Black, 1.0f, 0); device.BeginScene(); renderScene("DefaultTechnique"); device.EndScene(); device.Present(); pSurf.Dispose(); // Ultima pasada vertical va sobre la pantalla pp dicha device.SetRenderTarget(0, pOldRT); device.DepthStencilSurface = pOldDS; device.BeginScene(); effect.Technique = "PostProcessMotionBlur"; device.VertexFormat = CustomVertex.PositionTextured.Format; device.SetStreamSource(0, g_pVBV3D, 0); effect.SetValue("g_RenderTarget", g_pRenderTarget); effect.SetValue("texVelocityMap", g_pVel1); effect.SetValue("texVelocityMapAnt", g_pVel2); device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.Black, 1.0f, 0); effect.Begin(FX.None); effect.BeginPass(0); device.DrawPrimitives(PrimitiveType.TriangleStrip, 0, 2); effect.EndPass(); effect.End(); device.EndScene(); device.Present(); // actualizo los valores para el proximo frame antMatWorldView = mesh.Transform * TGCMatrix.FromMatrix(device.Transform.View); var aux = g_pVel2; g_pVel2 = g_pVel1; g_pVel1 = aux; }