draw2dimage rotate, scale (a flexible solution)

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
m4rvin
Posts: 32
Joined: Tue May 15, 2007 3:00 am
Contact:

draw2dimage rotate, scale (a flexible solution)

Post 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
Ulf
Posts: 281
Joined: Mon Jun 15, 2009 8:53 am
Location: Australia

Post by Ulf »

What for?

How will that be better?
You only need 2 corners.
I can hear birds chirping
:twisted:

I live in the Eye of Insanity.
m4rvin
Posts: 32
Joined: Tue May 15, 2007 3:00 am
Contact:

Post 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
FuzzYspo0N
Posts: 914
Joined: Fri Aug 03, 2007 12:43 pm
Location: South Africa
Contact:

Post 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.).
m4rvin
Posts: 32
Joined: Tue May 15, 2007 3:00 am
Contact:

Post 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.
Ulf
Posts: 281
Joined: Mon Jun 15, 2009 8:53 am
Location: Australia

Post 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!
I can hear birds chirping
:twisted:

I live in the Eye of Insanity.
m4rvin
Posts: 32
Joined: Tue May 15, 2007 3:00 am
Contact:

Post by m4rvin »

whats is more complex with passing a QUAD and passing a RECT?
m4rvin
Posts: 32
Joined: Tue May 15, 2007 3:00 am
Contact:

Post 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));
}
m4rvin
Posts: 32
Joined: Tue May 15, 2007 3:00 am
Contact:

Post by m4rvin »

you're spending too much time with birds ;)
Ulf
Posts: 281
Joined: Mon Jun 15, 2009 8:53 am
Location: Australia

Post 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.
I can hear birds chirping
:twisted:

I live in the Eye of Insanity.
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

We already have the 2d polygon function in Irrlicht 1.6, should be all you need to get non-rect images.
Ulf
Posts: 281
Joined: Mon Jun 15, 2009 8:53 am
Location: Australia

Post 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).
I can hear birds chirping
:twisted:

I live in the Eye of Insanity.
m4rvin
Posts: 32
Joined: Tue May 15, 2007 3:00 am
Contact:

Post by m4rvin »

4 corners (vtx[]) ... GL_QUAD?
isn't there a DX_QUAD?
Ulf
Posts: 281
Joined: Mon Jun 15, 2009 8:53 am
Location: Australia

Post 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.
I can hear birds chirping
:twisted:

I live in the Eye of Insanity.
Post Reply