Author Topic: Chladni w/ Glass Effect  (Read 5357 times)

0 Members and 1 Guest are viewing this topic.

Offline rdc

  • Pentium
  • *****
  • Posts: 1495
  • Karma: 140
  • Yes, it is me.
    • View Profile
    • Clark Productions
Chladni w/ Glass Effect
« on: February 02, 2014 »
Hey guys, it has been a long, long time. Not sure if anyone even remembers me but my logon still worked. :) I hope everyone is well.

I have been out of touch for a while; had quite a few major things come up in the past couple years so I haven't really done anything much or kept up with much either. I am finally getting back into programming and such again though as life has settled down some. Thought I would post a little tidbit. Not sure if I posted this before, but I have done a bit of tinkering on it recently. Compiles with the latest version and probably older versions as well. Yeah, I am still messing around with Chladni. :)

I am looking forward to getting caught up here. I have forgotten just about everything, so it is back to the books so to speak.

Keys

space : new image
s (lowercase) : save bmp
ESC]: quit

Code: [Select]
'Chladni Glass Demo
'http://local.wasp.uwa.edu.au/~pbourke/modelling/chladni/
'Richard D. Clark
'Public Domain



#Define sw 1024
#define sh 768

'Jofers
Type Pixel_Color
    B As Ubyte
    G As Ubyte
    R As Ubyte
    A As Ubyte
End Type

Union Pixel
    Channel As Pixel_Color
    Value   As Uinteger
End Union

const pi = Atn(1.0) * 4

dim shared tex(0 to sw - 1, 0 to sh - 1) as uinteger
dim shared ctex(0 to sw - 1, 0 to sh - 1) as uinteger
dim shared ntex(0 to sw - 1, 0 to sh - 1) as uinteger
dim shared pal(0 to 255) as uinteger
dim as string key
Dim As Integer ret

Function GetRandom(lowerbound as integer, upperbound as integer) as integer
   return Int((upperbound - lowerbound + 1) * Rnd + lowerbound)
end function
 
'Interpolation code by Rattrapmax6
Sub DoPalette(pal() as UInteger, sr as integer, sg as integer, sb as integer, er as integer, eg as integer, eb as integer)
    Dim As Integer i
    Dim iStart(3) As Integer
    Dim iEnd(3) As Integer
    Dim iShow(3) As Integer
    Dim Rend(3) As Double
    Dim InterPol(3) As Double
   
    InterPol(0) = Ubound(pal)
    iStart(1) = sr
    iStart(2) = sg
    iStart(3) = sb
    iEnd(1) = er
    iEnd(2) = eg
    iEnd(3) = eb
    InterPol(1) = (iStart(1) - iEnd(1)) / InterPol(0)
    InterPol(2) = (iStart(2) - iEnd(2)) / InterPol(0)
    InterPol(3) = (iStart(3) - iEnd(3)) / InterPol(0)       
    Rend(1) = iStart(1)
    Rend(2) = iStart(2)
    Rend(3) = iStart(3)   
   
    For i = 0 To Ubound(pal)
        iShow(1) = Rend(1)
        iShow(2) = Rend(2)
        iShow(3) = Rend(3)

        pal(i) = Rgb(iShow(1),iShow(2),iShow(3))

        Rend(1) -= InterPol(1)
        Rend(2) -= InterPol(2)
        Rend(3) -= InterPol(3)
    Next
   
End Sub

sub GeneratePalette(pal() as integer)
    dim as integer rs, gs, bs, re, ge, be
   
    rs = GetRandom(0, 255)
    gs = GetRandom(0, 255)
    bs = GetRandom(0, 255)
    re = 255
    ge = 255
    be = 255   
    DoPalette pal(), rs, gs, bs, re, ge, be
end sub

sub LoadChladni
    dim as integer x, y, n, m, x2, y2, l, i, nx, ny
    dim as integer clr1, clr2, r, g, b, iterations
    dim as integer rmax, chs
    dim as double hh, h, ambient, dif, spec
    dim as uinteger cc
    dim clr as Pixel
   
    chs = GetRandom(200, 512)           
    iterations = 10
    rmax = 15
    'Clear to 255
    for x = 0 to sw - 1
        for y = 0 to sh - 1
            tex(x, y) = 255
        next
    next
    'Generate chladni texture filling in 255 areas
    for i = 1 to iterations
        do
            n = rnd * rmax
            m = rnd * rmax
            sleep 1
        loop until (m <> n) 'and (m mod 2 <> 0) and (n mod 2 <>0)
        GeneratePalette pal()
        for x = 0 to sw - 1
            for y = 0 to sh - 1
                ambient = 0.4
                dif = 2.5
                spec = 1.0
                x2 = ( cos( n*pi*x/chs ) * cos( m*pi*y/chs ) ) * 128
                y2 = ( cos( m*pi*x/chs ) * cos( n*pi*y/chs ) ) * 128
                cc = x2 - y2
                if cc < 0 then cc = 0
                if cc > 255 then cc = 255
                if tex(x, y) = 255 then
                    tex(x, y) = cc
                    ctex(x, y) = pal(cc)
                end if
            next
        next
    next
    'Glassify image
    for x = 0 to sw - 1
        for y = 0 to sh - 1
            if (x + 1) <= sw - 1 then clr1 = tex(x + 1, y)
            if (x - 1) >= 0 then clr2 = tex(x - 1, y)
            nx = clr1 - clr2
            if (y + 1) <= sh - 1 then clr1 = tex(x, y + 1)
            if (y - 1) >= 0 then clr2 = tex(x, y - 1)
            ny = clr1 - clr2
            hh = 1 / sqr(nX * nX + nY * nY + 1)
            'shading = ambient + dif*dot + dot^2 * spec
            h = ambient + (dif * hh) + (hh * hh) * spec
            clr.Value = ctex(x, y)
            r = Int(clr.channel.r * h)
            g = Int(clr.channel.g * h)
            b = Int(clr.channel.b * h)
            if r < 0 then r = 0
            if g < 0 then g = 0
            if b < 0 then b = 0
            if r > 255 then r = 255
            if g > 255 then g = 255
            if b > 255 then b = 255
            ntex(x, y) = RGB(r, g, b)
        next
    next
    'Print Image
    screenlock
    cls   
    for x = 0 to sw - 1
        for y = 0 to sh - 1
            pset (x, y), ntex(x, y)
        next
    next
       
    screenunlock
end sub


Randomize timer

screenres sw, sh, 32,,1
if screenptr = 0 then
    end -1
end if
windowtitle "Chladni 3D"
setmouse ,,0
LoadChladni
do
    key = inkey
    if key = chr(32) then
        LoadChladni
    ElseIf key = "s" Then
    ret = BSave(Str(Timer) & ".bmp", 0)
    end if
    sleep 1
loop until key = chr(27)
setmouse ,,1
end

Offline ninogenio

  • Pentium
  • *****
  • Posts: 1668
  • Karma: 133
    • View Profile
Re: Chladni w/ Glass Effect
« Reply #1 on: February 03, 2014 »
hey rdc welcome back mate!!

yeah i remember you well  :) you have done some amazing things in the past, and judging by your new Chladni you still are  :o.

thats awesome!

i hope you don't mind but i have attached an exe of your awesome code too the bottom of my post. its for everyone who doesn't have fb installed too try out.. if you have any problems with me attaching this just let me know and i will remove.  :cheers:
Challenge Trophies Won:

Offline rdc

  • Pentium
  • *****
  • Posts: 1495
  • Karma: 140
  • Yes, it is me.
    • View Profile
    • Clark Productions
Re: Chladni w/ Glass Effect
« Reply #2 on: February 03, 2014 »
Thanks man. Appreciate the welcome. Yeah the attachment is fine. I should have thought of that myself. I need to relearn this demo stuff again, but I am looking forward to it. :)

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: Chladni w/ Glass Effect
« Reply #3 on: February 06, 2014 »
Nice to have you back Rick!  Chladni, now there's a word I haven't seen since a long time!

Jim
Challenge Trophies Won:

Offline Raizor

  • Founder Member
  • Pentium
  • ********
  • Posts: 1154
  • Karma: 175
    • View Profile
Re: Chladni w/ Glass Effect
« Reply #4 on: February 06, 2014 »
Really nice to look at rdc. Chladni patterns are rather fascinating, indeed :)
raizor

Challenge Trophies Won:

Offline rdc

  • Pentium
  • *****
  • Posts: 1495
  • Karma: 140
  • Yes, it is me.
    • View Profile
    • Clark Productions
Re: Chladni w/ Glass Effect
« Reply #5 on: February 07, 2014 »
Nice to have you back Rick!  Chladni, now there's a word I haven't seen since a long time!

Jim


Haha. I bet not. Thanks man.

Offline rdc

  • Pentium
  • *****
  • Posts: 1495
  • Karma: 140
  • Yes, it is me.
    • View Profile
    • Clark Productions
Re: Chladni w/ Glass Effect
« Reply #6 on: February 07, 2014 »
Really nice to look at rdc. Chladni patterns are rather fascinating, indeed :)


I appreciate it.

Offline emook

  • C= 64
  • **
  • Posts: 94
  • Karma: 12
    • View Profile
Re: Chladni w/ Glass Effect
« Reply #7 on: February 15, 2014 »
Very nice :)
----

R Tape loading error, 0:1

Offline rdc

  • Pentium
  • *****
  • Posts: 1495
  • Karma: 140
  • Yes, it is me.
    • View Profile
    • Clark Productions
Re: Chladni w/ Glass Effect
« Reply #8 on: February 15, 2014 »