Jump to content

  • Log In with Google      Sign In   
  • Create Account

Awesome job so far everyone! Please give us your feedback on how our article efforts are going. We still need more finished articles for our May contest theme: Remake the Classics

#ActualMaxFire

Posted 30 January 2012 - 07:15 AM

Hi guys' im having a few problems with clipping a 2D texture in SDL OpenGL mode.

Currently I have the image clipping but when it blends to the screen it distorts/twists ( I'll just put a screen shot up Posted Image ). Now I know its something to do with my Quad matrix but I just can't see the error. Here's my function any advice would be great Posted Image.

void MainWnd::RenderSprite( GraphicsTexture* graphic, SDL_Rect position, SDL_Rect clipping )
{
		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, clipping.w );
y = GetClippingPercentage( clipping.y, clipping.h );

glTexCoord2d( x, y );
glVertex3f( 0.0, 0.0, 0.0 );//top left

x = GetClippingPercentage( ( clipping.x + clipping.w ), graphic->_TextureDimentions.width );
y = GetClippingPercentage( clipping.y, graphic->_TextureDimentions.height );

glTexCoord2d( x, y );
glVertex3f( ( position.w + 0.0 ), 0.0, 0.0 );//top right

x = GetClippingPercentage( clipping.w + 0.0, graphic->_TextureDimentions.width + 0.0 );
y = GetClippingPercentage( ( clipping.y + clipping.h + 0.0 ), ( graphic->_TextureDimentions.height + 0.0 ) );

glTexCoord2d( x, y );
glVertex3f( ( position.w + 0.0 ), ( position.h + 0.0 ), 0.0 );//bottom right

x = GetClippingPercentage( clipping.x + 0.0 , graphic->_TextureDimentions.width + 0.0 );
y = GetClippingPercentage( clipping.h + 0.0, graphic->_TextureDimentions.height + 0.0 );

glTexCoord2d( x, y );
glVertex3f( 0.0, ( position.h + 0.0 ), 0.0 );//bottom left

glEnd();

glDisable( GL_TEXTURE_2D );

//Reset
	glLoadIdentity();
}
float MainWnd::GetClippingPercentage( int inner, int outer )
{
float value = ( float ) ( inner ) / ( float ) ( outer + 0.0 );
return value;
}

Update: I worked out by hand the clipping margins ( multiplying the float value by the texture size and the outcomes match the clipping rect ) so the math is correct. Am I missing something?

#2MaxFire

Posted 30 January 2012 - 07:14 AM

Hi guys' im having a few problems with clipping a 2D texture in SDL OpenGL mode.

Currently I have the image clipping but when it blends to the screen it distorts/twists ( I'll just put a screen shot up Posted Image ). Now I know its something to do with my Quad matrix but I just can't see the error. Here's my function any advice would be great Posted Image.

void MainWnd::RenderSprite( GraphicsTexture* graphic, SDL_Rect position, SDL_Rect clipping )
{
        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, clipping.w );
y = GetClippingPercentage( clipping.y, clipping.h );

glTexCoord2d( x, y ); 
glVertex3f( 0.0, 0.0, 0.0 );//top left

x = GetClippingPercentage( ( clipping.x + clipping.w ), graphic->_TextureDimentions.width );
y = GetClippingPercentage( clipping.y, graphic->_TextureDimentions.height );

glTexCoord2d( x, y ); 
glVertex3f( ( position.w + 0.0 ), 0.0, 0.0 );//top right

x = GetClippingPercentage( clipping.w + 0.0, graphic->_TextureDimentions.width + 0.0 );
y = GetClippingPercentage( ( clipping.y + clipping.h + 0.0 ), ( graphic->_TextureDimentions.height + 0.0 ) );

glTexCoord2d( x, y ); 
glVertex3f( ( position.w + 0.0 ), ( position.h + 0.0 ), 0.0 );//bottom right

x = GetClippingPercentage( clipping.x + 0.0 , graphic->_TextureDimentions.width + 0.0 );
y = GetClippingPercentage( clipping.h + 0.0, graphic->_TextureDimentions.height + 0.0 );

glTexCoord2d( x, y ); 
glVertex3f( 0.0, ( position.h + 0.0 ), 0.0 );//bottom left

glEnd(); 

glDisable( GL_TEXTURE_2D );

//Reset
    glLoadIdentity();
}
float MainWnd::GetClippingPercentage( int inner, int outer )
{
float value = ( float ) ( inner ) / ( float ) ( outer + 0.0 );
return value;
}

Update: I worked out by hand the clipping margins ( multiplying the float value by the texture size and the outcomes match the clipping rect ) so the math is correct. Am I missing something?

#1MaxFire

Posted 30 January 2012 - 03:54 AM

Hi guys' im having a few problems with clipping a 2D texture in SDL OpenGL mode.

Currently I have the image clipping but when it blends to the screen it distorts/twists ( I'll just put a screen shot up :P ). Now I know its something to do with my Quad matrix but I just can't see the error. Here's my function any advice would be great :).

void MainWnd::RenderSprite( GraphicsTexture* graphic, SDL_Rect position, SDL_Rect clipping )
{
    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, clipping.w );
  y = GetClippingPercentage( clipping.y, clipping.h );
  glTexCoord2d( x, y ); 
  glVertex3f( 0.0, 0.0, 0.0 );

  x = GetClippingPercentage( ( clipping.x + clipping.w ), graphic->_TextureDimentions.width);
  y = GetClippingPercentage( clipping.y, graphic->_TextureDimentions.height );
  glTexCoord2d( x, y ); 
  glVertex3f( position.w, 0.0, 0.0 );
  
  x = GetClippingPercentage( clipping.w, graphic->_TextureDimentions.width );
  y = GetClippingPercentage( clipping.y + clipping.h, graphic->_TextureDimentions.height);
  glTexCoord2d( x, y ); 
  glVertex3f( position.w, position.h, 0.0 );
  x = GetClippingPercentage( clipping.x, graphic->_TextureDimentions.width );
  y = GetClippingPercentage( clipping.h, graphic->_TextureDimentions.height );
  glTexCoord2d( x, y ); 
  glVertex3f( 0.0, position.h, 0.0 );
glEnd();
glDisable( GL_TEXTURE_2D );
//Reset
    glLoadIdentity();
}
float MainWnd::GetClippingPercentage( int inner, int outer )
{
float value = ( float ) ( inner ) / ( float ) ( outer + 0.0 );
return value;
}

PARTNERS