• 14
• 12
• 9
• 10
• 13

# Affine Texture Mapping

This topic is 3694 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

hi, I've just finished writing a function to texture a triangle using affine texture mapping but im not sure if there's somthing wrong with it or if it's just the way affine mapping works. here's a few screenshots of a sample image split into two triangles, it seems to curve at some points which i don't think is right: you can see the two triangles clearly in the first image and at the bottom left you can see the pixels sort of curving to the left. [Edited by - staticVoid2 on February 7, 2008 2:12:17 AM]

##### Share on other sites
i forgot to swap the gradients:

      /* (determines which side is which)if the gradient of the left side is         greater than that of the right side then the points need to be swaped */      if(dxdyl > dxdyr) {         swap(x1, x2, temp); // swap the points         swap(y1, y2, temp);         swap(tu1, tu2, temp);         swap(tv1, tv2, temp);      <b>swap(dudyl, dudyr, temp);</b>      <b>swap(dvdyl, dvdyr, temp);</b>         swap(dxdyl, dxdyr, tempf); // swap the gradients instead of recalculating      }

i think that eliminated the curve effect but does this look more like affine mapping?:

##### Share on other sites
That looks like affine texture mapping to me. Take a look at this for comparison.

##### Share on other sites
Looks like you need perspective correction. You could try doing it every four pixels or so and interpolating inbetween (subdivided affine texturing). I'm assuming you're after speed here, otherwise just do it at each pixel.

This is done by computing U/Z and V/Z and 1/Z at the vertices and interploating them like normal, then at each point where you perspective correct you simply take the interlopated 1/Z, extract Z from it, multiply the U/Z and V/Z by Z to get the the perspective correct U and V coordinates.

thanx.

##### Share on other sites
Only the second image in your first post looks correct. In the others, you have some kind of a bug in the way you interpolate the texture coordinates across the diagonal. It looks like only the triangle that makes up the bottom right half of the quad has a problem in your last image.

Affine texture mapping will produce the "kink" effect you see in the second image (as it's not perspective-correct) but shouldn't cause the kind of discontinuity evident in the other two. Both polygons should produce the same texture coordinates along that edge since it's just a linear interpolation.

[Edited by - Fingers_ on February 8, 2008 2:31:37 PM]

##### Share on other sites
yeh, there were a lot of bugs that i fixed, because I transferred the code to java to display the image and (for example) I used a swap macro which i thought would easily transfer into a java function but never because java does not support pointers to primitives so i had to inline 3 lines of code for evey swap function. there were a few other bugs aswell that i can't remember. I'm just implementing the clipping procedures in the function which is a nightmare but it seems to be working now.

##### Share on other sites
Why do you need the swap anyway? I can't think of any reason you would need them. In my software renderer I simply compute the gradients from the three vertices and thats it. They can be negative, but that is no problem at all.

##### Share on other sites
its so that i can scan left to right, how do you where to start scanning or which side is which?