Sign in to follow this  
Sundeepkahlon

Opengl - How to texture polygons that may not be quads

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 this post


Link to post
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 this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this