NaaLaa

Full Version: Maybe Perlin Noise
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
... do you mean something like ... The Matrix? Moo Ha Ha Ha ....
(05-01-2026, 05:53 AM)1micha.elok Wrote: [ -> ]I'm amazed that math can generate textures - rocks, bricks, ....even trees (fractals). It makes me wonder, is our entire universe just one big math composition ?  Big Grin

I'm doing in code the steps I usually take when creating textures in the gimp, creating bumpmaps from noise etc. I need to write more image manipulation functions though, for altering colors.
[Image: b7EQ2.gif]

Bouncing ball on Perlin Hills ....

Code:
'===============================
'
' Bouncing Ball on Perlin Hills
'
'===============================
#win32
randomize time()
set window "Bouncing Ball on Perlin Hills", 512, 256, false
set redraw off

green   = [100, 200, 100]
yellow  = [255, 255, 0]

visible scale = 1000, amp = 200
visible terrain=MakeTerrain(10,10), scroll_x, bx = width()/2, by=height()/2, bvy, ty_map = []



'------------
' MAIN LOOP
'------------
while not keydown(KEY_ESCAPE, true)
    'clear screen
    set color 0,0,0;cls

    ' Terrain
    scroll_x = scroll_x + 3.5 'scroll speed
    for x = 0 to width() - 1
        ty_map[x] = height() / 2 + terrain.ValueAt((x + scroll_x) / scale, 0.5) * amp
    next
    set color green
    for x = 0 to width() - 2
        ' Draw terrain fill (vertical line) + edge (diagonal)
        draw line x, ty_map[x], x, height()
        draw line x, ty_map[x], x + 1, ty_map[x + 1]
    next
    ' last vertical line
    draw line width() - 1, ty_map[width() - 1], width() - 1, height()

    ' Ball
    i = min(max(int(bx), 0), width() - 1)
    ty_curr = ty_map[i]
    bvy = bvy + 0.45
    by = by + bvy
    if by + 4 >= ty_curr
        by = ty_curr - 4
        bvy = -bvy * 0.75
    endif
    bx = bx + (width() / 2 - bx) * 0.08
    set color yellow; draw ellipse bx, by, 4,4, true

    redraw
    fwait 60
wend


'-------------
' FUNCTIONS
'-------------
function MakeTerrain(resX, resY)
    t = []
    t.g = dim(resX, resY)
    t.w = resX
    t.h = resY
   
    for y = 0 to resY - 1
        for x = 0 to resX - 1
            a = rnd() * PI * 2
            t.g[x][y] = [x: cos(a), y: sin(a)]
        next
    next

    t.ValueAt = function(x, y)
        m = this.g
        w = this.w; h = this.h
       
        x = x * w; y = y * h
        x0 = floor(x); y0 = floor(y)
        x1 = (x0 + 1) % w; y1 = (y0 + 1) % h
        x0 = x0 % w; y0 = y0 % h
        fx = x - floor(x); fy = y - floor(y)

        tl = -fx * m[x0][y0].x - fy * m[x0][y0].y
        tr = (1 - fx) * m[x1][y0].x - fy * m[x1][y0].y
        bl = -fx * m[x0][y1].x + (1 - fy) * m[x0][y1].y
        br = (1 - fx) * m[x1][y1].x + (1 - fy) * m[x1][y1].y

        return Bilinear(tl, tr, bl, br, Quad(0, 1, fx), Quad(0, 1, fy))
    endfunc

    return t
endfunc

' helper functions
function Bilinear(tl, tr, bl, br, x, y)
    ix = 1 - x
    return (1 - y) * (ix * tl + x * tr) + y * (ix * bl + x * br)
endfunc

function Quad(a, b, p)
    p2 = p * p
    p3 = p2 * p
    return a * (1 - 3 * p2 + 2 * p3) + b * (3 * p2 - 2 * p3)
endfunc
(05-03-2026, 05:30 AM)1micha.elok Wrote: [ -> ][Image: b7EQ2.gif]

