Jump to content
  • Advertisement
Sign in to follow this  
Tolito

OpenGL Alpha Blending

This topic is 1998 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Edited by Tolito

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

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!

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!