algorithm - 如何在 GLSL 顶点着色器中实现薄板样条算法?
问题描述
我想制作一个 GLSL 着色器,用于使用 TPS 算法扭曲图像/纹理。我将如何为此编写 GLSL 顶点着色器?
解决方案
答案是我需要制作一个顶点着色器,而不是片段着色器。
我需要的是用 GLSL 实际扭曲图像,似乎这篇文章描述了如何做到这一点:https ://testdrive-archive.azurewebsites.net/Graphics/Warp/Default.html
attribute vec2 aPosition;
varying vec2 vTexCoord;
#define MAXPOINTS 9
uniform vec2 p1[MAXPOINTS]; // where the reference points
uniform vec2 p2[MAXPOINTS]; // where the warp points
void main() {
vTexCoord = aPosition;
vec2 position = aPosition * 2.0 - 1.0; // convert 0 - 1 range to -1 to +1 range
for (int i = 0; i < MAXPOINTS; i++)
{
float dragdistance = distance(p1[i], p2[i]);
float mydistance = distance(p1[i], position);
if (mydistance < dragdistance)
{
vec2 maxdistort = (p2[i] - p1[i]) / 4.0;
float normalizeddistance = mydistance / dragdistance;
float normalizedimpact = (cos(normalizeddistance*3.14159265359)+1.0)/2.0;
position += (maxdistort * normalizedimpact);
}
}
//gl_Position = vec4(aPosition * 2.0 - 1.0, 0.0, 1.0);
gl_Position = vec4(position, 0.0, 1.0);
}
推荐阅读
- c# - 如何将附加功能添加到现有的 .net 站点
- python - Tyring 在 Django 中按年龄对数据库结果进行分组
- html - 我可以只用 CSS 创建这个形状吗?
- ios - 当应用程序处于后台且静音模式开启时需要播放声音
- python - 如何优化我的代码以使我的贪心算法更好?
- java - Spring Boot 2.1.5 无法将 java.lang.String 类型的属性值转换为所需的 java.time.LocalDate 类型
- r - 如何修复 mlogit 错误:“系统在计算上是奇异的:倒数条件数”
- mysql - 由于循环引用,如何在迁移到谷歌云 sql 时禁用外键检查
- assembly - X86: `movsxd rdx,edx` 指令是什么意思?
- database - 我可以以某种方式提供额外的上下文以允许 postgres 有效地排序/限制视图而不计算它们的行吗?