PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

Texture mapping 2D triangles

Hello,
I'm looking for a way to map a texture on a (deforming) 2d triangle.
Most of the papers I've found so far implement perspective corrected texture mapping. I'm not interested in this, since I want to map a texture onto a 2d triangle.

Some links to good resources or even code will be much appreciated.
Doing my own research, it seems affine texture mapping is what I'm after. Now the search continues for some good implementations :?
I found the following to be very useful:
www.d6.com/users/checker/
fast_rx wrote:I found the following to be very useful:
www.d6.com/users/checker/

Yeah I stumbled across Hecker's articles before, but was too lazy to read the whole thing. Anyway on second inspection he does go over affine mapping (after he's done with perspective correction).

I implemented a working (slow) version. Now it's down to optimizing. It seems that at best I can reduce to 1 division per triangle and several multiplications to calculate the gradients. Am I correct, or is there a way to get rid of the division altogether!?
The division per triangle is not the bottleneck of your polygon rasterizer. It's more important to get the scanline drawn as fast as possible.
Cache coherency plays a great role in texture mapping also.

Check out this document also (I'm not sure if you've seen it before)
http://www.multi.fi/~mbc/sources/fatmap2.txt

Cheers
Thanks,

The assembly innerloops should help with optimizing. I'm not using tiled textures, so I don't think I can do much about cache coherency. Another hurdle is that for textured triangles there's no point in reading 32 bits (2 pixels) of the texture at a time, since consecutive pixels in uv do not necessarily map to consecutive pixels in xy.

So the only points where I can optimize is reducing to 1 division/triangle and making the innerloop as tight as possible.

Interestingly, a more efficient use of the cache and 32bit reads are possible if instead one were to do an affine transformation FROM the texture uv coordinates TO the screen xy coordinates. However that entails gaps between pixels for stretched textures and overwrites for compressed textures... Though perhaps it is worth investigating if one knows beforehand that the texture will never be stretched...


Anyway I am having some trouble reducing to 1 division due to limited fixed point range. Here's some sample code:
Code:

//A,B,C,invC are in 24.8 fixed pt
int C = (X2 - X1) * (Y3 - Y1) - (X3 - X1) * (Y2 - Y1);
if (C == 0) return;

int invC = (1<<8) / C; // <--- not enough accuracy

int A = (U3 - U1) * (Y2 - Y1) - (U2 - U1) * (Y3 - Y1);
int B = (X3 - X1) * (U2 - U1) - (X2 - X1) * (U3 - U1);

//dudx, dudy are 16.16 fixed pt
const int dudx = -(A*invC);
const int dudy = -(B*invC);


I tried making invC 16.16 fixed pt, but without much gain. The texture still jitters. Would you recommend using floating point instead? Or perhaps I should try using Intel's gppMul_16_32s to gain some more accuracy?
To avoid jittering of your texture you'll probably have to make all coordinates fixed point (even the ones on screen) and use the subpixel portion of the screen coordinates to correct the ones of the texture while you're stepping through through it.

Read the link I've provided, it explains how to write a subpixel/subtexel accurate texture mapper.
xPeterx wrote:To avoid jittering of your texture you'll probably have to make all coordinates fixed point (even the ones on screen) and use the subpixel portion of the screen coordinates to correct the ones of the texture while you're stepping through through it.

Read the link I've provided, it explains how to write a subpixel/subtexel accurate texture mapper.

I already have everything in fixed point. The rasterizer works at subpixel and subtexel, accuracy with the following code:
Code:

// A,B,C in 24.8 fixed pt
int C = (X2 - X1) * (Y3 - Y1) - (X3 - X1) * (Y2 - Y1);
if (C == 0) return;

int A = (U3 - U1) * (Y2 - Y1) - (U2 - U1) * (Y3 - Y1);
int B = (X3 - X1) * (U2 - U1) - (X2 - X1) * (U3 - U1);

// 24.8 fixed pt
const int dudx = -((A<<8)/C);
const int dudy = -((B<<8)/C);

It's just when I try to replace the divisions by a multiplication by inverse C that the texture jitter occurs. This is because of the limited fixed point accuracy when computing invC (which leads to slightly wrong dudx, dvdx etc gradients).

I looked at the code in the link you provided and they use floating points to compute the gradients:
Code:

double d = (v0.x - v2.x) * (v1.y - v2.y) -
(v1.x - v2.x) * (v0.y - v2.y);

if(d == 0.0) return;

double id = 1.0/d * 65536.0;

long dudx = ((v0.u - v2.u) * (v1.y - v2.y) -
(v1.u - v2.u) * (v0.y - v2.y)) * id;

long dvdx = ((v0.v - v2.v) * (v1.y - v2.y) -
(v1.v - v2.v) * (v0.y - v2.y)) * id;

I was trying to avoid this because floats are slow. Have you been able to implement gradient computation in fixed point with only 1 division?
Yes I did it completely in fixed point, but I tend to use 16.16 fixed point for pretty much everything I do because I learned thinking of 1 as 65536 over the time, so I can't tell you if this will also work with 24.8 bit fixed point.
But for 16.16 it's obvious that 32 bits won't be sufficient but this is not a big deal since most CPUs can give a 64 bit result for a multiplication anyway.
I our experience, you will need to use 16:16 at least for the inverse for the gradient calculation. One option is to search FixedPoint, divide and class on this forum and look for a post that includes a link to Vincent GL-ES renderer. This has a nice fixed point class that will give you a nice fast inverse routine. You can use this instead of your (1<<8)/c. There is also a post about fast division (using inverse) in this forum, either of those will help, but the Vincent code is pretty much drag and drop.
Whoa that opened another whole can of worms 8O
Hehe, sometimes I think I've got a great implementation going and then someone else comes along and posts code that is even more ingenious.

Thanks guys for the help, please share more tips & tricks on the subject if you have any :D
That reminds me of twiddling the textures to increase cache coherency, torus mentioned it once, it might be useful to. Just search the forum ;)
Thanks,
'twas not easy, but I dug up the thread. I'm assuming you're referring to this one:
http://www.pocketmatrix.com/forums/view ... s&start=30

Let me put the technique into words to make sure that I understand how it works. By intertwining the uv coordinates as follows:
A = [u7|v7|u6|v6|u5|v5|u4|v4|u3|v3|u2|v2|u1|v1|u0|v0]

A = 0 means uv = (0,0)
A = 1 means uv = (0,1)
A = 2 means uv = (1,0) etc.

So this improves cache coherency because sequential data is preloaded into the cache. So depending on the texture screen orientation we get more cache hits even if we traverse in the v direction. This also means that the texture has to be rearanged such that the intertwined addressing accesses the corresponding pixels of the original texture.

Did I get the gist of it?