C++ DirectX - texturing a 2D mesh -
in directx i've been working textured quads using following struct:
struct tlvertex { float x; float y; float z; d3dcolor colour; float u; float v; };
however want make more complex 2d meshes , texture those, have no idea how i'm align texture on it. in textured quad, u , v properties of struct determine texture's orientation.
ideally want able either have 2d mesh texture on or new struct properties need, texture stretched/manipulated fit on mesh entirely no matter how distorted may look.
another thing i'd have 2d mesh texture slapped on no stretching of texture, etc. i'd want align it, if texture didn't fit shape of mesh, bits missing etc.
i've tried googling can find things relating 3d graphics. whilst realise technically working in 3d space, trying create 2d graphics. appreciate answers suggestions look/get started on achieving through complete examples if possible.
you should read how texture coordinates work.
let's consider following mesh. want apply undistorted texture (the dashed rectangle):
then specifying texture coordinates each vertex pretty simple:
u = (x - minx) / (maxx - minx) v = (y - miny) / (maxy - miny)
if want rotate texture, have project vertices on according axes.
if want distort texture, have specify texture coordinates on texture's edges. simple algorithm can utilized following one:
choose arbitrary point in polygon -> o each vertex v shoot ray o through v find intersection of ray minx / maxx / miny / maxy calculate texture coordinates @ intersection points above next
however, algorithm not guarantee every texel mapped mesh. e.g. top right corner of sample above not mapped above algorithm. furthermore, guarantee consistent mapping convex polygons.
here algorithm concave polygons. should produce consistent coordinates. however, not know how result like. algorithm can skip corners. possible include checks apply corners' coordinates specific vertices (e.g. when side changes):
calculate polygon's perimeter -> p //the texture coodinate space has perimeter of 4 currentpos := 0 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 next vertex next
Comments
Post a Comment