Author Topic: [PROCEDURAL] Fluffy Clouds  (Read 5407 times)

0 Members and 1 Guest are viewing this topic.

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17412
  • Karma: 498
  • evil/good
    • View Profile
    • My Homepage
[PROCEDURAL] Fluffy Clouds
« on: April 17, 2008 »
I don't know if I have used an accepted algorithm here or not, I guess that there must be 1,000's of ways to make clouds.

My one is slow :P

Anyway it makes quite a nice pattern and the source is here too.

Code: [Select]
'
'                          Cloud Texture By Shockwave
'Slow code... Could be optimised and made real time + animated without much trouble
' I just wanted to go for something quite realistic :-P
'-------------------------------------------------------------------------------

    #INCLUDE "TINYPTC_EXT.BI"
    #INCLUDE "WINDOWS.BI"
       
    OPTION STATIC
    OPTION EXPLICIT
    RANDOMIZE TIMER 

    CONST XRES=640
    CONST YRES=480

    DIM SHARED AS UINTEGER      BUFFER(XRES * YRES)
    DIM SHARED AS DOUBLE     TBUFFER(XRES * YRES)       
    DIM SHARED AS DOUBLE     PBUFFER(XRES * YRES,10)       
    DIM SHARED AS INTEGER Q=0
    DIM SHARED AS INTEGER ZZ=0


    DECLARE SUB PRECALCULATE()
    DECLARE SUB INTERFERENCE()
    DECLARE SUB MERGE_CLOUDS()
    DECLARE SUB UPDATE_ONTO_BUFFER(BYVAL ZOOM_FACTOR AS INTEGER,BYVAL BNUM AS INTEGER)
    DECLARE SUB ALIASBUFFER (BYVAL BANK AS INTEGER)
   
    DIM SHARED AS UINTEGER CPALETTE(1000000)
    PRINT "CLOUDS BY SHOCKWAVE... PRE-CALCULATING.. THIS MAY TAKE A WHILE."
    PRINT "NB, WHEN YOU ARE FED UP WITH LOOKING AT THE TEXTURE PRESS ESC."
        INTERFERENCE()   
        Q=29
        FOR ZZ=1 TO 10
            UPDATE_ONTO_BUFFER(Q,ZZ)
            Q=Q+3
        NEXT
        FOR ZZ=1 TO 10
            ALIASBUFFER(ZZ)
        NEXT
        MERGE_CLOUDS()

    PTC_ALLOWCLOSE(0)
    PTC_SETDIALOG(0,"",0,1)     
    IF (PTC_OPEN("PROCEDURAL CLOUDS",XRES,YRES)=0) THEN
    END-1
    END IF 


WHILE (GETASYNCKEYSTATE(VK_ESCAPE)<> -32767)

    PTC_UPDATE@BUFFER(0)
   
WEND
END


SUB MERGE_CLOUDS()
DIM AS INTEGER X,Z,V
DIM M AS DOUBLE
FOR X=0 TO XRES*YRES-1
   
 FOR Z=1 TO 10
 V=V+PBUFFER(X,Z)
 NEXT
      V=V/10

    BUFFER(X)=CPALETTE(V)
NEXT
END SUB


SUB ALIASBUFFER(BYVAL BANK AS INTEGER)
DIM AS INTEGER X,Y,V,XY,Z
FOR Z=1 TO 80
FOR X=XRES*2 TO ((XRES-2)*YRES)   
V=((PBUFFER(X+1,BANK)+PBUFFER(X-1,BANK)+PBUFFER(X+XRES,BANK)+PBUFFER(X-XRES,BANK)) /4)
PBUFFER(X,BANK)=V       
NEXT
NEXT

END SUB


'-------------------------------------------------------------------------------
'ZOOM THE BUFFER
'-------------------------------------------------------------------------------

