Jump to content
  • Advertisement
Sign in to follow this  
Jovan

OpenGL Alpha blending SDL-OpenGL

This topic is 4579 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 am using SDL's image library to load an image (png, contains alpha) and change it into an OpenGL texture by using GL_BGR_EXT. Everything goes fine, up until alpha. Whenever I try to use GL_BGRA_EXT or anything else with Alpha it crashes the program on start-up (classic windows error). Should I use libpng instead? I've done glEnable(GL_BLEND) and glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA) in the program's loop.
Surface::Surface(std::string filename) { 

	//Temporary storage for a surface
	temp = IMG_Load(filename.c_str());

	//Create the texture
	glGenTextures(1, &texture);

	//Load the texture
	glBindTexture(GL_TEXTURE_2D, texture);

	//Generate the texture
	glTexImage2D(GL_TEXTURE_2D, 0, 3, temp->w, temp->h, 0, GL_BGR_EXT, GL_UNSIGNED_BYTE, temp->pixels);

	//Assign variables to the width and the height, free the temporary surface
	width  = temp->w;
	height = temp->h;

	SDL_FreeSurface(temp);
}

void Surface::Apply(float x, float y, float angle, float depth) {

	//Wrap the texture
	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);

	//Use linear filtering
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

	//Enable texturing
	glEnable(GL_TEXTURE_2D);

	//Load the texture
	glBindTexture(GL_TEXTURE_2D, texture);
	
	glPushMatrix();

	//Center the image around x and y
	glTranslatef(x, y, depth);

	//Rotation
	glRotatef(angle, 0.0f, 0.0f, 1.0f);

	glBegin(GL_QUADS);
		//Top-left vertex
		glTexCoord2f(0,0);
		glVertex3f((GLfloat)(-0.5*width), (GLfloat)(-0.5*height), 0.0f);
	
		//Bottom-left vertex
		glTexCoord2f(0,1);
		glVertex3f((GLfloat)(-0.5*width), (GLfloat)(0.5*height), 0.0f);
	
		//Bottom-right vertex
		glTexCoord2f(1,1);
		glVertex3f((GLfloat)(0.5*width), (GLfloat)(0.5*height), 0.0f);
	
		//Top-right vertex
		glTexCoord2f(1,0);
		glVertex3f((GLfloat)(0.5*width), (GLfloat)(-0.5*height), 0.0f);
	glEnd();

	glPopMatrix();

	//Disable texturing
	glDisable(GL_TEXTURE_2D);

}

Share this post


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