enginehack - rotations with draw2DImage

Post those lines of code you feel like sharing or find what you require for your project here; or simply use them as tutorials.
Post Reply
CuteAlien
Admin
Posts: 10021
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

enginehack - rotations with draw2DImage

Post by CuteAlien »

I added rotations to some of the draw2DImage functions in my GL and DX drivers tonight. Sorry, you have to hack the engine for this yourself. But I can at least help you what needs to be done.

It's up to you to which draw2DImage functions you will add this, it should work for all the same way. But as this are interface functions you will have to add the parameters to IVideoDriver and all it's derived classes.

Example:

Code: Select all

virtual void draw2DImage(video::ITexture* texture, const core::position2d<s32>& destPos,
			const core::rect<s32>& sourceRect, const core::rect<s32>* clipRect = 0,
			SColor color=SColor(255,255,255,255), bool useAlphaChannelOfTexture=false
			, f32 angleInDegree=0.f) = 0;
The basic method is for both drivers the same. I set the modelview/world matrix to:
1. translate to the center of the image (arbitrary centers not yet supported, but would be trivial to add)
2. rotate by given angle
3. translate back to original position
4. call the old draw code
5. reset the modelview/world matrix

For d3d it looks like this:

Code: Select all

if ( angleInDegree != 0.f )
	{
		f32 cx = (vtx[0].Pos.X + vtx[2].Pos.X) / 2;
		f32 cy = (vtx[0].Pos.Y + vtx[2].Pos.Y) / 2;

		D3DXMATRIX mtrans1;
		D3DXMATRIX mRotation;
		D3DXMATRIX mtrans2;
		D3DXMATRIX mAllTogetherNow;

		D3DXMatrixTranslation( &mtrans1, cx, cy, 0.f );
		D3DXMatrixRotationZ( &mRotation, D3DXToRadian( angleInDegree ) );
		D3DXMatrixTranslation( &mtrans2, -cx, -cy, 0.f );

		mAllTogetherNow =  mtrans2 * mRotation * mtrans1;

		pID3DDevice->SetTransform( D3DTS_WORLD, &mAllTogetherNow );
	}

        // here is the usual drawing code like:
        // pID3DDevice->DrawIndexedPrimitiveUP

	if ( angleInDegree != 0.f )
	{
		core::matrix4 mat;
		pID3DDevice->SetTransform(D3DTS_WORLD, (D3DMATRIX*)((void*)mat.pointer()));
	}
For OGL it's like that:

Code: Select all

if ( angleInDegree != 0.f )
	{
		f32 cx = (npos.UpperLeftCorner.X + npos.LowerRightCorner.X) / 2;
		f32 cy = (npos.UpperLeftCorner.Y + npos.LowerRightCorner.Y) / 2;
		glMatrixMode(GL_MODELVIEW);
		glTranslatef(cx, cy, 0);
		glRotatef(angleInDegree, 0.0, 0.0, 1.0);
		glTranslatef(-cx, -cy, 0);
	}

        // here is the usual drawing code like:
	// glColor4ub 
	// glBegin(GL_QUADS);
        // <snip> 
	// glEnd();

	if ( angleInDegree != 0.f )
	{
		glLoadIdentity();
	}
Once rotation is supported by draw2DImage it's certainly trivial to add such a rotation parameter for example to CGUIImage and forward that to draw2DImage in the draw call.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
Post Reply