Jump to content
  • Advertisement
Sign in to follow this  

OpenGL Texture Opengl

This topic is 2263 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 trouble uploading the surface to opengl texture and switching between multiple textures.

The program compiles, but leaves a blank screen. I was loading a 32 bit PNG file with transparency.

This is the drawing class's Code

class EzDraw
EzDraw(int N);
void getImage(const char *);
bool selectTex(unsigned int num);
void Begin();
void End();
void DrawImage( SDL_Rect & rect, float sx , float sy , float sL );
std::vector<unsigned int> textures;
unsigned int Inc;

EzDraw::EzDraw(int N)
Inc = 0;
glGenTextures(N, &textures[ 0 ] );
static const Uint32 rmask = 0x000000FF;
static const Uint32 bmask = 0x0000FF00;
static const Uint32 gmask = 0x00FF0000;
static const Uint32 amask = 0xFF000000;
static const Uint32 rmask = 0xFF000000;
static const Uint32 bmask = 0x00FF0000;
static const Uint32 gmask = 0x0000FF00;
static const Uint32 amask = 0x000000FF;
void EzDraw::getImage(const char * filename)
SDL_Surface * surface = IMG_Load(filename);
if(surface == NULL)
// could not get filename
SDL_PixelFormat *format = surface->format;
Uint32 width = surface->w;
Uint32 height = surface->h;
Uint32 widthPow = (unsigned) pow( 2, ceil( log( width ) / log( 2 ) ) );
Uint32 heightPow = (unsigned) pow( 2, ceil( log( height ) / log( 2 ) ) );
// Create new empty surface.
SDL_Surface* newSurface = SDL_CreateRGBSurface( SDL_SRCALPHA,
widthPow, heightPow, 32,
rmask, bmask, gmask, amask );
// Fill sprite with alpha.
Uint32 alpha = 0;
alpha = SDL_MapRGBA( format, 0, 0, 0, amask );
SDL_Rect rect;
rect.x = 0;
rect.y = 0;
rect.h = heightPow;
rect.w = widthPow;
int ret = SDL_FillRect( newSurface, &rect, alpha);
surface->flags &= !SDL_SRCALPHA;
// Copy image data to our new surface.
SDL_BlitSurface( surface, 0, newSurface, 0 );
// Bind the texture.
glBindTexture(GL_TEXTURE_2D, textures[Inc]);
// Convert surface to Open GL format.
gluBuild2DMipmaps(GL_TEXTURE_2D, 4,
widthPow, heightPow, GL_RGBA,GL_UNSIGNED_BYTE,
// Free our temporary SDL buffers.
SDL_FreeSurface( surface );
SDL_FreeSurface( newSurface );

bool EzDraw::selectTex(unsigned int num)
if( num < Inc)
glBindTexture(GL_TEXTURE_2D, textures[num]);;
return true;
return false;

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!