Texture Opengl

Started by
0 comments, last by slicer4ever 11 years, 10 months ago
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
{
public:
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 );
private:
std::vector<unsigned int> textures;
unsigned int Inc;
};


EzDraw::EzDraw(int N)
{
Inc = 0;
glPixelStorei(GL_UNPACK_ALIGNMENT,4);
glGenTextures(N, &textures[ 0 ] );
}
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
static const Uint32 rmask = 0x000000FF;
static const Uint32 bmask = 0x0000FF00;
static const Uint32 gmask = 0x00FF0000;
static const Uint32 amask = 0xFF000000;
#else
static const Uint32 rmask = 0xFF000000;
static const Uint32 bmask = 0x00FF0000;
static const Uint32 gmask = 0x0000FF00;
static const Uint32 amask = 0x000000FF;
#endif
void EzDraw::getImage(const char * filename)
{
SDL_Surface * surface = IMG_Load(filename);
if(surface == NULL)
{
// could not get filename
return;
}
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;
SDL_SetAlpha( newSurface, SDL_SRCALPHA, SDL_ALPHA_TRANSPARENT );
// 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,
newSurface->pixels);
// Free our temporary SDL buffers.
SDL_FreeSurface( surface );
SDL_FreeSurface( newSurface );
++Inc;
}

bool EzDraw::selectTex(unsigned int num)
{
if( num < Inc)
{
glBindTexture(GL_TEXTURE_2D, textures[num]);;
return true;
}
return false;
}
Advertisement
you need to show us how your drawing them, also, on initial bind/upload, you should specify the min/mag filters.
Check out https://www.facebook.com/LiquidGames for some great games made by me on the Playstation Mobile market.

This topic is closed to new replies.

Advertisement