Greetings everyone,
I have read the conversation at
http://forums.libsdl.org/viewtopic.php?t=6946&sid=e1086f0489c98133fc10fe202ca08214
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[0]); // 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);
Many thanks,
Jeremy Jurksztowicz