gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

DS development > UV arrays

#45400 - iainprice - Fri Jun 10, 2005 8:48 am

Can someone explain UV arrays to me...

I have got a complex shape made of many triangles and wish to apply a single texture to the whole mesh. I have an array of x,y,z for all the triangles, a normals pack, textures pack but it is just the textures pack lifted from the cube demo, so each triagnle uses the complete texture so therefore it is repeated.

I am using lightwave, then converting to rwx and converting to a manual data format. I just don't understand how the 0,0,128 refers to the triangle...

#45432 - dovoto - Fri Jun 10, 2005 5:41 pm

http://biorust.com/index.php?page=tutorial_detail&tutid=85

Here is a simple introduction to UV mapping in a modeler...it uses blender but should still apply. Keep in mind that most 3D model formats are going to give you UVs ranging from 0 to 1 with numbers greater than one meaning the texture should repeate. To convert these UVs to the DS format simply multiply U by the width and V by the height of your texture (in proper fixed point number format of course).

For a brief and crapy description of mapping a texture to a triange:
1) 256x256 texture
2) A triangle with verticies (x1,y1,0) (x2,y2,0) (x3,y3,0):
Code:

//Triangle
       (x1,y1)----------- (x2,y2)
             |          /
             |        /
             |      /
             |    /
             |  /
             |/
        (x3,y3)


You will have 3 corresponding sets of texture coordinates that go along with these verticies. These "UV" coordinates represent a triangle mapped out inside your texture.
Code:

//texture
    (0,256)--------------------------------------------------(256,256)
           |        (u1,v1)----------- (u2,v2)               |
           |             |          /                        |
           |             |        /                          |
           |             |      /                            |
           |             |    /                              |
           |             |  /                                |
           |             |/                                  |
           |        (u3, v3)                                 |
           |                                                 |
           |                                                 |
           |                                                 |
      (0,0)---------------------------------------------------(256,0)


These coordinates do not have to coorispond in anyway to the shape of the triangle you are trying to texture as long as there is one texture coordinate for each vertex. The UV triangle will be cut out of your texture and rotated and stretched to fit your triangle.

In this case the data would be sent to the 3D engine as follows:
Code:

glVertex(x1,y1)
glTextureCoord(u1,v1)

glVertex(x2,y2)
glTextureCoord(u2,v2)

glVertex(x3,y3)
glTextureCoord(u3,v3)

You can change the order of the UVs and it will cause your texture to be rotated and mapped accordingly (which means if you convert all your data and it still is all messed up try messing with vertex and UV order)

As you can see trying to generate these UV coordinates to cover even a simple model by hand would be very much a pain in the ass. Most (if not all) 3D models provide tools to do this for you but have a rather annoying learning curve.

Extracting this data out of the 3D format is hopefully strait-forward as long as you keep in mind that UVs are normaly normalized to 1 (instead of the 0 to 256 the DS would need, it would be 0 to 1 in floating point format).[/code]
_________________
www.drunkencoders.com