如何实现纹理采样功能,例如GLSL中的纹理
How implement texture sampling function like texture in 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;
}
我相信您想进行手动双线性过滤吗?按大小的第一比例:
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)
然后过滤:
result = lerp ( lerp(n[0],n[1],uf), lerp(n[2],n[3],uf), vf );
其中LERP是线性插值:
lerp(float a, float b, float f) { return a+(b-a)*f; }
如果需要,则保留包装和负值。:)
相关文章:
- OpenInventor从9.8升级到10.4.2后,GLSL纹理返回零
- 子目录是否继承属性,例如add_definitions,include_directories和父Cmakelist.t
- 标准是否使用多余的大括号(例如 T{{{10}}})定义列表初始化?
- SFML纹理像播放器
- 将数字转换为字母(例如:123 转换为一二三)
- Visual studio代码重构似乎不起作用(例如,重命名符号-f2)
- 如何计算数据类型的范围,例如int
- OpenGL大的3D纹理(>2GB)非常慢
- UE4-如何在给定4个屏幕坐标的情况下缩放纹理或材质
- 着色器纹理值与创建纹理时写入的值不同
- OpenGL将纹理四边形渲染为(0,0)
- 宽度为奇数的16位纹理为片状
- 这是否符合C++标准:双响双响,例如!!(-0.0).
- 如何将CMSampleBufferRef/CIImage/UIImage转换为像素,例如uint8_t[]
- 使用 CUDA 和纹理进行图像减法
- 使用 make 编译 MPI,几个命名空间错误,例如"错误:未知类型名称'使用'?
- 如何使用文件中的文本,例如变量
- 输入值后,如何在一个括号下显示值,例如"{6,7,8,9}
- 给定一个类型,如何派生一个泛型更广泛的类型(例如,用于溢出安全求和)?
- 如何实现纹理采样功能,例如GLSL中的纹理