gpt4 book ai didi

c++ - 使用 openGL 着色器倾斜图像

转载 作者:行者123 更新时间:2023-11-30 02:49:00 24 4
gpt4 key购买 nike

为了减轻美术师的工作量,我正在为图像制作一个简单的骨骼动画系统。我已经概念化了所有部分的工作原理。但要让它按照我想要的方式进行动画处理,我需要能够实时扭曲图像。 (如果我做不到,我仍然知道如何让它工作,但如果我可以倾斜图像以增加视角,它会更漂亮)

我使用的是 SFML,它不支持图像倾斜。有人告诉我需要在图像上使用 OpenGL 着色器,但我得到的所有建议都是“学习 GLSL”。所以我想也许这里有人可以帮我一些忙。

有人知道我应该从哪里开始吗?或者怎么做?

基本上,我希望能够对图像进行变形以赋予它透视图(如下面的模型所示)

enter image description here左边的图像在顶部和底部倾斜。右侧的图像左右倾斜。

最佳答案

如何在 GLSL 中倾斜纹理的示例是以下(优化不佳的)着色器。理想情况下,您可能希望在您的常规程序中实际预先计算您的变换矩阵并将其作为统一传递,这样您就不会在着色器中的每一步都重新计算变换。如果您仍想在着色器中计算变换,请改为将偏斜因子作为制服传递。否则,每次要更改倾斜因子时,您都必须打开着色器并对其进行编辑。

这也适用于屏幕对齐的四边形。

垂直

attribute vec3 aVertexPosition;

varying vec2 texCoord;

void main(void){
// Set regular texture coords
texCoord = ((vec2(aVertexPosition.xy) + 1.0) / 2.0);

// How much we want to skew each axis by
float xSkew = 0.0;
float ySkew = 0.0;

// Create a transform that will skew our texture coords
mat3 trans = mat3(
1.0 , tan(xSkew), 0.0,
tan(ySkew), 1.0, 0.0,
0.0 , 0.0, 1.0
);

// Apply the transform to our tex coords
texCoord = (trans * (vec3(texCoord.xy, 0.0))).xy;

// Set vertex position
gl_Position = (vec4(aVertexPosition, 1.0));
}

碎片

precision highp float;

uniform sampler2D sceneTexture;

varying vec2 texCoord;

void main(void){
gl_FragColor = texture2D(sceneTexture, texCoord);
}

关于c++ - 使用 openGL 着色器倾斜图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21631963/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com