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 renderLight(float elapsedTime, List <Luz> luces, Vector3 cameraPosition) { //Configurar los valores de cada luz ColorValue[] lightColors = new ColorValue[luces.Count]; Vector4[] pointLightPositions = new Vector4[luces.Count]; float[] pointLightIntensity = new float[luces.Count]; float[] pointLightAttenuation = new float[luces.Count]; for (int i = 0; i < luces.Count; i++) { Luz lightMesh = luces[i]; lightColors[i] = ColorValue.FromColor(lightMesh.Color); pointLightPositions[i] = TgcParserUtils.vector3ToVector4(lightMesh.Posicion); //pointLightIntensity[i] = (float)GuiController.Instance.Modifiers["lightIntensity"]; //pointLightAttenuation[i] = (float)GuiController.Instance.Modifiers["lightAttenuation"]; pointLightIntensity[i] = 50; pointLightAttenuation[i] = 20; } //Cargar variables de shader this.lightEffect.SetValue("lightColor", lightColors); this.lightEffect.SetValue("lightPosition", pointLightPositions); this.lightEffect.SetValue("lightIntensity", pointLightIntensity); this.lightEffect.SetValue("lightAttenuation", pointLightAttenuation); this.lightEffect.SetValue("eyePosition", TgcParserUtils.vector3ToFloat4Array(cameraPosition)); //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 this.lightEffect.SetValue("materialEmissiveColor", ColorValue.FromColor(Color.Black)); this.lightEffect.SetValue("materialAmbientColor", ColorValue.FromColor(Color.White)); this.lightEffect.SetValue("materialDiffuseColor", ColorValue.FromColor(Color.White)); this.lightEffect.SetValue("materialSpecularColor", ColorValue.FromColor(Color.White)); this.lightEffect.SetValue("materialSpecularExp", 20f); Effect originalEffect = this.sphere.Effect; string originalTechnique = this.sphere.Technique; this.sphere.Effect = this.lightEffect; //El Technique depende del tipo RenderType del mesh "VERTEX_COLOR"; "DIFFUSE_MAP"; this.sphere.Technique = "DIFFUSE_MAP"; this.render(); this.sphere.Effect = originalEffect; this.sphere.Technique = originalTechnique; }
public override void Render() { this.boxFloor.Technique = "PS1"; this.boxFloor.Effect.SetValue("g_vLightPos", TgcParserUtils.vector3ToVector4(this.boxLuz.Position)); this.boxFloor.Effect.SetValue("g_vLightDir", TgcParserUtils.vector3ToVector4(new Vector3(0, 0, 0) - this.boxLuz.Position)); this.boxWall.Technique = "PS1"; this.boxFigure.Technique = "PS1"; this.boxFloor.render(); this.boxWall.render(); this.boxFigure.render(); this.boxLuz.render(); this.meshTetera.Technique = "PS2"; this.meshTetera.render(); this.meshBuggy.render(); }
public override void Render() { PreRender(); //Habilitar luz var lightEnable = (bool)Modifiers["lightEnable"]; Effect currentShader; string currentTechnique; if (lightEnable) { //Shader personalizado de iluminacion currentShader = effect; currentTechnique = "MultiDiffuseLightsTechnique"; } else { //Sin luz: Restaurar shader default currentShader = TgcShaders.Instance.TgcMeshShader; currentTechnique = TgcShaders.Instance.getTgcMeshTechnique(TgcMesh.MeshRenderType.DIFFUSE_MAP); } //Aplicar a cada mesh el shader actual foreach (var mesh in scene.Meshes) { mesh.Effect = currentShader; mesh.Technique = currentTechnique; } //Configurar los valores de cada luz var move = new Vector3(0, 0, (bool)Modifiers["lightMove"] ? interp.update(ElapsedTime) : 0); var lightColors = new ColorValue[lightMeshes.Length]; var pointLightPositions = new Vector4[lightMeshes.Length]; var pointLightIntensity = new float[lightMeshes.Length]; var pointLightAttenuation = new float[lightMeshes.Length]; for (var i = 0; i < lightMeshes.Length; i++) { var lightMesh = lightMeshes[i]; lightMesh.Position = origLightPos[i] + Vector3.Scale(move, i + 1); lightColors[i] = ColorValue.FromColor(lightMesh.Color); pointLightPositions[i] = TgcParserUtils.vector3ToVector4(lightMesh.Position); pointLightIntensity[i] = (float)Modifiers["lightIntensity"]; pointLightAttenuation[i] = (float)Modifiers["lightAttenuation"]; } //Renderizar meshes foreach (var mesh in scene.Meshes) { mesh.UpdateMeshTransform(); if (lightEnable) { //Cargar variables de shader mesh.Effect.SetValue("lightColor", lightColors); mesh.Effect.SetValue("lightPosition", pointLightPositions); mesh.Effect.SetValue("lightIntensity", pointLightIntensity); mesh.Effect.SetValue("lightAttenuation", pointLightAttenuation); mesh.Effect.SetValue("materialEmissiveColor", ColorValue.FromColor((Color)Modifiers["mEmissive"])); mesh.Effect.SetValue("materialDiffuseColor", ColorValue.FromColor((Color)Modifiers["mDiffuse"])); } //Renderizar modelo mesh.render(); } //Renderizar meshes de luz for (var i = 0; i < lightMeshes.Length; i++) { var lightMesh = lightMeshes[i]; lightMesh.render(); } PostRender(); }
public override void render(float elapsedTime) { Device device = GuiController.Instance.D3dDevice; //Habilitar luz bool lightEnable = (bool)GuiController.Instance.Modifiers["lightEnable"]; Effect currentShader; String currentTechnique; if (lightEnable) { //Shader personalizado de iluminacion currentShader = this.effect; currentTechnique = "ThreeLightsTechnique"; } else { //Sin luz: Restaurar shader default currentShader = GuiController.Instance.Shaders.TgcMeshShader; currentTechnique = GuiController.Instance.Shaders.getTgcMeshTechnique(TgcMesh.MeshRenderType.DIFFUSE_MAP); } //Aplicar a cada mesh el shader actual foreach (MeshLightData meshData in meshesWithLight) { meshData.mesh.Effect = currentShader; meshData.mesh.Technique = currentTechnique; } Vector3 eyePosition = GuiController.Instance.FpsCamera.getPosition(); //Renderizar meshes con BumpMapping foreach (MeshLightData meshData in meshesWithLight) { TgcMeshBumpMapping mesh = meshData.mesh; if (lightEnable) { mesh.Effect.SetValue("eyePosition", TgcParserUtils.vector3ToFloat4Array(eyePosition)); mesh.Effect.SetValue("bumpiness", (float)GuiController.Instance.Modifiers["bumpiness"]); mesh.Effect.SetValue("reflection", (float)GuiController.Instance.Modifiers["reflection"]); //Cargar variables de shader del Material mesh.Effect.SetValue("materialEmissiveColor", ColorValue.FromColor((Color)GuiController.Instance.Modifiers["mEmissive"])); mesh.Effect.SetValue("materialAmbientColor", ColorValue.FromColor((Color)GuiController.Instance.Modifiers["mAmbient"])); mesh.Effect.SetValue("materialDiffuseColor", ColorValue.FromColor((Color)GuiController.Instance.Modifiers["mDiffuse"])); mesh.Effect.SetValue("materialSpecularColor", ColorValue.FromColor((Color)GuiController.Instance.Modifiers["mSpecular"])); mesh.Effect.SetValue("materialSpecularExp", (float)GuiController.Instance.Modifiers["specularEx"]); //CubeMap mesh.Effect.SetValue("texCubeMap", cubeMap); //Cargar variables de shader de las 3 luces //Intensidad y atenuacion deberian ser atributos propios de cada luz float lightIntensity = (float)GuiController.Instance.Modifiers["lightIntensity"]; float lightAttenuation = (float)GuiController.Instance.Modifiers["lightAttenuation"]; mesh.Effect.SetValue("lightIntensity", new float[] { lightIntensity, lightIntensity, lightIntensity }); mesh.Effect.SetValue("lightAttenuation", new float[] { lightAttenuation, lightAttenuation, lightAttenuation }); mesh.Effect.SetValue("lightColor", new ColorValue[] { ColorValue.FromColor(meshData.lights[0].color), ColorValue.FromColor(meshData.lights[1].color), ColorValue.FromColor(meshData.lights[2].color) }); mesh.Effect.SetValue("lightPosition", new Vector4[] { TgcParserUtils.vector3ToVector4(meshData.lights[0].pos), TgcParserUtils.vector3ToVector4(meshData.lights[1].pos), TgcParserUtils.vector3ToVector4(meshData.lights[2].pos) }); } //Renderizar modelo mesh.render(); } //Renderizar meshes comunes foreach (TgcMesh mesh in commonMeshes) { mesh.render(); } }
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; //Habilitar luz bool lightEnable = (bool)GuiController.Instance.Modifiers["lightEnable"]; Effect currentShader; String currentTechnique; if (lightEnable) { //Shader personalizado de iluminacion currentShader = this.effect; currentTechnique = "MultiDiffuseLightsTechnique"; } else { //Sin luz: Restaurar shader default currentShader = GuiController.Instance.Shaders.TgcMeshShader; currentTechnique = GuiController.Instance.Shaders.getTgcMeshTechnique(TgcMesh.MeshRenderType.DIFFUSE_MAP); } //Aplicar a cada mesh el shader actual foreach (TgcMesh mesh in scene.Meshes) { mesh.Effect = currentShader; mesh.Technique = currentTechnique; } //Configurar los valores de cada luz Vector3 move = new Vector3(0, 0, ((bool)GuiController.Instance.Modifiers["lightMove"]) ? interp.update() : 0); ColorValue[] lightColors = new ColorValue[lightMeshes.Length]; Vector4[] pointLightPositions = new Vector4[lightMeshes.Length]; float[] pointLightIntensity = new float[lightMeshes.Length]; float[] pointLightAttenuation = new float[lightMeshes.Length]; for (int i = 0; i < lightMeshes.Length; i++) { TgcBox lightMesh = lightMeshes[i]; lightMesh.Position = origLightPos[i] + Vector3.Scale(move, i + 1); lightColors[i] = ColorValue.FromColor(lightMesh.Color); pointLightPositions[i] = TgcParserUtils.vector3ToVector4(lightMesh.Position); pointLightIntensity[i] = (float)GuiController.Instance.Modifiers["lightIntensity"]; pointLightAttenuation[i] = (float)GuiController.Instance.Modifiers["lightAttenuation"]; } //Renderizar meshes foreach (TgcMesh mesh in scene.Meshes) { if (lightEnable) { //Cargar variables de shader mesh.Effect.SetValue("lightColor", lightColors); mesh.Effect.SetValue("lightPosition", pointLightPositions); mesh.Effect.SetValue("lightIntensity", pointLightIntensity); mesh.Effect.SetValue("lightAttenuation", pointLightAttenuation); mesh.Effect.SetValue("materialEmissiveColor", ColorValue.FromColor((Color)GuiController.Instance.Modifiers["mEmissive"])); mesh.Effect.SetValue("materialDiffuseColor", ColorValue.FromColor((Color)GuiController.Instance.Modifiers["mDiffuse"])); } //Renderizar modelo mesh.render(); } //Renderizar meshes de luz for (int i = 0; i < lightMeshes.Length; i++) { TgcBox lightMesh = lightMeshes[i]; lightMesh.render(); } }
public override void Render() { PreRender(); //Habilitar luz var lightEnable = (bool)Modifiers["lightEnable"]; Effect currentShader; string currentTechnique; if (lightEnable) { //Shader personalizado de iluminacion currentShader = effect; currentTechnique = "ThreeLightsTechnique"; } else { //Sin luz: Restaurar shader default currentShader = TgcShaders.Instance.TgcMeshShader; currentTechnique = TgcShaders.Instance.getTgcMeshTechnique(TgcMesh.MeshRenderType.DIFFUSE_MAP); } //Aplicar a cada mesh el shader actual foreach (var meshData in meshesWithLight) { meshData.mesh.Effect = currentShader; meshData.mesh.Technique = currentTechnique; } var eyePosition = Camara.Position; //Renderizar meshes con BumpMapping foreach (var meshData in meshesWithLight) { var mesh = meshData.mesh; if (true) //FIXME da error cuando se desabilitan las luces.) (lightEnable) { mesh.Effect.SetValue("eyePosition", TgcParserUtils.vector3ToFloat4Array(eyePosition)); mesh.Effect.SetValue("bumpiness", (float)Modifiers["bumpiness"]); mesh.Effect.SetValue("reflection", (float)Modifiers["reflection"]); //Cargar variables de shader del Material 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"]); //CubeMap mesh.Effect.SetValue("texCubeMap", cubeMap); //Cargar variables de shader de las 3 luces //Intensidad y atenuacion deberian ser atributos propios de cada luz var lightIntensity = (float)Modifiers["lightIntensity"]; var lightAttenuation = (float)Modifiers["lightAttenuation"]; mesh.Effect.SetValue("lightIntensity", new[] { lightIntensity, lightIntensity, lightIntensity }); mesh.Effect.SetValue("lightAttenuation", new[] { lightAttenuation, lightAttenuation, lightAttenuation }); mesh.Effect.SetValue("lightColor", new[] { ColorValue.FromColor(meshData.lights[0].color), ColorValue.FromColor(meshData.lights[1].color), ColorValue.FromColor(meshData.lights[2].color) }); mesh.Effect.SetValue("lightPosition", new[] { TgcParserUtils.vector3ToVector4(meshData.lights[0].pos), TgcParserUtils.vector3ToVector4(meshData.lights[1].pos), TgcParserUtils.vector3ToVector4(meshData.lights[2].pos) }); } //Renderizar modelo mesh.render(); } //Renderizar meshes comunes foreach (var mesh in commonMeshes) { mesh.render(); } PostRender(); }