AminetAminet
Search:
84741 packages online
About
Recent
Browse
Search
Upload
Setup
Services

demo/misc/water-os4.lha

Mirror:Random
Showing: m68k-amigaos iconppc-amigaos iconppc-morphos iconi386-aros iconi386-amithlon iconppc-warpup iconppc-powerup icongeneric icon
No screenshot available
Short:A realtime SDL water effect
Author:Scott Scriven, AmigaOS 4.0 compile by Spot / Up Rough
Uploader:Varthall / Up Rough <varti02 hotpop com>
Type:demo/misc
Version:1.0
Architecture:ppc-amigaos
Date:2008-02-07
Download:demo/misc/water-os4.lha - View contents
Readme:demo/misc/water-os4.readme
Downloads:904

Everything included (code, executable, text, etc...) is under GPL and
Copyright (C) 2001 Scott Scriven


----------------------------------------
Internet addresses
----------------------------------------
My email address is:
        water()xyzz.org
My web page is at:
        http://www.xyzz.org/


--------------------------------------
Water
--------------------------------------
The water effect is one of my favorite effects.  It looks amazingly like the
real thing, if done right; and yet it doesn't have to conform to physics...

I've added some very non-water-like stuff to this version, using the same
algorithms but by doing slightly different things with it.

Just run the executable to see what it looks like...


------------------------------------
Files included...
------------------------------------
water .txt    The file you're now reading... I hope.
Makefile Just run "make"
water .c      The source code...
datatype.h More source
fixsin .c More source
fixsin .h More source
fps .c More source
fps .h More source
water320.bmp    Background picture for 320x200 water...


------------------------------------
The source code
------------------------------------

I've tried to comment the source a bit, so hopefully it's fairly readable.


------------------------------------
Performance -- speed
------------------------------------
When I run this on my 486/66, I get around 9 fps with the 320x200 version.
The 160x100 version gets around 37 fps.  There is no speed checking, so this
would look a bit silly on a PPro 200.

I've since gotten a new motherboard/cpu, and am now running it on a Cyrix
P166+ that seems a bit sluggish compared to brand-new systems with the same
cpu.  (but it's faster than any Pentium166 I've seen...)  I get around
35 fps on the 320x200 version of water now.

I'm fairly happy with the speed it goes.  It's about 1.25 - 1.5 times as fast
as the original water I saw, and that one was written in optimized assembly.
I've managed to get this to go faster even though it's pure C and it uses
more surrounding pixels to calculate the height map.

