简体   繁体   English

是否可以将 SDL_Texture 绘制到 OpenGL?

[英]Is it possible to draw a SDL_Texture to OpenGL?

i'm currently writing a game on SDL and I use a texture that i create in this way我目前正在 SDL 上编写游戏,并使用我以这种方式创建的纹理

SDL_Texture* target_texture = SDL_CreateTexture(ren, SDL_PIXELFORMAT_BGR555, SDL_TEXTUREACCESS_TARGET, 256, 224);

I later use我后来用

SDL_SetRenderTarget(ren, target_texture);

to make everything be drawn to this texture, everything works fine, after that I just disable the render target to texture, with为了让所有东西都被绘制到这个纹理上,一切正常,之后我只是禁用渲染目标到纹理,

SDL_SetRenderTarget(ren, NULL);

I want to convert my game to use OpenGL for drawing stuff, and to be able to use 3D in it, but I'm running into a issue.我想将我的游戏转换为使用 OpenGL 来绘制东西,并能够在其中使用 3D,但我遇到了问题。 SDL_GL_BindTexture doesn't seem to work with a SDL_Texture, My drawing code is SDL_GL_BindTexture 似乎不适用于 SDL_Texture,我的绘图代码是

        SDL_Rect rect = { 32, 16, 256, 224 };
        float w = 320.f;
        float h = 240.f;
        glViewport(0, 0, w, h);
        glClearColor(0.f, 0.f, 0.f, 1.f);
        glClear(GL_COLOR_BUFFER_BIT);

        float X = float(rect.x) / float(w);
        float Y = float(rect.y) / float(h);
        float Width = float(rect.w) / float(w);
        float Height = float(rect.y) / float(h);

        //Bind the SDL_Texture in OpenGL
        SDL_GL_BindTexture(target_texture, NULL, NULL);

        //Draw the SDL_Texture * as a Quad
        glEnable(GL_TEXTURE_2D);
        glBegin(GL_QUADS); {
            glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
            glTexCoord2f(0, 0); glVertex3f(X, Y, 0);
            glTexCoord2f(1, 0); glVertex3f(X + Width, Y, 0);
            glTexCoord2f(1, 1); glVertex3f(X + Width, Y + Height, 0);
            glTexCoord2f(0, 1); glVertex3f(X, Y + Height, 0);
        } glEnd();
        glDisable(GL_TEXTURE_2D);

        SDL_GL_SwapWindow(win);

This doesn't seems to work and only draws a white square to the window, which is not what i want, instead if i do this这似乎不起作用,只在 window 上绘制一个白色方块,这不是我想要的,如果我这样做

        SDL_RenderCopy(ren, target_texture, NULL, &rect);
        SDL_RenderPresent(ren);

It works fine, but it doesn't allow me to use OpenGL at all.它工作正常,但它根本不允许我使用 OpenGL。 How do i fix this?我该如何解决? Hopefully i explained it well enough希望我解释得足够好

Example minimal reproductible code:示例最小可复制代码:

#include <iostream>

#include <SDL.h>
#include <SDL_opengl.h>
#include <GL/gl.h>

int main(int argc, char* argv[])
{
    SDL_Window* win = SDL_CreateWindow("test", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 320, 240, SDL_WINDOW_SHOWN);
    SDL_Renderer* ren = SDL_CreateRenderer(win, 0, SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_ACCELERATED);
    SDL_Texture* target_texture = SDL_CreateTexture(ren, SDL_PIXELFORMAT_BGR555, SDL_TEXTUREACCESS_TARGET, 256, 224);
    while (true)
    {
        SDL_SetRenderTarget(ren, target_texture);

        SDL_SetRenderDrawColor(ren, 255, 0, 0, 255);


        SDL_RenderClear(ren);
        SDL_SetRenderTarget(ren, NULL);
        SDL_Rect rect = { 32, 16, 256, 224 };
        float w = 320.f;
        float h = 240.f;
        glViewport(0, 0, w, h);
        glClearColor(0.f, 0.f, 0.f, 1.f);
        glClear(GL_COLOR_BUFFER_BIT);

        float X = float(rect.x) / float(w);
        float Y = float(rect.y) / float(h);
        float Width = float(rect.w) / float(w);
        float Height = float(rect.y) / float(h);

        //Bind the SDL_Texture in OpenGL
        SDL_GL_BindTexture(target_texture, NULL, NULL);

        //Draw the SDL_Texture * as a Quad
        glEnable(GL_TEXTURE_2D);
        glBegin(GL_QUADS); {
            glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
            glTexCoord2f(0, 0); glVertex3f(X, Y, 0);
            glTexCoord2f(1, 0); glVertex3f(X + Width, Y, 0);
            glTexCoord2f(1, 1); glVertex3f(X + Width, Y + Height, 0);
            glTexCoord2f(0, 1); glVertex3f(X, Y + Height, 0);
        } glEnd();
        glDisable(GL_TEXTURE_2D);

        SDL_GL_SwapWindow(win);
        SDL_Delay(16);
    }
}

A few issues:几个问题:

  • You aren't passing SDL_WINDOW_OPENGL to SDL_CreateWindow()您没有将SDL_WINDOW_OPENGL传递给SDL_CreateWindow()
  • You aren't creating an OpenGL context with SDL_GL_CreateContext() .您没有使用SDL_GL_CreateContext()创建 OpenGL 上下文。
  • You're trying to use an accelerated SDL_Renderer & OpenGL at the same time;您正在尝试同时使用加速的 SDL_Renderer 和 OpenGL; there's currently no way to save/restore the GL state SDL_Renderer (might) be using to do its job目前没有办法保存/恢复 GL state SDL_Renderer(可能)正在使用它来完成它的工作

If you're dead-set on using a SDL_Renderer create one with the SDL_RENDERER_SOFTWARE flag.如果您死心塌地使用 SDL_Renderer,请使用SDL_RENDERER_SOFTWARE标志创建一个。

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

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