Android OpenGL2.0 intersection between two textures

微笑、不失礼 提交于 2020-01-06 15:54:30

问题


I'm making game in OpenGL2.0 and I want to check are two sprites have intersection but i don't need to check intersection between two rectangles.I have two sprites with texture,some part of texture is transparent,some not. I need to check intersection between sprites only on not trasnparent part.

Example: http://i.stack.imgur.com/ywGN5.png


回答1:


The easiest way to determine intersection between two sprites is by Bounding Box method.

Object 1 Bounding Box:

vec3 min1 = {Xmin, Ymin, Zmin} 
vec3 max1 = {Xmax, Ymax, Zmax}

Object 2 Bounding Box:

vec3 min2 = {Xmin, Ymin, Zmin} 
vec3 max2 = {Xmax, Ymax, Zmax}
  1. You must precompute the bounding box by traversing through the vertex buffer array for your sprites. http://en.wikibooks.org/wiki/OpenGL_Programming/Bounding_box

  2. Then during each render frame check if the bounding boxes overlap (compute on CPU).

    a) First convert the Mins & Maxs to world space.

    min1WorldSpace = modelViewMatrix * min1

    b) Then check their overlap.

I need to check intersection between sprites only on not trasnparent part.

Checking this test case maybe complicated depending on your scene. You may have to segment your transparent sprites into a separate sprite and compute their bounding box.

In your example it looks like the transparent object is encapsulate inside an opaque object so it's easy. Just compute two bounding boxes.




回答2:


I don't think there's a very elegant way of doing this with ES 2.0. ES 2.0 is a very minimal version of OpenGL, and you're starting to push the boundaries of what it can do. For example in ES 3.0, you could use queries, which would be very helpful in solving this nicely and efficiently.

What can be done in ES 2.0 is draw the sprites in a way so that only pixels in the intersection of the two end up producing color. This can be achieved with either using a stencil buffer, or with blending (see details below). But then you need to find out if any pixels were rendered, and there's no good mechanism in ES 2.0 that I can think of to do this. I believe you're pretty much stuck with reading back the result, using glReadPixels(), and then checking for non-black pixels on the CPU.

One idea I had to avoid reading back the whole image was to repeatedly downsample it until it reaches a size of 1x1. It would originally render to a texture, and then in each step, sample the current texture with linear sampling, rendering to a texture of half the size. I believe this would work, but I'm not sure if it would be more efficient than just reading back the whole image.

I won't provide full code for the proposed solution, but the outline looks like this. This is using blending for drawing only the pixels in the intersection.

  1. Set up an FBO with an RGBA texture attached as a color buffer. The size does not necessarily have to be the same as your screen resolution. It just needs to be big enough to give you enough precision for your intersection.
  2. Clear FBO with black clear color.
  3. Render first sprite with only alpha output, and no blending.

    glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_TRUE);
    glDisable(GL_BLEND);
    // draw sprite 1
    

    This leaves the alpha values of sprite 1 in the alpha of the framebuffer.

  4. Render the second sprite with destination alpha blending. The transparent pixels will need to have black in their RGB components for this to work correctly. If that's not already the case, change the fragment shader to create pre-multiplied colors (multiply rgb of the output by a).

    glColorMask(GL_TRUE GL_TRUE, GL_TRUE, GL_TRUE);
    glBlendFunc(GL_DST_ALPHA, GL_ZERO);
    glEnable(GL_BLEND);
    // draw sprite 2
    

    This renders sprite 2 with color output only where the alpha of sprite 1 was non-zero.

  5. Read back the result using glReadPixels(). The region being read needs to cover at least the bounding box of the two sprites.
  6. Add up all the RGB values of the pixels that were read.
  7. There was overlap between the two sprites if the resulting color is not black.


来源:https://stackoverflow.com/questions/24844950/android-opengl2-0-intersection-between-two-textures

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!