SUB UPDATE_ONTO_BUFFER(BYVAL ZOOM_FACTOR AS INTEGER,BYVAL BNUM AS INTEGER)
DIM AS INTEGER X,Y
DIM AS INTEGER XC,YC,XL,YL   
    YL=ZOOM_FACTOR
    FOR Y=0 TO YRES-1
    XL=ZOOM_FACTOR
    FOR X=0 TO XRES-1       
        PBUFFER(X+(Y*XRES),BNUM)=(TBUFFER(XL+(YL*XRES))*ZOOM_FACTOR)
        XC=XC+1
        IF XC>=ZOOM_FACTOR THEN
        XL=XL+1
        XC=0
        END IF
    NEXT
    YC=YC+1
    IF YC>=ZOOM_FACTOR THEN
    YL=YL+1
    YC=0
    END IF
    NEXT

END SUB

'-------------------------------------------------------------------------------
'                        GENERATES SOME "WHITE NOISE"
'-------------------------------------------------------------------------------

SUB INTERFERENCE()
    DIM AS INTEGER X,Y
    DIM AS DOUBLE I
    FOR Y=0 TO YRES-1
        FOR X=0 TO XRES-1
            I=INT(RND(1)*50)
            TBUFFER(X+(Y*XRES))=I
        NEXT
    NEXT
    DIM AS DOUBLE RR,GG,BB
    RR=60
    GG=120
    BB=180
    FOR X=1 TO 10000
        RR=RR+.086
        GG=GG+.086
        BB=BB+.123
       
        IF RR>255 THEN RR=255
        IF GG>255 THEN GG=255
        IF BB>255 THEN BB=255
       
        CPALETTE(X)=RGB(int(RR),int(GG),int(BB))
    NEXT
END SUB

Exe attached.

Nb. This is not animated in any way, once you've seen the clouds picture you've seen it all :)
Shockwave ^ Codigos
Challenge Trophies Won:

Offline Optimus

  • DBF Aficionado
  • ******
  • Posts: 2456
  • Karma: 128
    • View Profile
    • Optimouse Demo Site
Re: [PROCEDURAL] Fluffy Clouds
« Reply #1 on: April 18, 2008 »
This is great. Is it perlin noise? I wanted to do something similar yesterday :)

Also, Dr_D even more great, why didn't/don't you posted it in [procedural] as a compo entry or something?
Challenge Trophies Won:

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17412
  • Karma: 498
  • evil/good
    • View Profile
    • My Homepage
Re: [PROCEDURAL] Fluffy Clouds
« Reply #2 on: April 18, 2008 »
Thanks Optimus, I dont think this is exactly perlin noise, I was just messing about and this is what I came up with..
I read some sites about perlin noise before and also lots of other stuff about textures too.

This one generates random noise and merges and blends different magnifications of random noise, so I guess it could be Perlin noise?

Probably someone will tell me I did it wrong!
Anyway, I like this algo because you can make a lot of different things from it.
Shockwave ^ Codigos
Challenge Trophies Won:

Offline mind

  • Texture-San
  • DBF Aficionado
  • ******
  • Posts: 2324
  • Karma: 85
    • View Profile
    • boredom is a beatiful thing.
Re: [PROCEDURAL] Fluffy Clouds
« Reply #3 on: April 18, 2008 »
Thanks Optimus, I dont think this is exactly perlin noise, I was just messing about and this is what I came up with..
I read some sites about perlin noise before and also lots of other stuff about textures too.

This one generates random noise and merges and blends different magnifications of random noise, so I guess it could be Perlin noise?

Probably someone will tell me I did it wrong!
Anyway, I like this algo because you can make a lot of different things from it.


you are doing it wrong!!!    :whack:

naah, just kidding.. or well, if you wanted to do perlin noise(which it seems you didnt) you would be doing it wrong :)

anyways.. your approach gives basically the same result as perlin noise.. perlin noise is basically a n-Dimensional function interpolated across a set of pre-calc gradient vectors. to create something similar to your texture you generate some interpolated noise at different frequencies/amplitudes and add them together to create the final texture, do some tuning to get the nice cloudy look etc.

