# Opengl - How to texture polygons that may not be quads

This topic is 1473 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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.

Edited by sunk

##### Share on other sites

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.

##### Share on other sites

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

##### Share on other sites

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  .

##### Share on other sites

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

1. 1
2. 2
3. 3
Rutin
22
4. 4
JoeJ
16
5. 5

• 14
• 30
• 13
• 11
• 11
• ### Forum Statistics

• Total Topics
631776
• Total Posts
3002299
×