Jump to content

  • Log In with Google      Sign In   
  • Create Account


Alpha Blending


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1 Tolito   Members   -  Reputation: 256

Like
0Likes
Like

Posted 29 January 2013 - 05:16 PM

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

Attached Thumbnails

  • gradient.png

Edited by Tolito, 29 January 2013 - 05:23 PM.


Sponsor:

#2 Brother Bob   Moderators   -  Reputation: 7923

Like
2Likes
Like

Posted 29 January 2013 - 05:47 PM

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.



#3 Tolito   Members   -  Reputation: 256

Like
0Likes
Like

Posted 29 January 2013 - 05:53 PM

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!



#4 L. Spiro   Crossbones+   -  Reputation: 13004

Like
1Likes
Like

Posted 29 January 2013 - 05:59 PM

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


It is amazing how often people try to be unique, and yet they are always trying to make others be like them. - L. Spiro 2011
I spent most of my life learning the courage it takes to go out and get what I want. Now that I have it, I am not sure exactly what it is that I want. - L. Spiro 2013
I went to my local Subway once to find some guy yelling at the staff. When someone finally came to take my order and asked, “May I help you?”, I replied, “Yeah, I’ll have one asshole to go.”
L. Spiro Engine: http://lspiroengine.com
L. Spiro Engine Forums: http://lspiroengine.com/forums

#5 Tolito   Members   -  Reputation: 256

Like
0Likes
Like

Posted 29 January 2013 - 06:15 PM

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!






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS