Mapping normals to texture files

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • CommonGibbon
    3Dflower
    • Jul 2018
    • 9

    Mapping normals to texture files

    Howdy, I'm using 3DF zephyr steam edition and I've exported a textured mesh (with normals) to a .obj + .mtl + four texture files. My goal is to associated the normals exported in this way with their corresponding points in their respective composite texture files. The method I'd envisioned for doing this was to use the triangular faces in the obj file to associate normal vertices with texture vertices.
    I've read up on how the texture coordinates are formatted in .obj files, but what I think I've learned doesn't line up with reality. For instance:

    - I've found that most texture vertex values will be between 0 and 1, which I can simply scale up the dimensions by multiplying by appropriate dimensions of the texture files. The problem with this is that many of the faces are HUGE if I use this method, but since I have 324k faces, I'd expect each one to be very small.

    - I've learned that texture vertex values above one or below zero mean that the face is wrapped across multiple tiled instances of the texture, this should be impossible for the same reason above.

    My question is: how should I interpret the texture vertex values if I want to find their corresponding points in the composite texture images? I know I need to use the faces to associate a vertex with a particular texture, but beyond that I've become lost.

    If you need more information to make sense of my question, please ask. I also made a stackoverflow post which has more (probably too much) detail: https://stackoverflow.com/questions/...normal-mapping

    Thanks for taking a look at this!
  • Andrea Alessi
    3Dflow Staff
    • Oct 2013
    • 1335

    #2
    Hi CommonGibbon,

    If I understood your question, you wanted to embed the normal information directly in the texture - or, in other words, to create a normal map. This requires that the uv-mapping is the same as the texture map.

    You have to look at the faces rows: obj is formatted like this:

    f v1/vt1/vn1 v2/vt2/vn2 v3/vt3/vn3

    where every value is an index.

    So, the row

    f 1227/1040/1227 1253/1057/1253 1249/1049/1249

    means that this face by three vertices:

    The first vertex is the 1227th listed in the file ( e.g. v -7.771425 -4.446406 9.065235 ) which uses the 1040th listed texture uvs ( e.g. vt 0.215801 0.811166 0 ) and has the 1227th normal value listed in the file, eg. vn 0.559996 -0.789925 -0.249846

    second and third vertices are composed like so.

    You can then recreate a normal map using this information ( 0.559996 -0.789925 -0.249846 ) for the corresponding uv (t 0.215801 0.811166).

    If you need the information inside the triangle you need to interpolate between the normals of the three vertices.

    Personally, i use xNormal to do this process, but obviously you can write your own

    Comment

    • CommonGibbon
      3Dflower
      • Jul 2018
      • 9

      #3
      Hey Andrea,

      Thanks so much for the reply. I've l already made it as far as you've described, but the problem is that I'm not sure how to translate the vt texture vertices into pixel coordinates in my texture files. From what I've read online, it is usually as simple as just scaling up to the size of the texture, so vt 0.215801 0.811166 for a 1920x1920 texture file would correspond to pixel coordinate (0.215801*1920, 0.811166*1920) = (414, 1557). In the special case where the texture vertices are greater than 1 or less than zero, it means the triangle spans a tiled section of the texture file, so vt 3.11000 0.7000 would correspond to the rightmost pixel coordinate in the attached image.

      Does this sound correct to you? The problem is that it doesn't make much sense for my texture files. not only do I have extreme instances of texture coordinates such as vt 5600.000 0.7000 but, if I follow the conversion process I just described, my triangular faces would cover immense portions of texture files. If I have four texture files describing a complex object such as a head, it seems to me that each of my triangular faces should correspond to tiny portions of the texture files output by 3df, yet I have immense triangles which cover >20% of a texture file.

      I hope this is making sense.
      Last edited by CommonGibbon; 2018-07-23, 08:37 PM.

      Comment

      • CommonGibbon
        3Dflower
        • Jul 2018
        • 9

        #4
        I believe I've figured this out. As it turns out there were a couple of issues:
        1. I was 1 value off in my indexing; I thought I'd already adjusted to compensate for the vertex indexing starting at 1 vs python indexing starting at zero, but it turns out I'd skipped it at a crucial point.
        2. The x/u coordinate of the vt texture vertices are summed with the integer number representing which texture file the vertex corresponds to. For example if I have 11 textures and the texture vertex is 6.001502 .45525, that means that this vertex corresponds to the sixth image, but x pixel coordinate is 0.001502*image width.

        Sorry for the confusion!

        Comment

        • Andrea Alessi
          3Dflow Staff
          • Oct 2013
          • 1335

          #5
          Glad you fixed it

          let me know if you have any more questions!

          Comment

          Working...