Search Home Members Contacts
About Us
Products
Downloads
Community
Support
Pages: [1]
  Print  
Author Topic: Procedural Terrain Heightmap Generation with Erosion  (Read 8410 times)
Shadowsong
Customers
Community Member
*****
Posts: 328


« on: July 07, 2008, 03:12:47 PM »

Ok this is a full working solution for

Procedural Heightmap Generation including Erosion

This is far more than pure Perlin Noise or the like. It allows you to create not only natural-looking but even playable terrains. I think many people can use this for auto-terrain-generation in map editors or even use it instead of a map editor.

The code was originally written by Mietze in C++ and I simply converted it to VB.Net.
The original thread discussing the whole idea is here: http://www.truevision3d.com/forums/tv3d_sdk_65/heightmap_generator-t17991.0.html

This code works so far, however, making good use of the parameters requires understanding and lots of testing. I suppose you to read the above thread, or to be more precise this paper which explaines all of the nice algorithms: http://oddlabs.com/download/terrain_generation.pdf

I also have to say that I can't really feature support about this. If you are having problems, Mietze may help you. (Damn he should have posted this great code sometime ago  Cheesy )

Here are the states of the heightmap during the calculation (from Mietze's Studio's website, which is in German, see discussion thread for more detail):

Simple noise:


Advanced Voronoi diagram:


Combination of both:


Advanced perturbation:


Thermal and hydraulic erosion:


Render of final landscape:

Logged
Shadowsong
Customers
Community Member
*****
Posts: 328


« Reply #1 on: July 07, 2008, 03:14:32 PM »

First: the Channel class:

Code:
Imports MTV3D65

Public Class cChannel

#Region " Declarations "
    Private Randomizer As Random = New Random()
    Private PixelArray(,) As Single
    Private Size As Integer

    Public Structure tCELL
        Public x As Single
        Public y As Single
        Public color1 As Single
        Public color2 As Single
        Public distanceToMapCenter As Single
    End Structure

    Public Structure tDISTANCE_BUFFER
        Public distance As Single
        Public cell As Integer
    End Structure

#End Region

#Region " New "
    Public Sub New(ByVal nSize As Integer)
        Size = nSize
        ReDim PixelArray(nSize + 1, nSize + 1)
    End Sub
#End Region

