Advertisement Jump to content
Sign in to follow this  

OpenGL Drawing Plasma for Audio Visualization

This topic is 5001 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I was wondering if anyone had a simple algorithm for drawing plasma. I am trying to write an OpenGL program to simulate that plasma before I can implement it in hardware. The problem is that my plasmas have very regular shapes. Also, since they will have to change depending on the audio that is input (like a winamp visualization) I need to be able to affect the plasma by playing with some of the variables. But they need to have smooth changes. The current algorithm I have right now is as follows: I have a 512x512 matrix (representing my screen raster) that holds values of intensity from 0 to 511 that were obtained by adding 2 cos and 2 sin. Now when I draw to screen, I go through the matrix and each intensity value refers to a specific color stored in an array. Making the pattern move is done through adding an offset to the color index with wrap around. The smoothest results are obtained by storing a gradient in that array. However, it's too regular. I get circles and squares, etc. I'm looking for the more diffuse plasma look. As well as one with variables that affect how the plama behaves but without too drastic a change. I'm posting some of my code here. If you have any ideas or suggestions, please tell me.
void initColorArray()
	int i;
	float tempR, tempG, tempB;
	long pos1;
	tempR = 1.0f;
	tempG = 0.0f;
	tempB = 0.0f;

	colorArray = (Color*)malloc(sizeof(Color)*512);

	// Array loaded with a gradient
	for(i = 0; i < 512; i++)
		colorArray = createColor(tempR - i*1.0f/512.0f, tempG, tempB + i*1.0f/512.0f);
	// A table holding a few cos and sin values so that I only need to compute them once
	for (pos1 = 0;pos1 < 360;pos1++)
		tcos[pos1] = abs(cos(pos1*PI/180) * 128);
		tsin[pos1] = abs(sin(pos1*PI/180) * 128);

void Move_Plasma(long pos1, long pos2, long pos3, long pos4, int pattern)
	// pos1 through pos4 affect the general shape direction of movement
	long i,j,color,p1,p2,p3,p4;
	p1 = pos1;
	p2 = pos2;
	for (i = 0;i < 512;i++)
		p3 = pos3;
		p4 = pos4;
		for (j = 0;j < 512;j++)
			color = tcos[p1 % 360] + tcos[p2 % 360] + tsin[p3 % 360] + tsin[p4 % 360];

			// Pattern here affects what colors in the gradient are referenced
			if(pattern == 0)
				// do nothing
			else if(pattern == 1)
				color = (color + 10) % 512;
			else if(pattern == 2)
				color = (color + 20) % 512;
			else if(pattern == 3)
				color = (color + 30) % 512;

			Color tempC = colorArray[color];
			glColor3f(tempC.R, tempC.G, tempC.B);
			glVertex3f((float)i*0.0097f, (float)(-1*j*0.0097f), 0);
			// p1 through p4 affect the movement and the general shape of the pattern
			// however, changing them changes the pattern instantly. 
			// There is no smooth transition
			// these 2 affect the number of patterns along the height			
			p3 += 1; // + sideways transmutation for this one
			p4 += 1; // + vertical transmutation for this one
		// these 2 affect the number of patterns along the width
		p1 += 1; // + vertical transmutation
		p2 += 1; // + sideways transmutation

Thanks in advance

Share this post

Link to post
Share on other sites
The trick is too use an algorithm to generate a texture (what you are doing at the moment, from what I understand), and then to have another algorithm which deals with the animation of the picture.
Specifically, I am thinking of using a mesh warping to morph your picture from frame to frame.
I read a great little article years ago that showed a very primitive method for doing just that, which resulted in the picture "flowing" around the screen in a very smooth fashion.
Think of the sort of effects that you get in WinAmp, that's what I am talking about. Now apply this flowing algorithm to a simple plasma cloud texture, possibly with colour cycling, and you should have a good basis to work with.

if you dont know what a plasma cloud is, look it up, it's a very simple algorithm too.

Actually, after a little bit more googling, I think I found the site I was thinking of. I leave it as an exercise for you to find the article about warp maps...

You might also wanna check out the Demomaking series on Flipcode, some of their stuff might be useful?

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!