带着色器的SDL_Renderer和SDL_Texture?

时间:2018-10-17 16:09:29

标签: c++ sdl-2

我正在使用C ++中的SDL2开发游戏,当前代码使用带有SDL_Renderer的SDL_Texture来处理精灵。问题在于,HUD应该具有圆形的健康条,而当前关于如何执行此操作的想法是使用灰度类型的图像以及使用带有浮点或字节的着色器进行渲染。在执行此操作期间,有什么方法可以继续使用SDL_Renderer?

编辑:SDL_gpu是否允许我将OpenGL和SDL_Texture风格的渲染结合使用,从而能够重用许多旧代码?

1 个答案:

答案 0 :(得分:-1)

我将图形切换到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之间的运行状况浮动,则将其替换为半透明的红色,如果较高则使其透明。这样一来,您可以使用以下图像:

Greyscale Health Bar

这是通过片段着色器(与上述顶点着色器相同)运行的:

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;

}

哪给我们一个看起来像这样的结果

Screenshot of Health Bar Using Above Shader

相关问题