Problem drawing with OpenGL & and SDL_Renderer

Hi,
I’m trying to write an OpenGL quad renderer.
I’ve got an OpenGL window created using SDL_CreateWindow, and a renderer created using SDL_CreateRenderer (which is set to use Opengl)

I have SDL_Textures loaded from .png files.
And I have a runtime created OpenGL 2d texture of a box.

If I bind my Opengl box texture and draw using OpenGL calls, all works fine.
If I call SDL_RenderCopyEx with SDL_Textures, again, all works fine.

However, if I use SDL_GL_BindTexture and then draw with OpenGL calls, I get nothing. Verts and Colours are getting to the screen just fine, it’s the texture that’s the problem. In the shader the sampler2D is returning zeros.

I’ve been messing with this for days and I can’t figure it out.

Here’s the code that works with a run time texture:

Code:

glEnable(GL_TEXTURE_2D);
glEnable(GL_BLEND);
glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );

  //Bind program
  glUseProgram( gProgramID );
  glActiveTexture(GL_TEXTURE0);

    // bind texture
    glBindTexture(GL_TEXTURE_2D, g_texture); 

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);     
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);

    // bind color
    glBindBuffer(GL_ARRAY_BUFFER, bufferGLID[EBufferType_Color]);
    glBufferData(GL_ARRAY_BUFFER, colorBufferSize, colorBuffer, GL_DYNAMIC_DRAW);
    glEnableVertexAttribArray( sv_colour );
    glVertexAttribPointer(sv_colour, 4, GL_FLOAT, GL_FALSE, 0, 0);

    // bind vert positions
    glBindBuffer(GL_ARRAY_BUFFER, bufferGLID[EBufferType_Vert]);
    glBufferData(GL_ARRAY_BUFFER, positionBufferSize, positionBuffer, GL_DYNAMIC_DRAW);
	glEnableVertexAttribArray( sv_vert );
    glVertexAttribPointer(sv_vert, 2, GL_FLOAT, GL_FALSE, 0, 0);

    // bind texCoord
    glBindBuffer(GL_ARRAY_BUFFER, bufferGLID[EBufferType_TexCoord]);
    glBufferData(GL_ARRAY_BUFFER, texCoordBufferSize, texCoordBuffer, GL_DYNAMIC_DRAW);
    glEnableVertexAttribArray( sv_texCoord );
    glVertexAttribPointer(sv_texCoord, 2, GL_FLOAT, GL_FALSE, 0, 0);

    // draw 
    glDrawArrays(GL_TRIANGLES, 0, quadCount * 6);       

    // disable states
    glDisableVertexAttribArray(sv_texCoord);
    glDisableVertexAttribArray(sv_vert);
    glDisableVertexAttribArray(sv_colour);

    glBindTexture(GL_TEXTURE_2D, NULL); 

glUseProgram( NULL );

And here’s the code using SDL_Textures that doesn’t work…

Code:

  glEnable(GL_TEXTURE_2D);
  glEnable(GL_BLEND);
  glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );

//Bind program
glUseProgram( gProgramID );
    glActiveTexture(GL_TEXTURE0);

    // bind texture
    int r = SDL_GL_BindTexture(currSprite->textureInfo.texture, NULL, NULL);
    //engine->debugMsg.printff("glGetError = %d", glGetError());
    if(r == -1)
    {
	    engine->debugMsg.printff("SDL_GL_BindTexture - SDL Error: %s\n", SDL_GetError());
    }

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);    
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);

    // bind color
    glBindBuffer(GL_ARRAY_BUFFER, bufferGLID[EBufferType_Color]);
    glBufferData(GL_ARRAY_BUFFER, colorBufferSize, colorBuffer, GL_DYNAMIC_DRAW);
    glEnableVertexAttribArray( sv_colour );
    glVertexAttribPointer(sv_colour, 4, GL_FLOAT, GL_FALSE, 0, 0);

    // bind vert positions
    glBindBuffer(GL_ARRAY_BUFFER, bufferGLID[EBufferType_Vert]);
    glBufferData(GL_ARRAY_BUFFER, positionBufferSize, positionBuffer, GL_DYNAMIC_DRAW);
	glEnableVertexAttribArray( sv_vert );
    glVertexAttribPointer(sv_vert, 2, GL_FLOAT, GL_FALSE, 0, 0);

    // bind texCoord
    glBindBuffer(GL_ARRAY_BUFFER, bufferGLID[EBufferType_TexCoord]);
    glBufferData(GL_ARRAY_BUFFER, texCoordBufferSize, texCoordBuffer, GL_DYNAMIC_DRAW);
    glEnableVertexAttribArray( sv_texCoord );
    glVertexAttribPointer(sv_texCoord, 2, GL_FLOAT, GL_FALSE, 0, 0);

    // draw 
    glDrawArrays(GL_TRIANGLES, 0, quadCount * 6);       

    // disable states
    glDisableVertexAttribArray(sv_texCoord);
    glDisableVertexAttribArray(sv_vert);
    glDisableVertexAttribArray(sv_colour);

    SDL_GL_UnbindTexture(currSprite->textureInfo.texture);
    glUseProgram( NULL );

Oh yes, the SDL_GL_BindTexture doesn’t return -1, so that seems fine.

And one other thing, I can call SDL_RenderCopyEx, followed by OpenGL calls to draw my box, and they work fine together too.
It seems the SDL_GL_BindTexture isn’t working - I think…

2014-05-20 18:53 GMT+02:00 Starg :

And one other thing, I can call SDL_RenderCopyEx, followed by OpenGL
calls to draw my box, and they work fine together too.
It seems the SDL_GL_BindTexture isn’t working - I think…

On desktop OpenGL, SDL prefers the usage of rectangle textures. This means
you will
have to declare your sampler in the shader as “sampler2DRect”, and sample
from it with
the function “texture2DRect”. Here’s more detailed information if you’re
curious:
http://www.opengl.org/registry/specs/ARB/texture_rectangle.txt
Basically, the other important thing to remember is that rectangle textures
are not sampled
via normalized coordinates (0, 1) but with direct texel coordinates (so eg.
(20, 50) gives you
the pixel on the 21st row and 51st column).

Note that your shader code will break again should you use it on a mobile
target, because
on GLES2 SDL usues normal textures. Oh, and should SDL decide to properly
detect that
npot textures are available on desktop GL (which will be true on 99% of
today’s hardware),
your code will break yet again. I think you see what I’m getting at.

Is there any particular reason why you’re partly using SDL’s toy rendering
API instead of just
pure OpenGL? Using SDL_Texture isn’t going to give you any advantages
whatsoever.