I have read the conversation at
which suggests that mixing OpenGL shaders and SDL is a bad idea, and
yet I feel like it would be such a waste to redo the rendering code
from scratch, so I attempted an experiment. Now, I know very little
(nothing) about shaders, but managed to load and compile one
successfully. Now I want a uniform sampler in the fragment shader and
am hitting a roadblock.
My goal is simply to read the pixels from an offscreen render target,
modify them, and send to the main renderer. Using SDL I created an
offscreen surface which works perfectly. If I just render to it and
then switch the render target back to the main one, it copies over
fine. However, when trying to access a sampler from the fragment
shader, I get nothing but black.
I found the following on the opengl wiki, and wonder if it applies to
sampling and copying FROM the same texture as well: “Normally, you
should not sample a texture and render to that same texture at the
same time. This would give you undefined behavior. It might work on
some GPUs and with some driver version but not others.”
Clearly I’m in over my head, but before I continue, I would like to
confirm whether what I want to do is possible without hacking base
SDL. I’m wondering if anyone could spot a newbie mistake in the
following code. The commented out openGL code was an attempt to skip
the SDL render API, but it left me with an oddly coloured image.
RENDER LOOP BEGIN:
SDL_SetRenderTarget(RENDERER, OFFSCREEN); SDL_SetRenderDrawColor(RENDERER, 128, 128, 128, 255); SDL_RenderClear(RENDERER); // Render World Here WIN->render(Point(0, 0)); SDL_SetRenderTarget(RENDERER, 0); glUseProgram(SHADER_PROGRAMS); // If I comment this out, all
copying works fine.
glActiveTexture(GL_TEXTURE0); SDL_GL_BindTexture(OFFSCREEN, &w, &h); /* float w, h; glEnable(GL_TEXTURE_2D); SDL_GL_BindTexture(OFFSCREEN, &w, &h); glBegin(GL_QUADS); glTexCoord2f(0, 0); glVertex2f(0, 0); glTexCoord2f(0, 480); glVertex2f(0, 480); glTexCoord2f(640, 480); glVertex2f(640, 480); glTexCoord2f(640, 0); glVertex2f(640, 0); glEnd(); glDisable(GL_TEXTURE_2D); SDL_GL_UnbindTexture(OFFSCREEN); */ SDL_RenderCopy(RENDERER, OFFSCREEN, NULL, NULL); SDL_RenderPresent(RENDERER); SDL_GL_UnbindTexture(OFFSCREEN);