Dark Bit Factory & Gravity

GENERAL => Challenges & Competitions => Topic started by: Shockwave on April 17, 2008

Title: [PROCEDURAL] Fluffy Clouds
Post by: Shockwave 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 :)
Title: Re: [PROCEDURAL] Fluffy Clouds
Post by: Optimus 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?
Title: Re: [PROCEDURAL] Fluffy Clouds
Post by: Shockwave 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.
Title: Re: [PROCEDURAL] Fluffy Clouds
Post by: mind 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
Title: Re: [PROCEDURAL] Fluffy Clouds
Post by: Optimus 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..
Title: Re: [PROCEDURAL] Fluffy Clouds
Post by: Pixel_Outlaw on April 21, 2008
Very nice! The effect is quite convincing.  :inspired:
Title: Re: [PROCEDURAL] Fluffy Clouds
Post by: Dr_D on April 21, 2008
Yeah, nice... my first post got moved, so I just wanted to say it was nice again.  :cheers:
Title: Re: [PROCEDURAL] Fluffy Clouds
Post by: Shockwave on April 21, 2008
Thanks very much for the feedback folks :)

If I get time I will make something else too!
Title: Re: [PROCEDURAL] Fluffy Clouds
Post by: benny! on April 29, 2008
N1  :clap: