Question

let me explain the title a little. In the framebuffer I have some color values (for simplicity I am going to refer only to one color channel in binary e.g. 00000001 in a specific pixel). Then in that specific value I am going to write a new value. The OpenGL should examine the incoming value and the value that already exists in that pixel and keep the max of both. In other words I want it to perform a BITWISE OR between the incoming and the existing value. So, if the existing value is 00000001 and the incoming is 00000010 then the result must be 00000011.

In OpenGL ES 1.1 I think this was easily achieved using the glLogicOp (http://www.opengl.org/sdk/docs/man/xhtml/glLogicOp.xml) function. But this is NOT supported in OpenGL ES 2.0 (WHY WHY WHY they removed it?? :( ), and I cannot think of a blending function that can achieve a similar result (I think this is impossible using blending). Should I use the stencil buffer or another trick in order to achieve my goal?

Can you describe the steps for it?

P.S. I know everything is possible using shaders but this will require a huge effort to implement now. If it is the only solution then please give me directions for this approach.

Thanks in advance.

Was it helpful?

Solution 2

I must admit I was not so clear in my question. Anyway, I will refine the problem and present a resolution to it here.

I have many different kind of arbitrary shapes (the user can create as many of them he wants and crazily deform them) and I want to develop a method to detect from the color of a pixel on the screen how many of the shapes the user picked (the user has just touched on that pixel).

So, I color code the shapes with let's say some kind of color ids. I work in the binary level: the first shape on the screen has color id 00000001 the second shape has id 00000010 and so on. So, with a byte (in the red channel let's say) I can store up to 8 different color ids. The max number of shapes is 32 if I use an RGBA8888 format.

The algorithm that I initially developed was doing blending between the source color id and the destination color id with a glBlendFunc(GL_ONE, GL_ONE), and everything seemed to work fine. So if a touched pixel with color 00010101, then it meant that the first, third and fifth shapes were touched. BUT, there are some non-convex shapes (if this is the right word for folding shapes) that can be created by the user. In this case if the shape with color id 00000001 is folded by the user then the overlapping pixels will get id 00000010, because the shape writes 00000001 when it is first drawn on the framebuffer and then the overlapping pixels of the same shape add another 00000001 due to blending.

So, if in some way I could do a BITWISE OR (NOT logical as I initially mentioned in my question) between the color ids that were written in the framebuffer then the problem would be solved. Indeed this is the case with OpenGL ES 1.1, by using the glLogicOp(GL_OR) function. UNFORTUNATELY this is not available on OpenGL ES 2.0 and again one shader, one more pass etc must be created which I think is overkill and difficult to maintain.

The solution for OpenGL ES 2.0 is this:

  1. Clear the color of RenderTexture B to glClearColor(0,0,0,0)
  2. FOR each shape
  3. Clear the color of Rendertexture A to glClearColor(0,0,0,0)
  4. Draw the shape on Rendertexture A using a fragment shader that writes as a color its color id.
  5. Blend with GL_ONE, GL_ONE the Rendertexture A with the contents of Rendertexture B
  6. END FOR
  7. RenderTexture B has all the info we need.
  8. When the user touches the screen, sample the corresponding pixel from RenderTexture B using glReadPixel and find out from the binary code the touched shapes.

Best

OTHER TIPS

I think your going to have to use a shader for that.

1) Create two renderTargets[2] that will be used for swapping with a swapIndex.

2) Render to one renderTarget[swapIndex] and use the other renderTarget[1-swapIndex] as a reference texture that you will sample from in you GLSL shader.

3) In the fragment(aka pixel) shader you would sample from your texture(aka renderTarget[1-swapIndex]) and set "gl_FragColor" to max(texture, someColor).

4) Flip the swapIndex {swapIndex = 1-swapIndex;} and start over.

@Summon, sorry if I add an additional answer instead of a comment, I don't have enough reputation to leave comments.

Anyway:

Point 1: I am sure 100% you cannot perform bitwise or/and/shift in shaders since I had to trash about 2 weeks of programming for this oversight just last month. :) :)

Point 2: Look at this article on wikipedia about GLSL (section Operators):

Bitwise operators were added in version 1.30.

A. If you try to add #version 130 to your shader, they won't compile. B. If you try to add a bitwise operation such as A & B (notice the single &), A | B, A << 2, B >> 2 etc, it won't compile.

Reading around, on different books/forums/SDK, I have found the same confirmation.

I hope to have avoided the same headache I have had on this matter.

Maurizio Benedetti

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top