0
votes

I am working on a java/canvas based editor for creating textured shapes. The results are later rendered in OpenGL but I need to simulate the interpolation of the color pixels from a texture.

Assume I have a texture with 10 texel by 1 texel. I specifiy a range from 0.0 to 1.0 for a part of a canvas. So I interpolate the texture coordinate of each canvas pixel, find the two neighbouring texels and interpolate the color.

Now I want to draw from tex 0.0 to 2.0 with the texture being repeated. A modulo calculation is already not trivial anmyore, because 1.0, 2.0, ... need to be mapped to 1.0, while 1.x, 2.x, ... need to be mapped to 0.x.

So far i got this to work even for negative values. However the interpolation is "cut of" when the wrapping occurs. The range between the last texel (1.0) and the repeated first texel (0.0) is not respected.

How do I simulate that correctly? I guess I have to deal with the half pixel/texel offset. I want 0.0;1.0 to draw the texture once, 0.0;2.0 to draw it twice and -1.0;2.0 to draw it three times (and so on) with correct interpolation where the texture is repeated. This has to be possible with some simple shifting and modulo formulas right?

Is there maybe some "cheap trick" like adding two more texels on the outsides for continuous interpolation and doing some offset magic to fix scaling issues?

1

1 Answers

2
votes

Your error here is, that a texture coordinate of (0.0) does not hit the center of the first texel. It does hit the left edge of the first texel.

So for implementing wrapping with linear interpolation a texture coordinate of (0.0) would result in reading the first texel and the last texel and weighting them both with 0.5. A texture coordinate of (1.0) would then hit the right edge of the last pixel and would then result in exact the same color as tc (0.0).

enter image description here