Alpha Blending

Started by
3 comments, last by Tolito 11 years, 2 months ago

I have read many topics on this and I cannot get any of the solutions to work.


#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
#include <SDL/SDL_opengl.h>

int main(int argc, char *argv[])
{
    SDL_Surface *screen;
    if ( SDL_Init(SDL_INIT_VIDEO) != 0 ) {
        printf("Unable to initialize SDL: %s\n", SDL_GetError());
        return 1;
    }

    SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );

    screen = SDL_SetVideoMode( 128, 128, 32, SDL_OPENGL );
    if ( !screen ) {
		printf("Unable to set video mode: %s\n", SDL_GetError());
		return 1;
	}

	glClearColor( 0, 0, 0, 0 );

	glEnable( GL_TEXTURE_2D );

    glViewport( 0, 0, 128, 128 );

    glMatrixMode( GL_PROJECTION );
    glLoadIdentity();

    glOrtho( 0, 128, 128, 0, -1, 1 );

    glMatrixMode( GL_MODELVIEW );
    glLoadIdentity();

    GLuint texture;
    SDL_Surface *surface;

    if((surface=IMG_Load("gradient.png"))) {
        glGenTextures( 1, &texture );
        glBindTexture( GL_TEXTURE_2D, texture );
        glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
        glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
        glTexImage2D( GL_TEXTURE_2D, 0, 3, surface->w, surface->h, 0,
                      GL_RGBA, GL_UNSIGNED_BYTE, surface->pixels );
        SDL_FreeSurface( surface );
    }
    else {
        printf("SDL could not load gradient.png: %s\n", SDL_GetError());
        SDL_Quit();
        return 1;
    }

	glClear( GL_COLOR_BUFFER_BIT );
    glBindTexture( GL_TEXTURE_2D, texture );

    glBegin( GL_QUADS );
        glTexCoord2i( 0, 0 );
        glVertex3f( 0, 0, 0 );

        glTexCoord2i( 1, 0 );
        glVertex3f( 128, 0, 0 );

        glTexCoord2i( 1, 1 );
        glVertex3f( 128, 128, 0 );

        glTexCoord2i( 0, 1 );
        glVertex3f( 0, 128, 0 );
    glEnd();

    SDL_GL_SwapBuffers();

    SDL_Delay(7000);

    glDeleteTextures( 1, &texture );

    SDL_Quit();

	return 0;
}

This came from a tutorial by Kyle Foley. I just condensed the code so there would be less scrolling. I also modified it to load from a PNG file, for the OpenGL texture to use RGBA, and the like.

I have attached gradient.png to this topic. I am baffled at what to do to get alpha blending working. Thank you! smile.png

Advertisement

Your call to glTexImage discards the alpha channel since you only request a texture with three color channels. The third parameter is equivalent to GL_RGB, which means the texture contains a red, a green and a blue channel, but no alpha channel. Use GL_RGBA instead.

This is one of the things I tried a while back on my own. I should have included that change in the source code I posted. The result remains the same even when changing 3 to GL_RGBA as you mentioned. I just now tried it again. It couldn't be my computer or anything, could it? Have you tested this? Thank you for your response!

I don’t see anywhere where you have enabled blending via calls to ::glEnable( GL_BLEND ); and ::glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );.

L. Spiro

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid

Applying both of your solutions to the file works... looks like I was trying them individually before when trying to solve the problem on my own. A reputation++; for the both of you! Thank you!

This topic is closed to new replies.

Advertisement