Flipping an image

Hello,

i needed a suggestion regarding the method im using to flip an image:

i am getting live feed from a webcam. (im continuosly obtaining frames from the webcam using OpenCV)

Now, im drawing these frames onto a glut window. The frame im obtaining from OpenCV is flipped, so i wrote this:

image_data[] has the raw pixel information. This is later used in glDrawPixels() to draw the frame.


/* 640x480 image */
for (unsigned int i = 0, j = (640 * 480 * 3) - 1; i <= j; i++, j--) {
	unsigned char temp = image_data[i]; 
	image_data[i] = image_data[j];
	image_data[j] = temp;
}    

Now, this happens many times in a second. Is this the right way to do it? i mean isn’t it very expensive in terms of computation?

Any suggestions would be really helpful.

Thanks.

I suggest to you use a texture instead and just apply the texture to a quad.
1st, allocate the texture
glGenTextures(1, &textureID);
glBindTexture(GL_TEXTURE_2D, textureID);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_BGRA, GL_UNSIGNED_BYTE, NULL);

notice the NULL for glTexImage2D. It will just allocate memory for the texture in VRAM.

2nd
update the texture with glTexSubImage2D.

Also, since you will be using a non power of 2 texture, make sure GL 2.0 is supported.

Thanks a lot V-man, i will try the method suggested by you.

Don’t forget, if you flip the quad’s coordinates, the texture will get flipped also. So you can either flip by providing ‘flipped’ texture coordinates to your vertices, or you can flip the vertex coordinates themselves.

Thanks ugluk :).