I intend to make a Silcon Graphics version sometime, too.  That way I can
run it at high resolutions and still get decent speed.  A friend of mine,
a brilliant but misguided friend, has made a mac port.  So, if you're a
mac user, you might be able to find it somewhere (I don't know where).


--------------------------------------
How it works
--------------------------------------
Water is a four-step process.  First, you must create some sort of turbulence
in the water (like raindrops).  Then, make the height map ripple and move.
After that, we can apply the height map to a background image and then put it
onscreen.

We'll need two height maps, though.  That way we can use one as a "source"
map and one as a "destination".  This is to make the filter work correctly.

The main loop, pretty much, is:
{
  add_turbulence();
  calculate_height_map();
  apply_height_map();
  Put_it_onscreen();
}

Adding turbulence is easy.  Just do whatever you like to the height field 
that represents the surface of the water.  If you draw a "high" circle, it 
looks like something big splashed into the water....  I also like to have a
smoothly moving point that looks like a surfer.

Making the height map ripple is done by applying a rather odd filter to it.
The filter is (if I'm not mistaken):
  1  1  1
  1 -4  1
  1  1  1 division: 4
After filtering a single pixel, we multiply it times a number between 0 and 1
that represents the density of the water, which we do instead with a shift
and a subtraction.

The source is something like this:
{
  for each pixel(you'll need to use two height-field pages):
    temp= src[x-1][y-1]
        + src[x  ][y-1]
        + src[x+1][y-1]
        + src[x-1][y  ]
        -(src[x  ][y  ] * 4)
        + src[x+1][y  ]
        + src[x-1][y+1]
        + src[x  ][y+1]
        + src[x+1][y+1];
    
    temp /= 4;

    dest[x][y] = temp - (temp >> density);
}
The density seems about right at 4.


Applying the height map distorts the background image (whatever is 
"underneath" the water) and simulates light reflection.

We need to (for each pixel) figure out the slope of the water at that point,
and then add a corresponding value to the actual coordinate we read the pixel
from.  Also, we'll use the x slope as an indication of how much to increase
or decrease the brightness of that pixel (fake lighting).

It goes like this:
{
  for each pixel:
    deltax = map[x][y] - map[x+1][y]; // how far is the image
    deltay = map[x][y] - map[x][y+1]; // stretched?
    c = 128 - deltax; // apply fake lighting...
    offsetx = (deltax/8) +x; // final pixel we'll read from
    offsety = (deltay/8) +y;
    c = (BkGdImage[offsetx][offsety] * c) /128; // Get a value
    bound c between 0 and 255; // and make sure it doesn't overflow
    final[x][y] = c; // write the pixel...
}


After this step, you just need to put the final image onscreen and restart
the loop.


------------------------------------------
"Turbulence" (or, effects)
------------------------------------------
I've made several effects that you can try out...

HeightBlob: This makes (in the height map) a cylinder-like shape.  This
makes a sharp contrast and creates crisp edges and small ripples.

SineBlob: This takes a lot more cpu time than HeightBlob, but it makes
more interesting things happen.  In the height map, this creates a shape
that is like a sine wave but rotated around to form a "bump".  I am not a
good ascii-artist, but here's a small picture of what a bump would look
like from the side:        _,*`'*,_
This usually distorts the image underneath as if you had used a "pinch" or
"punch" effect in a paint program.

There is also a "WarpBlob", but it isn't used.  It makes half-sphere shapes
instead of "bump" shapes.  It looked okay in interactive mode, but didn't
work well when movement was turned off.


Surfer (in water mode): The surfer just moves around in a sine-wave pattern,
and is drawn by placing five pixels in a "+" shape.  This also tends to
create sharp edges.

Surfer (in other modes): This just draws SineBlobs in a sine-pattern, and
looks much smoother than the regular surfer.  It looks better for images
that don't work with lighting on.

"Swirly" mode:  This is very similar to if you stuck your finger in the
water and tried to create a whirlpool.  I like to use this (with lighting)
on the regular water background in "sludge" mode.

Raindrops:  This is just kind of cheesy.  I don't really like this effect,
but it's a nice way to "warm people up" for the more impressive effects.


-------------------------------------------------------------
My favorite combinations of effects (or, fun things to try)
-------------------------------------------------------------
First, if you're using an image that works with lighting, make sure to
turn lighting on whenever you use any of these effects.  Otherwise, make
sure the lighting stays off.  Also, I'm assuming that you have just entered
the program when I state which keys to press.

Whirlpool:  Press s, 4.
Neat surfer:  Press s, 1, D.
Bumpy:  Press b.
Bumpy Surf:  Press b, 1.
Really distorted:  Press D.  Make a little bit of disturbance somehow.  Now
        press z in rhythm with the rising and falling of the water, but make
        sure not to get the waves too high!  (program will crash if you do)
        If you're really good, it is possible to make the background
        move as if it was a trampoline (but you have to have *really* good
        timing with the z key).
Regular water:  Press w, 1, 3.
Distort-o-matic:  Press s, m.  Draw on the image with the mouse, distorting
        things however you want (more fun than GifWarp, if you remember that
        program...).  When you're done, press m again to "release" the
        surface of the water.

And, of course, just playing.  I like to use the picture of the lady's
face, and make ripples on the tip of her nose in rhythm with the water
in sludge mode.  It's fun to distort people's faces...  :)


-------------------------------------------
Please credit me for this if you use it...
-------------------------------------------


Contents of demo/misc/water-os4.lha
 PERMSSN    UID  GID    PACKED    SIZE  RATIO METHOD CRC     STAMP          NAME
---------- ----------- ------- ------- ------ ---------- ------------ -------------
[generic]                   63      69  91.3% -lh5- 16c6 Oct  8  1999 water-1.0/Docs/BUGS
[generic]                 5873   15145  38.8% -lh5- 40e2 Oct  8  1999 water-1.0/Docs/COPYING
[generic]                  320     531  60.3% -lh5- 4972 Oct  8  1999 water-1.0/Docs/README
[generic]                 3450    8203  42.1% -lh5- e122 Apr 28  2001 water-1.0/Docs/water.txt
[generic]                  368     715  51.5% -lh5- a824 Apr 28  2001 water-1.0/Source/fixsin.c
[generic]                  215     353  60.9% -lh5- 33e0 Apr 28  2001 water-1.0/Source/fixsin.h
[generic]                  346     729  47.5% -lh5- 290b Apr 28  2001 water-1.0/Source/fps.c
[generic]                  178     273  65.2% -lh5- 7f2e Apr 28  2001 water-1.0/Source/fps.h
[generic]                 2233    5598  39.9% -lh5- a21f Dec  6  1999 water-1.0/Source/install-sh
[generic]                 3065    9286  33.0% -lh5- 48cc Oct 17  2006 water-1.0/Source/Makefile
[generic]                  120     173  69.4% -lh5- 46a7 Dec  6  1999 water-1.0/Source/Makefile.am
[generic]                 2947    8878  33.2% -lh5- 20be Dec 21  1999 water-1.0/Source/Makefile.in
[generic]                 2135    6283  34.0% -lh5- b90d Dec  6  1999 water-1.0/Source/missing
[generic]                  380     722  52.6% -lh5- c7b6 Dec  6  1999 water-1.0/Source/mkinstalldirs
[generic]                 4684   19962  23.5% -lh5- e326 Apr 28  2001 water-1.0/Source/water.c
[generic]               131378  327516  40.1% -lh5- 9f2a Oct 17  2006 water-1.0/Water
[generic]                17465   65078  26.8% -lh5- 3824 Oct 17  2006 water-1.0/water320.bmp
[generic]                 2182    6036  36.1% -lh5- ff39 Dec 21  1999 water-1.0/Source/acinclude.m4
[generic]                 3733    9576  39.0% -lh5- f6f0 Dec 21  1999 water-1.0/Source/aclocal.m4
[generic]                  541    1193  45.3% -lh5- f52f Oct 17  2006 water-1.0/Source/config.cache
[generic]                  475    1255  37.8% -lh5- b162 Oct 17  2006 water-1.0/Source/config.log
[generic]                 2161    5992  36.1% -lh5- 8874 Oct 17  2006 water-1.0/Source/config.status
[generic]                14399   50093  28.7% -lh5- 622e Dec 21  1999 water-1.0/Source/configure
[generic]                  278     417  66.7% -lh5- af91 Dec 21  1999 water-1.0/Source/configure.in
[generic]                  330     808  40.8% -lh5- 030f Apr 28  2001 water-1.0/Source/datatype.h
---------- ----------- ------- ------- ------ ---------- ------------ -------------
 Total        25 files  199319  544884  36.6%            Feb  7 08:44
Page generated in 0.02 seconds
Aminet © 1992-2024 Urban Müller and the Aminet team. Aminet contact address: <aminetaminet net>