I use SDL for everything except for rendering, which I use OpenGL for. I initialize OpenGL with this function (this is with blending enabled):
bool initGL()
{
//Set the viewport
glViewport( 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT );
//Initialize Projection Matrix
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
glOrtho( 0.0, SCREEN_WIDTH, SCREEN_HEIGHT, 0.0, 1.0, -1.0 );
//Initialize Modelview Matrix
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
//Initialize clear color
glClearColor( 0.f, 0.f, 0.f, 1.f );
//Enable texturing
glEnable( GL_TEXTURE_2D );
//Enable blending
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable( GL_BLEND );
//Check for error
GLenum error = glGetError();
if( error != GL_NO_ERROR )
{
printf( "Error initializing OpenGL!\n" );
return false;
}
return true;
}
The Image in this post shows what the application renders without the enabling of blending on the left side, the right side is what it renders when I enable blending. The blue window within the application is made out of pieces of textures for the frame and the center is a colored quad, so when I render textures, they're not shown, but when I render geometrical shapes without textures, they're shown.
The for the rendering of that blue window within my application is quite large, so I'm just going to take out the rendering of one piece of texture and the rendering of a non-textured quad:
Non-texture quad rendering:
/******Window fill******/
glLoadIdentity(); //Remove any previous transformations
glDisable( GL_TEXTURE_2D ); //Temporarily disable textures so that the colors won't get messed up
glTranslatef( box.x + ThemeTemplate->quadWidth, box.y + ThemeTemplate->quadHeight, 0.f ); //Move to rendering point
//Render the window fill
glColor3ub( 0, 17, 34 );
glBegin( GL_QUADS );
glVertex2f( 0.f, 0.f );
glVertex2f( box.w - ( ThemeTemplate->quadWidth * 2 ), 0.f );
glVertex2f( box.w - ( ThemeTemplate->quadWidth * 2 ), box.h - ( ThemeTemplate->quadHeight * 2 ) );
glVertex2f( 0.f, box.h - ( ThemeTemplate->quadHeight * 2 ) );
glEnd();
glEnable( GL_TEXTURE_2D ); //Re-enable textures
glColor3f( 1, 1, 1 ); //Reset the color
/******------End------******/
I basically just render a quad at a preset location with the color R0 G17 B34 and then reset the color to FFFFFF so that the next thing that's rendered won't have its colors messed up by this quad's colors.
Texture rendering:
/******Top edge******/
glLoadIdentity(); //Remove any previous transformations
//Texture coordinates
ThemeTemplate->texLeft = 16.f / TexWidth;
ThemeTemplate->texRight = 18.f / TexWidth;
ThemeTemplate->texTop = 0.f;
ThemeTemplate->texBottom = 8.f / TexHeight;
//Set translation point
TranslateX = box.x + ThemeTemplate->quadWidth;
TranslateY = box.y;
glTranslatef( TranslateX, TranslateY, 0.f ); //Move to rendering point
//Fill the whole space
for ( int positionX = 0; positionX < ( box.w - ( ThemeTemplate->quadWidth * 2 ) ); positionX += 2 )
{
//Render textured quad
glBegin( GL_QUADS );
glTexCoord2f( ThemeTemplate->texLeft, ThemeTemplate->texTop ); glVertex2f ( 0.f, 0.f );
glTexCoord2f( ThemeTemplate->texRight, ThemeTemplate->texTop ); glVertex2f( 2.f, 0.f );
glTexCoord2f( ThemeTemplate->texRight, ThemeTemplate->texBottom ); glVertex2f( 2.f, ThemeTemplate->quadHeight );
glTexCoord2f( ThemeTemplate->texLeft, ThemeTemplate->texBottom ); glVertex2f( 0.f, ThemeTemplate->quadHeight );
glEnd();
glTranslatef( 2, 0, 0.f ); //Update rendering point
}
/******------End------******/
This is the top egde of the window's frame, I first select a piece of a sprite sheet and then set the rendering point. The frame is build up by small textures lined up to form a frame, so the for loop just makes sure that the textures are rendered next to each other. I then simply make a quad and render a texture that comes from a section of a spritesheet, this section is set with 'ThemeTemplate->texLeft' ect.
I'm quite confused when it comes to the source of the problem as I'm new to OpenGL, but perhaps the problem lies in my loading of textures?
(Sorry for the code dump)
bool Texture::load_texture( SDL_Surface *image )
{
// Check that the image's width is a power of 2
if ( (image->w & (image->w - 1)) != 0 )
fprintf( stderr, "warning: image.bmp's width is not a power of 2\n");
// Also check if the height is a power of 2
if ( (image->h & (image->h - 1)) != 0 )
fprintf( stderr, "warning: image.bmp's height is not a power of 2\n");
//Get the number of channels in the SDL_Surface
nOfColors = image->format->BytesPerPixel;
if ( nOfColors == 4 )
{
fprintf( stderr, "4 channels on texture\n" );
if ( image->format->Rmask == 0x000000ff )
texFormat = GL_RGBA;
else
texFormat = GL_BGRA;
}
else if ( nOfColors == 3 )
{
fprintf( stderr, "3 channels on texture\n" );
if ( image->format->Rmask == 0x000000ff )
texFormat = GL_RGB;
else
texFormat = GL_BGR;
}
else
{
fprintf( stderr, "warning: the image is not truecolor\n" );
//This error should not go unhandled
}
// Have OpenGL generate a texture object handle for us
glGenTextures( 1, &data );
// Bind the texture object
glBindTexture( GL_TEXTURE_2D, data );
// Edit the texture object's image data using the information SDL_Surface gives us
glTexImage2D( GL_TEXTURE_2D, 0, nOfColors, image->w, image->h, 0, texFormat, GL_ UNSIGNED_BYTE, image->pixels );
// Set the texture's stretching properties
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
// Unbind texture
glBindTexture( GL_TEXTURE_2D, NULL );
// Save the dimensions of the texture
TextureWidth = (float)image->w;
TextureHeight = (float)image->h;
return true;
}
The output to stderr is "4 channels on texture", for every texture, so all of the textures in my application has 4 channels. (I'm loading .png files, so they should also have 4 channels).
Let me know if there's something else you need to know.