Tricky texture addressing (clamping complications)

Started by
2 comments, last by GameDev.net 18 years, 4 months ago
I am loading a number of textures from file which are of "strange" sizes, for example, 192x192. It's necessary to put these into a pow2 texture, but stretching them to the full size is not an option. (If these constraints seem odd, it's because I have simplified the scenario somewhat.) The images are loaded into the upper left corner of their texture without any problems. The rest of the texture is black. What becomes a little touchy is rendering them. I know the correct ranges for the texture coordinates, which would be [0, .75] in the case of a 192x192 image embedded in a 256x256 texture. The problem arises when I'm nudging up against the edge of that 0.75 texture coordinate. Let's suppose I'm simply drawing a plane with the image on it and the full [0, .75] tex coord range. Now, I'm using linear texture filtering, so you can probably guess what happens at the edges -- bogus coloring when the texture filter chooses values outside the valid texture range. In the general case this is fixed by using the clamp address mode, but of course that only works for your normal [0,1] range. So I need to clamp the texture filter to an arbitrary range, and I can't figure out any easy way to do that. The only solution I've thought of so far is to set the filtering to nearest/point and use a pixel shader to do the actual bilinear filtering with 4 lookups. I don't like this solution, not at all. First of all, I lose any sort of anisotropic or more sophisticated filtering that the card can do. Second, my application is already very heavy on the pixel pipe, and ideally I'd be able to render about 4-6 gigapixels per second. I don't think that's possible with 4 texture lookups in the pixel shader, not on any consumer hardware. I need to do much better than this. So, graphics gurus, how can I achieve what I'm going for?
SlimDX | Ventspace Blog | Twitter | Diverse teams make better games. I am currently hiring capable C++ engine developers in Baltimore, MD.
Advertisement
What is stopping you from duplicating the pixels at the edge of the 192x192 image into the 256x256 texture?

Or adjust your tex coord to less than .75 so that you don't sample over the 192th pixel?

Also doesn't your card support npot textures?
The solution is to adjust your texture cooridnates by 0.5/256 which is half a pixel in your texture this has todo with the way texture addressing and bilinear filtering works. Well computing the fragment color for example the texture coordinate value 1/256 is the border between the first pixel and the second pixel not on an actual single pixel so adding 0.5/256 sides it over the middle of the second pixel. Being in the middle bilinear filtration only finds that one pixel not any of the pixels next to it.
In Direct3d the tex coordinate (0,0) maps to the top left corner of the texel/pixel wheras in OpenGL (0,0) maps to the center of the texel/pixel.

This topic is closed to new replies.

Advertisement