Still no luck.
I added the glEnable for the texture (As Patrick suggested), which I
thought was possibly the biggest issue - didn’t fix the immediate problem,
but I’m sure it was contributing.
Here is the new code for binding my texture:
void Surface::openglBindTexture( void )
{
assert( this->m_surface != NULL );
int glerror = 0;
if( !this->m_hastexture ) {
int w = (int)std::pow(2, std::ceil( std::log((float)this->m_surface->w) /
std::log(2.0f) ) );
int bpp;
Uint32 Rmask, Gmask, Bmask, Amask;
SDL_PixelFormatEnumToMasks(
/SDL_PIXELFORMAT_ABGR8888/ SDL_PIXELFORMAT_RGBA8888, &bpp,
&Rmask, &Gmask, &Bmask, &Amask
);
/* Create surface that will hold pixels passed into OpenGL. */
SDL_Surface *img_rgba8888 = SDL_CreateRGBSurface(0,
this->m_surface->w, this->m_surface->h, bpp,
Rmask, Gmask, Bmask, Amask
);
SDL_SetSurfaceAlphaMod( this->m_surface, 0xFF );
SDL_SetSurfaceBlendMode( this->m_surface, SDL_BLENDMODE_NONE );
SDL_BlitSurface( this->m_surface, NULL, img_rgba8888, NULL );
glGenTextures( 1, &this->m_texture );
glBindTexture( GL_TEXTURE_2D, this->m_texture );
//glTexImage2D( GL_TEXTURE_2D, 0, /GL_DEPTH_COMPONENT24/3, w, w, 0,
GL_RGBA, GL_UNSIGNED_BYTE, this->m_surface->pixels );
glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, w, w, 0, GL_RGBA,
GL_UNSIGNED_BYTE, NULL );
glerror = GLException::glError(); if( glerror != 0 ) throw new
GLException( “Surface::openglBindTexture::glTexImage2D”, glerror, FILE,
LINE );
glTexSubImage2D( GL_TEXTURE_2D, 0, 0, 0, this->m_surface->w,
this->m_surface->h, GL_RGBA, GL_UNSIGNED_BYTE, img_rgba8888->pixels ); //
!!! Throws an Exception !!!
glerror = GLException::glError(); if( glerror != 0 ) throw new
GLException( “Surface::openglBindTexture::glTexSubImage2D”, glerror,
FILE, LINE );
SDL_FreeSurface(img_rgba8888);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); // Linear
Filtering
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); // Linear
Filtering
glerror = GLException::glError();
if( glerror != 0 ) throw new GLException(
“Surface::openglBindTexture::Importing raw texture”, glerror, FILE,
LINE );
this->m_hastexture = true;
}
glBindTexture( GL_TEXTURE_2D, this->m_texture );
}
Also, here is my updated opengl setup code:
void Application::glSetup( void )
{
glClearColor( 0, 0, 0, 0 );
glViewport( 0, 0, 800, 600 );
glMatrixMode( GL_PROJECTION );
glLoadIdentity( );
glOrtho(0, 800, 0, 600, 0, 1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glLineWidth( 2.0f );
glEnable(GL_LINE_SMOOTH);
glEnable(GL_BLEND);
glEnable(GL_TEXTURE_2D);
}
I decided to open add a new window to test my surface just for a sanity
check - everything looks perfect in the surface, so the issue is definitely
in converting the surface to an OpenGL texture.
I wrote my own exception stuff, and I’m getting an opengl error on
my glTexSubImage2D call, and glGetError() gives me GL_INVALID_VALUE
I’ve tried going over all my stuff, tried a few different values (see
my SDL_PixelFormatEnumToMasks call), and I either get a white or a
blue-green box, but not my texture 
Sorry to keep bothering you gents with this, again, I can move it to
gamedev if it’s more appropriate there.
Also, thanks Atis for that code, I feel like it was a step or two in the
right direction. And to Patrick and Chris for advice and ideas.
-AlexOn Tue, May 15, 2012 at 10:17 AM, Atis wrote:
**
Here’s the code that I use with SDL2 to convert a surface into texture.
Note the lines that disable blending for source surface which seem to be
necessary when the original image surface has an alpha channel.
Code:
static void
surface_to_texture(SDL_Surface *img, unsigned *w, unsigned h)
{
/ OpenGL pixel format for destination surface. */
int bpp;
Uint32 Rmask, Gmask, Bmask, Amask;
SDL_PixelFormatEnumToMasks(SDL_PIXELFORMAT_ABGR8888, &bpp, &Rmask,
&Gmask, &Bmask, &Amask);
/* Create surface that will hold pixels passed into OpenGL. */
SDL_Surface *img_rgba8888 = SDL_CreateRGBSurface(0, img->w,
img->h, bpp,
Rmask, Gmask,
Bmask, Amask);
/*
* Disable blending for source surface. If this is not done, all
* destination surface pixels end up with crazy alpha values.
*/
SDL_SetSurfaceAlphaMod(img, 0xFF);
SDL_SetSurfaceBlendMode(img, SDL_BLENDMODE_NONE);
/* Blit to this surface, effectively converting the format. */
SDL_BlitSurface(img, NULL, img_rgba8888, NULL);
/* Store width and height as return values. */
assert(w && h);
*w = img->w;
*h = img->h;
unsigned pow_w = nearest_pow2(img->w);
unsigned pow_h = nearest_pow2(img->h);
/*
* Create a blank texture with power-of-two dimensions. Then load
* converted image data into its lower left.
*/
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, pow_w, pow_h, 0,
GL_RGBA, GL_UNSIGNED_BYTE, NULL);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, img->w, img->h, GL_RGBA,
GL_UNSIGNED_BYTE, img_rgba8888->pixels);
SDL_FreeSurface(img_rgba8888);
}
SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org