Dark Bit Factory & Gravity
PROGRAMMING => General coding questions => Topic started by: Wryneck on November 01, 2009
-
In a vague attempt to practice my graphics programming, I'm trying to code one of those classic plasma effects. You've all seen them a million times, I bet. Although I'm getting something which is somwhat close to what I'm after, it's not close enough. I haven't quite grasped the theory.
If you could donate a couple of minutes to explain how a simple plasma works, I'd be grateful.
First post, by the way! :cheers:
-
Hi Wryneck, welcome to the forum.
There are lots and lots of ways to make plasma like effects so there's no right or wrong way to do them, obviously you have something specific in mind, perhaps you saw it in a specific production?
In the old days they were often produced with some precalculated colour table and the copper co-processor on the Amiga. These days the same effect can almost always be accomplished in real time with a bit of sin and cos.
So in theory, just to explain how it works, you have width and height on your screen so two nested loops X and Y;
Pseudocode;
FOR Y = 0 TO YRES-1
FOR X=0 TO XRES-1
REDVALUE = SOME CALCULATION INCORPORATING X & Y
GRNVALUE = SOME CALCULATION INCORPORATING X & Y
BLUVALUE = SOME CALCULATION INCORPORATING X & Y
PLOT X,Y, RGB (REDVALUE , GRNVALUE , BLUVALUE)
NEXT
NEXT
If you need any more pointers, let us know.
-
A lot of them are done using 'simple harmonic motion' applied to the r,g,b channels.
SHM looks like this:
value = amplitude * sin(frequency * angle)
In our case, for a plasma, we want rgb values to come out, any one of these channels has a range 0-255. We know that sin(anything) goes between -1 and +1, so to scale that we need to do
value = 127 + 127 * sin(frequency * angle)
That will give us a number between 0 and 254, which is good enough.
Now we need to look at the frequency * angle bit. Usually you want this number to be some function of x, y pixels (x+y will do for a start), and you want the frequencies of the r,g,b channels to be different, so you might have something like:
red = 127 + 127 * sin(1 * (x+y))
green = 127 + 127 * sin(3 * (x+y))
blue = 127 + 127 * sin(5 * (x+y))
colour = (r<<16)+(g<<8)+b
That will give you a pretty rubbish plasma, but it gives you the idea. Almost all the cool Amiga plasmas were done by combining different functions for (x+y), and combining other trig functions such as cos, tan and the rest.
Other great plasmas can be done using 'Perlin noise' which is worth plugging in to Google.
Jim
-
With a little bit of poking about the source, I actually managed to come up some pretty zesty plasmas. Thanks for the replies! Definitely looking into perlin noise.