#Region " Functions & Properties "

    Public Sub SetPixel(ByVal x As Integer, ByVal y As Integer, ByVal pixel As Single)
        PixelArray(x, y) = pixel
    End Sub

    '#########################################################################

    Public Sub FillWith(ByVal pixel As Single)
        For x = 0 To Size - 1
            For y = 0 To Size - 1
                PixelArray(x, y) = pixel
            Next
        Next
    End Sub

    '#########################################################################

    Public Sub SetPixelWrap(ByRef x As Integer, ByRef y As Integer, ByVal pixel As Single)
        WrapCoordinates(x, y)
        PixelArray(x, y) = pixel
    End Sub

    '#########################################################################

    Public Sub WrapCoordinates(ByRef x As Integer, ByRef y As Integer)
        If (x < 0) Then x *= -1

        If (y < 0) Then y *= -1

        If (x >= Size) Then x = Size - (x Mod Size) - 1

        If (y >= Size) Then y = Size - (y Mod Size) - 1
    End Sub

    '#########################################################################

    Public Sub GetColorLimits(ByRef min As Single, ByRef max As Single)
        For x = 0 To Size - 1
            For y = 0 To Size - 1

                If (PixelArray(x, y) < min) Then
                    min = PixelArray(x, y)
                ElseIf (PixelArray(x, y) > max) Then
                    max = PixelArray(x, y)
                End If
            Next
        Next
    End Sub

    '#########################################################################

    Public ReadOnly Property GetSize() As Single
        Get
            Return Size
        End Get
    End Property

    '#########################################################################

    Public ReadOnly Property GetPixel(ByVal x As Integer, ByVal y As Integer) As Single
        Get
            Return PixelArray(x, y)
        End Get
    End Property

    '#########################################################################

    Public Function GetPixelWrap(ByRef x As Integer, ByRef y As Integer) As Single
        WrapCoordinates(x, y)
        Return PixelArray(x, y)
    End Function

    '#########################################################################

    Public ReadOnly Property GetPixelRGBA(ByVal x As Integer, ByVal y As Integer) As Integer
        Get
            Return Globals.RGBA(PixelArray(x, y), PixelArray(x, y), PixelArray(x, y), 1.0F)
        End Get
    End Property

    '#########################################################################

    Public Function InterpolateRGB(ByVal x As TV_COLOR, ByVal y As TV_COLOR, ByVal s As Single) As TV_COLOR
        Dim n As TV_COLOR = New TV_COLOR()

        n.r = x.r + s * (y.r - x.r)
        n.g = x.g + s * (y.g - x.g)
        n.b = x.b + s * (y.b - x.b)

        Return n
    End Function

    '#########################################################################

    Public Function InterpolateLinear(ByVal x0 As Single, ByVal x1 As Single, ByVal s As Single) As Single
        Return x0 + s * (x1 - x0)
    End Function

    '#########################################################################

    Public Function RGBA2TV3D(ByVal r As Integer, ByVal g As Integer, ByVal b As Integer, ByVal a As Integer) As TV_COLOR
        Return Globals.DecodeRGBA(Globals.RGBA256(r, g, b, a))
    End Function

    '#########################################################################

    'public Function  Color2TV3D(Color tColor) as TV_COLOR
    '        Return Globals.DecodeRGBA(Globals.RGBA256(Convert.ToInt32(tColor.R), Convert.ToInt32(tColor.G), Convert.ToInt32(tColor.B), Convert.ToInt32(tColor.A)))
    '    End Function

    '#########################################################################

    'public Function Color TV3D2Color(TV_COLOR tColor)
    '        Return Color.FromArgb((Int())(tColor.a * 255), (Int())(tColor.r * 255), (Int())(tColor.g * 255), (Int())(tColor.b * 255))
    '    End Function

#End Region

#Region " Smooth "

    Public Sub Smooth(ByVal nPasses As Integer, ByVal fFactor As Single)

        Dim faNewPixels(Size, Size) As Single
        Dim x, y, i, l, n, px, py As Integer
        Dim dMax, h, hi, di, dh As Single
        Dim hl As Single = 0.0F
        Dim T As Single = fFactor / Size

        '# Moore Neighborhood
        Dim vaNeighborhood(8) As TV_2DVECTOR
        vaNeighborhood(0) = New TV_2DVECTOR(-1, 1)
        vaNeighborhood(1) = New TV_2DVECTOR(1, 0)
        vaNeighborhood(2) = New TV_2DVECTOR(1, 1)
        vaNeighborhood(3) = New TV_2DVECTOR(0, -1)
        vaNeighborhood(4) = New TV_2DVECTOR(0, 1)
        vaNeighborhood(5) = New TV_2DVECTOR(-1, -1)
        vaNeighborhood(6) = New TV_2DVECTOR(-1, 0)
        vaNeighborhood(7) = New TV_2DVECTOR(-1, 1)


        For i = 0 To nPasses - 1

            For x = 0 To Size - 1

                For y = 0 To Size - 1

                    h = GetPixel(x, y)
                    dMax = 0.0F

                    For n = 0 To 7

                        hi = GetPixelWrap(x + vaNeighborhood(n).x, y + vaNeighborhood(n).y)
                        di = h - hi

                        If (di > dMax) Then

                            dMax = di
                            l = n
                        End If
                    Next

                    If (0 < dMax And dMax <= T) Then

                        dh = 0.5F * dMax
                        h -= dh

                        SetPixel(x, y, h)

                        px = x + vaNeighborhood(l).x
                        py = y + vaNeighborhood(l).y

                        WrapCoordinates(px, py)

                        hl = dh + GetPixel(px, py)

                        SetPixel(px, py, hl)
                    End If
                Next
            Next
        Next
    End Sub

#End Region

#Region " Normalize "

    Public Sub Normalize()

        Dim min As Single = 0.0F
        Dim max As Single = 0.0F

        GetColorLimits(min, max)

        For x = 0 To Size - 1
            For y = 0 To Size - 1
                PixelArray(x, y) = (PixelArray(x, y) - min) / (max - min)
            Next
        Next
    End Sub

#End Region

#Region " Add Erosion "

    Public Sub AddErosion(ByVal nPasses As Integer, ByVal fFactor As Single)

        Dim x As Integer = 0
        Dim y As Integer = 0
        Dim px As Integer = 0
        Dim py As Integer = 0
        Dim i As Integer = 0
        Dim n As Integer = 0
        Dim l As Integer = 0
        Dim dMax, h, hi, di, dh As Single
        Dim T As Single = fFactor / Size
        Dim hl As Single = 0.0F

        '# Moore Neighborhood
        Dim vaNeighborhood(8) As TV_2DVECTOR
        vaNeighborhood(0) = New TV_2DVECTOR(-1, 1)
        vaNeighborhood(1) = New TV_2DVECTOR(1, 0)
        vaNeighborhood(2) = New TV_2DVECTOR(1, 1)
        vaNeighborhood(3) = New TV_2DVECTOR(0, -1)
        vaNeighborhood(4) = New TV_2DVECTOR(0, 1)
        vaNeighborhood(5) = New TV_2DVECTOR(-1, -1)
        vaNeighborhood(6) = New TV_2DVECTOR(-1, 0)
        vaNeighborhood(7) = New TV_2DVECTOR(-1, 1)

        For i = 0 To nPasses - 1

            For x = 0 To Size - 1

                For y = 0 To Size - 1

                    h = GetPixel(x, y)
                    dMax = 0.0F

                    For n = 0 To 7

                        hi = GetPixelWrap(x + vaNeighborhood(n).x, y + vaNeighborhood(n).y)
                        di = h - hi

                        If (di > dMax) Then

                            dMax = di
                            l = n
                        End If
                    Next

                    If (0 < dMax And dMax <= T) Then

                        dh = 0.5F * dMax
                        h -= dh

                        SetPixel(x, y, h)

                        px = x + vaNeighborhood(l).x
                        py = y + vaNeighborhood(l).y

                        WrapCoordinates(px, py)

                        hl = dh + GetPixel(px, py)

                        SetPixel(px, py, hl)
                    End If
                Next
            Next
        Next
    End Sub

#End Region

#Region " Perturb "

    Public Sub Perturb(ByVal pNoiseMap As cChannel, ByVal fMagnitude As Single)

        Dim faNewPixels(Size, Size) As Single
        Dim x, y, x_coord_lo, x_coord_hi, y_coord_lo, y_coord_hi As Integer
        Dim p, x_coord, x_frac, y_coord, y_frac, val1, val2 As Single

        For x = 0 To Size - 1

            For y = 0 To Size - 1

                p = fMagnitude * (pNoiseMap.GetPixel(x, y) - 0.5F)

                x_coord = x + Size * p
                x_coord_lo = x_coord
                x_coord_hi = x_coord_lo + 1
                x_frac = x_coord - x_coord_lo

                y_coord = y + Size * p
                y_coord_lo = y_coord
                y_coord_hi = y_coord_lo + 1
                y_frac = y_coord - y_coord_lo

                val1 = Cosine_Interpolate(GetPixelWrap(x_coord_lo, y_coord_lo), GetPixelWrap(x_coord_hi, y_coord_lo), x_frac)
                val2 = Cosine_Interpolate(GetPixelWrap(x_coord_lo, y_coord_hi), GetPixelWrap(x_coord_hi, y_coord_hi), x_frac)

                faNewPixels(x, y) = Cosine_Interpolate(val1, val2, y_frac)
            Next
        Next

        PixelArray = faNewPixels
    End Sub

#End Region

#Region " Generate Midpoint Displacement Map "

    Public Sub GenerateMidpointDisplacementMap(ByVal fPersitence As Single)
        '# Diamond Square Algorithm
        Dim n As Integer = 2
        Dim avg, c As Single
        Dim ulx, uly, dimm As Integer
        Dim s, i, j, x, y As Integer

        While Math.Pow(2, n) < Size - 1
            n += 1
        End While

        dimm = 1 << (n - 2)

        '# seed the first few array values with reasonable numbers
        For j = 0 To 4
            For i = 0 To 4
                PixelArray(dimm * i, dimm * j) = 0.75F + (Randomizer.NextDouble() * 2 - 1) / 8
            Next
        Next
        '# run the algorithm
        c = 1.0F

        For s = 2 To n - 1

            c *= fPersitence
            dimm = (1 << (n - s))

            For j = 0 To (1 << s) - 1

                For i = 0 To (1 << s) - 1

                    uly = j * dimm
                    ulx = i * dimm
                    y = (1 << (n - s - 1)) + j * (1 << (n - s))
                    x = (1 << (n - s - 1)) + i * (1 << (n - s))

                    avg = 0
                    avg += PixelArray(ulx, uly)
                    avg += PixelArray(ulx + dimm, uly)
                    avg += PixelArray(ulx, uly + dimm)
                    avg += PixelArray(ulx + dimm, uly + dimm)
                    avg /= 4

                    PixelArray(x, y) = avg + c * (Randomizer.NextDouble() * 2 - 1)
                Next
            Next

            dimm = (1 << (n - s - 1))

            For j = 0 To (1 << (s + 1))

                For i = 0 To (1 << (s + 1))

                    If ((i + j) Mod 2 = 0) Then Continue For

                    y = j * dimm
                    x = i * dimm
                    avg = 0
                    avg += PixelArray(IIf((x - dimm >= 0), (x - dimm), (x - dimm + (1 << n))), y)
                    avg += PixelArray(x, IIf((y - dimm >= 0), (y - dimm), (y - dimm + (1 << n))))
                    avg += PixelArray(IIf((x + dimm <= (1 << n)), (x + dimm), (x + dimm - (1 << n))), y)
                    avg += PixelArray(x, IIf((y + dimm <= (1 << n)), (y + dimm), (y + dimm - (1 << n))))
                    avg /= 4

                    PixelArray(x, y) = avg + c * (Randomizer.NextDouble() * 2 - 1)
                Next
            Next
        Next

        Normalize()
    End Sub

#End Region

#Region " Save To Texture "

    Public Sub SaveToTexture(ByVal sFilename As String)

        Dim nTextureID As Integer = TextureFactory.CreateTexture(Size, Size, False)
        TextureFactory.LockTexture(nTextureID, False)
        For x = 0 To Size - 1
            For y = 0 To Size - 1
                TextureFactory.SetPixel(nTextureID, x, y, GetPixelRGBA(x, y))
            Next
        Next
        TextureFactory.UnlockTexture(nTextureID, False)
        TextureFactory.SaveTexture(nTextureID, sFilename, MTV3D65.CONST_TV_IMAGEFORMAT.TV_IMAGE_BMP)
        TextureFactory.DeleteTexture(nTextureID)

    End Sub

#End Region

#Region " Create From Texture "

    Public Sub CreateFromTexture(ByVal sFilename As String)

        Dim nTextureID As Integer = TextureFactory.LoadTexture(sFilename, "", Size, Size)

        TextureFactory.LockTexture(nTextureID, False)

        For x = 0 To Size - 1
            For y = 0 To Size - 1
                PixelArray(x, y) = Globals.DecodeRGBA(TextureFactory.GetPixel(nTextureID, x, y)).r
            Next
        Next

        TextureFactory.UnlockTexture(nTextureID, False)
        TextureFactory.DeleteTexture(nTextureID)
    End Sub

#End Region

#Region " Multiply With "

    Public Sub MultiplyWith(ByVal pRight As cChannel)

        If Not (pRight.Size = Size) Then Return

        For x = 0 To Size - 1
            For y = 0 To Size - 1
                PixelArray(x, y) *= pRight.PixelArray(x, y)
            Next
        Next
    End Sub

#End Region

