/// <summary> /// Unload contents /// </summary> public override void UnloadContent() { if (TarnishTexture != null) { TarnishTexture.Dispose(); } if (CubeTexture != null) { CubeTexture.Dispose(); } if (Cube != null) { Cube.Dispose(); } if (Sphere != null) { Sphere.Dispose(); } if (Skybox != null) { Skybox.Dispose(); } if (Reflection != null) { Reflection.Dispose(); } }
/// <summary> /// Liberar recursos /// </summary> public static void Dispose() { Textura.Dispose(); Shader.Dispose(); VerticesBuffer.Dispose(); IndicesBuffer.Dispose(); Vertices = null; }
public override void close() { effect.Dispose(); foreach (TgcMeshBumpMapping m in meshes) { m.dispose(); } cubeMap.Dispose(); lightMesh.dispose(); }
public override void Dispose() { effect.Dispose(); foreach (var m in meshes) { m.Dispose(); } cubeMap.Dispose(); lightMesh.Dispose(); }
internal void close() { ModeloRobot.dispose(); ModeloNave.dispose(); ModeloProyectil.dispose(); windShader.Dispose(); envMap.Dispose(); skeletalEnvMap.Dispose(); cubeMap.Dispose(); Vegetation.disposeAll(); Arbustos.disposeAll(); terrain.dispose(); player1.dispose(); skyBox.dispose(); foreach (Enemy enemy in enemies) { enemy.dispose(); } enemies.Clear(); foreach (Proyectil proy in proyectiles) { proy.dispose(); } proyectiles.Clear(); foreach (Barril barril in barriles) { barril.dispose(); } barriles.Clear(); Barriles.disposeAll(); foreach (Pasto pasto in pastos) { pasto.dispose(); } foreach (TgcMesh tesoro in tesoros) { tesoro.dispose(); } tesoros.Clear(); Tesoros.disposeAll(); foreach (TgcSprite pointer in pointers) { pointer.dispose(); } pointers.Clear(); }
public override void UnLoadResources() { if (pEffect != null) { pEffect.Dispose(); } if (pEnvMap != null) { pEnvMap.Dispose(); } }
public void close() { effect.Dispose(); //scene.disposeAll(); //scene2.disposeAll(); sceneAgua.disposeAll(); sceneCanoa.disposeAll(); terrain.dispose(); g_pCubeMapAgua.Dispose(); g_pShadowMap.Dispose(); g_pDSShadow.Dispose(); }
public override void Dispose() { effect.Dispose(); scene.disposeAll(); scene2.disposeAll(); scene3.disposeAll(); scene4.disposeAll(); terrain.dispose(); g_pCubeMapAgua.Dispose(); g_pShadowMap.Dispose(); g_pDSShadow.Dispose(); }
/// <summary> /// 从ResourceLocation加载天空盒纹理 /// </summary> /// <param name="dayTexture"></param> /// <param name="nightTexture"></param> public void LoadTexture(ResourceLocation dayTexture, ResourceLocation nightTexture) { if (dayTexture != null) { if (dayTex != null) { dayTex.Dispose(); dayTex = null; } this.dayTex = CubeTexture.FromStream(device, dayTexture.GetStream, Usage.None, Pool.Managed); } if (nightTexture != null) { if (nightTex != null) { nightTex.Dispose(); nightTex = null; } this.nightTex = CubeTexture.FromStream(device, nightTexture.GetStream, Usage.None, Pool.Managed); } }
public override void Dispose() { meshes.ForEach(m => m.Dispose()); pbrMesh.Dispose(); effect.Dispose(); cubeMap.Dispose(); irradianceMap.Dispose(); prefilterMap.Dispose(); depthStencils.ForEach(depthStencil => depthStencil.Dispose()); bdrfLUT.Dispose(); lightBoxes.ForEach(lightBox => lightBox.Dispose()); unitCube.Dispose(); fullQuadVertexBuffer.Dispose(); }
protected virtual void Dispose(bool disposeManaged) { textures.ForEach(x => x.Texture.Dispose()); if (defaultTexture != null) { defaultTexture.Dispose(); } if (enviroMap != null) { enviroMap.Dispose(); } if (lightDiffuseMap != null) { lightDiffuseMap.Dispose(); } textures = null; }
public override void Render() { PreRender(); ClearTextures(); bool invisibilidadActivada = (SwitchInvisibilidadJ1 - 1 == SwitchCamara) || (SwitchInvisibilidadJ2 == SwitchCamara); //Permito las particulas D3DDevice.Instance.ParticlesEnabled = true; D3DDevice.Instance.EnableParticles(); switch (SwitchInicio) { case 1: { Hud.PantallaInicio(); if (Input.keyPressed(Key.C)) { SwitchInicio = 2; } if (Input.keyPressed(Key.D1)) { Jugadores[1] = null; SwitchInicio = 3; SwitchMusica = true; SwitchFX = true; AutoFisico1.Encendido(); inGame = true; } if (Input.keyPressed(Key.D2)) { juegoDoble = true; SwitchInicio = 4; SwitchMusica = true; SwitchFX = true; SwitchCamara = 3; AutoFisico1.Encendido(); AutoFisico2.Encendido(); inGame = true; } break; } case 2: { Hud.PantallaControles(); if (Input.keyPressed(Key.V)) { SwitchInicio = 1; } break; } case 3: { var device = D3DDevice.Instance.Device; Tiempo += ElapsedTime; AutoFisico1.ElapsedTime = ElapsedTime; AutoFisico1.FXActivado = SwitchFX; // ShaderInvisibilidad ----- Invisibilidad.Technique = "DefaultTechnique"; var pOldRT = device.GetRenderTarget(0); var pSurf = g_pRenderTarget.GetSurfaceLevel(0); if (invisibilidadActivada) { device.SetRenderTarget(0, pSurf); } var pOldDS = device.DepthStencilSurface; if (invisibilidadActivada) { device.DepthStencilSurface = g_pDepthStencil; } device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.Black, 1.0f, 0); //--------------------------- Plaza.RenderAll(); AutoFisico1.Render(ElapsedTime); GrupoPolicias.Render(ElapsedTime); Cielo.Render(); pSurf.Dispose(); if (invisibilidadActivada) { device.DepthStencilSurface = pOldDS; device.SetRenderTarget(0, pOldRT); Invisibilidad.Technique = "PostProcess"; Invisibilidad.SetValue("time", Tiempo); device.VertexFormat = CustomVertex.PositionTextured.Format; device.SetStreamSource(0, g_pVBV3D, 0); Invisibilidad.SetValue("g_RenderTarget", g_pRenderTarget); device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.Black, 1.0f, 0); Invisibilidad.Begin(FX.None); Invisibilidad.BeginPass(0); device.DrawPrimitives(PrimitiveType.TriangleStrip, 0, 2); Invisibilidad.EndPass(); Invisibilidad.End(); } Hud.Juego(invisibilidadActivada, JugadorActivo, juegoDoble, pantallaDoble, AutoFisico1, AutoFisico2); if (AutoFisico1.Vida < 0) { TiempoFinal = Tiempo; Sonidos.SuenaGameOver(); SwitchInicio = 5; } if (Input.keyDown(Key.F10)) { Hud.Pausar(); } // Shader Enviroment Map -------------------------------- //D3DDevice.Instance.Device.EndScene(); var g_pCubeMap = new CubeTexture(D3DDevice.Instance.Device, 256, 1, Usage.RenderTarget, Format.A16B16G16R16F, Pool.Default); var pOldRT2 = D3DDevice.Instance.Device.GetRenderTarget(0); D3DDevice.Instance.Device.Transform.Projection = TGCMatrix.PerspectiveFovLH(Geometry.DegreeToRadian(90.0f), 1f, 1f, 10000f).ToMatrix(); // Genero las caras del enviroment map for (var nFace = CubeMapFace.PositiveX; nFace <= CubeMapFace.NegativeZ; ++nFace) { var pFace = g_pCubeMap.GetCubeMapSurface(nFace, 0); D3DDevice.Instance.Device.SetRenderTarget(0, pFace); TGCVector3 Dir, VUP; Color color; switch (nFace) { default: case CubeMapFace.PositiveX: // Left Dir = new TGCVector3(1, 0, 0); VUP = TGCVector3.Up; color = Color.Black; break; case CubeMapFace.NegativeX: // Right Dir = new TGCVector3(-1, 0, 0); VUP = TGCVector3.Up; color = Color.Red; break; case CubeMapFace.PositiveY: // Up Dir = TGCVector3.Up; VUP = new TGCVector3(0, 0, -1); color = Color.Gray; break; case CubeMapFace.NegativeY: // Down Dir = TGCVector3.Down; VUP = new TGCVector3(0, 0, 1); color = Color.Yellow; break; case CubeMapFace.PositiveZ: // Front Dir = new TGCVector3(0, 0, 1); VUP = TGCVector3.Up; color = Color.Green; break; case CubeMapFace.NegativeZ: // Back Dir = new TGCVector3(0, 0, -1); VUP = TGCVector3.Up; color = Color.Blue; break; } D3DDevice.Instance.Device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, color, 1.0f, 0); //Renderizar foreach (var mesh in AutoFisico1.Mayas) { mesh.Effect = EnvMap; mesh.Technique = "RenderScene"; mesh.Render(); } } D3DDevice.Instance.Device.SetRenderTarget(0, pOldRT2); D3DDevice.Instance.Device.Transform.View = Camara.GetViewMatrix().ToMatrix(); D3DDevice.Instance.Device.Transform.Projection = TGCMatrix.PerspectiveFovLH(Geometry.DegreeToRadian(45.0f), D3DDevice.Instance.AspectRatio, 1f, 10000f).ToMatrix(); //D3DDevice.Instance.Device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.Black, 1.0f, 0); EnvMap.SetValue("g_txCubeMap", g_pCubeMap); foreach (var mesh in AutoFisico1.Mayas) { mesh.Effect = EnvMap; mesh.Technique = "RenderScene"; mesh.Render(); } foreach (var rueda in AutoFisico1.Ruedas) { rueda.Effect = EnvMap; rueda.Technique = "RenderScene"; rueda.Render(); } foreach (var mesh in GrupoPolicias.Todos[0].Mayas) { mesh.Effect = EnvMap; mesh.Technique = "RenderScene"; mesh.Render(); } g_pCubeMap.Dispose(); //------------------------------------------------------------- Hud.Tiempo(FastMath.Floor(Tiempo)); break; } case 4: { var device = D3DDevice.Instance.Device; Tiempo += ElapsedTime; AutoFisico1.ElapsedTime = ElapsedTime; AutoFisico2.ElapsedTime = ElapsedTime; AutoFisico1.FXActivado = SwitchFX; AutoFisico2.FXActivado = SwitchFX; // ShaderInvisibilidad ----- Invisibilidad.Technique = "DefaultTechnique"; var pOldRT = device.GetRenderTarget(0); var pSurf = g_pRenderTarget.GetSurfaceLevel(0); if (invisibilidadActivada) { device.SetRenderTarget(0, pSurf); } var pOldDS = device.DepthStencilSurface; if (invisibilidadActivada) { device.DepthStencilSurface = g_pDepthStencil; } device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.Black, 1.0f, 0); //-------------------------- DrawText.drawText("Velocidad P1:" + AutoFisico1.Velocidad, 0, 90, Color.Green); Plaza.RenderAll(); AutoFisico1.Render(ElapsedTime); AutoFisico2.Render(ElapsedTime); GrupoPolicias.Render(ElapsedTime); Cielo.Render(); pSurf.Dispose(); if (invisibilidadActivada) { device.DepthStencilSurface = pOldDS; device.SetRenderTarget(0, pOldRT); Invisibilidad.Technique = "PostProcess"; Invisibilidad.SetValue("time", Tiempo); device.VertexFormat = CustomVertex.PositionTextured.Format; device.SetStreamSource(0, g_pVBV3D, 0); Invisibilidad.SetValue("g_RenderTarget", g_pRenderTarget); device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.Black, 1.0f, 0); Invisibilidad.Begin(FX.None); Invisibilidad.BeginPass(0); device.DrawPrimitives(PrimitiveType.TriangleStrip, 0, 2); Invisibilidad.EndPass(); Invisibilidad.End(); } Hud.Juego(invisibilidadActivada, JugadorActivo, juegoDoble, pantallaDoble, AutoFisico1, AutoFisico2); if (AutoFisico1.Vida < 0) { Hud.GanoJ2(); SwitchCamara = 2; Jugadores[1] = null; Sonidos.SuenaAplausos(); inGame = false; } if (AutoFisico2.Vida < 0) { Hud.GanoJ1(); SwitchCamara = 1; Jugadores[0] = null; Sonidos.SuenaAplausos(); inGame = false; } if (Input.keyDown(Key.F10)) { Hud.Pausar(); } if (Input.keyDown(Key.F10)) { Hud.Pausar(); } // Shader Enviroment Map -------------------------------- //D3DDevice.Instance.Device.EndScene(); var g_pCubeMap = new CubeTexture(D3DDevice.Instance.Device, 256, 1, Usage.RenderTarget, Format.A16B16G16R16F, Pool.Default); var pOldRT2 = D3DDevice.Instance.Device.GetRenderTarget(0); D3DDevice.Instance.Device.Transform.Projection = TGCMatrix.PerspectiveFovLH(Geometry.DegreeToRadian(90.0f), 1f, 1f, 10000f).ToMatrix(); // Genero las caras del enviroment map for (var nFace = CubeMapFace.PositiveX; nFace <= CubeMapFace.NegativeZ; ++nFace) { var pFace = g_pCubeMap.GetCubeMapSurface(nFace, 0); D3DDevice.Instance.Device.SetRenderTarget(0, pFace); TGCVector3 Dir, VUP; Color color; switch (nFace) { default: case CubeMapFace.PositiveX: // Left Dir = new TGCVector3(1, 0, 0); VUP = TGCVector3.Up; color = Color.Black; break; case CubeMapFace.NegativeX: // Right Dir = new TGCVector3(-1, 0, 0); VUP = TGCVector3.Up; color = Color.Red; break; case CubeMapFace.PositiveY: // Up Dir = TGCVector3.Up; VUP = new TGCVector3(0, 0, -1); color = Color.Gray; break; case CubeMapFace.NegativeY: // Down Dir = TGCVector3.Down; VUP = new TGCVector3(0, 0, 1); color = Color.Yellow; break; case CubeMapFace.PositiveZ: // Front Dir = new TGCVector3(0, 0, 1); VUP = TGCVector3.Up; color = Color.Green; break; case CubeMapFace.NegativeZ: // Back Dir = new TGCVector3(0, 0, -1); VUP = TGCVector3.Up; color = Color.Blue; break; } D3DDevice.Instance.Device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, color, 1.0f, 0); //Renderizar foreach (var mesh in AutoFisico1.Mayas) { mesh.Effect = EnvMap; mesh.Technique = "RenderScene"; mesh.Render(); } foreach (var mesh in AutoFisico2.Mayas) { mesh.Effect = EnvMap; mesh.Technique = "RenderScene"; mesh.Render(); } } D3DDevice.Instance.Device.SetRenderTarget(0, pOldRT2); D3DDevice.Instance.Device.Transform.View = Camara.GetViewMatrix().ToMatrix(); D3DDevice.Instance.Device.Transform.Projection = TGCMatrix.PerspectiveFovLH(Geometry.DegreeToRadian(45.0f), D3DDevice.Instance.AspectRatio, 1f, 10000f).ToMatrix(); //D3DDevice.Instance.Device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.Black, 1.0f, 0); EnvMap.SetValue("g_txCubeMap", g_pCubeMap); foreach (var mesh in AutoFisico1.Mayas) { mesh.Effect = EnvMap; mesh.Technique = "RenderScene"; mesh.Render(); } foreach (var mesh in AutoFisico2.Mayas) { mesh.Effect = EnvMap; mesh.Technique = "RenderScene"; mesh.Render(); } foreach (var rueda in AutoFisico1.Ruedas) { rueda.Effect = EnvMap; rueda.Technique = "RenderScene"; rueda.Render(); } foreach (var mesh in GrupoPolicias.Todos[0].Mayas) { mesh.Effect = EnvMap; mesh.Technique = "RenderScene"; mesh.Render(); } g_pCubeMap.Dispose(); //------------------------------------------------------------- Hud.Tiempo(FastMath.Floor(Tiempo)); break; } case 5: { SwitchFX = false; SwitchMusica = false; inGame = false; Hud.JuegoTerminado(); Hud.TiempoFinal(FastMath.Floor(TiempoFinal)); if (Input.keyPressed(Key.M)) { SwitchInicio = 1; } break; } } PostRender(); }
public override void render(float elapsedTime) { Device device = GuiController.Instance.D3dDevice; Control panel3d = GuiController.Instance.Panel3d; float aspectRatio = (float)panel3d.Width / (float)panel3d.Height; time += elapsedTime; if (GuiController.Instance.D3dInput.keyPressed(Microsoft.DirectX.DirectInput.Key.C)) { timer_preview = 0; camara_rot = !camara_rot; } if (GuiController.Instance.D3dInput.keyPressed(Microsoft.DirectX.DirectInput.Key.F)) { if (tipo_vista == 1) tipo_vista = 0; else tipo_vista = 1; ant_vista = tipo_vista; } if (GuiController.Instance.D3dInput.keyPressed(Microsoft.DirectX.DirectInput.Key.D)) { if (tipo_vista == 2) tipo_vista = ant_vista; else tipo_vista = 2; } if (GuiController.Instance.D3dInput.keyPressed(Microsoft.DirectX.DirectInput.Key.Space)) { if (vel_tanque <= 1) vel_tanque = 10; else vel_tanque = 1; } if (timer_preview>0) { timer_preview -= elapsedTime; if (timer_preview < 0) timer_preview = 0; } // animar tanque an_tanque -= elapsedTime * Geometry.DegreeToRadian(vel_tanque); float alfa = an_tanque; float x0 = 2000f * (float)Math.Cos(alfa); float z0 = 2000f * (float)Math.Sin(alfa); float offset_rueda = 10; float H = terrain.CalcularAltura(x0, z0) + alto_tanque / 2 - offset_rueda; if (H < nivel_mar) H = nivel_mar; mesh.Position = new Vector3(x0, H, z0); // direccion tangente sobre el piso: Vector2 dir_tanque = new Vector2(-(float)Math.Sin(alfa), (float)Math.Cos(alfa)); dir_tanque.Normalize(); // Posicion de la parte de adelante del tanque Vector2 pos2d = new Vector2(x0, z0); pos2d = pos2d + dir_tanque * (largo_tanque / 2); float H_frente = terrain.CalcularAltura(pos2d.X, pos2d.Y) + alto_tanque / 2 - offset_rueda; if (H_frente < nivel_mar-15) H_frente = nivel_mar-15; Vector3 pos_frente = new Vector3(pos2d.X, H_frente, pos2d.Y); Vector3 Vel = pos_frente - mesh.Position; Vel.Normalize(); mesh.Transform = CalcularMatriz(mesh.Position, mesh.Scale, Vel); // animo la canoa en circulos: alfa = -time * Geometry.DegreeToRadian(10.0f); x0 = 400f * (float)Math.Cos(alfa); z0 = 400f * (float)Math.Sin(alfa); canoa.Position = new Vector3(x0, 150, z0); dir_canoa = new Vector3(-(float)Math.Sin(alfa), 0, (float)Math.Cos(alfa)); canoa.Transform = CalcularMatriz(canoa.Position, canoa.Scale, dir_canoa); alfa_sol += elapsedTime* Geometry.DegreeToRadian(1.0f); if (alfa_sol > 2.5) alfa_sol = 1.5f; // animo la posicion del sol //g_LightPos = new Vector3(1500f * (float)Math.Cos(alfa_sol), 1500f * (float)Math.Sin(alfa_sol), 0f); g_LightPos = new Vector3(2000f * (float)Math.Cos(alfa_sol), 2000f * (float)Math.Sin(alfa_sol), 0f); g_LightDir = -g_LightPos; g_LightDir.Normalize(); if (timer_preview > 0) { float an = -time * Geometry.DegreeToRadian(10.0f); LookFrom.X = 1500f * (float)Math.Sin(an); LookFrom.Z = 1500f * (float)Math.Cos(an); } else { if (camara_rot) { GuiController.Instance.RotCamera.targetObject(mesh.BoundingBox); GuiController.Instance.CurrentCamera.updateCamera(); } else { GuiController.Instance.RotCamera.CameraCenter = new Vector3(0, 200, 0); GuiController.Instance.RotCamera.CameraDistance = 2000; GuiController.Instance.RotCamera.RotationSpeed = 1f; GuiController.Instance.RotCamera.ZoomFactor = 0.1f; } } // -------------------------------------------------------------------- device.EndScene(); if (g_pCubeMapAgua == null) { // solo la primera vez crea el env map del agua CrearEnvMapAgua(); // ya que esta creado, se lo asigno al effecto: effect.SetValue("g_txCubeMapAgua", g_pCubeMapAgua); } // Creo el env map del tanque: CubeTexture g_pCubeMap = new CubeTexture(device, 256, 1, Usage.RenderTarget, Format.A16B16G16R16F, Pool.Default); Surface pOldRT = device.GetRenderTarget(0); // ojo: es fundamental que el fov sea de 90 grados. // asi que re-genero la matriz de proyeccion device.Transform.Projection = Matrix.PerspectiveFovLH(Geometry.DegreeToRadian(90.0f),1f, near_plane,far_plane); // Genero las caras del enviroment map for (CubeMapFace nFace = CubeMapFace.PositiveX; nFace <= CubeMapFace.NegativeZ; ++nFace) { Surface pFace = g_pCubeMap.GetCubeMapSurface(nFace, 0); device.SetRenderTarget(0, pFace); Vector3 Dir, VUP; Color color; switch (nFace) { default: case CubeMapFace.PositiveX: // Left Dir = new Vector3(1, 0, 0); VUP = new Vector3(0, 1, 0); color = Color.Black; break; case CubeMapFace.NegativeX: // Right Dir = new Vector3(-1, 0, 0); VUP = new Vector3(0, 1, 0); color = Color.Red; break; case CubeMapFace.PositiveY: // Up Dir = new Vector3(0, 1, 0); VUP = new Vector3(0, 0, -1); color = Color.Gray; break; case CubeMapFace.NegativeY: // Down Dir = new Vector3(0, -1, 0); VUP = new Vector3(0, 0, 1); color = Color.Yellow; break; case CubeMapFace.PositiveZ: // Front Dir = new Vector3(0, 0, 1); VUP = new Vector3(0, 1, 0); color = Color.Green; break; case CubeMapFace.NegativeZ: // Back Dir = new Vector3(0, 0, -1); VUP = new Vector3(0, 1, 0); color = Color.Blue; break; } //Obtener ViewMatrix haciendo un LookAt desde la posicion final anterior al centro de la camara Vector3 Pos = mesh.Position; device.Transform.View = Matrix.LookAtLH(Pos, Pos + Dir, VUP); device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, color, 1.0f, 0); device.BeginScene(); //Renderizar renderScene(elapsedTime, true); device.EndScene(); //string fname = string.Format("face{0:D}.bmp", nFace); //SurfaceLoader.Save(fname, ImageFileFormat.Bmp, pFace); } // restuaro el render target device.SetRenderTarget(0, pOldRT); //TextureLoader.Save("test.bmp", ImageFileFormat.Bmp, g_pCubeMap); //Genero el shadow map RenderShadowMap(); // Restauro el estado de las transformaciones if (timer_preview > 0) device.Transform.View = Matrix.LookAtLH(LookFrom,LookAt,new Vector3(0,1,0)); else GuiController.Instance.CurrentCamera.updateViewMatrix(device); device.Transform.Projection = Matrix.PerspectiveFovLH(Geometry.DegreeToRadian(45.0f), aspectRatio, near_plane, far_plane); // Cargo las var. del shader: effect.SetValue("g_txCubeMap", g_pCubeMap); effect.SetValue("fvLightPosition", new Vector4(0, 400, 0, 0)); effect.SetValue("fvEyePosition", TgcParserUtils.vector3ToFloat3Array(timer_preview > 0 ? LookFrom : GuiController.Instance.RotCamera.getPosition())); effect.SetValue("time", time); // ----------------------------------------------------- // dibujo la escena pp dicha: device.BeginScene(); if (tipo_vista != 1) { // con shaders : if (tipo_vista == 2) // dibujo en una vista: device.Viewport = View1; else // dibujo en la pantalla completa device.Viewport = ViewF; device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.Black, 1.0f, 0); // 1ero sin el agua renderScene(elapsedTime, false); // Ahora dibujo el agua device.RenderState.AlphaBlendEnable = true; effect.SetValue("aux_Tex", terrain.terrainTexture); // posicion de la canoa (divido por la escala) effect.SetValue("canoa_x", x0 / 10.0f); effect.SetValue("canoa_y", z0 / 10.0f); piso.Technique = "RenderAgua"; piso.render(); } if (tipo_vista != 0) { // sin shaders if (tipo_vista == 2) // dibujo en una vista: device.Viewport = View2; else // dibujo en la pantalla completa device.Viewport = ViewF; device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.Black, 1.0f, 0); //Renderizar terreno terrain.render(); //Renderizar SkyBox skyBox.render(); // dibujo el bosque foreach (TgcMesh instance in bosque) instance.render(); // canoa ((TgcMesh)canoa).render(); // tanque ((TgcMesh)mesh).render(); // agua Blend ant_src = device.RenderState.SourceBlend; Blend ant_dest = device.RenderState.DestinationBlend; bool ant_alpha = device.RenderState.AlphaBlendEnable; device.RenderState.AlphaBlendEnable = true; device.RenderState.SourceBlend = Blend.SourceColor; device.RenderState.DestinationBlend = Blend.InvSourceColor; ((TgcMesh)piso).render(); device.RenderState.SourceBlend = ant_src; device.RenderState.DestinationBlend = ant_dest; device.RenderState.AlphaBlendEnable = ant_alpha; } g_pCubeMap.Dispose(); }
public void Run(OpsContext context, OpsStatement statement) { ShadeArgs args = statement.Arguments as ShadeArgs; ConstantTable constantTable = SetupDevice(context, args); EffectHandle hTarget = constantTable.GetConstant(null, "Target"); ArrayList containers = statement.GetContent(context); OpsConsole.WriteLine("Shading textures with \"{0}\"", args.File); foreach (OpsTexture container in containers) { if (hTarget != null) { context.Device.SetTexture( constantTable.GetSamplerIndex(hTarget), container.Texture); } if (container.Texture is Texture) { Texture oldTexture = container.Texture as Texture; Texture newTexture = OpsTextureHelper.CloneTexture(oldTexture, Usage.None, Pool.Managed); for (int mip = 0; mip < oldTexture.LevelCount; mip++) { SurfaceDescription sd = oldTexture.GetLevelDescription(mip); CheckFormatValid(sd.Format); Surface rt = context.Device.CreateRenderTarget(sd.Width, sd.Height, sd.Format, MultiSampleType.None, 0, true); context.Device.SetRenderTarget(0, rt); ShadeVertex[] vb = new ShadeVertex[] { ShadeVertex.ForTexture(-1.0f, -1.0f, sd.Width, sd.Height), ShadeVertex.ForTexture(1.0f, -1.0f, sd.Width, sd.Height), ShadeVertex.ForTexture(-1.0f, 1.0f, sd.Width, sd.Height), ShadeVertex.ForTexture(1.0f, 1.0f, sd.Width, sd.Height), }; context.Device.BeginScene(); context.Device.DrawUserPrimitives(PrimitiveType.TriangleStrip, 2, vb); context.Device.EndScene(); context.Device.SetRenderTarget(0, context.Device.GetBackBuffer(0, 0, BackBufferType.Mono)); SurfaceLoader.FromSurface(newTexture.GetSurfaceLevel(mip), rt, Filter.None | (container.SRGB?Filter.SrgbOut:0), 0); } oldTexture.Dispose(); container.Texture = newTexture; } else if (container.Texture is VolumeTexture) { VolumeTexture oldTexture = container.Texture as VolumeTexture; VolumeTexture newTexture = OpsTextureHelper.CloneVolume(oldTexture, Usage.None, Pool.Managed); for (int mip = 0; mip < oldTexture.LevelCount; mip++) { VolumeDescription vd = oldTexture.GetLevelDescription(mip); CheckFormatValid(vd.Format); Surface sliceRT = context.Device.CreateRenderTarget(vd.Width, vd.Height, vd.Format, MultiSampleType.None, 0, true); for (int slice = 0; slice < vd.Depth; slice++) { context.Device.SetRenderTarget(0, sliceRT); ShadeVertex[] vb = new ShadeVertex[] { ShadeVertex.ForVolume(-1.0f, -1.0f, slice, vd.Width, vd.Height, vd.Depth), ShadeVertex.ForVolume(1.0f, -1.0f, slice, vd.Width, vd.Height, vd.Depth), ShadeVertex.ForVolume(-1.0f, 1.0f, slice, vd.Width, vd.Height, vd.Depth), ShadeVertex.ForVolume(1.0f, 1.0f, slice, vd.Width, vd.Height, vd.Depth), }; context.Device.BeginScene(); context.Device.DrawUserPrimitives(PrimitiveType.TriangleStrip, 2, vb); context.Device.EndScene(); context.Device.SetRenderTarget(0, context.Device.GetBackBuffer(0, 0, BackBufferType.Mono)); OpsTextureHelper.LoadVolumeSliceFromSurface(newTexture, mip, slice, Filter.None | (container.SRGB?Filter.SrgbOut:0), sliceRT); } sliceRT.Dispose(); } oldTexture.Dispose(); container.Texture = newTexture; } else if (container.Texture is CubeTexture) { CubeTexture oldTexture = container.Texture as CubeTexture; CubeTexture newTexture = OpsTextureHelper.CloneCube(oldTexture, Usage.None, Pool.Managed); for (int mip = 0; mip < oldTexture.LevelCount; mip++) { SurfaceDescription sd = oldTexture.GetLevelDescription(mip); CheckFormatValid(sd.Format); Surface rt = context.Device.CreateRenderTarget(sd.Width, sd.Height, sd.Format, MultiSampleType.None, 0, true); RenderCubeMapFace(context, newTexture, rt, CubeMapFace.PositiveX, mip, sd.Width, container.SRGB); RenderCubeMapFace(context, newTexture, rt, CubeMapFace.PositiveY, mip, sd.Width, container.SRGB); RenderCubeMapFace(context, newTexture, rt, CubeMapFace.PositiveZ, mip, sd.Width, container.SRGB); RenderCubeMapFace(context, newTexture, rt, CubeMapFace.NegativeX, mip, sd.Width, container.SRGB); RenderCubeMapFace(context, newTexture, rt, CubeMapFace.NegativeY, mip, sd.Width, container.SRGB); RenderCubeMapFace(context, newTexture, rt, CubeMapFace.NegativeZ, mip, sd.Width, container.SRGB); } oldTexture.Dispose(); container.Texture = newTexture; } } }
public override void Render() { PreRender(); var aspectRatio = D3DDevice.Instance.AspectRatio; if (TgcD3dInput.Instance.keyPressed(Key.Space)) { vel_tanque++; if (vel_tanque > 10) { vel_tanque = 0; } } if (TgcD3dInput.Instance.keyPressed(Key.X)) { volar = !volar; } if (TgcD3dInput.Instance.keyPressed(Key.S)) { // swap mesh var mesh_aux = mesh; mesh = meshX; meshX = mesh; } //Cargar variables de shader effect.SetValue("fvLightPosition", new Vector4(0, 400, 0, 0)); effect.SetValue("fvEyePosition", TgcParserUtils.vector3ToFloat3Array(Camara.Position)); effect.SetValue("kx", (float)Modifiers["Reflexion"]); effect.SetValue("kc", (float)Modifiers["Refraccion"]); effect.SetValue("usar_fresnel", (bool)Modifiers["Fresnel"]); time += ElapsedTime; // animar tanque var alfa = -time *Geometry.DegreeToRadian(vel_tanque); var x0 = 2000f * (float)Math.Cos(alfa); var z0 = 2000f * (float)Math.Sin(alfa); float offset_rueda = 13; var H = CalcularAltura(x0, z0) + alto_tanque / 2 - offset_rueda; if (volar) { H += 300; } mesh.Position = new Vector3(x0, H, z0); // direccion tangente sobre el piso: var dir_tanque = new Vector2(-(float)Math.Sin(alfa), (float)Math.Cos(alfa)); dir_tanque.Normalize(); // Posicion de la parte de adelante del tanque var pos2d = new Vector2(x0, z0); pos2d = pos2d + dir_tanque * (largo_tanque / 2); var H_frente = CalcularAltura(pos2d.X, pos2d.Y) + alto_tanque / 2 - offset_rueda; if (volar) { H_frente += 300; } var pos_frente = new Vector3(pos2d.X, H_frente, pos2d.Y); var Vel = pos_frente - mesh.Position; Vel.Normalize(); mesh.Transform = CalcularMatriz(mesh.Position, mesh.Scale, Vel); var beta = -time *Geometry.DegreeToRadian(120.0f); avion.Position = new Vector3(x0 + 300f * (float)Math.Cos(beta), 400 + H, z0 + 300f * (float)Math.Sin(alfa)); dir_avion = new Vector3(-(float)Math.Sin(beta), 0, (float)Math.Cos(beta)); avion.Transform = CalcularMatriz(avion.Position, avion.Scale, dir_avion); CamaraRot.CameraCenter = mesh.BoundingBox.calculateBoxCenter(); // -------------------------------------------------------------------- D3DDevice.Instance.Device.EndScene(); var g_pCubeMap = new CubeTexture(D3DDevice.Instance.Device, 256, 1, Usage.RenderTarget, Format.A16B16G16R16F, Pool.Default); var pOldRT = D3DDevice.Instance.Device.GetRenderTarget(0); // ojo: es fundamental que el fov sea de 90 grados. // asi que re-genero la matriz de proyeccion D3DDevice.Instance.Device.Transform.Projection = Matrix.PerspectiveFovLH(Geometry.DegreeToRadian(90.0f), 1f, 1f, 10000f); // Genero las caras del enviroment map for (var nFace = CubeMapFace.PositiveX; nFace <= CubeMapFace.NegativeZ; ++nFace) { var pFace = g_pCubeMap.GetCubeMapSurface(nFace, 0); D3DDevice.Instance.Device.SetRenderTarget(0, pFace); Vector3 Dir, VUP; Color color; switch (nFace) { default: case CubeMapFace.PositiveX: // Left Dir = new Vector3(1, 0, 0); VUP = new Vector3(0, 1, 0); color = Color.Black; break; case CubeMapFace.NegativeX: // Right Dir = new Vector3(-1, 0, 0); VUP = new Vector3(0, 1, 0); color = Color.Red; break; case CubeMapFace.PositiveY: // Up Dir = new Vector3(0, 1, 0); VUP = new Vector3(0, 0, -1); color = Color.Gray; break; case CubeMapFace.NegativeY: // Down Dir = new Vector3(0, -1, 0); VUP = new Vector3(0, 0, 1); color = Color.Yellow; break; case CubeMapFace.PositiveZ: // Front Dir = new Vector3(0, 0, 1); VUP = new Vector3(0, 1, 0); color = Color.Green; break; case CubeMapFace.NegativeZ: // Back Dir = new Vector3(0, 0, -1); VUP = new Vector3(0, 1, 0); color = Color.Blue; break; } //Obtener ViewMatrix haciendo un LookAt desde la posicion final anterior al centro de la camara var Pos = mesh.Position; D3DDevice.Instance.Device.Transform.View = Matrix.LookAtLH(Pos, Pos + Dir, VUP); D3DDevice.Instance.Device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, color, 1.0f, 0); D3DDevice.Instance.Device.BeginScene(); //Renderizar renderScene(ElapsedTime, true); D3DDevice.Instance.Device.EndScene(); //string fname = string.Format("face{0:D}.bmp", nFace); //SurfaceLoader.Save(fname, ImageFileFormat.Bmp, pFace); } // restuaro el render target D3DDevice.Instance.Device.SetRenderTarget(0, pOldRT); //TextureLoader.Save("test.bmp", ImageFileFormat.Bmp, g_pCubeMap); // Restauro el estado de las transformaciones D3DDevice.Instance.Device.Transform.View = Camara.getViewMatrix(); D3DDevice.Instance.Device.Transform.Projection = Matrix.PerspectiveFovLH(Geometry.DegreeToRadian(45.0f), aspectRatio, 1f, 10000f); // dibujo pp dicho D3DDevice.Instance.Device.BeginScene(); D3DDevice.Instance.Device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.Black, 1.0f, 0); effect.SetValue("g_txCubeMap", g_pCubeMap); renderScene(ElapsedTime, false); g_pCubeMap.Dispose(); D3DDevice.Instance.Device.EndScene(); D3DDevice.Instance.Device.Present(); }
public override void render(float elapsedTime) { Device device = GuiController.Instance.D3dDevice; Control panel3d = GuiController.Instance.Panel3d; float aspectRatio = (float)panel3d.Width / (float)panel3d.Height; if (GuiController.Instance.D3dInput.keyPressed(Microsoft.DirectX.DirectInput.Key.Space)) { vel_tanque++; if (vel_tanque > 10) vel_tanque = 0; } if (GuiController.Instance.D3dInput.keyPressed(Microsoft.DirectX.DirectInput.Key.X)) volar = !volar; if (GuiController.Instance.D3dInput.keyPressed(Microsoft.DirectX.DirectInput.Key.S)) { // swap mesh TgcMesh mesh_aux = mesh; mesh = meshX; meshX = mesh; } //Cargar variables de shader effect.SetValue("fvLightPosition", new Vector4(0,400,0,0)); effect.SetValue("fvEyePosition", TgcParserUtils.vector3ToFloat3Array(GuiController.Instance.RotCamera.getPosition())); effect.SetValue("kx", (float)GuiController.Instance.Modifiers["Reflexion"]); effect.SetValue("kc", (float)GuiController.Instance.Modifiers["Refraccion"]); effect.SetValue("usar_fresnel", (bool)GuiController.Instance.Modifiers["Fresnel"]); time += elapsedTime; // animar tanque float alfa = -time * Geometry.DegreeToRadian(vel_tanque); float x0 = 2000f * (float)Math.Cos(alfa); float z0 = 2000f * (float)Math.Sin(alfa); float offset_rueda = 13; float H = CalcularAltura(x0, z0) + alto_tanque / 2 - offset_rueda; if (volar) H += 300; mesh.Position = new Vector3(x0, H, z0); // direccion tangente sobre el piso: Vector2 dir_tanque = new Vector2(-(float)Math.Sin(alfa), (float)Math.Cos(alfa)); dir_tanque.Normalize(); // Posicion de la parte de adelante del tanque Vector2 pos2d = new Vector2(x0, z0); pos2d = pos2d + dir_tanque * (largo_tanque / 2); float H_frente = CalcularAltura(pos2d.X, pos2d.Y) + alto_tanque / 2 - offset_rueda; if (volar) H_frente += 300; Vector3 pos_frente = new Vector3(pos2d.X, H_frente, pos2d.Y); Vector3 Vel = pos_frente - mesh.Position; Vel.Normalize(); mesh.Transform = CalcularMatriz(mesh.Position, mesh.Scale, Vel); float beta = -time * Geometry.DegreeToRadian(120.0f); avion.Position = new Vector3(x0 + 300f * (float)Math.Cos(beta), 400 + H, z0 + 300f * (float)Math.Sin(alfa)); dir_avion = new Vector3(-(float)Math.Sin(beta), 0, (float)Math.Cos(beta)); avion.Transform = CalcularMatriz(avion.Position, avion.Scale, dir_avion); GuiController.Instance.RotCamera.targetObject(mesh.BoundingBox); GuiController.Instance.CurrentCamera.updateCamera(); // -------------------------------------------------------------------- device.EndScene(); CubeTexture g_pCubeMap = new CubeTexture(device, 256, 1, Usage.RenderTarget, Format.A16B16G16R16F, Pool.Default); Surface pOldRT = device.GetRenderTarget(0); // ojo: es fundamental que el fov sea de 90 grados. // asi que re-genero la matriz de proyeccion device.Transform.Projection = Matrix.PerspectiveFovLH(Geometry.DegreeToRadian(90.0f), 1f, 1f, 10000f); // Genero las caras del enviroment map for (CubeMapFace nFace = CubeMapFace.PositiveX; nFace <= CubeMapFace.NegativeZ; ++nFace) { Surface pFace = g_pCubeMap.GetCubeMapSurface(nFace, 0); device.SetRenderTarget(0, pFace); Vector3 Dir, VUP; Color color; switch (nFace) { default: case CubeMapFace.PositiveX: // Left Dir = new Vector3(1, 0, 0); VUP = new Vector3(0, 1, 0); color = Color.Black; break; case CubeMapFace.NegativeX: // Right Dir = new Vector3(-1, 0, 0); VUP = new Vector3(0, 1, 0); color = Color.Red; break; case CubeMapFace.PositiveY: // Up Dir = new Vector3(0, 1, 0); VUP = new Vector3(0, 0, -1); color = Color.Gray; break; case CubeMapFace.NegativeY: // Down Dir = new Vector3(0, -1, 0); VUP = new Vector3(0, 0, 1); color = Color.Yellow; break; case CubeMapFace.PositiveZ: // Front Dir = new Vector3(0, 0, 1); VUP = new Vector3(0, 1, 0); color = Color.Green; break; case CubeMapFace.NegativeZ: // Back Dir = new Vector3(0, 0, -1); VUP = new Vector3(0, 1, 0); color = Color.Blue; break; } //Obtener ViewMatrix haciendo un LookAt desde la posicion final anterior al centro de la camara Vector3 Pos = mesh.Position; device.Transform.View = Matrix.LookAtLH(Pos, Pos + Dir, VUP); device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, color, 1.0f, 0); device.BeginScene(); //Renderizar renderScene(elapsedTime, true); device.EndScene(); //string fname = string.Format("face{0:D}.bmp", nFace); //SurfaceLoader.Save(fname, ImageFileFormat.Bmp, pFace); } // restuaro el render target device.SetRenderTarget(0, pOldRT); //TextureLoader.Save("test.bmp", ImageFileFormat.Bmp, g_pCubeMap); // Restauro el estado de las transformaciones GuiController.Instance.CurrentCamera.updateViewMatrix(device); device.Transform.Projection = Matrix.PerspectiveFovLH(Geometry.DegreeToRadian(45.0f), aspectRatio, 1f, 10000f); // dibujo pp dicho device.BeginScene(); device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.Black, 1.0f, 0); effect.SetValue("g_txCubeMap", g_pCubeMap); renderScene(elapsedTime, false); g_pCubeMap.Dispose(); }
public override void Render() { ClearTextures(); D3DDevice.Instance.Device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.Black, 1.0f, 0); D3DDevice.Instance.Device.BeginScene(); var aspectRatio = D3DDevice.Instance.AspectRatio; time += ElapsedTime; if (TgcD3dInput.Instance.keyPressed(Key.C)) { timer_preview = 0; camara_rot = !camara_rot; } if (TgcD3dInput.Instance.keyPressed(Key.F)) { if (tipo_vista == 1) { tipo_vista = 0; } else { tipo_vista = 1; } ant_vista = tipo_vista; } if (TgcD3dInput.Instance.keyPressed(Key.D)) { if (tipo_vista == 2) { tipo_vista = ant_vista; } else { tipo_vista = 2; } } if (TgcD3dInput.Instance.keyPressed(Key.Space)) { if (vel_tanque <= 1) { vel_tanque = 10; } else { vel_tanque = 1; } } if (timer_preview > 0) { timer_preview -= ElapsedTime; if (timer_preview < 0) { timer_preview = 0; } } // animar tanque an_tanque -= ElapsedTime * Geometry.DegreeToRadian(vel_tanque); var alfa = an_tanque; var x0 = 2000f * (float)Math.Cos(alfa); var z0 = 2000f * (float)Math.Sin(alfa); float offset_rueda = 10; var H = terrain.CalcularAltura(x0, z0) + alto_tanque / 2 - offset_rueda; if (H < nivel_mar) { H = nivel_mar; } mesh.Position = new Vector3(x0, H, z0); // direccion tangente sobre el piso: var dir_tanque = new Vector2(-(float)Math.Sin(alfa), (float)Math.Cos(alfa)); dir_tanque.Normalize(); // Posicion de la parte de adelante del tanque var pos2d = new Vector2(x0, z0); pos2d = pos2d + dir_tanque * (largo_tanque / 2); var H_frente = terrain.CalcularAltura(pos2d.X, pos2d.Y) + alto_tanque / 2 - offset_rueda; if (H_frente < nivel_mar - 15) { H_frente = nivel_mar - 15; } var pos_frente = new Vector3(pos2d.X, H_frente, pos2d.Y); var Vel = pos_frente - mesh.Position; Vel.Normalize(); mesh.Transform = CalcularMatriz(mesh.Position, mesh.Scale, Vel); // animo la canoa en circulos: alfa = -time *Geometry.DegreeToRadian(10.0f); x0 = 400f * (float)Math.Cos(alfa); z0 = 400f * (float)Math.Sin(alfa); canoa.Position = new Vector3(x0, 150, z0); dir_canoa = new Vector3(-(float)Math.Sin(alfa), 0, (float)Math.Cos(alfa)); canoa.Transform = CalcularMatriz(canoa.Position, canoa.Scale, dir_canoa); alfa_sol += ElapsedTime * Geometry.DegreeToRadian(1.0f); if (alfa_sol > 2.5) { alfa_sol = 1.5f; } // animo la posicion del sol //g_LightPos = new Vector3(1500f * (float)Math.Cos(alfa_sol), 1500f * (float)Math.Sin(alfa_sol), 0f); g_LightPos = new Vector3(2000f * (float)Math.Cos(alfa_sol), 2000f * (float)Math.Sin(alfa_sol), 0f); g_LightDir = -g_LightPos; g_LightDir.Normalize(); if (timer_preview > 0) { var an = -time *Geometry.DegreeToRadian(10.0f); LookFrom.X = 1500f * (float)Math.Sin(an); LookFrom.Z = 1500f * (float)Math.Cos(an); } else { if (camara_rot) { CamaraRot.CameraCenter = mesh.BoundingBox.calculateBoxCenter(); CamaraRot.updateCamera(ElapsedTime); //FIXME, puede que no haga falta esto. Camara = CamaraRot; } else { Camara = DefaultCamera; } } // -------------------------------------------------------------------- D3DDevice.Instance.Device.EndScene(); if (g_pCubeMapAgua == null) { // solo la primera vez crea el env map del agua CrearEnvMapAgua(); // ya que esta creado, se lo asigno al effecto: effect.SetValue("g_txCubeMapAgua", g_pCubeMapAgua); } // Creo el env map del tanque: var g_pCubeMap = new CubeTexture(D3DDevice.Instance.Device, 256, 1, Usage.RenderTarget, Format.A16B16G16R16F, Pool.Default); var pOldRT = D3DDevice.Instance.Device.GetRenderTarget(0); // ojo: es fundamental que el fov sea de 90 grados. // asi que re-genero la matriz de proyeccion D3DDevice.Instance.Device.Transform.Projection = Matrix.PerspectiveFovLH(Geometry.DegreeToRadian(90.0f), 1f, near_plane, far_plane); // Genero las caras del enviroment map for (var nFace = CubeMapFace.PositiveX; nFace <= CubeMapFace.NegativeZ; ++nFace) { var pFace = g_pCubeMap.GetCubeMapSurface(nFace, 0); D3DDevice.Instance.Device.SetRenderTarget(0, pFace); Vector3 Dir, VUP; Color color; switch (nFace) { default: case CubeMapFace.PositiveX: // Left Dir = new Vector3(1, 0, 0); VUP = new Vector3(0, 1, 0); color = Color.Black; break; case CubeMapFace.NegativeX: // Right Dir = new Vector3(-1, 0, 0); VUP = new Vector3(0, 1, 0); color = Color.Red; break; case CubeMapFace.PositiveY: // Up Dir = new Vector3(0, 1, 0); VUP = new Vector3(0, 0, -1); color = Color.Gray; break; case CubeMapFace.NegativeY: // Down Dir = new Vector3(0, -1, 0); VUP = new Vector3(0, 0, 1); color = Color.Yellow; break; case CubeMapFace.PositiveZ: // Front Dir = new Vector3(0, 0, 1); VUP = new Vector3(0, 1, 0); color = Color.Green; break; case CubeMapFace.NegativeZ: // Back Dir = new Vector3(0, 0, -1); VUP = new Vector3(0, 1, 0); color = Color.Blue; break; } //Obtener ViewMatrix haciendo un LookAt desde la posicion final anterior al centro de la camara var Pos = mesh.Position; D3DDevice.Instance.Device.Transform.View = Matrix.LookAtLH(Pos, Pos + Dir, VUP); D3DDevice.Instance.Device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, color, 1.0f, 0); D3DDevice.Instance.Device.BeginScene(); //Renderizar renderScene(ElapsedTime, true); D3DDevice.Instance.Device.EndScene(); //string fname = string.Format("face{0:D}.bmp", nFace); //SurfaceLoader.Save(fname, ImageFileFormat.Bmp, pFace); } // restuaro el render target D3DDevice.Instance.Device.SetRenderTarget(0, pOldRT); //TextureLoader.Save("test.bmp", ImageFileFormat.Bmp, g_pCubeMap); //Genero el shadow map RenderShadowMap(); // Restauro el estado de las transformaciones if (timer_preview > 0) { D3DDevice.Instance.Device.Transform.View = Matrix.LookAtLH(LookFrom, LookAt, new Vector3(0, 1, 0)); } else { D3DDevice.Instance.Device.Transform.View = Camara.getViewMatrix(); } // TODO FIX IT! esto no se bien para que lo hace aca. D3DDevice.Instance.Device.Transform.Projection = Matrix.PerspectiveFovLH(Geometry.DegreeToRadian(45.0f), aspectRatio, near_plane, far_plane); // Cargo las var. del shader: effect.SetValue("g_txCubeMap", g_pCubeMap); effect.SetValue("fvLightPosition", new Vector4(0, 400, 0, 0)); effect.SetValue("fvEyePosition", TgcParserUtils.vector3ToFloat3Array(timer_preview > 0 ? LookFrom : Camara.Position)); effect.SetValue("time", time); // ----------------------------------------------------- // dibujo la escena pp dicha: D3DDevice.Instance.Device.BeginScene(); if (tipo_vista != 1) { // con shaders : if (tipo_vista == 2) { // dibujo en una vista: D3DDevice.Instance.Device.Viewport = View1; } else { // dibujo en la pantalla completa D3DDevice.Instance.Device.Viewport = ViewF; } D3DDevice.Instance.Device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.Black, 1.0f, 0); // 1ero sin el agua renderScene(ElapsedTime, false); // Ahora dibujo el agua D3DDevice.Instance.Device.RenderState.AlphaBlendEnable = true; effect.SetValue("aux_Tex", terrain.terrainTexture); // posicion de la canoa (divido por la escala) effect.SetValue("canoa_x", x0 / 10.0f); effect.SetValue("canoa_y", z0 / 10.0f); piso.Technique = "RenderAgua"; piso.render(); } if (tipo_vista != 0) { // sin shaders if (tipo_vista == 2) { // dibujo en una vista: D3DDevice.Instance.Device.Viewport = View2; } else { // dibujo en la pantalla completa D3DDevice.Instance.Device.Viewport = ViewF; } D3DDevice.Instance.Device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.Black, 1.0f, 0); //Renderizar terreno terrain.render(); //Renderizar SkyBox skyBox.render(); // dibujo el bosque foreach (var instance in bosque) { instance.render(); } // canoa canoa.render(); // tanque mesh.render(); // agua var ant_src = D3DDevice.Instance.Device.RenderState.SourceBlend; var ant_dest = D3DDevice.Instance.Device.RenderState.DestinationBlend; var ant_alpha = D3DDevice.Instance.Device.RenderState.AlphaBlendEnable; D3DDevice.Instance.Device.RenderState.AlphaBlendEnable = true; D3DDevice.Instance.Device.RenderState.SourceBlend = Blend.SourceColor; D3DDevice.Instance.Device.RenderState.DestinationBlend = Blend.InvSourceColor; piso.render(); D3DDevice.Instance.Device.RenderState.SourceBlend = ant_src; D3DDevice.Instance.Device.RenderState.DestinationBlend = ant_dest; D3DDevice.Instance.Device.RenderState.AlphaBlendEnable = ant_alpha; } g_pCubeMap.Dispose(); RenderFPS(); RenderAxis(); D3DDevice.Instance.Device.EndScene(); D3DDevice.Instance.Device.Present(); }
private void AplicarShaderEnvironment() { this.autoPrincipal.getMesh().Effect = efectoShaderEnvironmentMap; var aspectRatio = D3DDevice.Instance.AspectRatio; efectoShaderEnvironmentMap.SetValue("fvLightPosition", new Vector4(0, 100, 0, 0)); efectoShaderEnvironmentMap.SetValue("fvEyePosition", TgcParserUtils.vector3ToFloat3Array(this.autoPrincipal.getCamara().Position)); efectoShaderEnvironmentMap.SetValue("kx", (float)0.1); efectoShaderEnvironmentMap.SetValue("kc", (float)0.25); efectoShaderEnvironmentMap.SetValue("usar_fresnel", false); D3DDevice.Instance.Device.EndScene(); var g_pCubeMap = new CubeTexture(D3DDevice.Instance.Device, 256, 1, Usage.RenderTarget, Format.A16B16G16R16F, Pool.Default); var pOldRT = D3DDevice.Instance.Device.GetRenderTarget(0); // ojo: es fundamental que el fov sea de 90 grados. // asi que re-genero la matriz de proyeccion D3DDevice.Instance.Device.Transform.Projection = Matrix.PerspectiveFovLH(Geometry.DegreeToRadian(90.0f), 1f, 1f, 10000f); // Genero las caras del enviroment map for (var nFace = CubeMapFace.PositiveX; nFace <= CubeMapFace.NegativeZ; ++nFace) { var pFace = g_pCubeMap.GetCubeMapSurface(nFace, 0); D3DDevice.Instance.Device.SetRenderTarget(0, pFace); Vector3 Dir, VUP; Color color; switch (nFace) { default: case CubeMapFace.PositiveX: // Left Dir = new Vector3(1, 0, 0); VUP = new Vector3(0, 1, 0); color = Color.Black; break; case CubeMapFace.NegativeX: // Right Dir = new Vector3(-1, 0, 0); VUP = new Vector3(0, 1, 0); color = Color.Red; break; case CubeMapFace.PositiveY: // Up Dir = new Vector3(0, 1, 0); VUP = new Vector3(0, 0, -1); color = Color.Gray; break; case CubeMapFace.NegativeY: // Down Dir = new Vector3(0, -1, 0); VUP = new Vector3(0, 0, 1); color = Color.Yellow; break; case CubeMapFace.PositiveZ: // Front Dir = new Vector3(0, 0, 1); VUP = new Vector3(0, 1, 0); color = Color.Green; break; case CubeMapFace.NegativeZ: // Back Dir = new Vector3(0, 0, -1); VUP = new Vector3(0, 1, 0); color = Color.Blue; break; } D3DDevice.Instance.Device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, color, 1.0f, 0); D3DDevice.Instance.Device.BeginScene(); //Renderizar // renderScene(ElapsedTime, true); renderScene(1, true); D3DDevice.Instance.Device.EndScene(); //string fname = string.Format("face{0:D}.bmp", nFace); //SurfaceLoader.Save(fname, ImageFileFormat.Bmp, pFace); } // restuaro el render target D3DDevice.Instance.Device.SetRenderTarget(0, pOldRT); //TextureLoader.Save("test.bmp", ImageFileFormat.Bmp, g_pCubeMap); // Restauro el estado de las transformaciones D3DDevice.Instance.Device.Transform.View = this.autoPrincipal.getCamara().GetViewMatrix(); D3DDevice.Instance.Device.Transform.Projection = Matrix.PerspectiveFovLH(Geometry.DegreeToRadian(45.0f), aspectRatio, 1f, 10000f); // dibujo pp dicho D3DDevice.Instance.Device.BeginScene(); D3DDevice.Instance.Device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.Black, 1.0f, 0); efectoShaderEnvironmentMap.SetValue("g_txCubeMap", g_pCubeMap); // renderScene(ElapsedTime, false); renderScene(1, false); g_pCubeMap.Dispose(); D3DDevice.Instance.Device.EndScene(); D3DDevice.Instance.Device.Present(); }
public void Render(AutoManejable AutoFisico1, AutoManejable AutoFisico2, PoliciasIA GrupoPolicias, Core.Camara.TgcCamera Camara, bool juegoDoble) { // Shader Enviroment Map -------------------------------- //D3DDevice.Instance.Device.EndScene(); var g_pCubeMap = new CubeTexture(D3DDevice.Instance.Device, 256, 1, Usage.RenderTarget, Format.A16B16G16R16F, Pool.Default); var pOldRT2 = D3DDevice.Instance.Device.GetRenderTarget(0); D3DDevice.Instance.Device.Transform.Projection = TGCMatrix.PerspectiveFovLH(Geometry.DegreeToRadian(90.0f), 1f, 1f, 10000f).ToMatrix(); // Genero las caras del enviroment map for (var nFace = CubeMapFace.PositiveX; nFace <= CubeMapFace.NegativeZ; ++nFace) { var pFace = g_pCubeMap.GetCubeMapSurface(nFace, 0); D3DDevice.Instance.Device.SetRenderTarget(0, pFace); TGCVector3 Dir, VUP; Color color; switch (nFace) { default: case CubeMapFace.PositiveX: // Left Dir = new TGCVector3(1, 0, 0); VUP = TGCVector3.Up; color = Color.Black; break; case CubeMapFace.NegativeX: // Right Dir = new TGCVector3(-1, 0, 0); VUP = TGCVector3.Up; color = Color.Red; break; case CubeMapFace.PositiveY: // Up Dir = TGCVector3.Up; VUP = new TGCVector3(0, 0, -1); color = Color.Gray; break; case CubeMapFace.NegativeY: // Down Dir = TGCVector3.Down; VUP = new TGCVector3(0, 0, 1); color = Color.Yellow; break; case CubeMapFace.PositiveZ: // Front Dir = new TGCVector3(0, 0, 1); VUP = TGCVector3.Up; color = Color.Green; break; case CubeMapFace.NegativeZ: // Back Dir = new TGCVector3(0, 0, -1); VUP = TGCVector3.Up; color = Color.Blue; break; } D3DDevice.Instance.Device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, color, 1.0f, 0); //Renderizar foreach (var mesh in AutoFisico1.Mayas) { mesh.Effect = EnvMap; mesh.Technique = "RenderScene"; mesh.Render(); } if (juegoDoble) { foreach (var mesh in AutoFisico2.Mayas) { mesh.Effect = EnvMap; mesh.Technique = "RenderScene"; mesh.Render(); } } } D3DDevice.Instance.Device.SetRenderTarget(0, pOldRT2); D3DDevice.Instance.Device.Transform.View = Camara.GetViewMatrix().ToMatrix(); D3DDevice.Instance.Device.Transform.Projection = TGCMatrix.PerspectiveFovLH(Geometry.DegreeToRadian(45.0f), D3DDevice.Instance.AspectRatio, 1f, 10000f).ToMatrix(); //D3DDevice.Instance.Device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.Black, 1.0f, 0); EnvMap.SetValue("g_txCubeMap", g_pCubeMap); foreach (var mesh in AutoFisico1.Mayas) { mesh.Effect = EnvMap; mesh.Technique = "RenderScene"; mesh.Render(); } foreach (var rueda in AutoFisico1.Ruedas) { rueda.Effect = EnvMap; rueda.Technique = "RenderScene"; rueda.Render(); } foreach (var mesh in GrupoPolicias.Todos[0].Mayas) { mesh.Effect = EnvMap; mesh.Technique = "RenderScene"; mesh.Render(); } if (juegoDoble) { foreach (var mesh in AutoFisico2.Mayas) { mesh.Effect = EnvMap; mesh.Technique = "RenderScene"; mesh.Render(); } foreach (var rueda in AutoFisico2.Ruedas) { rueda.Effect = EnvMap; rueda.Technique = "RenderScene"; rueda.Render(); } } g_pCubeMap.Dispose(); //------------------------------------------------------------- }
public void render() { device = GuiController.Instance.D3dDevice; verificarConfiguracion(); Camara.update(barcoUser.getPosition()); Control panel3d = GuiController.Instance.Panel3d; aspectRatio = (float)panel3d.Width / (float)panel3d.Height; time += GuiController.Instance.ElapsedTime; if (timer_preview > 0) { timer_preview -= GuiController.Instance.ElapsedTime; if (timer_preview < 0) { timer_preview = 0; } } //animarBarcosEnemigos(); animarPosicionSol(); /*if (timer_preview > 0) * { * float an = -time * Geometry.DegreeToRadian(10.0f); * lookFrom.X = 1500f * (float)Math.Sin(an); * lookFrom.Z = 1500f * (float)Math.Cos(an); * }*/ // -------------------------------------------------------------------- device.EndScene(); if (g_pCubeMapAgua == null) { // solo la primera vez crea el env map del agua crearEnvMapAgua(); } // Parametros para el movimiento del oceano /*desplazamiento += GuiController.Instance.ElapsedTime; * piso.Effect.SetValue("desplazamiento", desplazamiento); * piso.Effect.SetValue("maxHeightSuperficial", (float)GuiController.Instance.Modifiers.getValue("maxHeightSuperficial")); * piso.Effect.SetValue("amplitud", (float)GuiController.Instance.Modifiers.getValue("amplitud")); * piso.Effect.SetValue("frecuencia", (float)GuiController.Instance.Modifiers.getValue("frecuencia")); * piso.Effect.SetValue("smallvalue", 1);*/ // Matrices de transformacion para pasar de world a clip space /*piso.Effect.SetValue("world", GuiController.Instance.D3dDevice.Transform.World); * piso.Effect.SetValue("view", GuiController.Instance.D3dDevice.Transform.View); * piso.Effect.SetValue("proj", GuiController.Instance.D3dDevice.Transform.Projection);*/ // Dimensiones del Oceano /*piso.Effect.SetValue("radio", RADIO); * piso.Effect.SetValue("largo", LARGO); * piso.Effect.SetValue("dev", DISTANCIA_ENTRE_VERTICES);*/ // Creo el env map del barco principal crearEnvMapShip(); //Genero el shadow map renderShadowMap(); // Restauro el estado de las transformaciones loadEstadoTransformaciones(); // dibujo la escena pp dicha: device.BeginScene(); dibujarVista(); g_pCubeMap.Dispose(); Lluvia.render(); //renderModelos(); if ((bool)Parametros.getModificador("boundingBox")) { barcoUser.getBarco().BoundingBox.setRenderColor(Color.Red); barcoUser.getBarco().BoundingBox.render(); foreach (Enemigo elemento in enemigos) { elemento.getBarco().BoundingBox.setRenderColor(Color.Red); elemento.getBarco().BoundingBox.render(); } } ContadorEnemigos.render(barcoUser.calcularColisiones(enemigos)); foreach (Enemigo elemento in enemigos) { if (elemento.getColision()) { Utiles.renderExplosion(animatedSprite, elemento.getBarco().Position); quadtree.removeMesh(elemento.getBarco()); quadtree.removeMesh(elemento.getIDBarco()); } } enemigos.RemoveAll(pirata => pirata.getColision() == true); }