public HeightmapSample[] GenerateHeightmapSamples(HeightmapDefinition definition) { var samples = new HeightmapSample[definition.GridSize * definition.GridSize]; for (int row = 0; row < definition.GridSize; row++) { for (int column = 0; column < definition.GridSize; column++) { var sample = GetSampleInPlanetSpace(definition, column, row); samples[row * definition.GridSize + column] = sample; } } return(samples); }
VertexPositionNormalColor GetVertexInMeshSpace(HeightmapSample heightmapSample, int column, int row) { // We have two different reference frames to deal with here: // "Planet space" coordinates which is the coordinates of the vertex in real units relative to the planet center // "Mesh space" coordinates which is the coordinates of the vertex in real units after the center point of the mesh // has been translated to the center of the planet // We want to build a mesh where the center of the mesh is at 0,0 but the vertices are sphere-projected as though // they were out in their correct place in the sphere. Then we want to keep track of the mesh's real-world location // so we can do a camera-relative translation for rendering. // We start with a heightmap height and vector which is in planet space. We translate the vector "downward" by the // radius of the planet so that a zero-height point in the exact middle of the mesh surface would be at the origin. var meshSpaceVector = ConvertToMeshSpace(heightmapSample.Vector); var vertexColor = _terrainColorizer.GetColor(heightmapSample.Height, column, row, _gridSize, _extents); return(new VertexPositionNormalColor { Position = meshSpaceVector, Color = vertexColor }); }