private void loadgrid(int x) { // if already in memory, return if ((map[x].mesh != null) || (map[x].loading)) { return; } map[x].loading = true; LoadTerrainGrid lg = new LoadTerrainGrid(d3dDevice, map, x); Thread t = new Thread(new ThreadStart(lg.LoadGrid)); t.Start(); }
public Landscape(Device d3device) { matWorld.Translate(0f, 0f, 0f); d3dDevice = d3device; this.simple = Form1.terrainFlat; map = new MapMesh[81]; // Read data information from terrenoinfo.dat // so we can actually know which terrain will be shown // without loading the whole mesh into memory. /* for (int p1=0;p1<81;p1++) * { * LoadTerrainGrid lg2=new LoadTerrainGrid(d3dDevice,map,p1); * lg2.LoadGrid(); * } */ if (!simple) { StreamReader sr = new StreamReader(@"media\terrain\terrenoinfo.dat"); Matrix world = new Matrix(); for (int p = 0; p < 81; p++) { sr.ReadLine(); // read the grid number map[p].min.X = float.Parse(sr.ReadLine()); map[p].min.Y = float.Parse(sr.ReadLine()); map[p].min.Z = float.Parse(sr.ReadLine()); map[p].max.X = float.Parse(sr.ReadLine()); map[p].max.Y = float.Parse(sr.ReadLine()); map[p].max.Z = float.Parse(sr.ReadLine()); map[p].mesh = null; map[p].loading = false; map[p].mat = new Matrix(); map[p].mat.Translate((map[p].min.X + map[p].max.X) / 2f, (map[p].min.Y + map[p].max.Y) / 2f, (map[p].min.Z + map[p].max.Z) / 2f); map[p].center = new Vector3((map[p].min.X + map[p].max.X) / 2f, (map[p].min.Y + map[p].max.Y) / 2f, (map[p].min.Z + map[p].max.Z) / 2f); map[p].m_vecBoundsLocal = new Vector3[8]; map[p].m_vecBoundsLocal[0] = new Vector3(map[p].min.X, map[p].min.Y, map[p].min.Z); // xyz map[p].m_vecBoundsLocal[1] = new Vector3(map[p].max.X, map[p].min.Y, map[p].min.Z); // Xyz map[p].m_vecBoundsLocal[2] = new Vector3(map[p].min.X, map[p].max.Y, map[p].min.Z); // xYz map[p].m_vecBoundsLocal[3] = new Vector3(map[p].max.X, map[p].max.Y, map[p].min.Z); // XYz map[p].m_vecBoundsLocal[4] = new Vector3(map[p].min.X, map[p].min.Y, map[p].max.Z); // xyZ map[p].m_vecBoundsLocal[5] = new Vector3(map[p].max.X, map[p].min.Y, map[p].max.Z); // XyZ map[p].m_vecBoundsLocal[6] = new Vector3(map[p].min.X, map[p].max.Y, map[p].max.Z); // xYZ map[p].m_vecBoundsLocal[7] = new Vector3(map[p].max.X, map[p].max.Y, map[p].max.Z); // XYZ // transform local position to world coordinates map[p].m_vecBoundsWorld = new Vector3[8]; for (int i = 0; i < 8; i++) { map[p].m_vecBoundsWorld[i] = map[p].m_vecBoundsLocal[i]; // Vector3.TransformCoordinate( map[p].m_vecBoundsLocal[i], map[p].mat ); } // Determine planes of the bounding box map[p].m_planeBoundsWorld = new Plane[6]; map[p].m_planeBoundsWorld[0] = Plane.FromPoints(map[p].m_vecBoundsWorld[0], map[p].m_vecBoundsWorld[1], map[p].m_vecBoundsWorld[2]); // Near map[p].m_planeBoundsWorld[1] = Plane.FromPoints(map[p].m_vecBoundsWorld[6], map[p].m_vecBoundsWorld[7], map[p].m_vecBoundsWorld[5]); // Far map[p].m_planeBoundsWorld[2] = Plane.FromPoints(map[p].m_vecBoundsWorld[2], map[p].m_vecBoundsWorld[6], map[p].m_vecBoundsWorld[4]); // Left map[p].m_planeBoundsWorld[3] = Plane.FromPoints(map[p].m_vecBoundsWorld[7], map[p].m_vecBoundsWorld[3], map[p].m_vecBoundsWorld[5]); // Right map[p].m_planeBoundsWorld[4] = Plane.FromPoints(map[p].m_vecBoundsWorld[2], map[p].m_vecBoundsWorld[3], map[p].m_vecBoundsWorld[6]); // Top map[p].m_planeBoundsWorld[5] = Plane.FromPoints(map[p].m_vecBoundsWorld[1], map[p].m_vecBoundsWorld[0], map[p].m_vecBoundsWorld[4]); // Bottom map[p].box = Mesh.Box(d3dDevice, map[p].max.X - map[p].min.X, map[p].max.Y - map[p].min.Y, map[p].max.Z - map[p].min.Z); } sr.Close(); LoadTerrainGrid lg = new LoadTerrainGrid(d3dDevice, map, 32); lg.LoadGrid(32); lg.LoadGrid(41); lg.LoadGrid(21); lg.LoadGrid(31); lg.LoadGrid(33); lg.LoadGrid(34); lg.LoadGrid(40); lg.LoadGrid(42); lg.LoadGrid(43); lg.LoadGrid(39); lg.LoadGrid(20); lg.LoadGrid(22); lg.LoadGrid(23); } else { map[0].mesh = Mesh.FromFile(@"media\terrain\single.x", MeshFlags.SystemMemory, d3dDevice); // , out o, out materials); } if (Form1.smallTexture) { maptexture = TextureLoader.FromFile(d3dDevice, @"media\terrain\ps_texture_simple.jpg"); } else { maptexture = TextureLoader.FromFile(d3dDevice, @"media\terrain\ps_texture_4k.jpg"); } mapmaterial = new Material(); mapmaterial.Diffuse = mapmaterial.Ambient = System.Drawing.Color.FromArgb(149, 149, 149); }