Search code examples
c++openglgame-engine

Calculate Texture coordinates for procedural generated geometry


How can I calculate texture coordinates of such geometry?

The angle shown in the image (89.90 degree) may vary, therefore the geometry figure is changing and is not always such uniform.(maybe like geometry in the bottom of image) and red dots are generated procedurally depends on degree of smoothness given.

enter image description here


Solution

  • I would solve it by basic trigonometry.

    For simplicity and convenience lets assume:

    • coordinates [0,0] are in the middle of the geometry (where all the lines there intersect) and in the middle of the texture (and they map to each other - [0,0] in geometry is [0,0] in the texture).

    • the texture coordinates span from -1 to 1 (and also assume the geometry coordinates do too in the case of 90 degrees - in other cases it may get wider and shorter)

    • possitive values for x span right and y up. And assume that x geometry axis is aligned with the u texture axis no matter the angle (which is 89.90 in your figures).

    Something like this: enter image description here

    Then to transform from texture [u,v] to geometry [x,y] coordinates:

    x = u + v*cos(angle)
    y = v*sin(angle)
    

    To illustrate, it is basically a shear transformation and scale transformation to preserve length of y (or alternatively - similar to rotation transform, but rotating only one axis - y - not both). If I reverse that transformation (to get the texture coordinates we want):

    u = x - y*cotg(angle)
    v = y/sin(angle)
    

    With those equations I should be able to transform any geometry coordinates (a point) in the described situation into texture coordinates. For any angle in a (0, 180) range anyway

    (Hopefully I didn't make too many embarrassing errors in there)