private void SetupSurface(VertexPositionTextureNormalLightmap[] vertexdata, List<int> indexdata, int surface_id, int current_surface, int x, int y, int type) { int idx = current_surface * 4; int cell_idx = y * _width + x; Cell cell = _cells[cell_idx]; Surface surface = _surfaces[surface_id]; Vector3[] position = new Vector3[4]; Vector3[] normal = new Vector3[4]; switch (type) { case 0: { float x0 = (x - _width / 2) * _zoom; float x1 = (x - _width / 2 + 1) * _zoom; float z0 = (y - _height / 2) * _zoom; float z1 = (y - _height / 2 + 1) * _zoom; position[0] = new Vector3(x0, cell.Height[0], z0); position[1] = new Vector3(x1, cell.Height[1], z0); position[2] = new Vector3(x0, cell.Height[2], z1); position[3] = new Vector3(x1, cell.Height[3], z1); normal[0] = cell.Normal[1]; normal[1] = cell.Normal[2]; normal[2] = cell.Normal[3]; normal[3] = cell.Normal[4]; } break; case 1: { Cell cell2 = _cells[(y + 1) * _width + x]; float x0 = (x - _width / 2) * _zoom; float x1 = (x - _width / 2 + 1) * _zoom; float z0 = (y - _height / 2 + 1) * _zoom; position[0] = new Vector3(x0, cell.Height[2], z0); position[1] = new Vector3(x1, cell.Height[3], z0); position[2] = new Vector3(x0, cell2.Height[0], z0); position[3] = new Vector3(x1, cell2.Height[1], z0); normal[0] = new Vector3(0, 0, cell2.Height[0] > cell.Height[3] ? -1 : 1); normal[1] = normal[0]; normal[2] = normal[0]; normal[3] = normal[0]; } break; case 2: { Cell cell2 = _cells[y * _width + x + 1]; float x0 = (x - _width / 2 + 1) * _zoom; float z0 = (y - _height / 2) * _zoom; float z1 = (y - _height / 2 + 1) * _zoom; position[0] = new Vector3(x0, cell.Height[3], z1); position[1] = new Vector3(x0, cell.Height[1], z0); position[2] = new Vector3(x0, cell2.Height[2], z1); position[3] = new Vector3(x0, cell2.Height[0], z0); normal[0] = new Vector3(cell.Height[3] > cell2.Height[2] ? -1 : 1, 0, 0); normal[1] = normal[0]; normal[2] = normal[0]; normal[3] = normal[0]; } break; } int lm_w = (int)Math.Floor(Math.Sqrt(_lightmaps.Length)); int lm_h = (int)Math.Ceiling((float)_lightmaps.Length / lm_w); int lm_x = (int)Math.Floor((float)surface.Lightmap / lm_h); int lm_y = surface.Lightmap % lm_h; float[] lightmapU = new float[2]; float[] lightmapV = new float[2]; lightmapU[0] = (float)(0.1f + lm_x) / lm_w; lightmapU[1] = (float)(0.9f + lm_x) / lm_w; lightmapV[0] = (float)(0.1f + lm_y) / lm_h; lightmapV[1] = (float)(0.9f + lm_y) / lm_h; vertexdata[idx + 0] = new VertexPositionTextureNormalLightmap(position[0], normal[0], surface.TexCoord[0], new Vector2(lightmapU[0], lightmapV[0]), surface.Color); vertexdata[idx + 1] = new VertexPositionTextureNormalLightmap(position[1], normal[1], surface.TexCoord[1], new Vector2(lightmapU[1], lightmapV[0]), surface.Color); vertexdata[idx + 2] = new VertexPositionTextureNormalLightmap(position[2], normal[2], surface.TexCoord[2], new Vector2(lightmapU[0], lightmapV[1]), surface.Color); vertexdata[idx + 3] = new VertexPositionTextureNormalLightmap(position[3], normal[3], surface.TexCoord[3], new Vector2(lightmapU[1], lightmapV[1]), surface.Color); indexdata.Add(idx + 0); indexdata.Add(idx + 1); indexdata.Add(idx + 2); indexdata.Add(idx + 2); indexdata.Add(idx + 1); indexdata.Add(idx + 3); }
private void SetupSurface(VertexPositionTextureNormalLightmap[] vertexdata, List <int> indexdata, int surface_id, int current_surface, int x, int y, int type) { int idx = current_surface * 4; int cell_idx = y * _width + x; Cell cell = _cells[cell_idx]; Surface surface = _surfaces[surface_id]; Vector3[] position = new Vector3[4]; Vector3[] normal = new Vector3[4]; switch (type) { case 0: { float x0 = (x - _width / 2) * _zoom; float x1 = (x - _width / 2 + 1) * _zoom; float z0 = (y - _height / 2) * _zoom; float z1 = (y - _height / 2 + 1) * _zoom; position[0] = new Vector3(x0, cell.Height[0], z0); position[1] = new Vector3(x1, cell.Height[1], z0); position[2] = new Vector3(x0, cell.Height[2], z1); position[3] = new Vector3(x1, cell.Height[3], z1); normal[0] = cell.Normal[1]; normal[1] = cell.Normal[2]; normal[2] = cell.Normal[3]; normal[3] = cell.Normal[4]; } break; case 1: { Cell cell2 = _cells[(y + 1) * _width + x]; float x0 = (x - _width / 2) * _zoom; float x1 = (x - _width / 2 + 1) * _zoom; float z0 = (y - _height / 2 + 1) * _zoom; position[0] = new Vector3(x0, cell.Height[2], z0); position[1] = new Vector3(x1, cell.Height[3], z0); position[2] = new Vector3(x0, cell2.Height[0], z0); position[3] = new Vector3(x1, cell2.Height[1], z0); normal[0] = new Vector3(0, 0, cell2.Height[0] > cell.Height[3] ? -1 : 1); normal[1] = normal[0]; normal[2] = normal[0]; normal[3] = normal[0]; } break; case 2: { Cell cell2 = _cells[y * _width + x + 1]; float x0 = (x - _width / 2 + 1) * _zoom; float z0 = (y - _height / 2) * _zoom; float z1 = (y - _height / 2 + 1) * _zoom; position[0] = new Vector3(x0, cell.Height[3], z1); position[1] = new Vector3(x0, cell.Height[1], z0); position[2] = new Vector3(x0, cell2.Height[2], z1); position[3] = new Vector3(x0, cell2.Height[0], z0); normal[0] = new Vector3(cell.Height[3] > cell2.Height[2] ? -1 : 1, 0, 0); normal[1] = normal[0]; normal[2] = normal[0]; normal[3] = normal[0]; } break; } int lm_w = (int)Math.Floor(Math.Sqrt(_lightmaps.Length)); int lm_h = (int)Math.Ceiling((float)_lightmaps.Length / lm_w); int lm_x = (int)Math.Floor((float)surface.Lightmap / lm_h); int lm_y = surface.Lightmap % lm_h; float[] lightmapU = new float[2]; float[] lightmapV = new float[2]; lightmapU[0] = (float)(0.1f + lm_x) / lm_w; lightmapU[1] = (float)(0.9f + lm_x) / lm_w; lightmapV[0] = (float)(0.1f + lm_y) / lm_h; lightmapV[1] = (float)(0.9f + lm_y) / lm_h; vertexdata[idx + 0] = new VertexPositionTextureNormalLightmap(position[0], normal[0], surface.TexCoord[0], new Vector2(lightmapU[0], lightmapV[0]), surface.Color); vertexdata[idx + 1] = new VertexPositionTextureNormalLightmap(position[1], normal[1], surface.TexCoord[1], new Vector2(lightmapU[1], lightmapV[0]), surface.Color); vertexdata[idx + 2] = new VertexPositionTextureNormalLightmap(position[2], normal[2], surface.TexCoord[2], new Vector2(lightmapU[0], lightmapV[1]), surface.Color); vertexdata[idx + 3] = new VertexPositionTextureNormalLightmap(position[3], normal[3], surface.TexCoord[3], new Vector2(lightmapU[1], lightmapV[1]), surface.Color); indexdata.Add(idx + 0); indexdata.Add(idx + 1); indexdata.Add(idx + 2); indexdata.Add(idx + 2); indexdata.Add(idx + 1); indexdata.Add(idx + 3); }
public void SetupVertices() { objectCount = 0; for (int x = 0; x < _width; x++) { for (int y = 0; y < _height; y++) { int idx = y * _width + x; if (_cells[idx].TileUp != -1) objectCount++; if (_cells[idx].TileSide != -1) objectCount++; if (_cells[idx].TileOtherSide != -1) objectCount++; } } VertexPositionTextureNormalLightmap[] vertexdata = new VertexPositionTextureNormalLightmap[objectCount * 4]; List<int>[] indexdata = new List<int>[_textures.Length]; for (int i = 0; i < indexdata.Length; i++) { indexdata[i] = new List<int>(); } int cur_surface = 0; for (int x = 0; x < _width; x++) { for (int y = 0; y < _height; y++) { int idx = y * _width + x; if (_cells[idx].TileUp != -1) { int tid = _surfaces[_cells[idx].TileUp].Texture; SetupSurface(vertexdata, indexdata[tid], _cells[idx].TileUp, cur_surface, x, y, 0); cur_surface++; } if (_cells[idx].TileSide != -1) { int tid = _surfaces[_cells[idx].TileSide].Texture; SetupSurface(vertexdata, indexdata[tid], _cells[idx].TileSide, cur_surface, x, y, 1); cur_surface++; } if (_cells[idx].TileOtherSide != -1) { int tid = _surfaces[_cells[idx].TileOtherSide].Texture; SetupSurface(vertexdata, indexdata[tid], _cells[idx].TileOtherSide, cur_surface, x, y, 2); cur_surface++; } } } _vertices = new VertexBuffer(_graphicsDevice, typeof(VertexPositionTextureNormalLightmap), vertexdata.Length, BufferUsage.WriteOnly); _vertices.SetData(vertexdata); _indexes = new IndexBuffer[_textures.Length]; for (int i = 0; i < _indexes.Length; i++) { _indexes[i] = new IndexBuffer(_graphicsDevice, typeof(int), indexdata[i].Count, BufferUsage.WriteOnly); _indexes[i].SetData(indexdata[i].ToArray()); } }
public void SetupVertices() { objectCount = 0; for (int x = 0; x < _width; x++) { for (int y = 0; y < _height; y++) { int idx = y * _width + x; if (_cells[idx].TileUp != -1) { objectCount++; } if (_cells[idx].TileSide != -1) { objectCount++; } if (_cells[idx].TileOtherSide != -1) { objectCount++; } } } VertexPositionTextureNormalLightmap[] vertexdata = new VertexPositionTextureNormalLightmap[objectCount * 4]; List <int>[] indexdata = new List <int> [_textures.Length]; for (int i = 0; i < indexdata.Length; i++) { indexdata[i] = new List <int>(); } int cur_surface = 0; for (int x = 0; x < _width; x++) { for (int y = 0; y < _height; y++) { int idx = y * _width + x; if (_cells[idx].TileUp != -1) { int tid = _surfaces[_cells[idx].TileUp].Texture; SetupSurface(vertexdata, indexdata[tid], _cells[idx].TileUp, cur_surface, x, y, 0); cur_surface++; } if (_cells[idx].TileSide != -1) { int tid = _surfaces[_cells[idx].TileSide].Texture; SetupSurface(vertexdata, indexdata[tid], _cells[idx].TileSide, cur_surface, x, y, 1); cur_surface++; } if (_cells[idx].TileOtherSide != -1) { int tid = _surfaces[_cells[idx].TileOtherSide].Texture; SetupSurface(vertexdata, indexdata[tid], _cells[idx].TileOtherSide, cur_surface, x, y, 2); cur_surface++; } } } _vertices = new VertexBuffer(_graphicsDevice, typeof(VertexPositionTextureNormalLightmap), vertexdata.Length, BufferUsage.WriteOnly); _vertices.SetData(vertexdata); _indexes = new IndexBuffer[_textures.Length]; for (int i = 0; i < _indexes.Length; i++) { _indexes[i] = new IndexBuffer(_graphicsDevice, typeof(int), indexdata[i].Count, BufferUsage.WriteOnly); _indexes[i].SetData(indexdata[i].ToArray()); } }