【发布时间】:2019-12-06 03:49:22
【问题描述】:
我总是在 glsl 3 中制作我的着色器(使用 #version 330 行),但它已经开始很老了,所以我最近尝试在 glsl 4 中制作一个着色器,并将它与用于渲染的 SFML 库,而不是纯粹的 openGL。
目前,我的目标是为 2d 游戏做一个基本的着色器,它获取纹理每个像素的颜色并修改它们。我总是用gl_TexCoord[0].xy 这样做,但它现在似乎贬值了,所以我搜索并听说我必须将 in 和 out 变量与顶点着色器一起使用,所以我尝试了。
片段着色器
#version 400
in vec2 fragCoord;
out vec4 fragColor;
uniform sampler2D image;
void main(){
// Get the color
vec4 color = texture( image, fragCoord );
/*
* Do things with the color
*/
// Return the color
fragColor = color;
}
顶点着色器
#version 400
in vec3 position;
in vec2 textureCoord;
out vec2 fragCoord;
void main(){
// Set the position of the pixel and vertex (I guess)
fragCoord = textureCoord;
gl_Position = vec4( position, 1.0 );
}
我还看到我们可以添加投影、模型和视图矩阵,但我不知道如何使用 SFML(我什至不认为我们可以),我不想学习关于 openGL 或 SFML 的一些复杂事情只是为了改变 2d 游戏的一些颜色,所以这是我的问题:
有没有一种简单的方法来获取我们正在处理的像素的坐标?也许摆脱顶点着色器,或者在不使用矩阵的情况下使用它?
【问题讨论】: