public static void Cargar() { // Cargar terreno y textura terrain = new TgcSimpleTerrain(); string heightmap = GuiController.Instance.ExamplesMediaDir + "Heighmaps\\" + "HeightmapHawaii.jpg"; string textura = Utiles.TexturasDir("IslaTextura.png"); Vector3 PosicionIsla = new Vector3(0, 0, 0); PosicionIsla.Y = -Oceano.AplicarCPUShader(PosicionIsla).Y - 100; terrain.AlphaBlendEnable = false; terrain.loadHeightmap(heightmap, 100, 5f, PosicionIsla); terrain.loadTexture(textura); }
public static void Cargar() { SpriteDrawer = new Drawer2D(); AnimatedSprite = new TgcAnimatedSprite(Utiles.TexturasDir("moneda_sprite.png"), new Size(32, 32), 16, 10); AnimatedSprite.Position = new Vector2(GuiController.Instance.Panel3d.Width - 32 * 2, 0); Puntos2d = new TgcText2d(); Puntos2d.Text = Puntos.ToString(); Puntos2d.Color = Color.Yellow; Puntos2d.Align = TgcText2d.TextAlign.RIGHT; Puntos2d.Position = new Point(GuiController.Instance.Panel3d.Width - 32, 0); Puntos2d.Size = new Size(30, 20); Puntos2d.changeFont(new System.Drawing.Font("Sans-serif ", 15, FontStyle.Bold)); }
public static void Cargar() { for (int i = 0; i < 50; i++) { ParticleEmitter Emisor = new ParticleEmitter(Utiles.TexturasDir("lluvia.png"), Utiles.iAleatorio(50, 100)); float Viento = Utiles.fAleatorio(50, 100); Emisor.Speed = new Vector3(Viento, -Utiles.fAleatorio(50, 100), Viento); Emisor.Dispersion = Utiles.iAleatorio(10, 100); Emisor.MinSizeParticle = Utiles.fAleatorio(5, 10); Emisor.MaxSizeParticle = Utiles.fAleatorio(10, 30); Emisor.CreationFrecuency = Utiles.fAleatorio(0.01f, 0.5f); Emisor.ParticleTimeToLive = Utiles.fAleatorio(5, 10); Emisor.Distancia = Utiles.fAleatorio(0.5f, 1); Emisores.Add(Emisor); } }
/// <summary> /// Crea el SkyDome. /// </summary> public static void Cargar() { Textura = TextureLoader.FromCubeFile(GuiController.Instance.D3dDevice, Utiles.TexturasDir("cubemap-evul.dds")); VerticesBuffer = new VertexBuffer(typeof(CustomVertex.PositionNormalTextured), NivelDeDetalle * NivelDeDetalle * CustomVertex.PositionNormalTextured.StrideSize, GuiController.Instance.D3dDevice, Usage.WriteOnly, CustomVertex.PositionNormalTextured.Format, Pool.Default); Vertices = new CustomVertex.PositionNormalTextured[NivelDeDetalle * NivelDeDetalle]; for (int v = 0; v < NivelDeDetalle; v++) { for (int u = 0; u < NivelDeDetalle; u++) { // Alpha es el desplazamiento horizontal float al = (float)(-2.0f * Math.PI * ((float)u / (NivelDeDetalle - 1.0f))); // Theta es el desplazamiento vertical float th = (float)(0.6f * Math.PI * ((float)v / (NivelDeDetalle - 1.0f))); // Armo los vertices para el domo Vertices[v * NivelDeDetalle + u].X = (float)(Math.Sin(th) * Math.Sin(al)); Vertices[v * NivelDeDetalle + u].Y = (float)Math.Cos(th); Vertices[v * NivelDeDetalle + u].Z = (float)(Math.Sin(th) * Math.Cos(al)); } } VerticesBuffer.SetData(Vertices, 0, LockFlags.None); IndicesBuffer = new IndexBuffer(typeof(int), sizeof(int) * 6 * (NivelDeDetalle - 1) * (NivelDeDetalle - 1), GuiController.Instance.D3dDevice, Usage.WriteOnly, Pool.Default); int[] Indices = new int[sizeof(int) * 6 * (NivelDeDetalle - 1) * (NivelDeDetalle - 1)]; int i = 0; for (int v = 0; v < NivelDeDetalle - 1; v++) { for (int u = 0; u < NivelDeDetalle - 1; u++) { // Triangulo 1 |/ Indices[i++] = v * NivelDeDetalle + u; Indices[i++] = v * NivelDeDetalle + u + 1; Indices[i++] = (v + 1) * NivelDeDetalle + u; // Triangulo 2 /| Indices[i++] = (v + 1) * NivelDeDetalle + u; Indices[i++] = v * NivelDeDetalle + u + 1; Indices[i++] = (v + 1) * NivelDeDetalle + u + 1; } } IndicesBuffer.SetData(Indices, 0, LockFlags.None); // Carga el Effect para el SkyDome. Shader = Utiles.CargarShaderConTechnique("skybox.fx"); }
/// <summary> /// Metodo que carga los valores necesarios para inicializar el Oceano. /// </summary> public static void Cargar() { Device d3dDevice = GuiController.Instance.D3dDevice; // Creo la textura de reflexion surf_reflection = new Texture(d3dDevice, GuiController.Instance.Panel3d.Width, GuiController.Instance.Panel3d.Height, 1, Usage.RenderTarget, Format.A32B32G32R32F, Pool.Default); // Creo la textura de refraccion surf_refraction = new Texture(d3dDevice, GuiController.Instance.Panel3d.Width, GuiController.Instance.Panel3d.Height, 1, Usage.RenderTarget, Format.A32B32G32R32F, Pool.Default); // Cargo la textura de fresnel (relación entre los campos eléctricos transmitido y reflejado) "fresnel_water_sRGB.bmp" surf_fresnel = TextureLoader.FromFile(d3dDevice, Utiles.TexturasDir("fresnel_water_sRGB.bmp")); // Carga el shader del movimiento del oceano PerlinShader = Utiles.CargarShaderConTechnique("perlin.fx"); // Cargar informacion de vertices: (X,Y,Z) + coord textura _vertices = new CustomVertex.PositionNormalTextured[CANTIDAD_DE_VERTICES]; int i = 0; for (int x = -RADIO; x <= RADIO; x++) { for (int z = -RADIO; z <= RADIO; z++) { _vertices[i++] = new CustomVertex.PositionNormalTextured( new Vector3(x * DISTANCIA_ENTRE_VERTICES, ParametrosDeConfiguracion.Agua.NivelDelMar, z * DISTANCIA_ENTRE_VERTICES), _normal, ((float)(x + RADIO) / ((float)LARGO - 1)), ((float)(z + RADIO) / ((float)LARGO - 1)) ); } } ; // Creamos el VertexBuffer _vertexBuffer = new VertexBuffer(typeof(CustomVertex.PositionNormalTextured), CANTIDAD_DE_VERTICES, d3dDevice, Usage.Dynamic | Usage.WriteOnly, CustomVertex.PositionNormalTextured.Format, Pool.Default); // Almacenar información en VertexBuffer _vertexBuffer.SetData(_vertices, 0, LockFlags.None); //Creo el quadTree para este terreno QuadTree.Cargar(_pos.X, _pos.Z, TAMAÑO, GuiController.Instance.D3dDevice); // creo los indices para el IndexBuffer usando un array de int // Son por 3 vertices por triangulo y son 2 triangulos for (int z = 0; z < LARGO - 1; z++) { for (int x = 0; x < LARGO - 1; x++) { var lista = new List <int>(); //Primer Triangulo lista.Add(x + z * LARGO); lista.Add(x + 1 + z * LARGO); lista.Add(x + LARGO + 1 + z * LARGO); //Segundo Triangulo lista.Add(x + LARGO + 1 + z * LARGO); lista.Add(x + LARGO + z * LARGO); lista.Add(x + z * LARGO); //Cargo los indices en los nodos del QuadTree QuadTree.AgregarIndices(lista); } } ; //LOD I for (int z = 0; z < LARGO - 1; z = z + 2) { for (int x = 0; x < LARGO - 1; x = x + 2) { var lista = new List <int>(); //Primer Triangulo lista.Add(x + z * LARGO); lista.Add(x + 2 + z * LARGO); lista.Add(x + 2 * LARGO + 2 + z * LARGO); //Segundo Triangulo lista.Add(x + 2 * LARGO + 2 + z * LARGO); lista.Add(x + 2 * LARGO + z * LARGO); lista.Add(x + z * LARGO); //Cargo los indices en los nodos del QuadTree QuadTree.AgregarLODI(lista); } } ; //LOD II for (int z = 0; z < LARGO - 1; z = z + 4) { for (int x = 0; x < LARGO - 1; x = x + 4) { var lista = new List <int>(); //Primer Triangulo lista.Add(x + z * LARGO); lista.Add(x + 4 + z * LARGO); lista.Add(x + 4 * LARGO + 4 + z * LARGO); //Segundo Triangulo lista.Add(x + 4 * LARGO + 4 + z * LARGO); lista.Add(x + 4 * LARGO + z * LARGO); lista.Add(x + z * LARGO); //Cargo los indices en los nodos del QuadTree QuadTree.AgregarLODII(lista); } } ; // Genera los heightmaps entre los que interpola la superficie, se generan para 2,4 y 8 octavas para poder usar // las diferentes configuraciones cambiando los Modifiers. // 2 ocatavas (ruido fuerte). // perlin 1 textPerlinNoise1_2Octavas = PerlinNoise.GetNuevoHeightmap(Oceano.LARGO, Oceano.LARGO, 2, out PerlinNoise1_2Octavas); textPerlinNoise2_2Octavas = PerlinNoise.GetNuevoHeightmap(Oceano.LARGO, Oceano.LARGO, 2, out PerlinNoise2_2Octavas); // 4 ocatavas (ruido normal). textPerlinNoise1_4Octavas = PerlinNoise.GetNuevoHeightmap(Oceano.LARGO, Oceano.LARGO, 4, out PerlinNoise1_4Octavas); textPerlinNoise2_4Octavas = PerlinNoise.GetNuevoHeightmap(Oceano.LARGO, Oceano.LARGO, 4, out PerlinNoise2_4Octavas); // 8 octavas (ruido suave). textPerlinNoise1_8Octavas = PerlinNoise.GetNuevoHeightmap(Oceano.LARGO, Oceano.LARGO, 8, out PerlinNoise1_8Octavas); textPerlinNoise2_8Octavas = PerlinNoise.GetNuevoHeightmap(Oceano.LARGO, Oceano.LARGO, 8, out PerlinNoise2_8Octavas); // Carga los valores iniciales de la Matriz de Perlin Noise. PerlinNoise1 = PerlinNoise1_8Octavas; PerlinNoise2 = PerlinNoise2_8Octavas; // Carga los valores iniciales de la textura que se usara como Heightmap y Normalmap para la superficie del oceano. textPerlinNoise1 = textPerlinNoise1_8Octavas; textPerlinNoise2 = textPerlinNoise2_8Octavas; // Peso (alpha) para interpolar, usa el InterpoladorVaiven para ir alterandolo. interpoladorPerlinNoiseHeightmaps = new InterpoladorVaiven(); interpoladorPerlinNoiseHeightmaps.Min = 0; interpoladorPerlinNoiseHeightmaps.Max = 1; interpoladorPerlinNoiseHeightmaps.Speed = 0.5f; //guardar el stencil inicial //g_depthstencil = d3dDevice.DepthStencilSurface; }
/// <summary> /// Render del oceano y su shader. /// </summary> public static void Render() { if (!ParametrosDeConfiguracion.RenderOceano) { return; } Device d3dDevice = GuiController.Instance.D3dDevice; bool culling = ParametrosDeConfiguracion.VerFrustumCulling; if (culling) { // cargo la camara fps para hacer el culling en base al frustum de esa camara GuiController.Instance.ThirdPersonCamera.Enable = false; GuiController.Instance.FpsCamera.Enable = true; GuiController.Instance.FpsCamera.setCamera(Barco.mesh.Position, Barco.vDireccion * GuiController.Instance.Frustum.FarPlane.D); GuiController.Instance.CurrentCamera.updateCamera(); GuiController.Instance.CurrentCamera.updateViewMatrix(d3dDevice); GuiController.Instance.Frustum.updateVolume(d3dDevice.Transform.View, d3dDevice.Transform.Projection); } // Especificar formato de triangulos d3dDevice.VertexFormat = CustomVertex.PositionNormalTextured.Format; d3dDevice.SetStreamSource(0, _vertexBuffer, 0); // Almacenar información en IndexBuffer var listaIndices = QuadTree.IndiceVerticesVisibles(); var cantidadDeIndices = listaIndices.Count; if (cantidadDeIndices != 0) { var indexBuffer = new IndexBuffer(typeof(int), sizeof(int) * cantidadDeIndices, d3dDevice, Usage.Dynamic | Usage.WriteOnly, Pool.Default); indexBuffer.SetData(listaIndices.ToArray(), 0, LockFlags.None); d3dDevice.Indices = indexBuffer; } if (culling) { // vuelvo a cargar la camara en 3ra persona GuiController.Instance.ThirdPersonCamera.Enable = true; GuiController.Instance.FpsCamera.Enable = false; GuiController.Instance.CurrentCamera.updateCamera(); GuiController.Instance.CurrentCamera.updateViewMatrix(d3dDevice); } // Cargamos parametros en el shader // Matrices de transformacion para pasar de world a clip space PerlinShader.SetValue("world", GuiController.Instance.D3dDevice.Transform.World); PerlinShader.SetValue("view", GuiController.Instance.D3dDevice.Transform.View); PerlinShader.SetValue("proj", GuiController.Instance.D3dDevice.Transform.Projection); // Propiedades del Sol (posicion, shiness, strength) PerlinShader.SetValue("sun_vec", new Vector4(Sol.Posicion.X, Sol.Posicion.Y, Sol.Posicion.Z, 0.0f)); PerlinShader.SetValue("sun_shininess", 4 * ParametrosDeConfiguracion.Sol.Shininess); PerlinShader.SetValue("sun_strength", ParametrosDeConfiguracion.Sol.Strength); // Valores de Enviroment Mapping PerlinShader.SetValue("reflrefr_offset", ParametrosDeConfiguracion.Agua.ReflRefrOffset); PerlinShader.SetValue("LODbias", ParametrosDeConfiguracion.p_fLODbias); Vector3 posCamara = GuiController.Instance.CurrentCamera.getPosition(); PerlinShader.SetValue("view_position", new Vector4(posCamara.X, posCamara.Y, posCamara.Z, 1)); PerlinShader.SetValue("EnvironmentMap", SkyDome.Textura); PerlinShader.SetValue("FresnelMap", surf_fresnel); PerlinShader.SetValue("Refractionmap", surf_refraction); PerlinShader.SetValue("Reflectionmap", surf_reflection); // Dimensiones del Oceano PerlinShader.SetValue("radio", RADIO); PerlinShader.SetValue("largo", LARGO); PerlinShader.SetValue("dev", DISTANCIA_ENTRE_VERTICES); // periodo del día, para saber si reflejar el sol o la luna PerlinShader.SetValue("noche", ParametrosDeConfiguracion.EsDeNoche); // Caso particular para placas que no tengan texture lookup de // cualquier textura se usa el formato particular A32B32G32R32F. if (ParametrosDeConfiguracion.TexturaA32B32G32R32F) { textPerlinNoise1 = TextureLoader.FromFile(d3dDevice, Utiles.TexturasDir("PerlinNoiseHeightmap1.png"), 0, 0, 1, 0, Format.A32B32G32R32F, Pool.Managed, Filter.Point, Filter.Point, Color.Black.ToArgb()); textPerlinNoise2 = TextureLoader.FromFile(d3dDevice, Utiles.TexturasDir("PerlinNoiseHeightmap2.png"), 0, 0, 1, 0, Format.A32B32G32R32F, Pool.Managed, Filter.Point, Filter.Point, Color.Black.ToArgb()); } else { // Setea las texturas de acuerdo a las octavas seleccionadas en el combo. switch (ParametrosDeConfiguracion.Agua.Octavas) { case 2: textPerlinNoise1 = textPerlinNoise1_2Octavas; textPerlinNoise2 = textPerlinNoise2_2Octavas; PerlinNoise1 = PerlinNoise1_2Octavas; PerlinNoise2 = PerlinNoise2_2Octavas; break; case 4: textPerlinNoise1 = textPerlinNoise1_4Octavas; textPerlinNoise2 = textPerlinNoise2_4Octavas; PerlinNoise1 = PerlinNoise1_4Octavas; PerlinNoise2 = PerlinNoise2_4Octavas; break; case 8: textPerlinNoise1 = textPerlinNoise1_8Octavas; textPerlinNoise2 = textPerlinNoise2_8Octavas; PerlinNoise1 = PerlinNoise1_8Octavas; PerlinNoise2 = PerlinNoise2_8Octavas; break; } } // Texturas que representan los heightmaps PerlinShader.SetValue("perlinNoise1", textPerlinNoise1); PerlinShader.SetValue("perlinNoise2", textPerlinNoise2); // Parametros para el movimiento del oceano Desplazamiento += GuiController.Instance.ElapsedTime; PerlinShader.SetValue("desplazamiento", Desplazamiento); PerlinShader.SetValue("maxHeightSuperficial", ParametrosDeConfiguracion.Agua.AlturaSuperficieal); PerlinShader.SetValue("amplitud", ParametrosDeConfiguracion.Agua.Amplitud); PerlinShader.SetValue("frecuencia", ParametrosDeConfiguracion.Agua.Frecuencia); PerlinShader.SetValue("smallvalue", ParametrosDeConfiguracion.Agua.DistanciaEntreNormales); // Niebla PerlinShader.SetValue("FogActiva", ParametrosDeConfiguracion.Niebla); ColorValue fogcolor = Utiles.CamaraSumergida ? ColorValue.FromColor(ParametrosDeConfiguracion.Agua.Color) : ColorValue.FromColor(Color.DarkGray); PerlinShader.SetValue("FogColor", fogcolor); PerlinShader.SetValue("FogStart", Niebla.START); PerlinShader.SetValue("FogEnd", Niebla.END); // Colorear el oceano bajo la moneda Vector4 moneda_pos = new Vector4(Juego.Moneda.Position.X, Juego.Moneda.Position.Y, Juego.Moneda.Position.Z, 1); PerlinShader.SetValue("moneda_pos", moneda_pos); // Posicion de la isla para atenuacion de las olas PerlinShader.SetValue("isla_pos", new Vector4(Isla.Posicion.X, Isla.Posicion.Y, Isla.Posicion.Z, 1)); // Parametro de animacion del movimiento superficial // para interpolar entre los dos heightmaps de perlin noise interpoladorPerlinNoiseHeightmaps.update(); PerlinShader.SetValue("alpha", interpoladorPerlinNoiseHeightmaps.Current); // Aplico el effect if (ParametrosDeConfiguracion.Shader) { PerlinShader.Begin(FX.None); PerlinShader.BeginPass(0); } // Dibuja la superficie d3dDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, CANTIDAD_DE_VERTICES, 0, cantidadDeIndices / 3); // Fin del effect if (ParametrosDeConfiguracion.Shader) { PerlinShader.EndPass(); PerlinShader.End(); } }