07.01.2013 Views

3D graphics eBook - Course Materials Repository

3D graphics eBook - Course Materials Repository

3D graphics eBook - Course Materials Repository

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Bilinear filtering 14<br />

Are all true. Further, define<br />

With these we can simplify the interpolation equations:<br />

And combine them:<br />

Or, alternatively:<br />

Which is rather convenient. However, if the image is merely scaled (and not rotated, sheared, put into perspective, or<br />

any other manipulation), it can be considerably faster to use the separate equations and store y b (and sometimes y a , if<br />

we are increasing the scale) for use in subsequent rows.<br />

Sample code<br />

This code assumes that the texture is square (an extremely common occurrence), that no mipmapping comes into<br />

play, and that there is only one channel of data (not so common. Nearly all textures are in color so they have red,<br />

green, and blue channels, and many have an alpha transparency channel, so we must make three or four calculations<br />

of y, one for each channel).<br />

{<br />

double getBilinearFilteredPixelColor(Texture tex, double u, double v)<br />

u *= tex.size;<br />

v *= tex.size;<br />

int x = floor(u);<br />

int y = floor(v);<br />

double u_ratio = u - x;<br />

double v_ratio = v - y;<br />

double u_opposite = 1 - u_ratio;<br />

double v_opposite = 1 - v_ratio;<br />

double result = (tex[x][y] * u_opposite + tex[x+1][y] *<br />

u_ratio) * v_opposite +<br />

u_ratio) * v_ratio;<br />

}<br />

return result;<br />

(tex[x][y+1] * u_opposite + tex[x+1][y+1] *

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!