using System; using System.Windows.Forms; using Microsoft.DirectX.Direct3D; namespace blue_dish_te { /// /// Objekt pro vykreslovani sten. Objekt si vytvori VertexBuffer a naplni jej vrcholy. /// Metoda Render vykresli buffer do zarizeni device (parametr konstruktoru) /// internal class Stena { private Device device; private VertexBuffer va = null; private VertexBuffer vb = null; private VertexBuffer vc = null; private VertexBuffer vd = null; private Texture orig, my_a, my_b, my_c, my_n; public char type = '*'; public int wallDirection = 0; /// /// Konstruktor vytvori VertexBuffer a naplni ho souradnicemi /// /// Reference na device do ktereho se bude kreslit public Stena(Device dev) { device = dev; //nacteni textury try { orig = TextureLoader.FromFile(device, "textury/stena.jpg"); my_a = TextureLoader.FromFile(device, "textury/my/myzed_a.jpg"); my_b = TextureLoader.FromFile(device, "textury/my/myzed_b.jpg"); my_c = TextureLoader.FromFile(device, "textury/my/myzed_c.jpg"); my_n = TextureLoader.FromFile(device, "textury/my/myzed_num.jpg"); } catch (Exception e) { MessageBox.Show("Nepovedlo se nacist textury pro steny."); Console.WriteLine(e); return; } CustomVertex.PositionNormalTextured[] v; // vytvoreni stran krychle va = new VertexBuffer(typeof(CustomVertex.PositionNormalTextured), 5, device, Usage.WriteOnly, CustomVertex.PositionNormalTextured.Format, Pool.Managed); v = (CustomVertex.PositionNormalTextured[]) va.Lock(0, 0); //naplneni VB souradnicemi vrcholu x y z nx ny nz u v v[0] = new CustomVertex.PositionNormalTextured(2, 0, 0, 0, 0,-1, 1, 1); v[1] = new CustomVertex.PositionNormalTextured(0, 0, 0, 0, 0,-1, 0, 1); v[2] = new CustomVertex.PositionNormalTextured(2, 3, 0, 0, 0,-1, 1, 0); v[3] = new CustomVertex.PositionNormalTextured(0, 0, 0, 0, 0,-1, 0, 1); v[4] = new CustomVertex.PositionNormalTextured(0, 3, 0, 0, 0,-1, 0, 0); va.Unlock(); //odemknuti VB vb = new VertexBuffer(typeof(CustomVertex.PositionNormalTextured), 5, device, Usage.WriteOnly, CustomVertex.PositionNormalTextured.Format, Pool.Managed); v = (CustomVertex.PositionNormalTextured[]) vb.Lock(0, 0); //naplneni VB souradnicemi vrcholu x y z nx ny nz u v v[0] = new CustomVertex.PositionNormalTextured(0, 0, 0,-1, 0, 0, 1, 1); v[1] = new CustomVertex.PositionNormalTextured(0, 0, 2,-1, 0, 0, 0, 1); v[2] = new CustomVertex.PositionNormalTextured(0, 3, 0,-1, 0, 0, 1, 0); v[3] = new CustomVertex.PositionNormalTextured(0, 0, 2,-1, 0, 0, 0, 1); v[4] = new CustomVertex.PositionNormalTextured(0, 3, 2,-1, 0, 0, 0, 0); vb.Unlock(); //odemknuti VB vc = new VertexBuffer(typeof(CustomVertex.PositionNormalTextured), 5, device, Usage.WriteOnly, CustomVertex.PositionNormalTextured.Format, Pool.Managed); v = (CustomVertex.PositionNormalTextured[]) vc.Lock(0, 0); //naplneni VB souradnicemi vrcholu x y z nx ny nz u v v[0] = new CustomVertex.PositionNormalTextured(0, 0, 2, 0, 0, 1, 1, 1); v[1] = new CustomVertex.PositionNormalTextured(2, 0, 2, 0, 0, 1, 0, 1); v[2] = new CustomVertex.PositionNormalTextured(0, 3, 2, 0, 0, 1, 1, 0); v[3] = new CustomVertex.PositionNormalTextured(2, 0, 2, 0, 0, 1, 0, 1); v[4] = new CustomVertex.PositionNormalTextured(2, 3, 2, 0, 0, 1, 0, 0); vc.Unlock(); //odemknuti VB vd = new VertexBuffer(typeof(CustomVertex.PositionNormalTextured), 5, device, Usage.WriteOnly, CustomVertex.PositionNormalTextured.Format, Pool.Managed); v = (CustomVertex.PositionNormalTextured[]) vd.Lock(0, 0); //naplneni VB souradnicemi vrcholu x y z nx ny nz u v v[0] = new CustomVertex.PositionNormalTextured(2, 0, 2, 1, 0, 0, 1, 1); v[1] = new CustomVertex.PositionNormalTextured(2, 0, 0, 1, 0, 0, 0, 1); v[2] = new CustomVertex.PositionNormalTextured(2, 3, 2, 1, 0, 0, 1, 0); v[3] = new CustomVertex.PositionNormalTextured(2, 0, 0, 1, 0, 0, 0, 1); v[4] = new CustomVertex.PositionNormalTextured(2, 3, 0, 1, 0, 0, 0, 0); vd.Unlock(); //odemknuti VB } /// /// Vykresleni VertexBufferu jako trojuhelnikoveho stripu /// public void Render() { // nastaveni textury switch(type) { case 'a': device.SetTexture(0, my_a); break; case 'b': device.SetTexture(0, my_b); break; case 'c': device.SetTexture(0, my_c); break; case 'n': device.SetTexture(0, my_n); break; default: device.SetTexture(0, orig); break; } device.VertexFormat = CustomVertex.PositionNormalTextured.Format; // Nastaveni dat (streamu) vx, kde x je a, b, c, nebo d: // z // ^ +c+ // | b d // | +a+ // +----------> x // // nakreslime primitivy if ((wallDirection & 1) == 1) { device.SetStreamSource(0, va, 0); device.DrawPrimitives(PrimitiveType.TriangleStrip, 0, 3); } if ((wallDirection & 2) == 2) { device.SetStreamSource(0, vb, 0); device.DrawPrimitives(PrimitiveType.TriangleStrip, 0, 3); } if ((wallDirection & 4) == 4) { device.SetStreamSource(0, vc, 0); device.DrawPrimitives(PrimitiveType.TriangleStrip, 0, 3); } if ((wallDirection & 8) == 8) { device.SetStreamSource(0, vd, 0); device.DrawPrimitives(PrimitiveType.TriangleStrip, 0, 3); } } } }