private bool LoadColourMap(SharpDX.Direct3D11.Device device, string colorMapFilename) { Bitmap colourBitMap; try { // Open the color map file in binary. colourBitMap = new Bitmap(DSystemConfiguration.DataFilePath + colorMapFilename); } catch { return(false); } // This check is optional. // Make sure the color map dimensions are the same as the terrain dimensions for easy 1 to 1 mapping. m_ColourMapWidth = colourBitMap.Width; m_ColourMapHeight = colourBitMap.Height; if ((m_ColourMapWidth != m_TerrainWidth) || (m_ColourMapHeight != m_TerrainHeight)) { return(false); } // Calculate the size of the bitmap image data. int colourImageSize = m_ColourMapWidth * m_ColourMapHeight * 3; // Read the image data into the color map portion of the height map structure. int index; for (int j = 0; j < m_ColourMapHeight; j++) { for (int i = 0; i < m_ColourMapWidth; i++) { index = (m_ColourMapHeight * j) + i; DHeightMapType tempCopy = HeightMap[index]; tempCopy.r = colourBitMap.GetPixel(i, j).R / 255.0f; // 117.75f; //// 0.678431392 tempCopy.g = colourBitMap.GetPixel(i, j).G / 255.0f; //117.75f; // 0.619607866 tempCopy.b = colourBitMap.GetPixel(i, j).B / 255.0f; // 117.75f; // 0.549019635 HeightMap[index] = tempCopy; } } // Release the bitmap image data. colourBitMap.Dispose(); colourBitMap = null; return(true); }
private bool LoadMaterialMap(string materialMapFilename) { Bitmap materialBitMap; try { // Open the color map file in binary. materialBitMap = new Bitmap(DSystemConfiguration.DataFilePath + materialMapFilename); } catch { return(false); } // Make sure the material index map dimensions are the same as the terrain dimensions for 1 to 1 mapping. if ((materialBitMap.Width != m_TerrainWidth) || (materialBitMap.Height != m_TerrainHeight)) { return(false); } // Initialize the position in the image data buffer so each vertice has an material index associated with it. // Read the material index data into the height map structure. int index; for (int j = 0; j < m_TerrainHeight; j++) { for (int i = 0; i < m_TerrainWidth; i++) { index = (m_TerrainHeight * j) + i; DHeightMapType tempCopy = HeightMap[index]; tempCopy.rIndex = (int)materialBitMap.GetPixel(i, j).R; tempCopy.gIndex = (int)materialBitMap.GetPixel(i, j).G; tempCopy.bIndex = (int)materialBitMap.GetPixel(i, j).B; HeightMap[index] = tempCopy; } } // Release the bitmap image data. materialBitMap.Dispose(); materialBitMap = null; return(true); }
private bool CalculateNormals() { // Create a temporary array to hold the un-normalized normal vectors. int index; float length; Vector3 vertex1, vertex2, vertex3, vector1, vector2, sum; DVectorTypeShareNormal[] normals = new DVectorTypeShareNormal[(m_TerrainHeight - 1) * (m_TerrainWidth - 1)]; // Go through all the faces in the mesh and calculate their normals. for (int j = 0; j < (m_TerrainHeight - 1); j++) { for (int i = 0; i < (m_TerrainWidth - 1); i++) { int index1 = (j * m_TerrainHeight) + i; int index2 = (j * m_TerrainHeight) + (i + 1); int index3 = ((j + 1) * m_TerrainHeight) + i; // Get three vertices from the face. vertex1.X = HeightMap[index1].x; vertex1.Y = HeightMap[index1].y; vertex1.Z = HeightMap[index1].z; vertex2.X = HeightMap[index2].x; vertex2.Y = HeightMap[index2].y; vertex2.Z = HeightMap[index2].z; vertex3.X = HeightMap[index3].x; vertex3.Y = HeightMap[index3].y; vertex3.Z = HeightMap[index3].z; // Calculate the two vectors for this face. vector1 = vertex1 - vertex3; vector2 = vertex3 - vertex2; index = (j * (m_TerrainHeight - 1)) + i; // Calculate the cross product of those two vectors to get the un-normalized value for this face normal. Vector3 vecTestCrossProduct = Vector3.Cross(vector1, vector2); normals[index].x = vecTestCrossProduct.X; normals[index].y = vecTestCrossProduct.Y; normals[index].z = vecTestCrossProduct.Z; } } // Now go through all the vertices and take an average of each face normal // that the vertex touches to get the averaged normal for that vertex. for (int j = 0; j < m_TerrainHeight; j++) { for (int i = 0; i < m_TerrainWidth; i++) { // Initialize the sum. sum = Vector3.Zero; // Initialize the count. int count = 9; // Bottom left face. if (((i - 1) >= 0) && ((j - 1) >= 0)) { index = ((j - 1) * (m_TerrainHeight - 1)) + (i - 1); sum[0] += normals[index].x; sum[1] += normals[index].y; sum[2] += normals[index].z; count++; } // Bottom right face. if ((i < (m_TerrainWidth - 1)) && ((j - 1) >= 0)) { index = ((j - 1) * (m_TerrainHeight - 1)) + i; sum[0] += normals[index].x; sum[1] += normals[index].y; sum[2] += normals[index].z; count++; } // Upper left face. if (((i - 1) >= 0) && (j < (m_TerrainHeight - 1))) { index = (j * (m_TerrainHeight - 1)) + (i - 1); sum[0] += normals[index].x; sum[1] += normals[index].y; sum[2] += normals[index].z; count++; } // Upper right face. if ((i < (m_TerrainWidth - 1)) && (j < (m_TerrainHeight - 1))) { index = (j * (m_TerrainHeight - 1)) + i; sum.X += normals[index].x; sum.Y += normals[index].y; sum.Z += normals[index].z; count++; } // Take the average of the faces touching this vertex. sum.X = (sum.X / (float)count); sum.Y = (sum.Y / (float)count); sum.Z = (sum.Z / (float)count); // Calculate the length of this normal. length = (float)Math.Sqrt((sum.X * sum.X) + (sum.Y * sum.Y) + (sum.Z * sum.Z)); // Get an index to the vertex location in the height map array. index = (j * m_TerrainHeight) + i; // Normalize the final shared normal for this vertex and store it in the height map array. DHeightMapType editHeightMap = HeightMap[index]; editHeightMap.nx = (sum.X / length); editHeightMap.ny = (sum.Y / length); editHeightMap.nz = (sum.Z / length); HeightMap[index] = editHeightMap; } } // Release the temporary normals. normals = null; return(true); }