'SDL2 Draw scene to texture. SDL2 RenderTexture like SFML

I've been developing a 2D Engine using SFML + ImGui.

Here you can see an image:

The editor is rendered using ImGui and the scene window is a sf::RenderTexture where I draw the GameObjects and then is converted to ImGui::Image to render it in the editor.

Now I need to create a 3D Engine during this year in my Bachelor Degree but using SDL2 + ImGui and I want to recreate what I did with the 2D Engine.

I've managed to render the editor like I did in the 2D Engine using this Example that comes with ImGui.

3D Editor preview

But I don't know how to create an equivalent of sf::RenderTexture in SDL2, so I can draw the 3D scene there and convert it to ImGui::Image to show it in the editor.

If you can provide code will be better. And if you want me to provide any specific code tell me.



Solution 1:[1]

Think you're looking for something like this:

// Create a render texture
SDL_Texture *target = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, width, height);

// Activate the render texture
SDL_SetRenderTarget(renderer, target);

// (Do your rendering here)

// Disable the render texture
SDL_SetRenderTarget(renderer, NULL);

// (Use the render texture)

Solution 2:[2]

Here's my solution.

SDL2window::SDL2window()
{
    SDL_Init(SDL_INIT_VIDEO);
    window = SDL_CreateWindow(...);
    renderer = SDL_CreateRenderer(window,...);
    texture = SDL_CreateTexture(renderer,..., width, height);
    // create another texture for your imgui window here
    ImTexture = SDL_CreateTexture(renderer,..., tex_width, tex_height);
    ...
}
void SDL2window::update(Uint32 *framebuffer)
{
    ...
    // update your main window is necessary
    SDL_RenderCopyEx(renderer, texture, NULL, NULL, 0, 0, SDL_FLIP_VERTICAL);
    // be care of the size of framebuffer
    SDL_UpdateTexture(ImTexture, NULL, framebuffer, tex_width * sizeof(Uint32));
    ImGui::Render();
    ImGui_ImplSDLRenderer_RenderDrawData(ImGui::GetDrawData());
    SDL_RenderPresent(renderer);
}
void SDL2window::MyImgui()
{
    ...
    ImGui::Begin("My Texture");
    ImGui::Image(ImTexture, ImVec2(tex_width, tex_height));
    ImGui::End();
}

Then run MyImgui() in your main loop, it will work.
ImTexture Window
P.S. I like your SFML 2D engine's UI, it looks great.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Mario
Solution 2 caii