Skip to main content
GameDev.net gamedev.net
🔒 Locked

Pixel Manipulation, How?

Started by AfroMogli Jan 23, 2005 at 9:47 AM 0 replies 550+ views
Original Post
AfroMogli
AfroMogli
Hi! I'm currently working on a game project involving some water. To produce the water effect I'm converting some pascal code to C++ code from an article which you can find here on the site. Direct link: http://www.gamedev.net/reference/articles/article915.asp I have converted pretty much everything of the original code to C++ but when I got to the second part of the code I discovered that it contained some to me strange code. But before I'm going to start talking about the problem here you got the code to see how it all looks right now:
 
const int mapSize_X = 10;
const int mapSize_Y = 10;

int damp = 16;								// Damping factor

float current_WM[mapSize_X][mapSize_Y]; // Array for current wavemap
float new_WM[mapSize_X][mapSize_Y];		// Array for next wavemap
float temp_WM[mapSize_X][mapSize_Y];	// Array for the temporary wavemap

...

void CMap::Load()
{
// Initiates the two map arrays 
	for(int x =0; x<mapSize_X; x++)
	{
		for(int y=0; y<mapSize_Y; y++)
		{
			current_WM[x][y] = 0;
			new_WM[x][y] = 0; 
		}
	}	
	
} // End Load()

void CMap::Animate(float deltaTime)
{
		int y, x;
		float n;
		
		// The wave algoritm.
		for(y = 1; y<mapSize_Y; y++)
		{
			for(x = 1; x<mapSize_X; x++)
			{
				n = ((current_WM[x-1][y] + 
					 current_WM[x+1][y] +
					 current_WM[x][y-1] +
					 current_WM[x][y+1]) / 2) -
					 new_WM[x][y];

				n = n - (n/damp);

				new_WM[x][y] = n;
			}
		}
}// End Animate()

void CMap::Draw()
{
		float	xTempDiff, yTempDiff,
				xAngle, yAngle,
				xRefraction, yRefraction,
				xDisplace, yDisplace;

		int xDiff, yDiff;
		int rIndex = 2;					// Water index

		// Algoritm that calculates the refraction angle of incoming light. 
		for(int y = 1; y<mapSize_Y; y++)
		{
			for(int x = 1; x<mapSize_X; x++)
			{
				xTempDiff = new_WM[x+1][y] - new_WM[x][y];
				yTempDiff = new_WM[x][y+1] - new_WM[x][y];

				xDiff = (int)xTempDiff;
				yDiff = (int)yTempDiff;

				xAngle = atan(xDiff);						// Calculate angel 
				xRefraction = asin(sin(xAngle) / rIndex);	//    -||-   refraction
				xDisplace = tan(xRefraction) * xDiff;		//    -||-   displacement

				yAngle = atan(yDiff);						// Calculate angel 
				yRefraction = asin(sin(yAngle) / rIndex);	//    -||-   refraction
				yDisplace = tan(yRefraction) * yDiff;		//    -||-   displacement

				if(xDiff < 0)
				{
					if(yDiff < 0)
					{
						//newcolor = BackgroundImage[x-xDisplace, y-yDisplace]
					}
					else
					{
						//newcolor = BackgroundImage[x-xDisplace, y+yDisplace]
					}
				} // End if
				else
				{
					if(yDiff<0)
					{
						//newcolor = BackgroundImage[x+xDisplace, y-yDisplace]
					}
					else
					{
						//newcolor = BackgroundImage[x+xDisplace, y+yDisplace]
					}
				} // End else

				// TargetImage[x, y] = newcolor;

			} // End if
		}// End if

		
		for(int x = 0; x<mapSize_X; x++)
		{
			for(int y=0; y<mapSize_Y; y++)
			{
				temp_WM[x][y]		= current_WM[x][y];
				current_WM[x][y]	= new_WM[x][y];
			}
		}

		for(int x = 0; x<mapSize_X; x++)
			for(int y = 0; y<mapSize_Y; y++)
				new_WM[x][y] = temp_WM[x][y];

} // End Draw()

As you can see the last part of the code contained an if that was empty. I have written the original code in comment so that you see how it looks. Now, as you can see, depending on the xDiff and yDiff a new color will be picked from the background image by somehow getting the pixel from the requested x and y position. And then later on add this newcolour to the "TargetImage", so I'm asking; what is this "TargetImage" in OpenGL? Is it the Color Buffer or is it just another image? And... How do I get the pixel-color from an bmp-image? Please guys, I need water! :)
Does: C++ combined with OpenGL games and other visuals. Dream: To become a REAL gameprogrammer. Yeah!
Ajare
Ajare
If I understand you correctly...

To get the correct pixel from an image: you need to load the image into an array, and if you know the width, height and colour depth of it you get the pixel easily. For 8 bit (palette indexed) images, just do image_buffer[y * width + x], for 24 bit truecolour, do:

r = image_buffer[(y * width + x) * 3 + 0]
g = image_buffer[(y * width + x) * 3 + 1]
b = image_buffer[(y * width + x) * 3 + 2]

There are functions for loading .BMP files - check Nehe's Tutorial, for other image formats, you need to know the format. You just need to load the image data into an array, you don't need to upload the texture to memory.

The 'targetimage' is the screen. For drawing the pixel on screen, there are various approaches. OpenGL wasn't designed with single-pixel manipulation in mind, but I'd check out the glDrawPixels function. You could draw each pixel as a quad, but I wouldn't recommend it if you're worried about framerate.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.