public void setShadersValues() { Vector3 lightPosition = new Vector3(-100, 140, 3000);// (Vector3)GuiController.Instance.Modifiers["LightPosition"]; efectoOlas.SetValue("llueve", (Boolean)GuiController.Instance.Modifiers["lluvia"]); efectoOlas.SetValue("time", (float)GuiController.Instance.UserVars.getValue("time")); efectoOlas.SetValue("fvLightPosition", TgcParserUtils.vector3ToFloat3Array(lightPosition)); efectoOlas.SetValue("k_la", (float)GuiController.Instance.Modifiers["Ambient"]); efectoOlas.SetValue("fSpecularPower", 20.0f);//(float)GuiController.Instance.Modifiers["SpecularPower"]); efectoOlas.SetValue("blendAmount", (float)GuiController.Instance.Modifiers["blending"]); efectoOlas.SetValue("fvEyePosition", TgcParserUtils.vector3ToFloat3Array(GuiController.Instance.CurrentCamera.getPosition())); efectoOlas.SetValue("fogColor", ColorValue.FromColor((Color)GuiController.Instance.Modifiers["fog color"])); efectoOlas.SetValue("fogStart", (float)GuiController.Instance.Modifiers["fog start"]); efectoOlas.SetValue("blendStart", (float)GuiController.Instance.Modifiers["blend start"]); efectoOlas.SetValue("camara3p", (Boolean)GuiController.Instance.Modifiers["camaraEnBarco"]); efectoOlas.SetValue("rayo", rayo); efectoOlas.SetValue("delta", 150.0f);//(float)GuiController.Instance.Modifiers["delta"]); efectoOlas.SetValue("reflection", (float)GuiController.Instance.Modifiers["reflection"]); //CubeMap efectoOlas.SetValue("texCubeMap", cubeMap); efectoCascada.SetValue("time", (float)GuiController.Instance.UserVars.getValue("time")); efectoCascada.SetValue("fvEyePosition", TgcParserUtils.vector3ToFloat3Array(GuiController.Instance.CurrentCamera.getPosition())); efectoCascada.SetValue("fvLightPosition", TgcParserUtils.vector3ToFloat3Array(lightPosition)); efectoCascada.SetValue("camara3p", (Boolean)GuiController.Instance.Modifiers["camaraEnBarco"]); efectoCascada.SetValue("texCubeMap", cubeMap); efectoCascada.SetValue("reflection", (float)GuiController.Instance.Modifiers["reflection"]); efectoCascada.SetValue("blendAmount", (float)GuiController.Instance.Modifiers["blending"]); efectoCascada.SetValue("k_la", (float)GuiController.Instance.Modifiers["Ambient"]); }
public void update(float elapsedTime) { GuiController.Instance.FpsCamera.Enable = false; // Actualizo la direccion if (GuiController.Instance.D3dInput.keyDown(Microsoft.DirectX.DirectInput.Key.A)) { dir_an += 1f * elapsedTime; } if (GuiController.Instance.D3dInput.keyDown(Microsoft.DirectX.DirectInput.Key.D)) { dir_an -= 1f * elapsedTime; } // calculo la velocidad Vector2 vel = new Vector2((float)Math.Sin(dir_an), (float)Math.Cos(dir_an)); // actualizo la posicion pos += vel * kvel * elapsedTime; // actualizo los parametros de la camara float dH = 1.0f; // altura del personaje float H = terrain.CalcularAltura(pos.X, pos.Y); Vector2 pos_s = pos + vel * 2; Vector3 lookFrom = new Vector3(pos.X, H + dH, pos.Y); Vector3 lookAt = new Vector3(pos_s.X, H, pos_s.Y); GuiController.Instance.D3dDevice.Transform.View = Matrix.LookAtLH(lookFrom, lookAt, new Vector3(0, 1, 0)); effect.SetValue("fvEyePosition", TgcParserUtils.vector3ToFloat3Array(lookFrom)); }
public override void render(float elapsedTime) { update(elapsedTime); Device device = GuiController.Instance.D3dDevice; Control panel3d = GuiController.Instance.Panel3d; float aspectRatio = (float)panel3d.Width / (float)panel3d.Height; time += elapsedTime; Vector3 lightDir = (Vector3)GuiController.Instance.Modifiers["LightDir"]; effect.SetValue("g_LightDir", TgcParserUtils.vector3ToFloat3Array(lightDir)); effect.SetValue("min_cant_samples", (float)GuiController.Instance.Modifiers["minSample"]); effect.SetValue("max_cant_samples", (float)GuiController.Instance.Modifiers["maxSample"]); effect.SetValue("fHeightMapScale", (float)GuiController.Instance.Modifiers["HeightMapScale"]); //effect.SetValue("fvEyePosition", TgcParserUtils.vector3ToFloat3Array(GuiController.Instance.FpsCamera.getPosition())); effect.SetValue("time", time); device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.Black, 1.0f, 0); device.BeginScene(); //Renderizar terreno con POM effect.Technique = "ParallaxOcclusion"; terrain.executeRender(effect); device.EndScene(); }
public void ProcesarLuces() { if (camara == null) { return; } var lightDir = new Vector3(20, 0, 0); lightDir.Normalize(); foreach (var mesh in lstMeshes) { Vector3 posicionCamara = camara.Position; Vector3 posicionLuz = new Vector3(0, 30, 0); mesh.Effect.SetValue("lightColor", ColorValue.FromColor((Color.White))); mesh.Effect.SetValue("lightPosition", TgcParserUtils.vector3ToVector4(posicionLuz)); mesh.Effect.SetValue("eyePosition", TgcParserUtils.vector3ToFloat4Array(posicionCamara)); mesh.Effect.SetValue("spotLightDir", TgcParserUtils.vector3ToFloat3Array(lightDir)); mesh.Effect.SetValue("lightIntensity", 200f); mesh.Effect.SetValue("lightAttenuation", 0.1f); mesh.Effect.SetValue("spotLightAngleCos", 55); mesh.Effect.SetValue("spotLightExponent", 7f); //Cargar variables de shader de Material. El Material en realidad deberia ser propio de cada mesh. Pero en este ejemplo se simplifica con uno comun para todos mesh.Effect.SetValue("materialEmissiveColor", ColorValue.FromColor((Color.Gray))); mesh.Effect.SetValue("materialAmbientColor", ColorValue.FromColor((Color.White))); mesh.Effect.SetValue("materialDiffuseColor", ColorValue.FromColor((Color.White))); mesh.Effect.SetValue("materialSpecularColor", ColorValue.FromColor((Color.White))); mesh.Effect.SetValue("materialSpecularExp", 1f); mesh.render(); } }
public void setValues(TgcMesh mesh, Vector3 posicionCamara) { Effect currentShader; currentShader = TgcShaders.Instance.TgcMeshSpotLightShader; mesh.Effect = currentShader; var direccionLuz = lightDir; mesh.Technique = "DIFFUSE_MAP";//TgcShaders.Instance.getTgcMeshTechnique(mesh.RenderType); mesh.Effect = TgcShaders.Instance.TgcMeshSpotLightShader; //El Technique depende del tipo RenderType del mesh mesh.Technique = TgcShaders.Instance.getTgcMeshTechnique(mesh.RenderType); mesh.Effect.SetValue("lightColor", ColorValue.FromColor(Color.Violet)); mesh.Effect.SetValue("lightPosition", TgcParserUtils.vector3ToFloat4Array(lightPos)); mesh.Effect.SetValue("eyePosition", TgcParserUtils.vector3ToFloat4Array(posicionCamara)); mesh.Effect.SetValue("spotLightDir", TgcParserUtils.vector3ToFloat3Array(direccionLuz)); mesh.Effect.SetValue("lightIntensity", lightIntensity); mesh.Effect.SetValue("lightAttenuation", 0.3f); mesh.Effect.SetValue("spotLightAngleCos", FastMath.ToRad(45f)); mesh.Effect.SetValue("spotLightExponent", 20f); mesh.Effect.SetValue("materialEmissiveColor", ColorValue.FromColor(Color.Gray)); mesh.Effect.SetValue("materialAmbientColor", ColorValue.FromColor(Color.White)); mesh.Effect.SetValue("materialDiffuseColor", ColorValue.FromColor(Color.White)); mesh.Effect.SetValue("materialSpecularColor", ColorValue.FromColor(Color.White)); mesh.Effect.SetValue("materialSpecularExp", 10f); }
public override void render(float elapsedTime) { Device device = GuiController.Instance.D3dDevice; Vector3 lightPosition = (Vector3)GuiController.Instance.Modifiers["LightPosition"]; //Cargar variables de shader globales a todos los objetos mesh.Effect.SetValue("fvLightPosition", TgcParserUtils.vector3ToFloat3Array(lightPosition)); mesh.Effect.SetValue("fvEyePosition", TgcParserUtils.vector3ToFloat3Array(GuiController.Instance.RotCamera.getPosition())); mesh.Effect.SetValue("fvAmbient", ColorValue.FromColor((Color)GuiController.Instance.Modifiers["AmbientColor"])); mesh.Effect.SetValue("fvDiffuse", ColorValue.FromColor((Color)GuiController.Instance.Modifiers["DiffuseColor"])); mesh.Effect.SetValue("fvSpecular", ColorValue.FromColor((Color)GuiController.Instance.Modifiers["SpecularColor"])); mesh.Effect.SetValue("fSpecularPower", (float)GuiController.Instance.Modifiers["SpecularPower"]); //Mover mesh que representa la luz lightBox.Position = lightPosition; //Mover mesh Vector3 meshPos = (Vector3)GuiController.Instance.Modifiers["MeshPos"]; mesh.Position = meshPos; mesh.render(); lightBox.render(); }
private void AplicarShaderChoque() { /* INICIO CHOQUE */ if (this.colisionoAlgunaVez && this.getMesh().Position.Y == 5) { ChoqueDelantero = 1; } if (this.colisiono() && !this.colisionoAlgunaVez && this.getMesh().Position.Y == 5) { ChoqueTrasero = -1; } if (this.colisiono() && this.getMesh().Position.Y == 5) { ChoqueDelantero = 1; } float vida = base.getLifeLevel().nivelDeVida(); int energia = 10; if (vida < 100) { if (vida < 40) { if (vida < 20) { energia = 2; } else { energia = 4; } } else { if (vida < 70) { energia = 7; } } } efectoShaderChoque.SetValue("Energia", energia); efectoShaderChoque.SetValue("ChoqueAtras", ChoqueTrasero); efectoShaderChoque.SetValue("ChoqueAdelante", ChoqueDelantero); efectoShaderChoque.SetValue("fvLightPosition", new Vector4(0, 100, 0, 0)); efectoShaderChoque.SetValue("fvEyePosition", TgcParserUtils.vector3ToFloat3Array(this.getCamara().Position)); this.getMesh().Effect = efectoShaderChoque; this.getMesh().Technique = "RenderScene"; }
void updateShaderVars() { float ballr2 = ball.rad * ball.rad; lightVector = (Vector3)GuiController.Instance.Modifiers.getValue("LightDir"); lightVector.Normalize(); C_Hoja.mesh.Effect.SetValue("xBallPos", TgcParserUtils.vector3ToFloat3Array(ball.pos)); C_Hoja.mesh.Effect.SetValue("xBallRad2", ballr2); C_Hoja.mesh.Effect.SetValue("xLightDir", TgcParserUtils.vector3ToFloat3Array(lightVector)); C_Hoja.mesh.Effect.SetValue("xCameraPos", TgcParserUtils.vector3ToFloat3Array(GuiController.Instance.CurrentCamera.getPosition())); C_Hoja.mesh.Effect.SetValue("xSpecularPower", 999999999f); earth.mesh.Effect.SetValue("xBallPos", TgcParserUtils.vector3ToFloat3Array(ball.pos)); earth.mesh.Effect.SetValue("xBallRad2", ballr2); earth.mesh.Effect.SetValue("xLightDir", TgcParserUtils.vector3ToFloat3Array(lightVector)); earth.mesh.Effect.SetValue("xCameraPos", TgcParserUtils.vector3ToFloat3Array(GuiController.Instance.CurrentCamera.getPosition())); earth.mesh.Effect.SetValue("xSpecularPower", 999999999f); earth2.mesh.Effect.SetValue("xBallPos", TgcParserUtils.vector3ToFloat3Array(ball.pos)); earth2.mesh.Effect.SetValue("xBallRad2", ballr2); earth2.mesh.Effect.SetValue("xLightDir", TgcParserUtils.vector3ToFloat3Array(lightVector)); earth2.mesh.Effect.SetValue("xCameraPos", TgcParserUtils.vector3ToFloat3Array(GuiController.Instance.CurrentCamera.getPosition())); earth2.mesh.Effect.SetValue("xSpecularPower", 999999999f); water.mesh.Effect.SetValue("xBallPos", TgcParserUtils.vector3ToFloat3Array(ball.pos)); water.mesh.Effect.SetValue("xBallRad2", ballr2); water.mesh.Effect.SetValue("xLightDir", TgcParserUtils.vector3ToFloat3Array(lightVector)); water.mesh.Effect.SetValue("xCameraPos", TgcParserUtils.vector3ToFloat3Array(GuiController.Instance.CurrentCamera.getPosition())); water.mesh.Effect.SetValue("xSpecularPower", 256f); water.mesh.Effect.SetValue("xWaveLength", (float)GuiController.Instance.Modifiers.getValue("xWaveLength")); water.mesh.Effect.SetValue("xWaveHeight", (float)GuiController.Instance.Modifiers.getValue("xWaveHeight")); water.mesh.Effect.SetValue("xWindForce", (float)GuiController.Instance.Modifiers.getValue("xWindForce")); water.mesh.Effect.SetValue("xTime", ball.time); ball.mesh.Effect.SetValue("xLightDir", TgcParserUtils.vector3ToFloat3Array(lightVector)); ball.mesh.Effect.SetValue("xCameraEye", TgcParserUtils.vector3ToFloat3Array(GuiController.Instance.CurrentCamera.getPosition())); ball.mesh.Effect.SetValue("xTime", ball.time); ball.mesh.Effect.SetValue("xBallPos", TgcParserUtils.vector3ToFloat3Array(ball.pos)); ball.mesh.Effect.SetValue("xBallRad", ball.rad); float kBooble = (float)GuiController.Instance.Modifiers.getValue("xKBallBooble"); ball.mesh.Effect.SetValue("xKBallBooble", ball.dieing ? kBooble * 2 : kBooble); ball.mesh.Effect.SetValue("xCameraPos", TgcParserUtils.vector3ToFloat3Array(GuiController.Instance.CurrentCamera.getPosition())); ball.mesh.Effect.SetValue("xSpecularPower", 20f); }
private void cargarLuces(Vector3 posLuz) { Effect pointShader = TgcShaders.loadEffect(GuiController.Instance.AlumnoEjemplosMediaDir + "PhongShading.fx"); foreach (TgcMesh mesh in meshes) { mesh.Effect = pointShader; mesh.Technique = "DefaultTechnique";//GuiController.Instance.Shaders.getTgcMeshTechnique(mesh.RenderType); pointShader.SetValue("fvLightPosition", TgcParserUtils.vector3ToFloat3Array(posLuz)); pointShader.SetValue("fvEyePosition", TgcParserUtils.vector3ToFloat3Array(GuiController.Instance.ThirdPersonCamera.getPosition())); pointShader.SetValue("k_la", 0.55f); pointShader.SetValue("k_ld", 1); pointShader.SetValue("k_ls", 0.35f); pointShader.SetValue("fSpecularPower", 10); } }
//FIN OBB //guardar private void guardarObbs(List <TgcObb> obbs) { try { //Crear XML XmlDocument doc = new XmlDocument(); XmlNode root = doc.CreateElement("tgcScene"); //name XmlElement nameNode = doc.CreateElement("name"); nameNode.InnerText = "pistaObbs"; root.AppendChild(nameNode); //obbs XmlElement obbsNode = doc.CreateElement("obbs"); obbsNode.SetAttribute("count", obbs.Count.ToString()); foreach (TgcObb unObb in obbs) { XmlElement obbNode = doc.CreateElement("obb"); obbNode.SetAttribute("center", TgcParserUtils.printFloat3Array(TgcParserUtils.vector3ToFloat3Array(unObb.Center))); obbNode.SetAttribute("extents", TgcParserUtils.printFloat3Array(TgcParserUtils.vector3ToFloat3Array(unObb.Extents))); obbNode.SetAttribute("orientation0", TgcParserUtils.printFloat3Array(TgcParserUtils.vector3ToFloat3Array(unObb.Orientation[0]))); obbNode.SetAttribute("orientation1", TgcParserUtils.printFloat3Array(TgcParserUtils.vector3ToFloat3Array(unObb.Orientation[1]))); obbNode.SetAttribute("orientation2", TgcParserUtils.printFloat3Array(TgcParserUtils.vector3ToFloat3Array(unObb.Orientation[2]))); obbsNode.AppendChild(obbNode); } root.AppendChild(obbsNode); //Guardar XML, borrar si ya existe doc.AppendChild(root); string sceneFileName = "pistaObbs-TgcScene.xml"; string sceneFilePath = GuiController.Instance.AlumnoEjemplosMediaDir + "TheC#\\Auto\\" + sceneFileName; if (File.Exists(sceneFileName)) { File.Delete(sceneFileName); } doc.Save(sceneFilePath); } catch (Exception ex) { throw new Exception("Error al crear XML de escena: pistaObbs", ex);; } }
public void render() { bullet.render(); sombra.Effect.SetValue("height", EjemploAlumno.Instance.heightOlas); sombra.Effect.SetValue("time", EjemploAlumno.Instance.time); //sombra.Effect.SetValue("radioBala", bullet.Radius); //sombra.Effect.SetValue("posBalaX", posicion.X); //sombra.Effect.SetValue("posBalaZ", posicion.Z); //salpicadura.Effect.SetValue("height", EjemploAlumno.Instance.heightOlas); salpicadura.Effect.SetValue("time", tiempo);//EjemploAlumno.Instance.time); //EjemploAlumno.Instance.cargarEfectoLuz(salpicadura.Effect); if (EjemploAlumno.Instance.lloviendo) { salpicadura.Effect.SetValue("fvLightPosition", TgcParserUtils.vector3ToFloat3Array(EjemploAlumno.Instance.lightMesh.Position)); salpicadura.Effect.SetValue("fvEyePosition", TgcParserUtils.vector3ToFloat3Array(GuiController.Instance.ThirdPersonCamera.getPosition())); salpicadura.Effect.SetValue("k_la", 0.3f); salpicadura.Effect.SetValue("k_ld", 0.3f); salpicadura.Effect.SetValue("k_ls", 0f); salpicadura.Effect.SetValue("fSpecularPower", 250); } else { salpicadura.Effect.SetValue("fvLightPosition", TgcParserUtils.vector3ToFloat3Array(EjemploAlumno.Instance.lightMesh.Position)); salpicadura.Effect.SetValue("fvEyePosition", TgcParserUtils.vector3ToFloat3Array(GuiController.Instance.ThirdPersonCamera.getPosition())); salpicadura.Effect.SetValue("k_la", 4f); salpicadura.Effect.SetValue("k_ld", 5f); salpicadura.Effect.SetValue("k_ls", 2.5f); salpicadura.Effect.SetValue("fSpecularPower", 60); } if (sombraActiva) { sombra.render(); } else { salpicadura.render(); } }
public override void aplicarEfecto(TgcSkeletalMesh mesh, Vector3 posicionCamara, Vector3 direccionLuz) { mesh.Effect = TgcShaders.Instance.TgcSkeletalMeshPointLightShader; //El Technique depende del tipo RenderType del mesh mesh.Technique = "DIFFUSE_MAP"; mesh.Effect.SetValue("lightColor", ColorValue.FromColor(Color.FromArgb(0, 255, 207, 46))); mesh.Effect.SetValue("lightPosition", TgcParserUtils.vector3ToFloat4Array(posicionCamara)); mesh.Effect.SetValue("eyePosition", TgcParserUtils.vector3ToFloat4Array(posicionCamara)); mesh.Effect.SetValue("lightIntensity", Energia); mesh.Effect.SetValue("lightAttenuation", 0.005f); mesh.Effect.SetValue("spotLightAngleCos", FastMath.ToRad(30f)); mesh.Effect.SetValue("spotLightExponent", 15f); mesh.Effect.SetValue("spotLightDir", TgcParserUtils.vector3ToFloat3Array(direccionLuz)); //Cargar variables de shader de Material. El Material en realidad deberia ser propio de cada mesh. Pero en este ejemplo se simplifica con uno comun para todos mesh.Effect.SetValue("materialEmissiveColor", ColorValue.FromColor(Color.Black)); mesh.Effect.SetValue("materialAmbientColor", ColorValue.FromColor(Color.FromArgb(0, 255, 207, 46))); mesh.Effect.SetValue("materialDiffuseColor", ColorValue.FromColor(Color.White)); mesh.Effect.SetValue("materialSpecularColor", ColorValue.FromColor(Color.FromArgb(0, 255, 207, 46))); mesh.Effect.SetValue("materialSpecularExp", 10000f); }
private void loadEstadoTransformaciones() { 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); }
public void cargarEfectoLuz(Effect efecto) { Vector3 posLuz = lightMesh.Position; if (lloviendo) { efecto.SetValue("fvLightPosition", TgcParserUtils.vector3ToFloat3Array(posLuz)); efecto.SetValue("fvEyePosition", TgcParserUtils.vector3ToFloat3Array(GuiController.Instance.ThirdPersonCamera.getPosition())); efecto.SetValue("k_la", 0.3f); efecto.SetValue("k_ld", 0.3f); efecto.SetValue("k_ls", 0f); efecto.SetValue("fSpecularPower", 250); } else { efecto.SetValue("fvLightPosition", TgcParserUtils.vector3ToFloat3Array(posLuz)); efecto.SetValue("fvEyePosition", TgcParserUtils.vector3ToFloat3Array(GuiController.Instance.ThirdPersonCamera.getPosition())); efecto.SetValue("k_la", 4f); efecto.SetValue("k_ld", 5f); efecto.SetValue("k_ls", 2.5f); efecto.SetValue("fSpecularPower", 60); } }
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() { 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 override void Render() { PreRender(); //Habilitar luz var lightEnable = (bool)Modifiers["lightEnable"]; Effect currentShader; if (lightEnable) { //Con luz: Cambiar el shader actual por el shader default que trae el framework para iluminacion dinamica con SpotLight currentShader = TgcShaders.Instance.TgcMeshSpotLightShader; } else { //Sin luz: Restaurar shader default currentShader = TgcShaders.Instance.TgcMeshShader; } //Aplicar a cada mesh el shader actual foreach (var mesh in scene.Meshes) { mesh.Effect = currentShader; //El Technique depende del tipo RenderType del mesh mesh.Technique = TgcShaders.Instance.getTgcMeshTechnique(mesh.RenderType); } //Actualzar posicion de la luz var lightPos = (Vector3)Modifiers["lightPos"]; lightMesh.Position = lightPos; //Normalizar direccion de la luz var lightDir = (Vector3)Modifiers["lightDir"]; lightDir.Normalize(); //Renderizar meshes foreach (var mesh in scene.Meshes) { if (lightEnable) { //Cargar variables shader de la luz mesh.Effect.SetValue("lightColor", ColorValue.FromColor((Color)Modifiers["lightColor"])); mesh.Effect.SetValue("lightPosition", TgcParserUtils.vector3ToFloat4Array(lightPos)); mesh.Effect.SetValue("eyePosition", TgcParserUtils.vector3ToFloat4Array(Camara.Position)); mesh.Effect.SetValue("spotLightDir", TgcParserUtils.vector3ToFloat3Array(lightDir)); mesh.Effect.SetValue("lightIntensity", (float)Modifiers["lightIntensity"]); mesh.Effect.SetValue("lightAttenuation", (float)Modifiers["lightAttenuation"]); mesh.Effect.SetValue("spotLightAngleCos", FastMath.ToRad((float)Modifiers["spotAngle"])); mesh.Effect.SetValue("spotLightExponent", (float)Modifiers["spotExponent"]); //Cargar variables de shader de Material. El Material en realidad deberia ser propio de cada mesh. Pero en este ejemplo se simplifica con uno comun para todos mesh.Effect.SetValue("materialEmissiveColor", ColorValue.FromColor((Color)Modifiers["mEmissive"])); mesh.Effect.SetValue("materialAmbientColor", ColorValue.FromColor((Color)Modifiers["mAmbient"])); mesh.Effect.SetValue("materialDiffuseColor", ColorValue.FromColor((Color)Modifiers["mDiffuse"])); mesh.Effect.SetValue("materialSpecularColor", ColorValue.FromColor((Color)Modifiers["mSpecular"])); mesh.Effect.SetValue("materialSpecularExp", (float)Modifiers["specularEx"]); } //Renderizar modelo mesh.render(); } //Renderizar mesh de luz lightMesh.render(); PostRender(); }
public /*override*/ void render(float elapsedTime, Personaje personaje, Vector3 llegada, Explosion explosion) { 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 )) // vista_unica = !vista_unica; Vector3 lightPosition = (Vector3)GuiController.Instance.Modifiers["LightPosition"]; //Modifico parametros para que la luz gire en circulos /*cont = cont + elapsedTime * Geometry.DegreeToRadian(120.0f); * lightPosition.X = lightPosition.X + (10f * (float)Math.Cos(cont)); * lightPosition.Y = lightPosition.Y + (10f * (float)Math.Sin(cont)); * lightPosition.Z = lightPosition.Z + (50f * (float)Math.Sin(cont));*/ effect.Technique = "DefaultTechnique"; //Cargar variables de shader effect.SetValue("fvLightPosition", TgcParserUtils.vector3ToFloat3Array(lightPosition)); //effect.SetValue("fvEyePosition", TgcParserUtils.vector3ToFloat3Array(GuiController.Instance.RotCamera.getPosition())); effect.SetValue("k_la", (float)GuiController.Instance.Modifiers["Ambient"]); effect.SetValue("k_ld", (float)GuiController.Instance.Modifiers["Diffuse"]); effect.SetValue("k_ls", (float)GuiController.Instance.Modifiers["Specular"]); //effect.SetValue("fvAmbient", ColorValue.FromColor((Color)GuiController.Instance.Modifiers["AmbientColor"])); //effect.SetValue("fvDiffuse", ColorValue.FromColor((Color)GuiController.Instance.Modifiers["DiffuseColor"])); //effect.SetValue("fvSpecular", ColorValue.FromColor((Color)GuiController.Instance.Modifiers["SpecularColor"])); float distancia = FastMath.Pow2(personaje.getPersonaje().Position.X - llegada.X) + FastMath.Pow2(personaje.getPersonaje().Position.Z - llegada.Z); distancia = FastMath.Sqrt(distancia); distancia = Convert.ToInt32(distancia); if (explosion.estaEjecutandose()) { GuiController.Instance.Fog.Enabled = false; } else { if (distancia < DISNTANCIA_SIRENA) { GuiController.Instance.Fog.Enabled = false; } else { GuiController.Instance.Fog.Enabled = (bool)GuiController.Instance.Modifiers["Enabled"]; } } GuiController.Instance.Fog.StartDistance = (float)GuiController.Instance.Modifiers["startDistance"]; GuiController.Instance.Fog.EndDistance = (float)GuiController.Instance.Modifiers["endDistance"]; GuiController.Instance.Fog.Density = (float)GuiController.Instance.Modifiers["density"]; GuiController.Instance.Fog.Color = (Color)GuiController.Instance.Modifiers["color"]; //Actualizar valores de la Niebla GuiController.Instance.Fog.updateValues(); //Cambio los valores del Specular Power de la luz, para simular el parpadeo de la sirena r = t % 10; if (r == 0) { effect.SetValue("fSpecularPower", (float)10); } else { effect.SetValue("fSpecularPower", (float)200); } if (t == 1000) { t = 0; } else { t = t + 1; } //Mover mesh que representa la luz //lightBox.Position = lightPosition; // solo una vista //device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.Black, 1.0f, 0); device.Viewport = ViewF; /*foreach (MyMesh m in scene.Meshes) * { * if (m.Name != "escenarioPrueba") * { * m.effect = effect; * } * m.render();*/ patrulla.render(); //lightBox.render(); //} }
public void render(float time) { d3dDevice.RenderState.AlphaBlendEnable = false; skyBox.render(); sonido.render(time); sonido2.render(time); bool showQuadtree = (bool)GuiController.Instance.Modifiers["showQuadtree"]; bool showTerrain = (bool)GuiController.Instance.Modifiers["showTerrain"]; if (showTerrain) { foreach (TgcMesh mesh in terrenoMesh) { mesh.render(); } } bool showBB = (bool)GuiController.Instance.Modifiers.getValue("showBoundingBox"); bool frustumCullingEnabled = (bool)GuiController.Instance.Modifiers["culling"]; bool disparando = ControladorJuego.getInstance().personaje.cDisparando; Vector3 lightPosition = ControladorJuego.getInstance().personaje.disparo.PosicionActual; Vector3 initialPosition = ControladorJuego.getInstance().personaje.disparo.getPosicionInicial(); //solo aplico shader cuando estoy a cierta distancia del disparo y este existe Vector3 posicionDisparo = ControladorJuego.getInstance().personaje.disparo.PosicionActual; quadtree.render(GuiController.Instance.Frustum, showQuadtree); //Analizar cada malla contra el Frustum - con fuerza bruta int totalMeshes = 0; foreach (TgcMeshShader mesh in terreno.Meshes) { if (disparando /*&& calcularDistancia(mesh.Position ,posicionDisparo) >500*/) { //Cargar variables de shader globales a todos los objetos mesh.Effect.SetValue("fvLightPosition", TgcParserUtils.vector3ToFloat3Array(lightPosition)); mesh.Effect.SetValue("fvEyePosition", TgcParserUtils.vector3ToFloat3Array(initialPosition)); mesh.Effect.SetValue("fvAmbient", ColorValue.FromColor((Color)GuiController.Instance.Modifiers["AmbientColor"])); mesh.Effect.SetValue("fvDiffuse", ColorValue.FromColor((Color)GuiController.Instance.Modifiers["DiffuseColor"])); mesh.Effect.SetValue("fvSpecular", ColorValue.FromColor((Color)GuiController.Instance.Modifiers["SpecularColor"])); mesh.Effect.SetValue("fSpecularPower", (float)GuiController.Instance.Modifiers["SpecularPower"]); } mesh.render(); } //Render meshes if (showBB) { foreach (TgcMesh mesh in terreno.Meshes) { mesh.BoundingBox.render(); } } camera.Acercar(); //foreach (TgcMesh mesh in meshes2) //{ // mesh.render(); //} //foreach (TgcMesh mesh in meshes3) //{ // mesh.render(); //} }
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.Space)) { pom = !pom; } if (GuiController.Instance.D3dInput.keyPressed(Microsoft.DirectX.DirectInput.Key.L)) { phong = !phong; } if (GuiController.Instance.D3dInput.keyPressed(Microsoft.DirectX.DirectInput.Key.S)) { if (++nro_textura >= 3) { nro_textura = 0; } } Vector3 lightDir = (Vector3)GuiController.Instance.Modifiers["LightDir"]; effect.SetValue("g_LightDir", TgcParserUtils.vector3ToFloat3Array(lightDir)); effect.SetValue("min_cant_samples", (float)GuiController.Instance.Modifiers["minSample"]); effect.SetValue("max_cant_samples", (float)GuiController.Instance.Modifiers["maxSample"]); effect.SetValue("fHeightMapScale", (float)GuiController.Instance.Modifiers["HeightMapScale"]); effect.SetValue("fvEyePosition", TgcParserUtils.vector3ToFloat3Array(GuiController.Instance.RotCamera.getPosition())); device.EndScene(); effect.SetValue("time", time); switch (nro_textura) { case 0: default: effect.SetValue("aux_Tex", g_pBaseTexture); effect.SetValue("height_map", g_pHeightmap); break; case 1: effect.SetValue("aux_Tex", g_pBaseTexture2); effect.SetValue("height_map", g_pHeightmap2); break; case 2: effect.SetValue("aux_Tex", g_pBaseTexture3); effect.SetValue("height_map", g_pHeightmap3); break; } effect.SetValue("phong_lighting", phong); device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.Black, 1.0f, 0); device.BeginScene(); mesh.Effect = effect; mesh.Technique = pom ? "ParallaxOcclusion" : "BumpMap"; mesh.render(); GuiController.Instance.Text3d.drawText((pom ? "ParallaxOcclusion" : "BumpMap") + " " + (phong?"Phong Lighting":"Iluminación estática") , 0, 0, Color.Yellow); }
public override void Render() { PreRender(); var lightPosition = (Vector3)Modifiers["LightPosition"]; if (TgcD3dInput.Instance.keyPressed(Key.Space)) { efecto_blur = !efecto_blur; } //Cargar variables de shader effect.SetValue("fvLightPosition", TgcParserUtils.vector3ToFloat3Array(lightPosition)); effect.SetValue("fvEyePosition", TgcParserUtils.vector3ToFloat3Array(Camara.Position)); effect.SetValue("k_la", (float)Modifiers["Ambient"]); effect.SetValue("k_ld", (float)Modifiers["Diffuse"]); effect.SetValue("k_ls", (float)Modifiers["Specular"]); effect.SetValue("fSpecularPower", (float)Modifiers["SpecularPower"]); D3DDevice.Instance.Device.EndScene(); // dibujo la escena una textura effect.Technique = "DefaultTechnique"; // guardo el Render target anterior y seteo la textura como render target var pOldRT = D3DDevice.Instance.Device.GetRenderTarget(0); var pSurf = g_pRenderTarget.GetSurfaceLevel(0); D3DDevice.Instance.Device.SetRenderTarget(0, pSurf); // hago lo mismo con el depthbuffer, necesito el que no tiene multisampling var pOldDS = D3DDevice.Instance.Device.DepthStencilSurface; // Probar de comentar esta linea, para ver como se produce el fallo en el ztest // por no soportar usualmente el multisampling en el render to texture. D3DDevice.Instance.Device.DepthStencilSurface = g_pDepthStencil; D3DDevice.Instance.Device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.Black, 1.0f, 0); D3DDevice.Instance.Device.BeginScene(); foreach (var instance in instances) { instance.Technique = "DefaultTechnique"; instance.render(); } D3DDevice.Instance.Device.EndScene(); //TextureLoader.Save("scene.bmp", ImageFileFormat.Bmp, g_pRenderTarget); // genero el normal map: pSurf = g_pNormals.GetSurfaceLevel(0); D3DDevice.Instance.Device.SetRenderTarget(0, pSurf); D3DDevice.Instance.Device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.Black, 1.0f, 0); D3DDevice.Instance.Device.BeginScene(); foreach (var instance in instances) { instance.Technique = "NormalMap"; instance.render(); } D3DDevice.Instance.Device.EndScene(); // restuaro el render target y el stencil D3DDevice.Instance.Device.DepthStencilSurface = pOldDS; D3DDevice.Instance.Device.SetRenderTarget(0, pOldRT); //TextureLoader.Save("normal.bmp", ImageFileFormat.Bmp, g_pNormals); // dibujo el quad pp dicho : D3DDevice.Instance.Device.BeginScene(); effect.Technique = efecto_blur ? "CopyScreen" : "EdgeDetect"; D3DDevice.Instance.Device.VertexFormat = CustomVertex.PositionTextured.Format; D3DDevice.Instance.Device.SetStreamSource(0, g_pVBV3D, 0); effect.SetValue("g_Normals", g_pNormals); effect.SetValue("g_RenderTarget", g_pRenderTarget); D3DDevice.Instance.Device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.Black, 1.0f, 0); effect.Begin(FX.None); effect.BeginPass(0); D3DDevice.Instance.Device.DrawPrimitives(PrimitiveType.TriangleStrip, 0, 2); effect.EndPass(); effect.End(); RenderFPS(); RenderAxis(); D3DDevice.Instance.Device.EndScene(); D3DDevice.Instance.Device.Present(); }
public override void Render() { PreRender(); var device = D3DDevice.Instance.Device; time += ElapsedTime; if (Input.keyPressed(Key.Space)) { pom = !pom; } if (Input.keyPressed(Key.L)) { phong = !phong; } if (Input.keyPressed(Key.S)) { if (++nro_textura >= 3) { nro_textura = 0; } } var lightDir = (Vector3)Modifiers["LightDir"]; effect.SetValue("g_LightDir", TgcParserUtils.vector3ToFloat3Array(lightDir)); effect.SetValue("min_cant_samples", (float)Modifiers["minSample"]); effect.SetValue("max_cant_samples", (float)Modifiers["maxSample"]); effect.SetValue("fHeightMapScale", (float)Modifiers["HeightMapScale"]); effect.SetValue("fvEyePosition", TgcParserUtils.vector3ToFloat3Array(Camara.Position)); device.EndScene(); effect.SetValue("time", time); switch (nro_textura) { case 0: default: effect.SetValue("aux_Tex", g_pBaseTexture); effect.SetValue("height_map", g_pHeightmap); break; case 1: effect.SetValue("aux_Tex", g_pBaseTexture2); effect.SetValue("height_map", g_pHeightmap2); break; case 2: effect.SetValue("aux_Tex", g_pBaseTexture3); effect.SetValue("height_map", g_pHeightmap3); break; } effect.SetValue("phong_lighting", phong); device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.Black, 1.0f, 0); device.BeginScene(); mesh.Effect = effect; mesh.Technique = pom ? "ParallaxOcclusion" : "BumpMap"; mesh.render(); DrawText.drawText( (pom ? "ParallaxOcclusion" : "BumpMap") + " " + (phong ? "Phong Lighting" : "Iluminación estática"), 0, 20, Color.Yellow); RenderFPS(); RenderAxis(); device.EndScene(); device.Present(); }
public override void Render() { ClearTextures(); D3DDevice.Instance.Device.BeginScene(); var lightPosition = (Vector3)Modifiers["LightPosition"]; //Cargar variables de shader effect.SetValue("fvLightPosition", TgcParserUtils.vector3ToFloat3Array(lightPosition)); effect.SetValue("fvEyePosition", TgcParserUtils.vector3ToFloat3Array(Camara.Position)); effect.SetValue("k_la", (float)Modifiers["Ambient"]); effect.SetValue("k_ld", (float)Modifiers["Diffuse"]); effect.SetValue("k_ls", (float)Modifiers["Specular"]); effect.SetValue("fSpecularPower", (float)Modifiers["SpecularPower"]); //Mover mesh que representa la luz lightBox.Position = lightPosition; if (!(bool)Modifiers["viewports"]) { // solo una vista D3DDevice.Instance.Device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.Black, 1.0f, 0); D3DDevice.Instance.Device.Viewport = ViewF; foreach (var m in scene.Meshes) { m.Effect = effect; m.Technique = "DefaultTechnique"; m.UpdateMeshTransform(); m.render(); } lightBox.render(); } else { // 3 vistas: // 1- vista: usando el shader D3DDevice.Instance.Device.Viewport = View1; D3DDevice.Instance.Device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.Black, 1.0f, 0); foreach (var m in scene.Meshes) { m.Effect = effect; m.Technique = "DefaultTechnique"; m.UpdateMeshTransform(); m.render(); } lightBox.render(); // 2- vista: fixed pipeline con iluminacion dinamica D3DDevice.Instance.Device.Viewport = View2; D3DDevice.Instance.Device.SetRenderState(RenderStates.Lighting, true); D3DDevice.Instance.Device.SetRenderState(RenderStates.SpecularEnable, true); D3DDevice.Instance.Device.Lights[0].Position = lightPosition; D3DDevice.Instance.Device.Lights[0].Update(); D3DDevice.Instance.Device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.Black, 1.0f, 0); foreach (var m in scene.Meshes) { m.Effect = TgcShaders.Instance.TgcMeshShader; m.RenderType = TgcMesh.MeshRenderType.DIFFUSE_MAP; m.Technique = TgcShaders.Instance.getTgcMeshTechnique(m.RenderType); m.UpdateMeshTransform(); m.render(); } lightBox.render(); // 3- vista: fixed pipeline con iluminacion estatica D3DDevice.Instance.Device.Viewport = View3; D3DDevice.Instance.Device.SetRenderState(RenderStates.Lighting, false); D3DDevice.Instance.Device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.Black, 1.0f, 0); foreach (var m in scene.Meshes) { m.Effect = TgcShaders.Instance.TgcMeshShader; m.RenderType = TgcMesh.MeshRenderType.DIFFUSE_MAP; m.Technique = TgcShaders.Instance.getTgcMeshTechnique(m.RenderType); m.UpdateMeshTransform(); m.render(); } lightBox.render(); } RenderFPS(); RenderAxis(); D3DDevice.Instance.Device.EndScene(); D3DDevice.Instance.Device.Present(); }
public override void render(float elapsedTime) { update(elapsedTime); Device device = GuiController.Instance.D3dDevice; Control panel3d = GuiController.Instance.Panel3d; float aspectRatio = (float)panel3d.Width / (float)panel3d.Height; time += elapsedTime; Vector3 lightDir = (Vector3)GuiController.Instance.Modifiers["LightDir"]; effect.SetValue("g_LightDir", TgcParserUtils.vector3ToFloat3Array(lightDir)); effect.SetValue("min_cant_samples", (float)GuiController.Instance.Modifiers["minSample"]); effect.SetValue("max_cant_samples", (float)GuiController.Instance.Modifiers["maxSample"]); effect.SetValue("fHeightMapScale", (float)GuiController.Instance.Modifiers["HeightMapScale"]); effect.SetValue("fvEyePosition", TgcParserUtils.vector3ToFloat3Array(GuiController.Instance.FpsCamera.getPosition())); effect.SetValue("time", time); effect.SetValue("aux_Tex", g_pBaseTexture); effect.SetValue("height_map", g_pHeightmap); effect.SetValue("phong_lighting", true); device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.Black, 1.0f, 0); device.BeginScene(); foreach (TgcMesh mesh in scene.Meshes) { bool va = true; int nro_textura = 0; mesh.Effect = effect; if (mesh.Name.Contains("Floor")) { effect.SetValue("g_normal", TgcParserUtils.vector3ToFloat3Array(new Vector3(0, -1, 0))); effect.SetValue("g_tangent", TgcParserUtils.vector3ToFloat3Array(new Vector3(1, 0, 0))); effect.SetValue("g_binormal", TgcParserUtils.vector3ToFloat3Array(new Vector3(0, 0, 1))); nro_textura = 0; } else if (mesh.Name.Contains("Roof")) { effect.SetValue("g_normal", TgcParserUtils.vector3ToFloat3Array(new Vector3(0, 1, 0))); effect.SetValue("g_tangent", TgcParserUtils.vector3ToFloat3Array(new Vector3(1, 0, 0))); effect.SetValue("g_binormal", TgcParserUtils.vector3ToFloat3Array(new Vector3(0, 0, 1))); nro_textura = 0; va = false; } else if (mesh.Name.Contains("East")) { effect.SetValue("g_normal", TgcParserUtils.vector3ToFloat3Array(new Vector3(1, 0, 0))); effect.SetValue("g_tangent", TgcParserUtils.vector3ToFloat3Array(new Vector3(0, 0, 1))); effect.SetValue("g_binormal", TgcParserUtils.vector3ToFloat3Array(new Vector3(0, 1, 0))); nro_textura = 1; } else if (mesh.Name.Contains("West")) { effect.SetValue("g_normal", TgcParserUtils.vector3ToFloat3Array(new Vector3(-1, 0, 0))); effect.SetValue("g_tangent", TgcParserUtils.vector3ToFloat3Array(new Vector3(0, 0, 1))); effect.SetValue("g_binormal", TgcParserUtils.vector3ToFloat3Array(new Vector3(0, 1, 0))); nro_textura = 1; } else if (mesh.Name.Contains("North")) { effect.SetValue("g_normal", TgcParserUtils.vector3ToFloat3Array(new Vector3(0, 0, -1))); effect.SetValue("g_tangent", TgcParserUtils.vector3ToFloat3Array(new Vector3(1, 0, 0))); effect.SetValue("g_binormal", TgcParserUtils.vector3ToFloat3Array(new Vector3(0, 1, 0))); nro_textura = 1; } else if (mesh.Name.Contains("South")) { effect.SetValue("g_normal", TgcParserUtils.vector3ToFloat3Array(new Vector3(0, 0, 1))); effect.SetValue("g_tangent", TgcParserUtils.vector3ToFloat3Array(new Vector3(1, 0, 0))); effect.SetValue("g_binormal", TgcParserUtils.vector3ToFloat3Array(new Vector3(0, 1, 0))); nro_textura = 1; } switch (nro_textura) { case 0: default: effect.SetValue("aux_Tex", g_pBaseTexture); effect.SetValue("height_map", g_pHeightmap); break; case 1: effect.SetValue("aux_Tex", g_pBaseTexture2); effect.SetValue("height_map", g_pHeightmap2); break; case 2: effect.SetValue("aux_Tex", g_pBaseTexture3); effect.SetValue("height_map", g_pHeightmap3); break; case 3: effect.SetValue("aux_Tex", g_pBaseTexture4); effect.SetValue("height_map", g_pHeightmap4); break; } if (va) { mesh.Technique = "ParallaxOcclusion2"; mesh.render(); } } //Render personames enemigos foreach (TgcSkeletalMesh m in enemigos) { m.render(); } // Render hud renderHUD(); gui.trapezoidal_style = false; //radar de proximidad float max_dist = 80; foreach (TgcSkeletalMesh m in enemigos) { Vector3 pos_personaje = GuiController.Instance.FpsCamera.getPosition(); Vector3 pos_enemigo = m.Position * 1; float dist = (pos_personaje - pos_enemigo).Length(); if (dist < max_dist) { pos_enemigo.Y = m.BoundingBox.PMax.Y * 0.75f + m.BoundingBox.PMin.Y * 0.25f; pos_enemigo.Project(device.Viewport, device.Transform.Projection, device.Transform.View, device.Transform.World); if (pos_enemigo.Z > 0 && pos_enemigo.Z < 1) { float an = (max_dist - dist) / max_dist * 3.1415f * 2.0f; int d = (int)dist; gui.DrawArc(new Vector2(pos_enemigo.X + 20, pos_enemigo.Y), 40, 0, an, 10, dist < 30 ? Color.Tomato : Color.WhiteSmoke); gui.DrawLine(pos_enemigo.X, pos_enemigo.Y, pos_enemigo.X + 20, pos_enemigo.Y, 3, Color.PowderBlue); gui.DrawLine(pos_enemigo.X + 20, pos_enemigo.Y, pos_enemigo.X + 40, pos_enemigo.Y - 20, 3, Color.PowderBlue); gui.TextOut((int)pos_enemigo.X + 50, (int)pos_enemigo.Y - 20, "Proximidad " + d, Color.PowderBlue); } } } gui.trapezoidal_style = true; device.EndScene(); }
public void renderizarLuz(int tipo, Vector3 posicionCamara, Vector3 direccionDeLuz, TgcMesh mesh, float intensidad, float temblor) { g_LightPos = posicionCamara; //g_LightDir = direccionDeLuz - g_LightPos; g_LightDir = direccionDeLuz; g_LightDir.Normalize(); var random = FastMath.Cos(6 * temblor); //Actualzar posición de la luz Vector3 lightPos = posicionCamara; //Normalizar direccion de la luz Vector3 lightDir = direccionDeLuz; lightDir.Normalize(); //Cargar variables shader de la luz if (tipo == 0) { lightColors[0] = ColorValue.FromColor(Color.White); pointLightPositions[0] = TgcParserUtils.vector3ToVector4(lightPos); pointLightIntensity[0] = intensidad; pointLightAttenuation[0] = 0.48f; for (int i = 1; i < 5; i++) { lightColors[i] = ColorValue.FromColor(Color.White); pointLightPositions[i] = TgcParserUtils.vector3ToVector4(posicionesDeLuces[i - 1]); pointLightIntensity[i] = (float)GuiController.Instance.Modifiers["lightIntensity"]; pointLightAttenuation[i] = (float)GuiController.Instance.Modifiers["lightAttenuation"]; } mesh.Effect.SetValue("lightColor", lightColors); mesh.Effect.SetValue("lightPosition", pointLightPositions); mesh.Effect.SetValue("eyePosition", TgcParserUtils.vector3ToFloat4Array(lightPos)); mesh.Effect.SetValue("spotLightDir", TgcParserUtils.vector3ToFloat3Array(lightDir)); mesh.Effect.SetValue("lightIntensity", pointLightIntensity); mesh.Effect.SetValue("lightAttenuation", pointLightAttenuation); mesh.Effect.SetValue("spotLightAngleCos", FastMath.ToRad(39f)); mesh.Effect.SetValue("spotLightExponent", 14f); //Variables de los materiales mesh.Effect.SetValue("materialEmissiveColor", ColorValue.FromColor(Color.Black)); mesh.Effect.SetValue("materialAmbientColor", ColorValue.FromColor(Color.White)); mesh.Effect.SetValue("materialDiffuseColor", ColorValue.FromColor(Color.White)); mesh.Effect.SetValue("materialSpecularColor", ColorValue.FromColor(Color.White)); mesh.Effect.SetValue("materialSpecularExp", 11f); mesh.render(); } else if (tipo == 1) { // Calculo la matriz de view de la luz //mesh.Effect.SetValue("g_vLightPos", new Vector4(g_LightPos.X, g_LightPos.Y, g_LightPos.Z, 1)); mesh.Effect.SetValue("g_vLightPos", new Vector4(posicionCamara.X, posicionCamara.Y, posicionCamara.Z, 1)); //mesh.Effect.SetValue("g_vLightDir", new Vector4(g_LightDir.X, g_LightDir.Y, g_LightDir.Z, 1)); mesh.Effect.SetValue("g_vLightDir", new Vector4(g_LightDir.X, g_LightDir.Y, g_LightDir.Z, 1)); mesh.Effect.SetValue("texProy", miTex); g_LightView = Matrix.LookAtLH(g_LightPos, g_LightPos + g_LightDir, new Vector3(0, 0, 1)); // inicializacion standard: //mesh.Effect.SetValue("g_mProjLight", g_mShadowProj); mesh.Effect.SetValue("g_mViewLightProj", g_LightView * g_mShadowProj); lightColors[0] = ColorValue.FromColor(Color.Yellow); pointLightPositions[0] = TgcParserUtils.vector3ToVector4(lightPos); pointLightIntensity[0] = intensidad; //POR 2?? pointLightAttenuation[0] = 0.67f; for (int i = 1; i < 5; i++) { lightColors[i] = ColorValue.FromColor(Color.White); pointLightPositions[i] = TgcParserUtils.vector3ToVector4(posicionesDeLuces[i - 1]); pointLightIntensity[i] = (float)GuiController.Instance.Modifiers["lightIntensity"]; pointLightAttenuation[i] = (float)GuiController.Instance.Modifiers["lightAttenuation"]; } mesh.Effect.SetValue("lightColor", lightColors); mesh.Effect.SetValue("lightPosition", pointLightPositions); mesh.Effect.SetValue("eyePosition", TgcParserUtils.vector3ToFloat4Array(lightPos)); mesh.Effect.SetValue("lightIntensity", pointLightIntensity); mesh.Effect.SetValue("lightAttenuation", pointLightAttenuation); //Cargar variables de shader de Material. El Material en realidad deberia ser propio de cada mesh. Pero en este ejemplo se simplifica con uno comun para todos mesh.Effect.SetValue("materialEmissiveColor", ColorValue.FromColor(Color.Black)); mesh.Effect.SetValue("materialAmbientColor", ColorValue.FromColor(Color.Yellow)); mesh.Effect.SetValue("materialDiffuseColor", ColorValue.FromColor(Color.White)); mesh.Effect.SetValue("materialSpecularColor", ColorValue.FromColor(Color.Yellow)); mesh.Effect.SetValue("materialSpecularExp", 33f); mesh.render(); } else if (tipo == 2) { lightColors[0] = ColorValue.FromColor(Color.LightGoldenrodYellow); pointLightPositions[0] = TgcParserUtils.vector3ToVector4(lightPos); pointLightIntensity[0] = intensidad + random * 3; pointLightAttenuation[0] = 0.67f; for (int i = 1; i < 5; i++) { lightColors[i] = ColorValue.FromColor(Color.White); pointLightPositions[i] = TgcParserUtils.vector3ToVector4(posicionesDeLuces[i - 1]); pointLightIntensity[i] = (float)GuiController.Instance.Modifiers["lightIntensity"]; pointLightAttenuation[i] = (float)GuiController.Instance.Modifiers["lightAttenuation"]; } mesh.Effect.SetValue("lightColor", lightColors); mesh.Effect.SetValue("lightPosition", pointLightPositions); mesh.Effect.SetValue("eyePosition", TgcParserUtils.vector3ToFloat4Array(lightPos)); mesh.Effect.SetValue("lightIntensity", pointLightIntensity); mesh.Effect.SetValue("lightAttenuation", pointLightAttenuation); //Cargar variables de shader de Material. El Material en realidad deberia ser propio de cada mesh. Pero en este ejemplo se simplifica con uno comun para todos mesh.Effect.SetValue("materialEmissiveColor", ColorValue.FromColor(Color.Black)); mesh.Effect.SetValue("materialAmbientColor", ColorValue.FromColor(Color.White)); mesh.Effect.SetValue("materialDiffuseColor", ColorValue.FromColor(Color.White)); mesh.Effect.SetValue("materialSpecularColor", ColorValue.FromColor(Color.Orange)); mesh.Effect.SetValue("materialSpecularExp", 23f); mesh.render(); } }
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)) { vista_unica = !vista_unica; } Vector3 lightPosition = (Vector3)GuiController.Instance.Modifiers["LightPosition"]; //Cargar variables de shader effect.SetValue("fvLightPosition", TgcParserUtils.vector3ToFloat3Array(lightPosition)); effect.SetValue("fvEyePosition", TgcParserUtils.vector3ToFloat3Array(GuiController.Instance.RotCamera.getPosition())); effect.SetValue("k_la", (float)GuiController.Instance.Modifiers["Ambient"]); effect.SetValue("k_ld", (float)GuiController.Instance.Modifiers["Diffuse"]); effect.SetValue("k_ls", (float)GuiController.Instance.Modifiers["Specular"]); effect.SetValue("fSpecularPower", (float)GuiController.Instance.Modifiers["SpecularPower"]); //Mover mesh que representa la luz lightBox.Position = lightPosition; if (vista_unica) { // solo una vista device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.Black, 1.0f, 0); device.Viewport = ViewF; foreach (TgcMesh m in scene.Meshes) { m.Effect = effect; m.Technique = "DefaultTechnique"; m.render(); } lightBox.render(); } else { // 3 vistas: // 1- vista: usando el shader device.Viewport = View1; device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.Black, 1.0f, 0); foreach (TgcMesh m in scene.Meshes) { m.Effect = effect; m.Technique = "DefaultTechnique"; m.render(); } lightBox.render(); // 2- vista: fixed pipeline con iluminacion dinamica device.Viewport = View2; device.SetRenderState(RenderStates.Lighting, true); device.SetRenderState(RenderStates.SpecularEnable, true); device.Lights[0].Position = lightPosition; device.Lights[0].Update(); device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.Black, 1.0f, 0); foreach (TgcMesh m in scene.Meshes) { m.Technique = "DefaultTechnique"; m.render(); } lightBox.render(); // 3- vista: fixed pipeline con iluminacion estatica device.Viewport = View3; device.SetRenderState(RenderStates.Lighting, false); device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.Black, 1.0f, 0); foreach (TgcMesh m in scene.Meshes) { m.Technique = "DefaultTechnique"; m.render(); } lightBox.render(); } }
public override void render(float elapsedTime) { Device device = GuiController.Instance.D3dDevice; Control panel3d = GuiController.Instance.Panel3d; float aspectRatio = (float)panel3d.Width / (float)panel3d.Height; Vector3 lightPosition = (Vector3)GuiController.Instance.Modifiers["LightPosition"]; if (GuiController.Instance.D3dInput.keyPressed(Microsoft.DirectX.DirectInput.Key.Space)) { efecto_blur = !efecto_blur; } //Cargar variables de shader effect.SetValue("fvLightPosition", TgcParserUtils.vector3ToFloat3Array(lightPosition)); effect.SetValue("fvEyePosition", TgcParserUtils.vector3ToFloat3Array(GuiController.Instance.RotCamera.getPosition())); effect.SetValue("k_la", (float)GuiController.Instance.Modifiers["Ambient"]); effect.SetValue("k_ld", (float)GuiController.Instance.Modifiers["Diffuse"]); effect.SetValue("k_ls", (float)GuiController.Instance.Modifiers["Specular"]); effect.SetValue("fSpecularPower", (float)GuiController.Instance.Modifiers["SpecularPower"]); device.EndScene(); // dibujo la escena una textura effect.Technique = "DefaultTechnique"; // guardo el Render target anterior y seteo la textura como render target Surface pOldRT = device.GetRenderTarget(0); Surface pSurf = g_pRenderTarget.GetSurfaceLevel(0); device.SetRenderTarget(0, pSurf); // hago lo mismo con el depthbuffer, necesito el que no tiene multisampling Surface pOldDS = device.DepthStencilSurface; // Probar de comentar esta linea, para ver como se produce el fallo en el ztest // por no soportar usualmente el multisampling en el render to texture. device.DepthStencilSurface = g_pDepthStencil; device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.Black, 1.0f, 0); device.BeginScene(); foreach (TgcMesh instance in instances) { instance.Technique = "DefaultTechnique"; instance.render(); } device.EndScene(); //TextureLoader.Save("scene.bmp", ImageFileFormat.Bmp, g_pRenderTarget); // genero el normal map: pSurf = g_pNormals.GetSurfaceLevel(0); device.SetRenderTarget(0, pSurf); device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.Black, 1.0f, 0); device.BeginScene(); foreach (TgcMesh instance in instances) { instance.Technique = "NormalMap"; instance.render(); } device.EndScene(); // restuaro el render target y el stencil device.DepthStencilSurface = pOldDS; device.SetRenderTarget(0, pOldRT); //TextureLoader.Save("normal.bmp", ImageFileFormat.Bmp, g_pNormals); // dibujo el quad pp dicho : device.BeginScene(); effect.Technique = efecto_blur? "CopyScreen" : "EdgeDetect"; device.VertexFormat = CustomVertex.PositionTextured.Format; device.SetStreamSource(0, g_pVBV3D, 0); effect.SetValue("g_Normals", g_pNormals); effect.SetValue("g_RenderTarget", g_pRenderTarget); 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(); }