Matrix to Represent a Triangle in Screen Space
So i have a set of four points in 3D Space.
P1 [0, 0, 0]
P2 [128, 0, 0] P3 [0, 128, 0] P4 [128, 128, 0]Which I'm then projecting orthographically to the screen effectively giving me two wireframe triangles in screen space.
I now want to map a texture to this "plane" comprised of the two triangles. So i take my square texture, and using u,v coordinates I can map the texture into these two triangles.
The problem comes when I try to add a z component to the verticies. The resulting triangles in screen space are now somewhat distorted but the texture mapping is completely off.
It seems to me though that since it's just three points, they still form an affine rectangle of some sort and we can represent that rectangle as a matrix in the form:
[a b 0] [c d 0] [tx ty 1]
Can anyone give me some tips, ideas about how to convert three 2D points in screen space into a matrix in the above form?
All the tutorials etc that I've looked up explain a loose t开发者_如何学Goheory of texture mapping, and then let OpenGl/DirectX native functions do the actual mapping.
I'm looking for more of a direct approach where given three points, i can map a texure to that triangle using an affine matrix. (Not doing a scanline rendering approach)
Thanks!
I'm not sure I understand what exactly you're looking for, but here's a way to transform from the (u,v) coordinates, to the screen coordinates.
Lets say you projected P1, P2, P3 to screen coordinates, and got 3 points with screen coordinates (x1,y1), (x2,y2) and (x3,y3).
Now we need a transformation from the form
[a, b, c] [d, e, f] [0, 0, 1]*[u, v, 1] = [x, y, 1]
and we want it to transform (0,0) to (x1,y1); (1,0) to (x2,y2) and (0,1) to (x3,y3). (this is because you want it to map your texture triangle to the projected triangle).
so we write all the equations:
0 a + 0 b + 1 c = x1
0 d + 0 e + 1 f = y1
1 a + 0 b + 1 c = x2
1 d + 0 e + 1 c = y2
0 a + 1 b + 1 c = x3
0 d + 1 e + 1 f = y3
and we solve them, and get:
a = x2-x1
b = x3-x1
c = x1
d = y2-y1
e = y3-y1
f = y1
精彩评论