Search code examples
openglgraphics3dtexture-mappinguv-mapping

Jagged lines while handling UV values out of range [0,1]


I am parsing an OBJ which has texture coordinates more than 1 and less than 0 as well. I then write it back by making the UV values in the range [0,1]. Based on the understanding from another question on SO, am doing the conversion to the range [0,1] as follows.

                if (oldU > 1.0) or (oldU < 0.0):
                    oldU = math.modf(oldU)[0] # Returns the floating part
                    if oldU < 0.0 :
                        oldU = 1 + oldU

                if (oldV > 1.0) or (oldV < 0.0):
                    oldV = math.modf(oldV)[0] # Returns the floating part
                    if oldV < 0.0:
                        oldV = 1 + oldV

But I see some jagged lines in my output obj file and the original obj file when rendered in some software:

enter image description here Original

enter image description here Restricted to [0,1]


Solution

  • This may work not as you've expected.

    Given some triangle edge that starts at U=0.9 and ends at U=1.1 then after your UV clipping you'll get start at 0.9 but end at 0.1 so the triangle will use different part of your texture. I believe this happens at the bottom of your mesh.

    In general there's no problem with using UV outside of 0-1 range so first try to render the mesh as it is and see if you have any problems.

    If you really want to move UVs to 0-1 range then scale and move UVs instead of clipping them per vertex. Iterate over all vertices and store min and max values for U and V, then scale UV for every vartex, so min becomes 0 and max becomes 1.