#Region " Combine With "

    Public Sub CombineWith(ByVal pRight As cChannel, ByVal fRightOpacity As Single)

        If Not (pRight.Size = Size) Then Return

        Dim fLeftOpacity As Single = 1.0F - fRightOpacity

        For x = 0 To Size - 1
            For y = 0 To Size - 1
                PixelArray(x, y) = PixelArray(x, y) * fLeftOpacity + pRight.PixelArray(x, y) * fRightOpacity
            Next
        Next
    End Sub

#End Region

#Region " Generate Voronoi Map "

    Public Sub GenerateVoronoiMap(ByVal pRedChannel As cChannel, ByVal pGreenChannel As cChannel, ByVal pBlueChannel As cChannel, ByVal c1 As Single, ByVal c2 As Single, ByVal nCellCount As Integer)

        Dim faDistanceBuffer(pRedChannel.GetSize(), pRedChannel.GetSize()) As tDISTANCE_BUFFER
        Dim fTotalMinDistance As Single = Single.MaxValue
        Dim fTotalMaxDistance As Single = Single.MinValue
        Dim d1, d2 As Single
        Dim fDistance As Single = 0.0F
        Dim vaPointBuffer(nCellCount) As tCELL
        Dim i, x, y As Integer



        For i = 0 To nCellCount - 1

            vaPointBuffer(i).x = Randomizer.NextDouble() * pRedChannel.GetSize()
            vaPointBuffer(i).y = Randomizer.NextDouble() * pRedChannel.GetSize()
            vaPointBuffer(i).color1 = Randomizer.NextDouble()

            vaPointBuffer(i).distanceToMapCenter = Math.Sqrt(Math.Pow(vaPointBuffer(i).x - pRedChannel.GetSize() / 2, 2) + Math.Pow(vaPointBuffer(i).y - pRedChannel.GetSize() / 2, 2))

            'for (n = 0 n < nCellCount n++)
            '   '# Die eigene Zelle überspringen
            '   If( n != i ) Then
            '       vaPointBuffer(i).color2 = (float)m_pRandomizer.Next(0, 2) % 2
            '   End If
            'Next
        Next

        For x = 0 To pRedChannel.GetSize() - 1
            For y = 0 To pRedChannel.GetSize() - 1

                d1 = Single.MaxValue
                d2 = Single.MaxValue

                For i = 0 To nCellCount - 1
                    '# a² = b² + c² = Pythagoras (bzw. Euclidean distance metric)
                    fDistance = Math.Sqrt(Math.Pow(vaPointBuffer(i).x - x, 2) + Math.Pow(vaPointBuffer(i).y - y, 2))
                    If (fDistance < d1) Then
                        d2 = d1
                        d1 = fDistance
                        faDistanceBuffer(x, y).cell = i
                    ElseIf (fDistance < d2) Then
                        d2 = fDistance
                    End If
                Next

                faDistanceBuffer(x, y).distance = c1 * d1 + c2 * d2
                If (faDistanceBuffer(x, y).distance < fTotalMinDistance) Then
                    fTotalMinDistance = faDistanceBuffer(x, y).distance
                ElseIf (faDistanceBuffer(x, y).distance > fTotalMaxDistance) Then
                    fTotalMaxDistance = faDistanceBuffer(x, y).distance
                End If
            Next
        Next

        d1 = Single.MaxValue
        d2 = Single.MinValue

        For i = 0 To nCellCount - 1

            If (vaPointBuffer(i).distanceToMapCenter < d1) Then
                d1 = vaPointBuffer(i).distanceToMapCenter
            ElseIf (vaPointBuffer(i).distanceToMapCenter > d2) Then
                d2 = vaPointBuffer(i).distanceToMapCenter
            End If
        Next

        For i = 0 To nCellCount - 1
            vaPointBuffer(i).color2 = 1.0F - (vaPointBuffer(i).distanceToMapCenter - d1) / (d2 - d1)
        Next

        For x = 0 To pRedChannel.GetSize() - 1

            For y = 0 To pRedChannel.GetSize() - 1

                '# Normalisieren
                pRedChannel.SetPixel(x, y, (faDistanceBuffer(x, y).distance - fTotalMinDistance) / (fTotalMaxDistance - fTotalMinDistance))
                pGreenChannel.SetPixel(x, y, vaPointBuffer(faDistanceBuffer(x, y).cell).color1)
                pBlueChannel.SetPixel(x, y, vaPointBuffer(faDistanceBuffer(x, y).cell).color2)
            Next
        Next
    End Sub

