Skip to main content
GameDev.net gamedev.net
🔒 Locked

texture sampling

Started by paic Dec 21, 2005 at 4:51 AM 4 replies 600+ views
Original Post
paic
paic
Hi, Let's say I have an image as a unsigned char *, and I want to sample it at a given coordinate. For the moment, I do something like :

int indexX = nearestInt(u * imageSizeX);
int indexY = nearestInt(v * imageSizeY);
return(image[indexX + indexY * imageSizeX);
This gives simple point filtering. Now I'd like to implement a trilinear filtering (or simply a bilinear or linear filtering), but each time I search for texture filtering in google, I found tons of tutorials on filtering with OpenGL or DirectX And pixel interpolation gives me a lot of thesis. I just want a simple algorithm that would be easy to implement ^^ Anyone has a link or some documentation that would help me to implement texture filtering ? Thx in advance
ZQJ
ZQJ
I think bilinear filtering is pretty simple, something like:

int lowX = floor(u * ImageSizeX);int lowY = floor(v * ImageSizeY);float fracX = u * ImageSizeX - lowX;float fracY = v * ImageSizeY - lowY;return   image[lowX + lowY*imageSizeX] * (1 - fracX) * (1 - fracY) +   image[(lowX+1) + lowY*imageSizeX] * fracX * (1 - fracY) +   image[lowX + (lowY+1)*imageSizeX] * (1 - fracX) * fracY +   image[(lowX+1) + (lowY+1)*imageSizeX] * fracX * fracY;


Mipmapping is a bit more complicated, I'm not exactly sure how that's done. (I think it depends quite a lot on exactly how the rest of your texturing routine works).
bluntman
bluntman
One thing you should do, if performance is important, is to use a function other than floor(). There are quite a few float to int conversion functions written in asm and most of them are about twice as fast as floor(). I have one but its at home (I am in work). I will post it if you want it.
ToohrVyk
ToohrVyk
ZQJ: your code implements linear filtering.
OP: as long as you don't look into anisotropic filtering, linear, bilinear and trilinear filtering are essentially just interpolation methods. You can check out trilinear (cubic) interpolation on this page (it explains Perlin Noise implementation, but uses a 2D greyscale texture to represent the data, and filters that texture to increase the resolution of the result).
ZQJ
ZQJ
Quote:
Original post by ToohrVyk
ZQJ: your code implements linear filtering.


Hmm. I'm pretty sure that it is bilinear (=linear in 2 directions). This article on flipcode seems to think so. Anyway if you only fetch 4 pixels how else are you supposed to do it?

And no, my example certainly wasn't meant to be how to do it fast, floor() is almost certainly not how to do it in practice.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.