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 );