Jump to content
  • Advertisement
Sign in to follow this  
XTAL256

Copying raw pixel data

This topic is 3668 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

I am working on a game engine similar to Worms where the map environment is deformable. In order to do this (on a per-pixel basis) i need to copy the pixel from the tile images to my map image to build the map from tiles. I am using OpenGL but i can't use a texture for the map image since it needs to be power-of-two. So is there a way using OpenGL that i can copy pixels from source to destination not using the frame buffer? i.e. something like this:
copyPixels(void* src,  int srcX,  int srcY, int srcW, int srcH,
           void* dest, int destX, int destY)
or will i have to write it myself?

Share this post


Link to post
Share on other sites
Advertisement
Is this right?

void copyPixels(unsigned char* src, int srcX, int srcY, int w, int h,
unsigned char* dest, int destX, int destY) {
for (int i = 0; i < h; i++) {
memcpy(dest+((i+destY)*w)+destX, src+((i+srcY)*w)+srcX, w);
}
}



And is there a better way of doing it?
I tried stepping through it in the debugger (Visual Studio) and it didn't seem to work, but my code is far from complete there might be a problem elsewhere.

EDIT: oh, i just realised that the image data is RGBA so it will need to be 4 bytes. But i also have terrain data that is only one byte so i suppose i will just make it void* and specify the number of bytes.

[Edited by - XTAL256 on July 19, 2008 6:56:22 AM]

Share this post


Link to post
Share on other sites
you will have to stretch the texture to be a power of two, first find the power greater than the image width and height, so:


void newValue(int a)
{
int size = 1;
while(size < a)
size <<= 1;
return size;
}

destX = newValue(<imagewidth>);
destY = newValue(<imageheight>);




assuming that the color formats will stay the same then you can stretch the image quite easily:




template <class T>
copyPixels(void* src, int srcX, int srcY, int srcW, int srcH,
void* dest, int destX, int destY)
{

float xInterpolant = (float)srcX / (float)destX;
float yInterpolant = (float)srcY / (float)destY;

T* dst = (T*)dest;
T* source = (T*)src;

float cy = 0.0f;
for(int y = srcY; y < srcH; ++y)
{
float cx = 0.0f;
for(int x = srcX; x < srcW; ++x)
{
dst[(int)cy * srcW + (int)cx)]
= source[y * srcW + x];

cx += xInterpolant;
}
cy += yInterpolant;
}

}








ive used templates just to illustrate what size of data you are copying to and from, ive the color format is 32-bit use int, if it is 16-bit use short etc. etc.

please anyone correct me if the code is wrong.

[Edited by - staticVoid2 on July 20, 2008 5:16:34 AM]

Share this post


Link to post
Share on other sites
Thanks for the code but i don't think i will be using powers-of-two. The problem is that i need to manipulate and display each pixel, i.e. if a player destroys part of the map by shooting a rocket at it then there will be a hole left in the ground. Also i am planning on making dirt fall down if part of a cliff or cave is destroyed. This will be done per pixel so i won't be able to stretch my image. I believe i can just use glDrawPixels to draw the raw pixel data to the screen, am i right? But i don't think i can use a texture (as power-of-two).

Share this post


Link to post
Share on other sites
I have got my code working but i realised that i will eventually need to do a bit more. Currently i am just looping through each line copying the data from one array to the other. But that doesn't do a few things that i may eventually need, e.g.
- part of the src bitmap goes over the bounds of the dest bitmap, currently i get array index out of bounds error.
- alpha channel. Transparent areas will show the dest bitmap underneath instead of copying transparency (colour 0,0,0,0) over the dest bitmap. Also for varying gradients of transparency.
I will get around to this problem when i have my game working but i would just like to know know if there is a way to do this using OpenGL. That is, copy pixels from one memory buffer to another (ideally without the need to draw to the screen buffer using glDrawPixels then glCopyPixels to copy screen to another buffer).
thanks.

Share this post


Link to post
Share on other sites
Hi again. I am still stuck on this problem. To solve the two issues in my last post i thought i would draw each tile to the screen buffer (or whatever it's called) using glDrawPixels and then copy the screen using glCopyPixels. But i can't get it to work and there are still some problems with that method. I store each set of tiles in a single image (which i call a "composite image"), this is read from an image file. This is the way a lot of games store sprites or tiles, by packing them all in a single image, so i want to do it that way too. But i need to copy a specific tile from that image to the screen.

As you can see, i need to copy a rectangle (x,y,w,h) from one image to, well, i am going to copy it to the screen (or another buffer, i don't really know that much about OGL buffers). But the important thing is that i can't use glDrawPixels because i am passing the whole composite image when i only want to copy a portion of it. Can anyone help me?
thanks

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!