【问题标题】:Fastest way to draw filled quad/triangle with the SDL2 renderer?使用 SDL2 渲染器绘制填充四边形/三角形的最快方法?
【发布时间】:2021-11-25 13:47:56
【问题描述】:

我有一个使用 SDL2 编写的游戏,以及用于绘图的 SDL2 渲染器(硬件加速)。绘制填充的四边形或三角形有技巧吗?

目前我只是通过绘制大量线条 (SDL_Drawlines) 来填充它们,但是性能很糟糕。

我不想进入 OpenGL。

【问题讨论】:

  • 嗯,第一个使用opengl,第二个只有矩形:)
  • 不确定您是否会找到合理的 OpenGL 替代方案(不包括专有/操作系统特定的兄弟姐妹 Direct3d 和 Metal)。我曾经在 Qt / QPainter 中绘制了一个 HUD(对我渲染的 3d 内容的注释),希望 Qt 似乎也在后台使用 OpenGL 来实现这一点。在收到性能不佳的投诉后,我将代码移植到“手写”OpenGL,任务成功。当然,可能还有其他 API 做得更好……

标签: c++ sdl sdl-2


【解决方案1】:

SDL_RenderGeometry()/SDL_RenderGeometryRaw() 被添加到SDL 2.0.18:

/**
 * The structure that defines a point (floating point)
 *
 * \sa SDL_EnclosePoints
 * \sa SDL_PointInRect
 */
typedef struct SDL_FPoint
{
    float x;
    float y;
} SDL_FPoint;

...

/**
 *  Vertex structure
 */
typedef struct SDL_Vertex
{
    SDL_FPoint position;        /**< Vertex position, in SDL_Renderer coordinates  */
    SDL_Color  color;           /**< Vertex color */
    SDL_FPoint tex_coord;       /**< Normalized texture coordinates, if needed */
} SDL_Vertex;

...

/**
 * Render a list of triangles, optionally using a texture and indices into the
 * vertex array Color and alpha modulation is done per vertex
 * (SDL_SetTextureColorMod and SDL_SetTextureAlphaMod are ignored).
 *
 * \param texture (optional) The SDL texture to use.
 * \param vertices Vertices.
 * \param num_vertices Number of vertices.
 * \param indices (optional) An array of integer indices into the 'vertices'
 *                array, if NULL all vertices will be rendered in sequential
 *                order.
 * \param num_indices Number of indices.
 * \return 0 on success, or -1 if the operation is not supported
 *
 * \sa SDL_Vertex
 */
extern DECLSPEC int SDLCALL SDL_RenderGeometry(SDL_Renderer *renderer,
                                               SDL_Texture *texture,
                                               const SDL_Vertex *vertices, int num_vertices,
                                               const int *indices, int num_indices);

/**
 * Render a list of triangles, optionally using a texture and indices into the
 * vertex arrays Color and alpha modulation is done per vertex
 * (SDL_SetTextureColorMod and SDL_SetTextureAlphaMod are ignored).
 *
 * \param texture (optional) The SDL texture to use.
 * \param xy Vertex positions
 * \param xy_stride Byte size to move from one element to the next element
 * \param color Vertex colors (as SDL_Color)
 * \param color_stride Byte size to move from one element to the next element
 * \param uv Vertex normalized texture coordinates
 * \param uv_stride Byte size to move from one element to the next element
 * \param num_vertices Number of vertices.
 * \param indices (optional) An array of indices into the 'vertices' arrays,
 *                if NULL all vertices will be rendered in sequential order.
 * \param num_indices Number of indices.
 * \param size_indices Index size: 1 (byte), 2 (short), 4 (int)
 * \return 0 on success, or -1 if the operation is not supported
 */
extern DECLSPEC int SDLCALL SDL_RenderGeometryRaw(SDL_Renderer *renderer,
                                               SDL_Texture *texture,
                                               const float *xy, int xy_stride,
                                               const int *color, int color_stride,
                                               const float *uv, int uv_stride,
                                               int num_vertices,
                                               const void *indices, int num_indices, int size_indices);

请注意,由于 API 缺少 Z 坐标的数据通道,因此只能实现 affine texturing

【讨论】:

  • 哇,错过了。 SDL2 渲染器的游戏规则改变者 :)
  • 应该为像nuklear 这样的即时模式GUI 提供基于SDL_Renderer 的后端。
  • 感谢您让我了解核武器。我在我的游戏中使用了 Dear-imgui(带有一个中间库来使用 SDL2 渲染器渲染 imgui - 有点慢),但这看起来也很不错。
  • 作为后续,我刚刚构建了最新的 SDL 源码,并且第一次画了一个实心三角形。真是令人兴奋的日子,谢谢。我不会注意到这一点,因为我只查看官方版本。
  • SDL 2.0.20 已发布,SDL_RenderGeometryRaw 已更改。它现在接收SDL_Color,而不是intgithub.com/libsdl-org/SDL/releases/tag/release-2.0.20
【解决方案2】:

不可能。 SDL2 不包含成熟的渲染引擎。

一些选项:

  • 您可以采用Skia(Chrome 中使用的图形库,等等),然后要么坚持使用软件渲染器,要么实例化 OpenGL 上下文并使用硬件后端。

  • 您可以使用另一个 2D 绘图库,例如 Raylib

  • 或者只是硬着头皮使用 OpenGL 绘制三角形。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-07-26
    • 1970-01-01
    • 2018-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-30
    • 2014-01-01
    相关资源
    最近更新 更多