SDL_Renderer & SDL_Texture 使用着色器?
SDL_Renderer & SDL_Texture with shaders?
我正在使用 C++ 编写 SDL2 游戏,当前代码使用 SDL_Texture 和 SDL_Renderer 来处理精灵。问题是 HUD 应该有一个圆形的健康栏,而目前关于如何做到这一点的想法是使用灰度类型图像并使用带有浮点数或字节传递给它的着色器进行渲染。有没有办法在执行此操作时继续使用 SDL_Renderer?
编辑:SDL_gpu 是否允许我结合 OpenGL 和 SDL_Texture-esque 渲染,以便能够重用大量旧代码?
我为我的图形切换到了 SDL-GPU,它允许在 GPU_Image 上使用 GLSL 着色器,这相当于 SDL_Texture。基本着色器格式如下,图像制服设置为 GPU_ShaderBlock:
base.vert
#version 150
attribute vec3 gpu_Vertex;
attribute vec2 gpu_TexCoord;
attribute vec4 gpu_Color;
uniform mat4 gpu_ModelViewProjectionMatrix;
varying vec4 color;
varying vec2 texCoord;
void main (void) {
color = gpu_Color;
texCoord = vec2 (gpu_TexCoord);
gl_Position = gpu_ModelViewProjectionMatrix * vec4 (gpu_Vertex, 1.0);
}
base.frag
#version 150
varying vec4 color;
varying vec2 texCoord;
uniform sampler2D tex;
void main (void) {
gl_FragColor = texture (tex, texCoord);
}
这允许您根据需要使用 GLSL 着色器来操纵纹理。一个简单的例子是所要求的圆形健康栏。我将使用的着色器示例获取红色值,如果它低于 0.0f 和 1.0f 之间的健康浮点数,它会用半透明的红色替换它,如果它更高则使其透明。这使您可以使用如下所示的图像:
这是运行通过片段着色器(与上面相同的顶点着色器):
health.frag
#version 150
varying vec4 color;
varying vec2 texCoord;
varying float healthFloat;
uniform sampler2D tex;
uniform float health;
void main (void) {
vec4 texColor = texture (tex, texCoord);
if (texColor.x <= health) {
texColor = vec4 (1.0, 0.0, 0.0, 0.8);
} else {
texColor = vec4 (0.0, 0.0, 0.0, 0.0);
}
gl_FragColor = texColor;
}
结果如下:
我正在使用 C++ 编写 SDL2 游戏,当前代码使用 SDL_Texture 和 SDL_Renderer 来处理精灵。问题是 HUD 应该有一个圆形的健康栏,而目前关于如何做到这一点的想法是使用灰度类型图像并使用带有浮点数或字节传递给它的着色器进行渲染。有没有办法在执行此操作时继续使用 SDL_Renderer?
编辑:SDL_gpu 是否允许我结合 OpenGL 和 SDL_Texture-esque 渲染,以便能够重用大量旧代码?
我为我的图形切换到了 SDL-GPU,它允许在 GPU_Image 上使用 GLSL 着色器,这相当于 SDL_Texture。基本着色器格式如下,图像制服设置为 GPU_ShaderBlock:
base.vert
#version 150
attribute vec3 gpu_Vertex;
attribute vec2 gpu_TexCoord;
attribute vec4 gpu_Color;
uniform mat4 gpu_ModelViewProjectionMatrix;
varying vec4 color;
varying vec2 texCoord;
void main (void) {
color = gpu_Color;
texCoord = vec2 (gpu_TexCoord);
gl_Position = gpu_ModelViewProjectionMatrix * vec4 (gpu_Vertex, 1.0);
}
base.frag
#version 150
varying vec4 color;
varying vec2 texCoord;
uniform sampler2D tex;
void main (void) {
gl_FragColor = texture (tex, texCoord);
}
这允许您根据需要使用 GLSL 着色器来操纵纹理。一个简单的例子是所要求的圆形健康栏。我将使用的着色器示例获取红色值,如果它低于 0.0f 和 1.0f 之间的健康浮点数,它会用半透明的红色替换它,如果它更高则使其透明。这使您可以使用如下所示的图像:
这是运行通过片段着色器(与上面相同的顶点着色器):
health.frag
#version 150
varying vec4 color;
varying vec2 texCoord;
varying float healthFloat;
uniform sampler2D tex;
uniform float health;
void main (void) {
vec4 texColor = texture (tex, texCoord);
if (texColor.x <= health) {
texColor = vec4 (1.0, 0.0, 0.0, 0.8);
} else {
texColor = vec4 (0.0, 0.0, 0.0, 0.0);
}
gl_FragColor = texColor;
}
结果如下: