简体   繁体   English

带着色器的SDL_Renderer和SDL_Texture?

[英]SDL_Renderer & SDL_Texture with shaders?

I'm working on a game with SDL2 in C++, and the current code uses SDL_Texture with SDL_Renderer to handle sprites. 我正在使用C ++中的SDL2开发游戏,当前代码使用SDL_Texture和SDL_Renderer来处理子画面。 The problem is that the HUD is supposed to have a circular health bar, and the current thought on how to do that was to use a grey scale type image and using a shader with a float or byte passed to it for rendering. 问题在于,HUD应该具有圆形的健康条,而当前关于如何执行此操作的想法是使用灰度类型的图像以及使用带有浮点或字节的着色器进行渲染。 Is there any way to keep using SDL_Renderer while doing this? 在执行此操作期间,有什么方法可以继续使用SDL_Renderer?

Edit: Will SDL_gpu allow me to combine OpenGL and SDL_Texture-esque rendering that will be able to reuse a lot of the old code? 编辑:SDL_gpu是否允许我将OpenGL和SDL_Texture风格的渲染结合使用,从而能够重用许多旧代码?

I made a switch over to SDL-GPU for my graphics, and it allows for GLSL shaders to be used on GPU_Image, which is the equivalent of SDL_Texture. 我将图形切换到SDL-GPU,它允许在GPU_Image(与SDL_Texture等效)上使用GLSL着色器。 The basic shader format is that below, with the image uniforms being set with GPU_ShaderBlock: 基本的着色器格式如下,其中图像统一设置为GPU_ShaderBlock:

base.vert 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);

}

This allows you to use GLSL shaders to manipulate your textures if you want. 如果需要,这使您可以使用GLSL着色器来操纵纹理。 An easy example is the circular health bar as asked. 一个简单的示例是所要求的圆形健康栏。 The shader example I'll use gets the red value, and if it is lower than the health float between 0.0f and 1.0f, it replaces it with a semi-transparent red, and if it is higher it makes it transparent. 我将使用的着色器示例获取红色值,如果它低于0.0f和1.0f之间的运行状况浮动,则将其替换为半透明的红色,如果较高则使其透明。 This lets you use an image like the one below: 这样一来,您可以使用以下图像:

灰度健康吧

This is run through the fragment shader (same vertex shader as above): 这是通过片段着色器(与上述顶点着色器相同)运行的:

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;

}

Which gives us a result that looks like this: 这样我们得到的结果如下所示:

使用高于着色器的Health Bar屏幕截图

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM