Example 2: Fournier, Fussell, Carpenter algorithm modified to generate seamless clouds (using non-content data).

vb.net Code:
  1. Public Class Game1
  2.     Inherits Microsoft.Xna.Framework.Game
  3.  
  4.     Private WithEvents graphics As GraphicsDeviceManager
  5.     Private WithEvents spriteBatch As SpriteBatch
  6.  
  7.     Private mytexture As Texture2D 'Modified texturename.
  8.  
  9.     Public Sub New()
  10.         graphics = New GraphicsDeviceManager(Me)
  11.         Content.RootDirectory = "Content"
  12.     End Sub
  13.  
  14.     Protected Overrides Sub Initialize()
  15.         MyBase.Initialize()
  16.         IsMouseVisible = True
  17.         Window.AllowUserResizing = True
  18.         Window.Title = "Example 2 (Fournier, Fussell, Carpenter modified to generate seamless clouds)" 'New title
  19.  
  20.         Dim depth As Integer = 10             'The depth to which the FFC-algorithm will go (2 ^ 10 = 1024)
  21.         Dim dimension As Integer = 1 << depth 'The dimension of the resulting bitmap (1024 x 1024).
  22.  
  23.         mytexture = New Texture2D(GraphicsDevice, dimension, dimension) 'Declare a new texture and initialize it.
  24.         mytexture.SetData(FournierFussellCarpenterNoise.Generate(depth, 1.0F, Color.Blue, Color.White, Color.DarkGray))
  25.     End Sub
  26.  
  27.     Protected Overrides Sub LoadContent()
  28.         MyBase.LoadContent()
  29.         spriteBatch = New SpriteBatch(GraphicsDevice)
  30.     End Sub
  31.  
  32.     Protected Overrides Sub UnloadContent()
  33.         MyBase.UnloadContent()
  34.         mytexture.Dispose() 'Dispose of the texture.
  35.     End Sub
  36.  
  37.     Protected Overrides Sub Update(ByVal gameTime As GameTime)
  38.         If Keyboard.GetState.IsKeyDown(Keys.Escape) Then Me.Exit()
  39.  
  40.         MyBase.Update(gameTime)
  41.     End Sub
  42.  
  43.     Protected Overrides Sub Draw(ByVal gameTime As GameTime)
  44.         GraphicsDevice.Clear(Color.Black)
  45.  
  46.         spriteBatch.Begin()
  47.         spriteBatch.Draw(mytexture, Vector2.Zero, Color.White) 'Draw the texture.
  48.         spriteBatch.End()
  49.         MyBase.Draw(gameTime)
  50.     End Sub
  51. End Class

A new class called FournierFussellCarpenterNoise (create this class in the main project and copy/paste the code):
vb.net Code:
  1. Public Class FournierFussellCarpenterNoise
  2.  
  3.     Private Shared rnd As New Random()
  4.  
  5.     Public Shared Function Generate(depth As Integer, roughness As Single, c1 As Color, c2 As Color, c3 As Color) As Color()
  6.  
  7.         Dim wh As Integer = 1 << depth 'Actual supposed dimension of the heightmap
  8.         Dim heightmap(wh - 1, wh - 1) As Single
  9.         Dim rval(wh * wh - 1) As Color
  10.         Dim k As Integer = 0
  11.  
  12.         Dim linecount As Integer = 1
  13.         Dim stepsize As Integer = 1 << (depth - 1)
  14.         Dim x, y As Integer
  15.         Dim xa, xb, xc, ya, yb, yc As Integer
  16.  
  17.         'Actually sets the 4 corner-values to roughness * [0.8; 0.9[, since the texture is seamlessly implemented.
  18.         heightmap(0, 0) = roughness * (Convert.ToSingle(rnd.NextDouble()) / 10.0F + 0.8F)
  19.  
  20.         While stepsize > 0
  21.  
  22.             'The square step------------------------------------------------------
  23.             y = stepsize
  24.  
  25.             For i As Integer = 1 To linecount
  26.  
  27.                 x = stepsize
  28.  
  29.                 ya = y + stepsize
  30.                 yb = y - stepsize
  31.                 yc = y
  32.  
  33.                 If ya < 0 Then ya += wh
  34.                 If ya >= wh Then ya -= wh
  35.                 If yb < 0 Then yb += wh
  36.                 If yb >= wh Then yb -= wh
  37.                 If yc >= wh Then yc -= wh
  38.  
  39.                 For j As Integer = 1 To linecount
  40.  
  41.                     xa = x + stepsize
  42.                     xb = x - stepsize
  43.                     xc = x
  44.  
  45.                     If xa < 0 Then xa += wh
  46.                     If xa >= wh Then xa -= wh
  47.                     If xb < 0 Then xb += wh
  48.                     If xb >= wh Then xb -= wh
  49.                     If xc >= wh Then xc -= wh
  50.  
  51.                     heightmap(xc, yc) = (heightmap(xa, ya) + heightmap(xa, yb) + _
  52.                                          heightmap(xb, ya) + heightmap(xb, yb)) / 4.0F + _
  53.                         roughness * Convert.ToSingle(rnd.NextDouble() - 0.5)
  54.  
  55.                     x += 2 * stepsize
  56.  
  57.                 Next
  58.  
  59.                 y += 2 * stepsize
  60.  
  61.             Next
  62.  
  63.             'The diamond step-------------------------------------------------------
  64.             For i As Integer = 1 To linecount
  65.  
  66.                 x = 0
  67.                 y = stepsize * (2 * i - 1)
  68.  
  69.                 While y <= wh
  70.  
  71.                     xa = x + stepsize
  72.                     xb = x - stepsize
  73.                     xc = x
  74.                     ya = y + stepsize
  75.                     yb = y - stepsize
  76.                     yc = y
  77.  
  78.                     If xa < 0 Then xa += wh
  79.                     If xa >= wh Then xa -= wh
  80.                     If xb < 0 Then xb += wh
  81.                     If xb >= wh Then xb -= wh
  82.                     If xc >= wh Then xc -= wh
  83.                     If ya < 0 Then ya += wh
  84.                     If ya >= wh Then ya -= wh
  85.                     If yb < 0 Then yb += wh
  86.                     If yb >= wh Then yb -= wh
  87.                     If yc >= wh Then yc -= wh
  88.  
  89.                     heightmap(xc, yc) = (heightmap(xc, ya) + heightmap(xc, yb) + _
  90.                                          heightmap(xa, yc) + heightmap(xb, yc)) / 4.0F + _
  91.                         roughness * Convert.ToSingle(rnd.NextDouble() - 0.5)
  92.  
  93.                     heightmap(yc, xc) = (heightmap(yc, xa) + heightmap(yc, xb) + _
  94.                                          heightmap(ya, xc) + heightmap(yb, xc)) / 4.0F + _
  95.                         roughness * Convert.ToSingle(rnd.NextDouble() - 0.5)
  96.  
  97.                     x += stepsize
  98.                     y += stepsize
  99.  
  100.                 End While
  101.  
  102.             Next
  103.  
  104.             linecount *= 2
  105.             stepsize \= 2
  106.             roughness /= 2
  107.  
  108.         End While
  109.  
  110.         'Conversion to bitmap
  111.         For i As Integer = 0 To wh - 1
  112.             For j As Integer = 0 To wh - 1
  113.  
  114.                 Dim v As Single = heightmap(i, j)
  115.  
  116.                 If v < 0.0F Then v = 0.0F
  117.                 If v > 1.0F Then 'Anything above 1.0 will interpolate colors c2 and c3
  118.                     rval(k) = Color.Lerp(c2, c3, v - 1.0F)
  119.                 ElseIf v > 0.6F Then 'Anything between 0.6 and 1.0 will interpolate c1 and c2
  120.                     rval(k) = Color.Lerp(c1, c2, (v - 0.6F) * 2.5F)
  121.                 Else 'Anything below 0.6 will be c1 (color of sky
  122.                     rval(k) = c1
  123.                 End If
  124.  
  125.                 k += 1
  126.             Next
  127.         Next
  128.  
  129.         Return rval
  130.  
  131.     End Function
  132.  
  133. End Class

You need not focus on the class FournierFussellCarpenterNoise and how it works. If interested, the algorithm is discribed in detail here: link, but for the purpose of this example it is not neccessary to understand it. Suffice to say, I modified it somewhat to generate seamless textures (by using Mod operations on indices). It is used here to display how to initialize textures using SetData and properly dispose unmanaged data. If you look at the Game1 class, you will notice, that the texture holding the generated image is initialized through its constructor and 'drawn' by suppplying an array of colors fitting the dimensions of the texture (ie. length of array is width * height of texture). At program termination, the texture is disposed in the UnloadContent method.
Feel free at this point to experiment with the generation (ie. supply various colors and/or roughness) or skip ahead to example 2b, if you don't really care about the algorithm.