How to copy an arbitrary shape texture?

I execute complex 3D objects. Here is a simple example with a spherical object:

enter image description here

Next, I apply the clipping plane to these objects and render the texture on that plane, giving the impression that you are looking at the inside of the object as if it were sliced. For instance:

enter image description here

The problem is the uneven texture border. It will stick out across the surface boundary. Here is another corner where you can see how it sticks out. The surface and texture are obtained from the same source data, but the surface is smoothed and has a higher resolution than the texture.

enter image description here

I want you to somehow fix the texture so that it never goes beyond the border of the surface. In addition, I do not want to simply reduce the texture, because although this may prevent it from standing outside, it will create internal gaps between the edge of the texture and the edge of the surface. I would prefer the texture to be too large and trimmed so that it sits flush with the edge of the surface .

Here where I am:

I decided that the first step would be to determine the intersection of the plane and the surface. So now I have it as an ordered list of line segments. However, I am not sure how to continue this information (or if this is even a better approach).

I read on stencil buffers. One approach may be to turn the intersection line into a 2d shape and draw it into a stencil buffer. Then apply this when drawing the texture. (Although I think this is a lot of work, as forms can be complex.)

I am wondering if I can somehow use an already painted surface (in combination with a stencil buffer or some other technique) to somehow fix the texture - without having to go through the additional nuisance of drawing the intersection line, etc.

What is the best approach here? (Any online examples you can point me to would also be very helpful.)

+2
source share
1 answer

If you crop convex objects and know the coordinates of the cropped points, you can create a polygonal "cap" yourself - just draw the selected points in the correct order using GL_TRIANGLE_FAN, and that is. It will not work with a non-convex object - this will require a triangulation algorithm. You can use glu tesselators to triangulate polygons, but it can be tricky.

If the cropped area can be determined by the formula, you can write a shader that will precisely pinch pixels at a certain distance (i.e. if x^2+x^2+z^2 > r^2 do not draw pixel ).

You can also draw inverse faces with a shader that will draw each inverse pixel as if it were on a clip plane using a simple ray tracing. This is complicated and may be redundant in your case. Dead Rising used a similar technique in their game engine.

You can also use a stencil buffer.

First draw the back surfaces GL_INCR ( glStencilOp(GL_KEEP, GL_INCR, GL_INCR) ), then draw the front surfaces GL_DECR ( glStencilOp(GL_KEEP, GL_DECR, GL_DECR) ). Then draw the texture only where the stencil is non-zero. ( glStencilFunc(GL_GREATER, 0, 0xff); glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP); ). However, if you have many overlapping figures, you will need to take special care of them.

- edit -

However, I am not sure how to continue this information (or if this is even the best approach).

Draw it like a fan of a triangle. For convex objects, this is all you need. For non convex objects that will not work.

ve read on stencil buffers. One approach may be to turn the intersection line into a 2d shape

No, that will not work. The region you want to fill with the texture must have a specific stencil value. The way stencil cropping works.

to somehow fix the texture

In OpenGL, you have 6 (?) Clip planes. If you need more, you will need advanced methods - stencil, getting intersection lines, shaders or triangulation.

Any online examples you can point me to will also be very helpful.

Drawing filled concave polygons using a stencil buffer

+3
source

All Articles