Sign in to follow this  

Texture Mapping a Mesh

This topic is 4712 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Does anyone know how to texture map a mesh created from scratch? I have a "blank mesh" that I create using D3DXCreateMeshFVF that I then fill with vertices and indices. I load a texture using D3DXCreateTextureFromFile, now how do I map it to the mesh? The mesh is essentially square as it it a terrain patch. If I set the texture coordinates to each of the 4 "corners" of the patch the mesh turns the color (of what I assume is) of the first pixel in my texture, which is how I know I'm at least loading in the texture. If I set the texture coordinates to 4 vertices that are neighbors, my texture just loads to that quad on my mesh, and the rest of my mesh is the color of the first pixel. I would like to know how to both stretch my texture over the entire mesh, as well as how to tile it.

Share this post


Link to post
Share on other sites
Yeah, I tried that. Even when I map to one quad the texture is still warped. For example, here's my mesh:
Quote:
012
345
678

The numbers represent the index of the array to my vertex buffer. If I map:
Quote:
vert[0].u = 0;
vert[0].v = 0;
vert[1].u = 1;
vert[1].v = 0;
vert[3].u = 0;
vert[3].v = 1;
vert[4].u = 1;
vert[4].v = 1;

The texture I load from the file gets mapped to that quad, but it's all skewed, like the corners are forced to go in the wrong places, and if I map something like this:
Quote:
vert[0].u = 0;
vert[0].v = 0;
vert[2].u = 1;
vert[2].v = 0;
vert[6].u = 0;
vert[6].v = 1;
vert[8].u = 1;
vert[8].v = 1;

I just get a mesh the color of the first pixel in my texture. At the corners are a hint of detail, but it's skewed too.

Share this post


Link to post
Share on other sites
OK, upon closer look, I think it's because my mesh is indexed. So how do I go about setting texture coordinates on an indexed mesh, especially if I want to tile? Since all the vertices share faces, putting even one texture coordinate in my mesh causes the texture to share up to 6 faces, causing the skewing I've been seeing. I've found a few scant articles on adding a second set of texture coordinates per vertex, but this can't be how to texture a mesh. I've also found a few articles on how this is the artist's job, but I need this in code. Can anyone help?

Share this post


Link to post
Share on other sites
I don't know the code in my head, but here's the concept:

(-1,-1,0), (1,-1,0), (1,1,), (-1,1,0) // quad (X,Y,Z)

(0,0), (1,0), (1,1), (0,1) // UV coords for that quad (U,V)


From your examples it seems that you're not making one quad, but four quads.

If that's the case then these should be the correct coords:

Vertices:

012
345
678

UVs

vert[0].u = 0;
vert[0].v = 0;
vert[1].u = 0.5;
vert[1].v = 0;
vert[2].u = 1;
vert[2].v = 0;

vert[3].u = 0;
vert[3].v = 0.5;
vert[4].u = 0.5;
vert[4].v = 0.5;
vert[5].u = 1;
vert[5].v = 0.5;

vert[6].u = 0;
vert[6].v = 1;
vert[7].u = 0.5;
vert[7].v = 1;
vert[8].u = 1;
vert[8].v = 1;

Share this post


Link to post
Share on other sites

This topic is 4712 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this