PDA

View Full Version : Logical operations



SmegPlaza
01-08-2008, 09:10 AM
Hi,

As a relative newcomer to OpenGL I have a question which I'm struggling to find a definitive answer for - maybe you guys can help me please ?

I'm developing an application where I am blitting 2D, rectangular pixel arrays from one context to another, and I want to perform logical operations on the pixels as they are transferred. I've found the glLogicOp() function, in conjunction with the XOR operation, which seems to do the trick.

If I blit an array of (source) pixels onto an array of pre-existing, and different, (destination) pixels, using the XOR logical operation, I would expect the resulting pixel array to be dissimilar to both the original source and destination arrays. If the source and destination arrays are the same I would expect the resulting array to be empty i.e. full of 'blank' pixels.

My question is, is there a way to detect what the result of that XOR operation is ? In other words can I tell whether the resulting pixel array is empty or not ?

Ideally, this should be a fast test. The pixel arrays may be large, and the test has to be done frequently, so checking every pixel individually would not be a good solution.

Any suggestions you can make would be greatly appreciated.

Smeg

k_szczech
01-08-2008, 10:38 AM
I assume you don't want to use fragment shaders for it, right?
You can do the folowing:
1. Copy these pixels to texture (glCopyTexImage2D)
2. Set this texture's filtering to GL_NEAREST (no filtering)
3. Draw a quad with this texture, but shrink it 2x vertically so you only render odd lines (1,3,5,7...)
4. Enable blending, set blend function to (GL_ONE, GL_ONE)
5. Render again, but offset texture coordinates, so you would only render even lines now (2,4,6,8...)
6. Disable blending
7. repeat steps 1-6 untill your texture will have size of 1x1 (shrink horizontally and vertically)
8. Retrieve texture contents (glGetTexImage)

The result is one pixel that is a sum of all pixels from the original image. If it's 0, then it means all pixels were 0.

This should work fast, because it works on GPU, and the only thing that CPU does is to test one pixel at the end.

With shaders it would be even better - you would just render one quad, and shader would discard all pixels that have value of 0. An occlusion query would tell you how many pixels were rendered.

I hope everything's clear :)