#End Region

End Class
« Last Edit: July 07, 2008, 03:16:10 PM by Shadowsong » Logged
Shadowsong
Customers
Community Member
*****
Posts: 328


« Reply #2 on: July 07, 2008, 03:14:56 PM »

Second: an example of how to use it:

Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        '# PARAMETERS (Change these to get different effects!)
        Dim nTextureSize As Integer = 512

        Dim fPersitence As Single = 0.5
        Dim nCellCount As Integer = 2
        Dim fErosionFactor As Single = 10
        Dim fPerturbationFactor As Single = 2
        Dim bNormalize As Boolean = True
        Dim bIslandStyle As Boolean = False
        Dim fNoiseOpacity As Single = 0.5

        '# THE DIFFERENT LAYERS
        Dim m_pFinalHeightmap As cChannel = New cChannel(nTextureSize)
        Dim pNoise As cChannel = New cChannel(nTextureSize)
        Dim pRedVoronoi As cChannel = New cChannel(nTextureSize)
        Dim pGreenVoronoi As cChannel = New cChannel(nTextureSize)
        Dim pBlueVoronoi As cChannel = New cChannel(nTextureSize)

        '# COMPUTE THE HEIGHTMAP
        pNoise.GenerateMidpointDisplacementMap(fPersitence)

        m_pFinalHeightmap.GenerateVoronoiMap(pRedVoronoi, pGreenVoronoi, pBlueVoronoi, -1.0F, 1.0F, nCellCount)
        m_pFinalHeightmap = pRedVoronoi

        If bIslandStyle = True Then
            m_pFinalHeightmap.MultiplyWith(pBlueVoronoi)
        Else
            m_pFinalHeightmap.MultiplyWith(pGreenVoronoi)
        End If

        m_pFinalHeightmap.CombineWith(pNoise, fNoiseOpacity)

        If bNormalize = True Then m_pFinalHeightmap.Normalize()

        m_pFinalHeightmap.Perturb(pNoise, fPerturbationFactor)

        m_pFinalHeightmap.AddErosion(50, fErosionFactor)

        '# SAVE THE HEIGHTMAP TO A BMP
        m_pFinalHeightmap.SaveToTexture(Application.StartupPath & "\Heightmap.bmp")

    End Sub
Logged
Mietze
Community Member
*
Posts: 415

Pleeease, don't let it crash!


WWW
« Reply #3 on: July 07, 2008, 04:18:03 PM »

Hmm... why the heck do I know these pictures? Smiley
Logged

Check out my blog at www.e-studioz.de - The finest in gross trash Wink
serial
Customers
Community Member
*****
Posts: 380


« Reply #4 on: July 08, 2008, 02:29:57 AM »

I got it working.  Looks good.  Not directly plug and play though needed a tiny bit of hammering. 

I'm going to convert this to use GDI and make a few other modifications so that each layer can be previewed. 

I'll post that modification and source when I'm done with it.  :-D

 
Logged
Shadowsong
Customers
Community Member
*****
Posts: 328


« Reply #5 on: July 08, 2008, 08:24:57 AM »

Maybe Mietze could post the orignial C++ code if someone is interested.

Why is it not plug and play? What's wrong in there?
I think everybody should be able to make a GUI with parameter boxes and previews themselves out of my example.

You could simply use a PictureBox and so something like Picture.FromFile(...bmp) to preview the heightmap. Or you could change the "SaveToTexture" method, so that it doesn't write to a bitmap file but it saves the texture (instead of the local "nTextureID"). You could then directly render the texture with TV.