nice stuff anyhow.. but its so horribly horribly slow it hurts my brain to wait.. go optimize :D
Challenge Trophies Won:

Offline Optimus

  • DBF Aficionado
  • ******
  • Posts: 2456
  • Karma: 128
    • View Profile
    • Optimouse Demo Site
Re: [PROCEDURAL] Fluffy Clouds
« Reply #4 on: April 19, 2008 »
Yes it has the similar result to the perin function. I was talking with some coder friend about perlin and told him I was doing it the way you did it (that's because I read a tutorial about perlin with a presentation of images that looked like magnifications of the actual noise image that are added together. It's the same thing but I think I found it's easier to do perlin noise and experiment with different things by following the perlin way:

You make a static inline function than takes some data (e.g. x,y or even z if in 3D space or maybe some timer variables for animation) and returns something back (a color or gradient value or anything).

This is the perlin I just made today:

static inline int Perlin(int x, int y, int k1, int k2, int k3)
{
   return (fsin1[x+k1] + fsin2[y+k2] + fsin3[x+y+k3]);
}

And then in the main loop you say:

    int c;
   for (int y=0;y<ScreenHeight;y++)
   {
      for (int x=0;x<ScreenWidth;x++)
      {
         c = (Perlin(x,y,k1,k2,k3) + (Perlin(x<<1, y<<1,k1,k2,k3)>>1) + (Perlin(x<<2, y<<2,k1,k2,k3)>>2) + (Perlin(x<<3, y<<3,k1,k2,k3)>>3)) & 511;
         if (c>255) c = 511 - c;
         *vram++ = ((c>>2)<<16) | ((c>>1)<<8) | c;
      }
   }

Like for each pixel you call the perlin function normally, then with the x,y values *2 (<<1) and the final result divided by two (>>1) then <<2 and >>2 then <<3 and >>3 and so on. The good thing for this is that you don't need to generate textures and their magnifications that takes up memory but in that programm the main loop is fixed and then you just change the perlin functions which is one line up there and you get a lot of different stuff. And I could not have the int fsin1,fsin2,fsin3 values (which are just LUT tables of sines) but you can have real sines (maybe slower for realtime but good for texture generation) and stuff. This one with my simple plasma function does great animated stuff with the perlin thing and gets around 100fps in 640x480 animated. Not bad..

The code above is actually the entry I started to do. But maybe I will do something else except perlin because it's already done here. Or maybe I will finish this one first and then try something different..
Challenge Trophies Won:

Offline Pixel_Outlaw

  • Pentium
  • *****
  • Posts: 1382
  • Karma: 83
    • View Profile
Re: [PROCEDURAL] Fluffy Clouds
« Reply #5 on: April 21, 2008 »
Very nice! The effect is quite convincing.  :inspired:
Challenge Trophies Won:

Offline Dr_D

  • Atari ST
  • ***
  • Posts: 151
  • Karma: 29
    • View Profile
Re: [PROCEDURAL] Fluffy Clouds
« Reply #6 on: April 21, 2008 »
Yeah, nice... my first post got moved, so I just wanted to say it was nice again.  :cheers:
The Dr. is INsane!!!

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17412
  • Karma: 498
  • evil/good
    • View Profile
    • My Homepage
Re: [PROCEDURAL] Fluffy Clouds
« Reply #7 on: April 21, 2008 »
Thanks very much for the feedback folks :)

If I get time I will make something else too!
Shockwave ^ Codigos
Challenge Trophies Won:

Offline benny!

  • Senior Member
  • DBF Aficionado
  • ********
  • Posts: 4384
  • Karma: 228
  • in this place forever!
    • View Profile
    • bennyschuetz.com - mycroBlog
Re: [PROCEDURAL] Fluffy Clouds
« Reply #8 on: April 29, 2008 »
N1  :clap:
[ mycroBLOG - POUET :: whatever keeps us longing - for another breath of air - is getting rare ]

Challenge Trophies Won: