Jump to content
  • Advertisement
Sign in to follow this  

OpenGL Loading PNG with SDL_Image

This topic is 4137 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 trying to load and draw an image using SDL_Image to OpenGL, but its just drawing a big blue box, not even where the coords are. My image is 256x16. It has worked before and the only thing I've changes is the way it loads the images. Main code:
Image *img = graphics->loadImage("vital_bar.png");

	graphics->drawImage( img, Rect( 0, 0, 256, 16 ), Rect( 10, 10, 0, 0 ) );

Image *VideoDriver::loadImage( std::string filename )
	SDL_Surface *tmp = IMG_Load( filename.c_str() );
	GLuint texture;
	if(tmp == NULL)
		logger->write( "Faild to load image: %s", filename.c_str() );
		return false;
		SDL_SetColorKey( tmp, SDL_SRCCOLORKEY, SDL_MapRGB( tmp->format, 255, 128, 0 ));

		if( ( tmp->w & (tmp->w - 1)) != 0 )
			logger->write( "warning: %s's width is not a power of 2.", filename.c_str() );

		if( (tmp->h & (tmp->h -1)) != 0 )
			logger->write( "warning: %s's height is not a power of 2.", filename.c_str() );

		glGenTextures( 1, &texture );
		glBindTexture( GL_TEXTURE_2D, texture );

		glTexImage2D( GL_TEXTURE_2D, 0, 3, tmp->w, tmp->h, 0, GL_BGR, GL_UNSIGNED_BYTE, tmp->pixels );

		mHeight = tmp->h;
		mWidth = tmp->w;

		Image *img = new Image( texture );

		SDL_FreeSurface( tmp );

		return img;
#include "log.h"
#include <SDL_opengl.h>

extern Logger *logger;

class Image
	Image( GLuint texture );
	GLuint getTexture();
	int getWidth();
	int getHeight();
	GLuint mTexture;
	int mWidth, mHeight;

Drawing Code:
void VideoDriver::drawImage( Image *image, Rect &src, Rect &dest )
	if(image == NULL)

	float texX1	= src.x / (float)image->getWidth();
	float texY1 = src.y / (float)image->getHeight();
	float texX2 = (src.x + src.width) / (float)image->getWidth();
	float texY2 = (src.y + src.height) / (float)image->getHeight();

	glBindTexture( GL_TEXTURE_2D, image->getTexture() );
	glEnable( GL_TEXTURE_2D );

	glBegin( GL_QUADS );
		glTexCoord2f( texX1, texY1 );
        glVertex3f( (float)dest.x , (float)dest.y, 0 );

		glTexCoord2f( texX2, texY1 );
        glVertex3f( (float)dest.x + (float)src.width, (float)dest.y, 0 );

		glTexCoord2f( texX2, texY2 );
        glVertex3f( (float)dest.x + (float)src.width, (float)dest.y + (float)src.height, 0 );

		glTexCoord2f( texX1, texY2 );
        glVertex3f( (float)dest.x, (float)dest.y + (float)src.height, 0 );

	glDisable( GL_TEXTURE_2D );


Share this post

Link to post
Share on other sites
Check the values you're using as texture coordinates. A solid color quad suggests your texture coordinates are all (0,0), so you're only seeing the color of upper left-most pixel. (though I can't say this for certain, as I don't know what the texture looks like).

You say you've "only changed the way images are loaded", but that can have a lot of effects. For example, is your Image class returning the correct width and height? If not, this will mess up the texture coordinates you're calculating.

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!