简体   繁体   中英

C++ DirectX - texturing a 2D mesh

In DirectX I've been working with textured quads using the following struct:

struct TLVERTEX
{
    float x;
    float y;
    float z;
    D3DCOLOR colour;
    float u;
    float v;
};

However I want to make more complex 2D meshes and texture those, but I have no idea how I'm to align the texture on it. In the textured quad, the u and v properties of the struct determine the texture's orientation.

Ideally what I want to ultimately be able to do is either have a 2D mesh with the texture on it or what new struct properties I would need, with the texture stretched/manipulated to fit on the mesh entirely no matter how distorted it may ultimately look.

Another thing I'd like to do is say have a 2D mesh with a texture slapped on to it with no stretching of the texture, etc. I'd just want to align it, so if the texture didn't fit the shape of the mesh, bits would be missing etc.

I've tried Googling but can only find things relating to 3D graphics. Whilst I realise I am technically working in 3D space, what I am ultimately trying to do is create 2D graphics. I would appreciate answers with anything from suggestions where to look/get started on achieving this through to complete examples if possible.

You should really read about how texture coordinates work.

Let's consider the following mesh. We want to apply an undistorted texture (the dashed rectangle):

样品

Then specifying the texture coordinates for each vertex is pretty simple:

u = (x - minX) / (maxX - minX)
v = (y - minY) / (maxY - minY)

If you want to rotate the texture, you have to project the vertices on according axes.

If you want to distort the texture, you have to specify texture coordinates on the texture's edges. A simple algorithm that can be utilized is the following one:

Choose an arbitrary point in the polygon -> o
For each vertex v
    Shoot a ray from o through v
    Find the intersection of this ray with minX / maxX / minY / maxY
    Calculate the texture coordinates at the intersection points as above
Next

However, this algorithm does not guarantee that every texel is mapped to the mesh. Eg the top right corner of the sample above is not mapped to anything with the above algorithm. Furthermore, it does only guarantee consistent mapping for convex polygons.

Here is an algorithm for concave polygons. It should produce consistent coordinates. However, I do not know how the result will look like. This algorithm can also skip corners. It is possible to include checks to apply the corners' coordinates to specific vertices (eg when a side changes):

Calculate the polygon's perimeter -> p
//The texture coodinate space has a perimeter of 4
currentPos := 0
for each vertex
    if(4 * currentPos < 1)
        uv = (4 * currentPos / p, 0) // top edge
    else if(4 * currentPos < 2)
        uv = (1, 4 * currentPos / p - 1); //right edge
    else if(4 * currentPos < 3)
        uv = (1 -  4 * currentPos / p - 2, 1); //bottomedge
    else
        uv = (0, 1 - 4 * currentPos / p - 3); //leftedge
    currentPos += distance to next vertex
next

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM