Jump to content

  • Log In with Google      Sign In   
  • Create Account

#ActualMaxFire

Posted 01 February 2012 - 07:23 AM

Hi guys edited this post due to location my problem (my original post was way off the mark), basically when I load an image using IMg_Load libery I then have to convert this image to 2^n dimensions for OpenGL blending. The way I go about this is to create a new surface with the same color format as the original one then simply blit the old onto the new. This causes a side effect, for some reason alpha is spread across the whole of the image making the background image show through. Any Ideas on how to prevent this, I have tried calling SDL_SetAlpha but nothing changed.

Here's my surface code

surface = SDL_CreateRGBSurface( SDL_SWSURFACE, Get2nNumber( origionalSurface->w ) , Get2nNumber( origionalSurface->h ), origionalSurface->format->BitsPerPixel, origionalSurface->format->Rmask, origionalSurface->format->Gmask, origionalSurface->format->Bmask, origionalSurface->format->Amask );
  SDL_Rect corner;
  corner.x = 0;
  corner.y = 0;

  SDL_BlitSurface( origionalSurface, NULL, surface, &corner );

#3MaxFire

Posted 01 February 2012 - 07:22 AM

Hi guys edited this post due to location my problem (my original post was way off the mark), basically when I load an image using IMg_Load libery I then have to convert this image to a 2^n number for OpenGL blending. The way I go about this is to create a new surface with the same color format as the original one then simply blit the old onto the new. This causes a side effect, for some reason alpha is spread across the whole of the image making the background image show through. Any Ideas on how to prevent this, I have tried calling SDL_SetAlpha but nothing changed.

Here's my surface code

surface = SDL_CreateRGBSurface( SDL_SWSURFACE, Get2nNumber( origionalSurface->w ) , Get2nNumber( origionalSurface->h ), origionalSurface->format->BitsPerPixel, origionalSurface->format->Rmask, origionalSurface->format->Gmask, origionalSurface->format->Bmask, origionalSurface->format->Amask ); 
  SDL_Rect corner;
  corner.x = 0;
  corner.y = 0;
 
  SDL_BlitSurface( origionalSurface, NULL, surface, &corner );

#2MaxFire

Posted 01 February 2012 - 06:04 AM

Hi gys got a slight problem, basically I am enabling glEnable( GL_BLEND ) so that I can use alpha pixels on my sprites, but this causes an odd effect, if I have two images (one rendering on top of another) the whole top image blends to the one below changing the colours giving it a glow effect.

I believe this is either down to the way I load the texture or I have simply forgot to disable something. Heres my startup, texture load and render function.

bool MainWnd::Init()
{
_initializedOK = false;
//initialise everthing
if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 )
{
  return _initializedOK;
}
SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
//set up the _pScreen
//glmod
//_pScreen = SDL_SetVideoMode( Initialize::SCREEN_WIDTH, Initialize::SCREEN_HEIGHT, Initialize::SCREEN_BPP, SDL_DOUBLEBUF|SDL_HWSURFACE );
_pScreen = SDL_SetVideoMode( Initialize::SCREEN_WIDTH, Initialize::SCREEN_HEIGHT, Initialize::SCREEN_BPP, SDL_OPENGL );

if( _pScreen == NULL )
{
  return false;
}
//Initialize OpenGL
if( InitGL() == false )
{
  return false;
}
if( _pScreen == NULL )
{
  return false;
}
//Initialize SDL_ttf
	if( TTF_Init() == -1 )
	{
		return false;  
	}
ClearScreen();
_initializedOK = true;
return _initializedOK;
}
bool MainWnd::InitGL()
{
	//Set clear color
	glClearColor( 0, 0, 0, 0 );
glViewport( 0, 0, Initialize::SCREEN_WIDTH, Initialize::SCREEN_HEIGHT );
SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
	//Set projection
	glMatrixMode( GL_PROJECTION );
	glLoadIdentity();
glOrtho( 0, Initialize::SCREEN_WIDTH, Initialize::SCREEN_HEIGHT, 0, -1, 1 );
glEnable( GL_BLEND );
glBlendFunc( GL_ONE, GL_ONE_MINUS_SRC_ALPHA );
	glDisable( GL_LIGHTING );
glDisable( GL_DEPTH_TEST );
	//Initialize modelview matrix
	glMatrixMode( GL_MODELVIEW );
	glLoadIdentity();

	//If there was any errors
	if( glGetError() != GL_NO_ERROR )
	{
		return false;
	}
	//If everything initialized
	return true;
}

GraphicsTexture* ImageLoader::LoadGL( string location )
{
GLuint texture;   // This is a handle to our texture object
SDL_Surface* surface; // This surface will tell us the details of the image
GLenum texture_format;
GLint  nOfColors;
//convert surface to 2^n
SDL_Surface* origionalSurface = LoadSDL( location );
if( origionalSurface != NULL )
{
  int spriteWidth = origionalSurface->w;
  int spriteHeight =  origionalSurface->h;
  int textureWidth = 0;
  int textureHeight = 0;
  surface = SDL_CreateRGBSurface( SDL_SWSURFACE, Get2nNumber( origionalSurface->w ) , Get2nNumber( origionalSurface->h ), 32, 0, 0,0 , 0 );
  SDL_FillRect( surface, &surface->clip_rect, SDL_MapRGBA( surface->format,0, 0, 0, 0 ) );
  SDL_Rect corner;
  corner.x = 0;
  corner.y = 0;
  SDL_BlitSurface( origionalSurface, NULL, surface, &corner );	
  if( surface != NULL )
  {
   //error check
   // Check that the image's width is a power of 2
   if ( ( surface->w & ( surface->w - 1 ) ) != 0 )
   {
	cout << "warning: image's width is not a power of 2\n";
   }

   // Also check if the height is a power of 2
   if ( ( surface->h & ( surface->h - 1 ) ) != 0 )
   {
	cout << "warning: image's height is not a power of 2\n";
   }
   bool colorFlag = true;
   // get the number of channels in the SDL surface
   nOfColors = surface->format->BytesPerPixel;
   if (nOfColors == 4)	 // contains an alpha channel
   {
	 if (surface->format->Rmask == 0x000000ff)
	 {
	   texture_format = GL_RGBA;
	 }
	 else
	 {
	   texture_format = GL_BGRA;
	 }
   }
   else if ( nOfColors == 3 )	 // no alpha channel
   {
	 if ( surface->format->Rmask == 0x000000ff )
	 {
	   texture_format = GL_RGB;
	 }
	 else
	 {
	   texture_format = GL_BGR;
  
	 }
   }
   else
   {
	 printf( "warning: the image is not truecolor..  this will probably break\n" );
	 colorFlag = false;	
   }
   if( colorFlag )
   {  
	textureWidth = surface->w;
	textureHeight = surface->h;
	// Have OpenGL generate a texture object handle for us
	glGenTextures( 1, &texture );
	// Bind the texture object
	glBindTexture( GL_TEXTURE_2D, texture );

	// Set the texture's stretching properties
	  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
	  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
	  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

	// Edit the texture object's image data using the information SDL_Surface gives us
	glTexImage2D( GL_TEXTURE_2D, 0, nOfColors, surface->w, surface->h, 0,
		 texture_format, GL_UNSIGNED_BYTE, surface->pixels );
	//SDL_FreeSurface( origionalSurface );
	SDL_FreeSurface( surface );
	GraphicsTexture* graphicsTexture = new GraphicsTexture( texture, texture_format, nOfColors, textureWidth, textureHeight, spriteWidth, spriteHeight );
	return graphicsTexture;
   }
  }
}
return NULL;
}

void MainWnd::RenderSprite( GraphicsTexture* graphic, SDL_Rect position, SDL_Rect clipping, bool blend )
{
if( blend == false )
{
  glDisable( GL_BLEND );
}
else
{
  glEnable( GL_BLEND );
}
// Enable 2D rendering
	glColor4ub( 255,255,255,255 );
glEnable( GL_TEXTURE_2D );
  
glBindTexture( GL_TEXTURE_2D, graphic->GetTexture() );
glTranslatef( position.x, position.y, 0 );
float x = 0;
float y = 0;
	glBegin( GL_QUADS );

  x = GetClippingPercentage( clipping.x + 0.0, graphic->_TextureDimentions.width + 0.0 );
  y = GetClippingPercentage( clipping.y + 0.0, graphic->_TextureDimentions.height+ 0.0 );
  glTexCoord2f( x, y );
  glVertex3f( 0.0f, 0.0f, 0.0f );//top left

  x = GetClippingPercentage( ( clipping.x + clipping.w ), graphic->_TextureDimentions.width );
  y = GetClippingPercentage( clipping.y, graphic->_TextureDimentions.height );
  glTexCoord2f( x, y );
  glVertex3f( ( clipping.w + 0.0f ), 0.0f, 0.0f );//top right
  
  x = GetClippingPercentage( clipping.x  + clipping.w + 0.0, graphic->_TextureDimentions.width + 0.0 );
  y = GetClippingPercentage( ( clipping.y + clipping.h + 0.0 ), ( graphic->_TextureDimentions.height + 0.0 ) );
  glTexCoord2f( x, y );
  glVertex3f( ( clipping.w + 0.0f ), ( clipping.h + 0.0f ), 0.0f );//bottom right
  x = GetClippingPercentage( clipping.x + 0.0 , graphic->_TextureDimentions.width + 0.0 );
  y = GetClippingPercentage( clipping.y + clipping.h + 0.0, graphic->_TextureDimentions.height + 0.0 );
  glTexCoord2f( x, y );
  glVertex3f( 0.0f, ( clipping.h + 0.0f ), 0.0f );//bottom left
glEnd();

glDisable( GL_TEXTURE_2D );
glDisable( GL_BLEND );
//Reset
	glLoadIdentity();
}

p.s sorry for the vast amount of code Posted Image but I am full on stuck been reading documentation and tutorials for hours with no success

#1MaxFire

Posted 31 January 2012 - 04:24 PM

Hi gys got a slight problem, basically I am enabling glEnable( GL_BLEND ) so that I can use alpha pixels on my sprites, but this causes an odd effect, if I have two images (one rendering on top of another) the whole top image blends to the one below changing the colours giving it a glow effect.

I believe this is either down to the way I load the texture or I have simply forgot to disable something. Heres my startup, texture load and render function.

bool MainWnd::Init()
{
_initializedOK = false;
//initialise everthing
if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 )
{
  return _initializedOK;
}
SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
//set up the _pScreen
//glmod
//_pScreen = SDL_SetVideoMode( Initialize::SCREEN_WIDTH, Initialize::SCREEN_HEIGHT, Initialize::SCREEN_BPP, SDL_DOUBLEBUF|SDL_HWSURFACE );
_pScreen = SDL_SetVideoMode( Initialize::SCREEN_WIDTH, Initialize::SCREEN_HEIGHT, Initialize::SCREEN_BPP, SDL_OPENGL );

if( _pScreen == NULL )
{
  return false;
}
//Initialize OpenGL
if( InitGL() == false )
{
  return false;
}
if( _pScreen == NULL )
{
  return false;
}
//Initialize SDL_ttf
	if( TTF_Init() == -1 )
	{
		return false;  
	}
ClearScreen();
_initializedOK = true;
return _initializedOK;
}
bool MainWnd::InitGL()
{
	//Set clear color
	glClearColor( 0, 0, 0, 0 );
glViewport( 0, 0, Initialize::SCREEN_WIDTH, Initialize::SCREEN_HEIGHT );
SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
	//Set projection
	glMatrixMode( GL_PROJECTION );
	glLoadIdentity();
glOrtho( 0, Initialize::SCREEN_WIDTH, Initialize::SCREEN_HEIGHT, 0, -1, 1 );
glEnable( GL_BLEND );
glBlendFunc( GL_ONE, GL_ONE_MINUS_SRC_ALPHA );
	glDisable( GL_LIGHTING );
glDisable( GL_DEPTH_TEST );
	//Initialize modelview matrix
	glMatrixMode( GL_MODELVIEW );
	glLoadIdentity();

	//If there was any errors
	if( glGetError() != GL_NO_ERROR )
	{
		return false;
	}
	//If everything initialized
	return true;
}

GraphicsTexture* ImageLoader::LoadGL( string location )
{
GLuint texture;   // This is a handle to our texture object
SDL_Surface* surface; // This surface will tell us the details of the image
GLenum texture_format;
GLint  nOfColors;
//convert surface to 2^n
SDL_Surface* origionalSurface = LoadSDL( location );
if( origionalSurface != NULL )
{
  int spriteWidth = origionalSurface->w;
  int spriteHeight =  origionalSurface->h;
  int textureWidth = 0;
  int textureHeight = 0;
  surface = SDL_CreateRGBSurface( SDL_SWSURFACE, Get2nNumber( origionalSurface->w ) , Get2nNumber( origionalSurface->h ), 32, 0, 0,0 , 0 );
  SDL_FillRect( surface, &surface->clip_rect, SDL_MapRGBA( surface->format,0, 0, 0, 0 ) );
  SDL_Rect corner;
  corner.x = 0;
  corner.y = 0;
  SDL_BlitSurface( origionalSurface, NULL, surface, &corner );	
  if( surface != NULL )
  {
   //error check
   // Check that the image's width is a power of 2
   if ( ( surface->w & ( surface->w - 1 ) ) != 0 )
   {
	cout << "warning: image's width is not a power of 2\n";
   }

   // Also check if the height is a power of 2
   if ( ( surface->h & ( surface->h - 1 ) ) != 0 )
   {
	cout << "warning: image's height is not a power of 2\n";
   }
   bool colorFlag = true;
   // get the number of channels in the SDL surface
   nOfColors = surface->format->BytesPerPixel;
   if (nOfColors == 4)	 // contains an alpha channel
   {
	 if (surface->format->Rmask == 0x000000ff)
	 {
	   texture_format = GL_RGBA;
	 }
	 else
	 {
	   texture_format = GL_BGRA;
	 }
   }
   else if ( nOfColors == 3 )	 // no alpha channel
   {
	 if ( surface->format->Rmask == 0x000000ff )
	 {
	   texture_format = GL_RGB;
	 }
	 else
	 {
	   texture_format = GL_BGR;
  
	 }
   }
   else
   {
	 printf( "warning: the image is not truecolor..  this will probably break\n" );
	 colorFlag = false;	
   }
   if( colorFlag )
   {  
	textureWidth = surface->w;
	textureHeight = surface->h;
	// Have OpenGL generate a texture object handle for us
	glGenTextures( 1, &texture );
	// Bind the texture object
	glBindTexture( GL_TEXTURE_2D, texture );

	// Set the texture's stretching properties
	  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
	  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
	  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

	// Edit the texture object's image data using the information SDL_Surface gives us
	glTexImage2D( GL_TEXTURE_2D, 0, nOfColors, surface->w, surface->h, 0,
		 texture_format, GL_UNSIGNED_BYTE, surface->pixels );
	//SDL_FreeSurface( origionalSurface );
	SDL_FreeSurface( surface );
	GraphicsTexture* graphicsTexture = new GraphicsTexture( texture, texture_format, nOfColors, textureWidth, textureHeight, spriteWidth, spriteHeight );
	return graphicsTexture;
   }
  }
}
return NULL;
}

void MainWnd::RenderSprite( GraphicsTexture* graphic, SDL_Rect position, SDL_Rect clipping, bool blend )
{
if( blend == false )
{
  glDisable( GL_BLEND );
}
else
{
  glEnable( GL_BLEND );
}
// Enable 2D rendering
	glColor4ub( 255,255,255,255 );
glEnable( GL_TEXTURE_2D );
  
glBindTexture( GL_TEXTURE_2D, graphic->GetTexture() );
glTranslatef( position.x, position.y, 0 );
float x = 0;
float y = 0;
	glBegin( GL_QUADS );

  x = GetClippingPercentage( clipping.x + 0.0, graphic->_TextureDimentions.width + 0.0 );
  y = GetClippingPercentage( clipping.y + 0.0, graphic->_TextureDimentions.height+ 0.0 );
  glTexCoord2f( x, y );
  glVertex3f( 0.0f, 0.0f, 0.0f );//top left

  x = GetClippingPercentage( ( clipping.x + clipping.w ), graphic->_TextureDimentions.width );
  y = GetClippingPercentage( clipping.y, graphic->_TextureDimentions.height );
  glTexCoord2f( x, y );
  glVertex3f( ( clipping.w + 0.0f ), 0.0f, 0.0f );//top right
  
  x = GetClippingPercentage( clipping.x  + clipping.w + 0.0, graphic->_TextureDimentions.width + 0.0 );
  y = GetClippingPercentage( ( clipping.y + clipping.h + 0.0 ), ( graphic->_TextureDimentions.height + 0.0 ) );
  glTexCoord2f( x, y );
  glVertex3f( ( clipping.w + 0.0f ), ( clipping.h + 0.0f ), 0.0f );//bottom right
  x = GetClippingPercentage( clipping.x + 0.0 , graphic->_TextureDimentions.width + 0.0 );
  y = GetClippingPercentage( clipping.y + clipping.h + 0.0, graphic->_TextureDimentions.height + 0.0 );
  glTexCoord2f( x, y );
  glVertex3f( 0.0f, ( clipping.h + 0.0f ), 0.0f );//bottom left
glEnd();

glDisable( GL_TEXTURE_2D );
glDisable( GL_BLEND );
//Reset
	glLoadIdentity();
}

p.s sorry for the vast amount of code Posted Image but I am full on stuck been reading documentation and tutorials for hours with now success

PARTNERS