proper use of buildTextureTransform()

You are an experienced programmer and have a problem with the engine, shaders, or advanced effects? Here you'll get answers.
No questions about C++ programming or topics which are answered in the tutorials!
Post Reply
Arcoroc
Posts: 51
Joined: Wed Oct 01, 2008 11:40 pm
Location: Canada

proper use of buildTextureTransform()

Post by Arcoroc »

I am struggling to properly implement uv transformation.

I have setup an orthographic camera, and i am calling the below method within a class that holds a planar quad node. Its purpose is to set the uv on the node so that only a source rectangle from the texture will map into the quad.

Here is how it is used:

Code: Select all

			
MyQuadNode.setTextureAndUV( "myTexture.png", rect<s32>(0,0,100,100) ) ;     
which takes a rectangle 100 pixel square and map it with proper pixel size to the quad rendered to screen.
It should be simple enough, and in fact works great with tex rect coordinates starting at (0,0) as above.

It does not work anymore if i pass, say,

Code: Select all

MyQuadNode.setTextureAndUV( "myTexture.png", rect<s32>(10,10,100,100) ) ;
Node that source rect is no longer (0,0) based. I can't seem to find what i am missing.
Here is the source code for the uv method:

Code: Select all

void MyQuadNode::setTextureAndUV(  const video::ITexture* texture, const RenderMan::rect<s32>& sourceTexRect )
	{
		
	dimension2du		texsize = texture->getSize() ;
	dimension2df		uvScaleFactor( sourceTexRect.getWidth()/texsize.Width, sourceTexRect.getHeight()/texsize.Height )  ;
	position2d		rectCenter =  sourceTexRect.getCenter(); 
	vector2df		translation0 = vector2df( (1.f*rectCenter.X)/texsize.Width, (1.f*rectCenter.Y)/texsize.Height )/vector2df( uvScaleFactor.Width, uvScaleFactor.Height ) ;
	vector2df		translation = translation0 - vector2df(0.5f, 0.5f); 
		
		getMaterial().setTexture(0, texture ); 
		getMaterial().getTextureMatrix( 0 ).buildTextureTransform( 0*DEGTORAD64,
																   vector2df(0.f, 0.f),    
																   translation ,
																  uvScaleFactor );
	}

Thanks for any suggestion.
L/
Arcoroc
Posts: 51
Joined: Wed Oct 01, 2008 11:40 pm
Location: Canada

Post by Arcoroc »

If you thought that the translation calculation code above might have something to do with the extra beer, you would be entirely right.
The below should work much better.

Code: Select all

void MyQuadNode::setTextureAndUV(  const video::ITexture* texture, const RenderMan::rect<s32>& sourceTexRect ) 
   { 
       
   dimension2du      texsize = texture->getSize() ; 
   dimension2df		uvScaleFactor( (f32)sourceTexRect.getWidth()/texsize.Width, (f32)sourceTexRect.getHeight()/texsize.Height )  ;
   vector2df		translation ( (f32)sourceTexRect.UpperLeftCorner.X/texsize.Width, (f32)sourceTexRect.UpperLeftCorner.Y/texsize.Height ) ;
      
      getMaterial().setTexture(0, texture ); 
      getMaterial().getTextureMatrix( 0 ).buildTextureTransform( 0*DEGTORAD64, 
                                                   vector2df(0.f, 0.f),    
                                                   translation , 
                                                  uvScaleFactor ); 
   }
L/
Post Reply