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!