Page 1 of 1

draw2dimage rotate, scale (a flexible solution)

Posted: Sun Dec 20, 2009 7:28 am
by m4rvin
just modify a copy of

Code: Select all

draw2dImage( image, dest_rect, source_rect, ...) 
to

Code: Select all

draw2dImage( image, dest_QUAD, source_rect, ...) 
so instead of giving 2 corners with destRect you pass four corners with a quad. my quad is actually just an array of 4 Vec2Ds defining the corners.

implementation is simply copying the "destRect" implemenation by zola... and modifiying it a tiny bit

from:

Code: Select all


void COpenGLDriver::draw2DImage(const video::ITexture* texture, const core::rect<s32>& destRect,
		const core::rect<s32>& sourceRect, const core::rect<s32>* clipRect,
		const video::SColor* const colors, bool useAlphaChannelOfTexture)
{

...

	glBegin(GL_QUADS);

	glColor4ub(useColor[0].getRed(), useColor[0].getGreen(), useColor[0].getBlue(), useColor[0].getAlpha());
	glTexCoord2f(tcoords.UpperLeftCorner.X, tcoords.UpperLeftCorner.Y);
	glVertex2f(GLfloat(destRect.UpperLeftCorner.X), GLfloat(destRect.UpperLeftCorner.Y));

	glColor4ub(useColor[3].getRed(), useColor[3].getGreen(), useColor[3].getBlue(), useColor[3].getAlpha());
	glTexCoord2f(tcoords.LowerRightCorner.X, tcoords.UpperLeftCorner.Y);
	glVertex2f(GLfloat(destRect.LowerRightCorner.X), GLfloat(destRect.UpperLeftCorner.Y));

	glColor4ub(useColor[2].getRed(), useColor[2].getGreen(), useColor[2].getBlue(), useColor[2].getAlpha());
	glTexCoord2f(tcoords.LowerRightCorner.X, tcoords.LowerRightCorner.Y);
	glVertex2f(GLfloat(destRect.LowerRightCorner.X), GLfloat(destRect.LowerRightCorner.Y));

	glColor4ub(useColor[1].getRed(), useColor[1].getGreen(), useColor[1].getBlue(), useColor[1].getAlpha());
	glTexCoord2f(tcoords.UpperLeftCorner.X, tcoords.LowerRightCorner.Y);
	glVertex2f(GLfloat(destRect.UpperLeftCorner.X), GLfloat(destRect.LowerRightCorner.Y));

	glEnd();
to:

Code: Select all


void draw2DImage(const video::ITexture* texture, const core::position2d<s32> corners[4],
			const core::rect<s32>& sourceRect, const core::rect<s32>* clipRect,
			const video::SColor * const colors, bool useAlphaChannelOfTexture)
{

...

glBegin(GL_QUADS);

	glColor4ub(useColor[0].getRed(), useColor[0].getGreen(), useColor[0].getBlue(), useColor[0].getAlpha());
	glTexCoord2f(tcoords.UpperLeftCorner.X, tcoords.UpperLeftCorner.Y);
	glVertex2f(GLfloat(corners[0].X), GLfloat(corners[0].Y));

	glColor4ub(useColor[3].getRed(), useColor[3].getGreen(), useColor[3].getBlue(), useColor[3].getAlpha());
	glTexCoord2f(tcoords.LowerRightCorner.X, tcoords.UpperLeftCorner.Y);
	glVertex2f(GLfloat(corners[1].X), GLfloat(corners[1].Y));

	glColor4ub(useColor[2].getRed(), useColor[2].getGreen(), useColor[2].getBlue(), useColor[2].getAlpha());
	glTexCoord2f(tcoords.LowerRightCorner.X, tcoords.LowerRightCorner.Y);
	glVertex2f(GLfloat(corners[2].X), GLfloat(corners[2].Y));

	glColor4ub(useColor[1].getRed(), useColor[1].getGreen(), useColor[1].getBlue(), useColor[1].getAlpha());
	glTexCoord2f(tcoords.UpperLeftCorner.X, tcoords.LowerRightCorner.Y);
	glVertex2f(GLfloat(corners[3].X), GLfloat(corners[3].Y));

	glEnd();

yes, i know, there is no rotation & scaling code here :)

but this allows me to transform, rotate, scale the four corners myself, outside of ivideodriver implementation.

very flexible: meaning i can rotate, scale horizontally, or vertically, or even make a trapezoid or any deformation of a rectangle

Posted: Sun Dec 20, 2009 8:29 am
by Ulf
What for?

How will that be better?
You only need 2 corners.

Posted: Sun Dec 20, 2009 11:09 am
by m4rvin
four corners are better than two. quad isn't a rectangle

one example.. animate, flip a card open in solitaire games (card needs to be a bit trapezoidial)

flip a page in a book open.

think 2d games - lots of uses

Posted: Sun Dec 20, 2009 12:29 pm
by FuzzYspo0N
It is useful, but its not nice to use. It means manually doing every translation manually every time you draw. If you write a nice wrapper class it will help.

If you want some rotation and scaling code :

http://irrlicht.sourceforge.net/phpBB2/ ... p?p=213127

And, if you want a properly flexible overload of this function, you should just pass in core::matrix4 translation , and apply that to the sprite (allowing what you want, skewing, rotation, etc etc.).

Posted: Sun Dec 20, 2009 2:09 pm
by m4rvin
yes, i agree that there are better ways of achieving rotation & scaling, skewing and etc

but for implementation purposes of draw2dImage ... wouldn't it be sensible to pass an actual quad with four corners than a rect? the implementation uses GL_QUADS after all.

Posted: Sun Dec 20, 2009 2:41 pm
by Ulf
m4rvin wrote:but for implementation purposes of draw2dImage ... wouldn't it be sensible to pass an actual quad with four corners than a rect? the implementation uses GL_QUADS after all.
GL_QUADS are just a define. It's a number not a rectangle.
Does directX use GL_QUADS?

If you want the destination to not be a rectangle (e.g. trapezoid), then 4 corners are needed.
But is that what draw2DImage does? Draw non-rectangles?
It's getting more complex now!

Posted: Sun Dec 20, 2009 3:30 pm
by m4rvin
whats is more complex with passing a QUAD and passing a RECT?

Posted: Sun Dec 20, 2009 3:33 pm
by m4rvin
please count

Code: Select all

	S3DVertex vtx[4];
	vtx[0] = S3DVertex((f32)poss.UpperLeftCorner.X, (f32)poss.UpperLeftCorner.Y, 0.0f,
			0.0f, 0.0f, 0.0f, color,
			tcoords.UpperLeftCorner.X, tcoords.UpperLeftCorner.Y);
	vtx[1] = S3DVertex((f32)poss.LowerRightCorner.X, (f32)poss.UpperLeftCorner.Y, 0.0f,
			0.0f, 0.0f, 0.0f, color,
			tcoords.LowerRightCorner.X, tcoords.UpperLeftCorner.Y);
	vtx[2] = S3DVertex((f32)poss.LowerRightCorner.X, (f32)poss.LowerRightCorner.Y, 0.0f,
			0.0f, 0.0f, 0.0f, color,
			tcoords.LowerRightCorner.X, tcoords.LowerRightCorner.Y);
	vtx[3] = S3DVertex((f32)poss.UpperLeftCorner.X, (f32)poss.LowerRightCorner.Y, 0.0f,
			0.0f, 0.0f, 0.0f, color,
			tcoords.UpperLeftCorner.X, tcoords.LowerRightCorner.Y);

	s16 indices[6] = {0,1,2,0,2,3};

	setVertexShader(EVT_STANDARD);

	pID3DDevice->DrawIndexedPrimitiveUP(D3DPT_TRIANGLELIST, 0, 4, 2, &indices[0],
		D3DFMT_INDEX16,&vtx[0],	sizeof(S3DVertex));
}

Posted: Sun Dec 20, 2009 3:38 pm
by m4rvin
you're spending too much time with birds ;)

Posted: Sun Dec 20, 2009 7:26 pm
by Ulf
m4rvin wrote:whats is more complex with passing a QUAD and passing a RECT?
It's not the PASSING.
It's making a trapezoid in the software versions.

I see the 4 corners (vtx[]) But where is the GL_QUAD? lol.

A rect is good enough for now. We want to get rotation working do we? I mean, don't we?

Look here for rotation:
https://sourceforge.net/tracker/?func=d ... tid=540679

Rotation is implemented for directX and openGL. Scaling is implemented in all drivers. I'll finalise my patch for the software rotation later.
Cheers.
you're spending too much time with birds Wink
hehe. I didn't get it at first. lol.


**EDIT**
I suppose we could add a new overload which takes a QUAD, i.e. if the administrators think it's worth it now.
But we should keep the current rect version. I believe, anyway.

Only need a quad if it's not going to be a rectangle.
And as said, it's going to take some work to implement it in software. Rotation, scaling, and a 4 point polygon of any shape? No, I don't want to do it for now. lol.

Posted: Sun Dec 20, 2009 11:43 pm
by hybrid
We already have the 2d polygon function in Irrlicht 1.6, should be all you need to get non-rect images.

Posted: Mon Dec 21, 2009 2:49 am
by Ulf
hybrid wrote:We already have the 2d polygon function in Irrlicht 1.6, should be all you need to get non-rect images.
But then you need to use the material settings etc.. don't you?

*EDIT*
And the software driver versions of draw2DVertex... are not implemented.

(draw2DVertexPrimitiveList would be more effective in software drivers than would a rotate and scale, but even it is not implemented)

Even though the software versions of rotate and scale will slow it down a lot, I'd like to have the option. They could still be used "extremely" moderately.
Like 1 texture LOL. I'll make a separate rotate function which the software drivers can use more efficiently (if the programmer sets it up properly by saving one rotated image for each texture).

Posted: Mon Dec 21, 2009 4:22 am
by m4rvin
4 corners (vtx[]) ... GL_QUAD?
isn't there a DX_QUAD?

Posted: Mon Dec 21, 2009 11:18 am
by Ulf
I see the 4 corners (vtx[]) But where is the GL_QUAD? lol.
I was making a joke... being pedantic.
I know what you are talking about.