2016-12-08 3 views
0

テクスチャサンプリング機能を実装しましたが、結果はglslのテクスチャと同じではありませんでした。私のコードは次のようなものです:glslのテクスチャのようなテクスチャサンプリング機能を実装する方法

// This function warp sample from texture 
float MyTexture::Sampler(float u,float v) 
{ 
    float _u = (u - floorf(u)) * float(this->width - 1); 
    float _v = (v - floorf(v)) * float(this->height - 1); 
    int x0 = floorf(_u); 
    int y0 = floorf(_v); 
    int x1 = (x0 + 1) % (this->width - 1); 
    int y1 = (y0 + 1) % (this->height - 1); 

    float u1 = (_u - floorf(_u)); 
    float v1 = (_v - floorf(_v)); 
    float u0 = 1.0f - u1; 
    float v0 = 1.0f - v1; 

    float a = (float)texture[x0 * 4 + y0 * this->width * 4]; 
    float b = (float)texture[x0 * 4 + y1 * this->width * 4]; 
    float c = (float)texture[x1 * 4 + y0 * this->width * 4]; 
    float d = (float)texture[x1 * 4 + y1 * this->width * 4]; 

    return (u0 * (v0 * a + v1 * b) + u1 * (v0 * c + v1 * d))/255; 
} 
+3

あなたはどんな結果を期待していますか?コードの結果はどうなりますか? –

答えて

2

私は手動の双線形フィルタリングをしたいと思いますか?サイズによって まず規模:

u = u * texwidth; 

は、整数と小数に分割:

int ui = (int)u; // works if u is positive 
float uf = u - ui; // must be [0..1] 

は、Vのために同じ操作を行います。

n[0] = sample(ui,vi); 
n[1] = sample(ui+1,vi); 
n[2] = sample(ui,vi+1) 
n[3] = sample(ui+1,vi+1) 

は、フィルタ:次に最近隣、4隣人をサンプリングし、あなたがそれを必要とする場合

lerp(float a, float b, float f) { return a+(b-a)*f; } 

行うラッピングと負の値が残っている:lerpは線形補間である

result = lerp (lerp(n[0],n[1],uf), lerp(n[2],n[3],uf), vf); 

。 :)

関連する問題