texture translatation

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
jAyTeA
Posts: 36
Joined: Wed Sep 06, 2006 7:50 am
Location: Hattorf am Harz, Germany

texture translatation

Post by jAyTeA »

hello,

i want to use the texture transfomation matrix to move it on mesh. I use following code:

Code: Select all

matrix4* matTexMap = &texMap->getTransformation();
				
matTexMap->setTranslation( vector3df(0.2f,0.2f,0) );
matTexMap->setScale( vector3df( 0.5f, 0.5f, 0 ) );
matTexMap->setRotationRadians( vector3df(0,0,PI) );
The scaling and the rotation are working without problems, but the texture doesn't move.
What is wrong with this code?
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

Hmm, should work, I did it the same way.
jAyTeA
Posts: 36
Joined: Wed Sep 06, 2006 7:50 am
Location: Hattorf am Harz, Germany

Post by jAyTeA »

hmm, i just recognized that just one thing works at one time. When rotation is set at last the texture is rotateted, but not scaled, if scaling is set last the rotation isn't recognized.

But Translation doesn't work at all.
Do i have to set some flags or a specific MaterialType?
sio2
Competition winner
Posts: 1003
Joined: Thu Sep 21, 2006 5:33 pm
Location: UK

Re: texture translatation

Post by sio2 »

jAyTeA wrote:hello,

i want to use the texture transfomation matrix to move it on mesh. I use following code:

Code: Select all

matrix4* matTexMap = &texMap->getTransformation();
				
matTexMap->setTranslation( vector3df(0.2f,0.2f,0) );
matTexMap->setScale( vector3df( 0.5f, 0.5f, 0 ) );
matTexMap->setRotationRadians( vector3df(0,0,PI) );
The scaling and the rotation are working without problems, but the texture doesn't move.
What is wrong with this code?
Concatenate the translation/scale/rotation into a single matrix:

matrix4 *matScale, *matTrans, *matRot, *matFinal;
matTrans->setTranslation( vector3df(0.2f,0.2f,0) );
matScale->setScale( vector3df( 0.5f, 0.5f, 0 ) );
matRot->setRotationRadians( vector3df(0,0,PI) );
matFinal = matScale * matRot * matTrans;

Note: Be careful of the order of operations when computing matFinal!
jAyTeA
Posts: 36
Joined: Wed Sep 06, 2006 7:50 am
Location: Hattorf am Harz, Germany

Post by jAyTeA »

sry, sio2, but that code doesn't work, same problem.
Also the following code doesn't work:

Code: Select all

texMap->getTransformation().setScale( vector3df( 0.2f, 0.2f, 0 ) );
texMap->getTransformation().setRotationRadians( vector3df(0,0,PI) );
texMap->getTransformation().setTranslation( vector3df(0.2f,0.2f,0) );
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

You can't just call setScale then setRotation and then setTranslation on the same matrix. Those functions work independently. A call to setRotation will screw up a matrix that has been scaled. The only way to do it is to concatenate matrices as mentioned above.

Try this code..

Code: Select all

matrix4 matScale;
matScale.setScale(vector3df(.5f, .5f, .5f));

matrix4 matRot;
matRot.setRotationRadians(vector3df(0, 0, PI));

matrix4 matTranslate;
matTranslate.setTranslation(vector3df(0.2f, 0.2f, 0));

texMap->getTransformation() = (matScale * matRot * matTranslate);
BTW, if you set the translation to some constant value like you're doing, the texture isn't going to 'move' it is just going to be offset. The following would make the texture 'move'...

Code: Select all

u32 now = device->getTimer()->getRealTime();

f32 sn = sinf(now / 2000.f);
f32 cn = cosf(now / 3000.f);

core::matrix4 matScale;
matScale.setScale(core::vector3df(sn, cn, 0.f));

core::matrix4 matRot;
matRot.setRotationDegrees(core::vector3df(sn * 45.f, sn * 30.f, 0));

core::matrix4 matTranslate;
matTranslate.setTranslation(core::vector3df(sn, cn, 0));

texMap->getTransformation() = (matScale * matRot * matTranslate);
sio2
Competition winner
Posts: 1003
Joined: Thu Sep 21, 2006 5:33 pm
Location: UK

Post by sio2 »

Yep, its strange. I just had a quick look in the Irrlicht source code and it should work. It looks as though texture transforms are always "on".

If you're using D3D9 you could try PIX and see what the texture transform matrix is at the point of the draw call.

If you could make a small test app and post the source we could investigate further.
jAyTeA
Posts: 36
Joined: Wed Sep 06, 2006 7:50 am
Location: Hattorf am Harz, Germany

Post by jAyTeA »

thx, vitek, your code works perfekt!

this was just a test for later integration into the mainloop.

I also looked at the source and saw that the three functions all manipulatet other variables of the matrix, so i thought that it has to go.

Thanks everyone for your help!
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

You can safely use setTranslation without matrix multiplication, but do it as the last operation.
radishlaw
Posts: 6
Joined: Tue Jan 16, 2007 2:25 pm

Post by radishlaw »

sorry to dig this up
I wanted to handle explosion with a billboard scene node with all the frames
of the effect in a single texture.

So far I am unsuccessful.
It seems that the last code vitek posted works - at least the second part.
For some reason the translation is visible only when rotating the texture (the visual effect is different from that without translation). The first part of code shows no offset in the loop of my program.
Just using setTraslation also show no offset in the texture.

I understand that the texture matrix method will be changed next version, but I would like to know the reason behind this failure.
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

The changes for the next version will just expose the matrix at a different position, so the basic behavior will be the same.
So for a plain rectangle with coords (0,0) to (1,1) should use a scale (1/n,1) and use an offset of ((j-1)*1/n,0) for n texture patches and the j-th texture displaying.
Post Reply