• Create Account

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

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

4 replies to this topic

### #1sunk  Members

228
Like
0Likes
Like

Posted 29 June 2014 - 11:36 AM

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, 29 June 2014 - 11:43 AM.

### #2Samith  Members

2450
Like
3Likes
Like

Posted 29 June 2014 - 11:49 AM

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.

### #3sunk  Members

228
Like
0Likes
Like

Posted 29 June 2014 - 03:20 PM

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

### #4sunk  Members

228
Like
0Likes
Like

Posted 10 July 2014 - 02:36 PM

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  .

### #5kalle_h  Members

2402
Like
0Likes
Like

Posted 10 July 2014 - 02:47 PM

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

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.