Bouncing ball on Perlin Hills ....

Code:
'===============================
'
' Bouncing Ball on Perlin Hills
'
'===============================
#win32
randomize time()
set window "Bouncing Ball on Perlin Hills", 512, 256, false
set redraw off

green   = [100, 200, 100]
yellow  = [255, 255, 0]

visible scale = 1000, amp = 200
visible terrain=MakeTerrain(10,10), scroll_x, bx = width()/2, by=height()/2, bvy, ty_map = []



'------------
' MAIN LOOP
'------------
while not keydown(KEY_ESCAPE, true)
    'clear screen
    set color 0,0,0;cls

    ' Terrain
    scroll_x = scroll_x + 3.5 'scroll speed
    for x = 0 to width() - 1
        ty_map[x] = height() / 2 + terrain.ValueAt((x + scroll_x) / scale, 0.5) * amp
    next
    set color green
    for x = 0 to width() - 2
        ' Draw terrain fill (vertical line) + edge (diagonal)
        draw line x, ty_map[x], x, height()
        draw line x, ty_map[x], x + 1, ty_map[x + 1]
    next
    ' last vertical line
    draw line width() - 1, ty_map[width() - 1], width() - 1, height()

    ' Ball
    i = min(max(int(bx), 0), width() - 1)
    ty_curr = ty_map[i]
    bvy = bvy + 0.45
    by = by + bvy
    if by + 4 >= ty_curr
        by = ty_curr - 4
        bvy = -bvy * 0.75
    endif
    bx = bx + (width() / 2 - bx) * 0.08
    set color yellow; draw ellipse bx, by, 4,4, true

    redraw
    fwait 60
wend


'-------------
' FUNCTIONS
'-------------
function MakeTerrain(resX, resY)
    t = []
    t.g = dim(resX, resY)
    t.w = resX
    t.h = resY
   
    for y = 0 to resY - 1
        for x = 0 to resX - 1
            a = rnd() * PI * 2
            t.g[x][y] = [x: cos(a), y: sin(a)]
        next
    next

    t.ValueAt = function(x, y)
        m = this.g
        w = this.w; h = this.h
       
        x = x * w; y = y * h
        x0 = floor(x); y0 = floor(y)
        x1 = (x0 + 1) % w; y1 = (y0 + 1) % h
        x0 = x0 % w; y0 = y0 % h
        fx = x - floor(x); fy = y - floor(y)

        tl = -fx * m[x0][y0].x - fy * m[x0][y0].y
        tr = (1 - fx) * m[x1][y0].x - fy * m[x1][y0].y
        bl = -fx * m[x0][y1].x + (1 - fy) * m[x0][y1].y
        br = (1 - fx) * m[x1][y1].x + (1 - fy) * m[x1][y1].y

        return Bilinear(tl, tr, bl, br, Quad(0, 1, fx), Quad(0, 1, fy))
    endfunc

    return t
endfunc

' helper functions
function Bilinear(tl, tr, bl, br, x, y)
    ix = 1 - x
    return (1 - y) * (ix * tl + x * tr) + y * (ix * bl + x * br)
endfunc

function Quad(a, b, p)
    p2 = p * p
    p3 = p2 * p
    return a * (1 - 3 * p2 + 2 * p3) + b * (3 * p2 - 2 * p3)
endfunc

Awesome!
Great demo, particularly as it's only 100 lines of code. I look forward to studying the code..... thanks for sharing....
Interesting. Replace the "ball" with a motor cycle or car; apply a little physics to the vehicle; control the speed of the scroll; add a timer; score based on timer; persistent high score... Could be the makings of an "endless runner" type game... Moo Ha Ha Ha....

Very cool demo!!


Textures and landscape based on that perlin noise thing, everything generated in code. It looks a bit like Mordor at the moment, will post the source code when things look better Smile It's a really large map that has been divided into "chunks", and I only render the chunks that are determined visible.
All that heat and no steaks? Mmmm... B.B.Q's....
Pages: 1 2