Opengl - How to texture polygons that may not be quads

Started by
3 comments, last by kalle_h 9 years, 9 months ago

Hi, I'm currently using Box2D, Opengl and SFML (still pretty new to this stuff) to build a splitting engine where the user can use their mouse to split shapes into smaller ones. I'm having a hard time actually figuring out how the split shapes would be textured after they have been sliced. For example, say I have a quad which has been textured correctly. I slice the square on the top right causing a new triangle and a 5 sided polygon to be created. How would the 2 new shapes be textured after this? I've only seen examples of quads and triangles being textured in opengl and subrecting a texture in SFML won't work since it only supports rects. Any thoughts? Thanks.

Advertisement

Let's say your quad is defined by four verts: A, B, C, D. The top left is A, the top right is B, the bottom right is C and the bottom left is D. Therefore the edges are AB (top), BC (right), CD (bottom) and DA (left). When you slice off vert B, the you must add a new vert on both edges that connected to B (so, a new vert must appear somewhere on AB and BC). Let's call the vert on AB P, and the vert on BC Q. The texture coordinates for P will be the interpolated tex coords between A and B. Similarly, the texture coordinates for Q will be the interpolated texture coordinates between B and C.

So, if P is halfway between A and B, then its texture coordinates will be 0.5 * A_texcoord + 0.5 * B_texcoord. If P is 90% along AB toward B, then the texture coordinates will be 0.1 * A_texcoord + 0.9 * B_texcoord. Does that make sense? In general, if t is a value that represents how far along AB the new vert P is (t = 0.0 meaning P is at A, and t = 1.0 meaning P is at B) then the texture coordinates for P will be (1 - t) * A_texcoord + t * B_texcoord. The same logic applies for vertex Q.

Thanks! I think this makes sense. I'll try implementing this and post back with an update.

As promised, I'm giving a simple update in case someone is interested. I've implemented a simple example of how to do this, the code can be found here: https://github.com/SundeepK/Splitter . I'm still working on things, and it's not tested 100%, but it's something smile.png .

For splitting meshes you can interpolate linearly every vertex attribute. This also work for 3d.

This topic is closed to new replies.

Advertisement