简体   繁体   English

C ++ DirectX-纹理化2D网格

[英]C++ DirectX - texturing a 2D mesh

In DirectX I've been working with textured quads using the following struct: 在DirectX中,我一直在使用以下结构处理纹理四边形:

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. 但是我想制作更复杂的2D网格并对其进行纹理化,但是我不知道如何在其上对齐纹理。 In the textured quad, the u and v properties of the struct determine the texture's orientation. 在带纹理的四边形中,结构的u和v属性确定纹理的方向。

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. 理想情况下,我最终想要做的是要么在其上具有纹理的2D网格物体,要么将需要新的结构属性,并对其进行拉伸/操纵以使其完全适合网格物体,无论它最终看起来如何变形。

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. 我想做的另一件事是说有一个贴有纹理的2D网格,没有拉伸纹理,等等。我只想对齐它,所以如果纹理不适合形状网格,位会丢失等。

I've tried Googling but can only find things relating to 3D graphics. 我尝试了谷歌搜索,但只能找到与3D图形有关的内容。 Whilst I realise I am technically working in 3D space, what I am ultimately trying to do is create 2D graphics. 虽然我意识到自己在3D空间中进行技术工作,但最终要做的是创建2D图形。 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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