You will have parameters from a GUI in most cases, but I decided to have them hardcode in the example to give people a rough idea of what the values should be. However I have no idea if those I have in there are common values. Mietze should know  Grin
Logged
serial
Customers
Community Member
*****
Posts: 380


« Reply #6 on: July 08, 2008, 09:57:54 AM »

A few declarations didn't make in it to the cChannel class.   X,Y, Globals, and TextureFactory.  Don't want to hijack your thread. Personally I do a lot of stuff
in my editor with GDI. 
Logged
Mietze
Community Member
*
Posts: 415

Pleeease, don't let it crash!


WWW
« Reply #7 on: July 08, 2008, 10:09:37 AM »

Maybe Mietze could post the orignial C++ code if someone is interested.

Its C# Wink
Logged

Check out my blog at www.e-studioz.de - The finest in gross trash Wink
Shadowsong
Customers
Community Member
*****
Posts: 328


« Reply #8 on: July 09, 2008, 04:10:25 AM »

Ah well Globals and TextureFactory are globals in my code. Sorry for not thinking about that. Everybody should use his instances of those there. X and Y are integers.

@ Mietze: where's the difference Grin Roll Eyes
Logged
Shadowsong
Customers
Community Member
*****
Posts: 328


« Reply #9 on: July 11, 2008, 08:40:41 AM »

@ Mietze: The parameter "fPersistance" for "GenerateMidpointDisplacementMap" seems not to work!
Whatever I set persistance to, it either puts out an "expected" noise such as in the first screenshot above, or just absolutely pixelated stuff (like every pixel has a random color).
With values = 1 or between 0 and 0.2 it works. Any other value produces pixeling. And there is no transition, if I use 1 it's ok. If I use 1.0001 it jumps to absolute random.

What's going on there?
Logged
Mietze
Community Member
*
Posts: 415

Pleeease, don't let it crash!


WWW
« Reply #10 on: July 11, 2008, 10:43:56 AM »

@ Mietze: The parameter "fPersistance" for "GenerateMidpointDisplacementMap" seems not to work!
Whatever I set persistance to, it either puts out an "expected" noise such as in the first screenshot above, or just absolutely pixelated stuff (like every pixel has a random color).
With values = 1 or between 0 and 0.2 it works. Any other value produces pixeling. And there is no transition, if I use 1 it's ok. If I use 1.0001 it jumps to absolute random.

What's going on there?
what about understanding the code you have translated instead of just copy and pasting it? Take a look at this line...

Code:
for (x = 0; x < m_nSize; x++)
{
    for (y = 0; y < m_nSize; y++)
    {
        p = fMagnitude * (pNoiseMap.GetPixel(x, y) - 0.5f);
... and go figure Wink You may want to use GetPixelWrap() with values > 1
Logged

Check out my blog at www.e-studioz.de - The finest in gross trash Wink
Hawthorne
Customers
Community Member
*****
Posts: 275


« Reply #11 on: July 28, 2008, 08:14:52 AM »

I got it working.  Looks good.  Not directly plug and play though needed a tiny bit of hammering. 

I'm going to convert this to use GDI and make a few other modifications so that each layer can be previewed. 

I'll post that modification and source when I'm done with it.  :-D

 

Indeed, I would like to see this in GDI as well, maps, unless generated real time are normally in an editor or something else. It would be very cool to apply the errosion technique to an already existing image file.

-Pat
Logged
Mietze
Community Member
*
Posts: 415

Pleeease, don't let it crash!


WWW
« Reply #12 on: July 30, 2008, 03:29:10 AM »

I'm sorry, but I had to delete the images from my FTP server... Undecided
Logged

Check out my blog at www.e-studioz.de - The finest in gross trash Wink
Snowcrash
Customers
Community Member
*****
Posts: 6


« Reply #13 on: December 17, 2008, 01:35:02 PM »

Is the Cosine_Interpolate a part of TV3D  Huh, or should I role my own?

Thanks
Logged
rootsage
Customers
Community Member
*****
Posts: 444

Gamer Enthusiast


WWW
« Reply #14 on: December 23, 2008, 06:00:34 PM »

Is the Cosine_Interpolate a part of TV3D  Huh, or should I role my own?

Thanks

I haven't seen it in TV3D, I wrote my own.
Code:
public float Cosine_Interpolate(float a, float b, float x)
{
    float ft = x * 3.1415927;
    float f = (1 - MyMath.ACos(ft)) * 0.5;
   
    return a * (1 - f) + b * f;
}

In that, MyMath is a declaration of the TVMathLibrary... You can mess around with the Pi and the 0.5 number for different results too. The above seems to work well for me Smiley
Logged

while( !( succeed = try_again()) );
------
10 print "Is this recursive?"
20 goto 10
Canning
Community Member
*
Posts: 592


« Reply #15 on: February 12, 2011, 12:42:51 AM »

I am trying the code and get the following error:

Quote
System.AccessViolationException was unhandled
  Message=Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
  Source=MTV3D65
  StackTrace:
       at CTVTextureFactory.CreateTexture(CTVTextureFactory* , Int32 , Int32 , Boolean , SByte* )
       at MTV3D65.TVTextureFactory.CreateTexture(Int32 iWidth, Int32 iHeight, Boolean bHasAlpha)
       at Project1.cChannel.SaveToTexture(String sFilename) in I:\VB\Truevision3d\Projects\2010 template\Class2.vb:line 424
       at Project1.frmLauncher.Button1_Click(Object sender, EventArgs e) in I:\VB\Truevision3d\Projects\2010 template\frmLauncher.vb:line 221
       at System.Windows.Forms.Control.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
       at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ButtonBase.WndProc(Message& m)
       at System.Windows.Forms.Button.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.Run(Form mainForm)
       at Project1.frmLauncher.Main() in I:\VB\Truevision3d\Projects\2010 template\frmLauncher.Designer.vb:line 0
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.Runtime.Hosting.ManifestRunner.Run(Boolean checkAptModel)
       at System.Runtime.Hosting.ManifestRunner.ExecuteAsAssembly()
       at System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext activationContext, String[] activationCustomData)
       at System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext activationContext)
       at System.Activator.CreateInstance(ActivationContext activationContext)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssemblyDebugInZone()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException:

at line:

Code:
Dim nTextureID As Integer = Texturefactory.CreateTexture(Size, Size, False)

Serial:

Quote
I'm going to convert this to use GDI and make a few other modifications so that each layer can be previewed.

I'll post that modification and source when I'm done with it.  :-D

Any luck with the progress on this?

« Last Edit: February 12, 2011, 01:15:37 AM by Canning » Logged

I am using 2010 VB.NET and TV 6.5 with Windows 7
Lenn
Customers
Community Member
*****
Posts: 876

+/-


« Reply #16 on: February 12, 2011, 09:44:42 AM »

Hi Canning, welcome to TV3D forums! Do you know how to create a texture in TV3D? I suggest you go through some of the samples for newbies, and have a look. There are also other samples that create textures like Zak's shaders.
Logged

TV3D 6.5 Community Docs - Read, use and please contribute!
serial
Customers
Community Member
*****
Posts: 380


« Reply #17 on: February 12, 2011, 03:51:19 PM »

Personally I think Canning is trying to cut and paste a game together.  Just me though. 
Logged
arnienet
Customers
Community Member
*****
Posts: 263


WWW
« Reply #18 on: February 13, 2011, 09:57:34 AM »

Either that or a game editor combination. Lots of questions, little research. I admire the drive to complete something, but if that desire were channeled into research as well, a good app might emerge with some personal achievement kudos included. Hopefully then something might be given back to the community, but at least we are being thanked for our efforts now, always a good move Smiley
Logged

Total Dev time = 50% to code, 50% to test, 50% to find errors, 50% to fix, that's why it takes twice as long.

Dawn World MMO
Pages: [1]
  Print  
 
Jump to:  

Powered by SMF 1.1.3 | SMF © 2006-2007, Simple Machines LLC
Seo4Smf v0.2 © Webmaster's Talks