Hello,
I am currently learning SDL2 and c++ and i ran into an issue while trying to combine OpenGL’s shaders and SDL’s textures. My idea is to render my images to an SDL_texture and then bind this texture to my OpenGL context in order to apply shaders. I already figured out how to bind a texture to my context but as soon as i apply SDL_RenderCopy to copy my images onto it, it breaks (black screen). When binding a texture that was copied onto another texture earlier, I experience the same thing. Although applying SDL_RenderClear works fine as well as using an unmodified texture created from an SDL_Surface with SDL_CreateTextureFromSurface. I think something locks the texture but I can’t figure out how to unlock it (SDL_UnlockTexture does nothing). I already tried out a bunch of things but unfortunately nothing worked. If someone knows a solution, please let me know.
Thank you in advance.
The code snippet for creating and binding the texture:
// load image as texture
SDL_Surface* surface = IMG_Load("img.png");
SDL_Texture* image = SDL_CreateTextureFromSurface(renderer, surface);
SDL_FreeSurface(surface);
// create main texture
SDL_Texture* overlay = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, 500, 500);
SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
SDL_SetRenderTarget(renderer, overlay);
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, image, NULL, NULL); // this line breaks the overlay texture somehow.
SDL_SetRenderTarget(renderer, NULL);
SDL_RenderPresent(renderer);
// generate OpenGL texture
GLuint texture;
glGenTextures(1, &texture);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture);
SDL_GL_BindTexture(overlay, NULL, NULL);
glUniform1i(glGetUniformLocation(shader.getShaderId(), "imageTexture"), 0);```