Jump to content

  • Log In with Google      Sign In   
  • Create Account

Interested in a FREE copy of HTML5 game maker Construct 2?

We'll be giving away three Personal Edition licences in next Tuesday's GDNet Direct email newsletter!

Sign up from the right-hand sidebar on our homepage and read Tuesday's newsletter for details!


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.

  • You cannot reply to this topic
4 replies to this topic

#1 sunk   Members   -  Reputation: 211

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.


Sponsor:

#2 Samith   Members   -  Reputation: 2260

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.



#3 sunk   Members   -  Reputation: 211

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.



#4 sunk   Members   -  Reputation: 211

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 smile.png .



#5 kalle_h   Members   -  Reputation: 1469

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.



PARTNERS