I thought I understood the theory of how texture mapping works, but it turns out I don't.
I'm trying to setup the TEV so that the texture drawn over a geometry is fixed to the screen pixels, that is the texels should map 1:1 to the screen pixels. Here's a screenshot of the desired outcome (the texture is the blackboard with the white lines, the red is the screen background):
The way I thought that it should work:
- Enable texture coordinate generation from vertices' positions:
Code: Select all
GX_SetTexCoordGen2(GX_TEXCOORD0, GX_TG_MTX3x4, GX_TG_POS, GX_TEXMTX0, GX_FALSE, GX_DTTMTX0);
- For the GX_TEXMTX0 matrix, use the modelview matrix combined with the projection matrix, so that each vertex will get projected onto a -1,1 x -1,1 square (that is, S and T coordinates will be ranging from -1 to 1)
- To map the S and T coordinates to the range 0, 1 (the usual range for a texture, avoiding clamping or tiling) set a post matrix in GX_DTTMTX0 which translates the S and T coordinates right and down by 1 unit, and then a scale transformation which reduces them by half (since the range from -1 to 1 is 2, but from 0 to 1 is just 1).
After a few trial and errors, I finally (almost accidentally) got a working transformation, but I don't understand why it works, and why it needs to be like this. Here it is:
- Enable texture coordinate generation from vertices' positions (like before)
- For the GX_TEXMTX0 matrix, use the modelview matrix multiplied by a scale matrix which scales the X by proj[0][0] and the Y by proj[1][1] (in other words, we only use the scaling parts of the projection matrix, and not all of it)
- To map the S and T coordinates to the range 0, 1 set the following post matrix in GX_DTTMTX0:
The 0.5 values on the 00 and 11 elements are expected, since we are reducing the coordinates range by half, but why are the translation components set on the third column and not on the fourth one? And why are they 0.5 instead of being 1?
Code: Select all
// ignore the - on the 00 element, that's to mirror the texture horizontally Mtx pm = { {-0.5, 0, 0.5, 0}, {0, 0.5, 0.5, 0}, {0, 0, 1, 0}, };
https://github.com/mardy/projection-test
Any help will be appreciated. Also I did not understand why the post matrix needs to be a 3x4 one: since we only get S and T coordinates after the transformation, wouldn't a 2x4 matrix be enough?