Extern 2D-Library

Post your questions, suggestions and experiences regarding to Image manipulation, 3d modeling and level editing for the Irrlicht engine here.
geckoman
Posts: 143
Joined: Thu Nov 27, 2008 11:05 am
Location: Germany
Contact:

Extern 2D-Library

Post by geckoman »

I thought of using an extern Library for things like rotating or scaling images (Background of Menu or Loading screens etc.) as there are no really Irrlicht solutions for that.
Do you think that's the right way, or do you know a fast, lightweight and easy library?
mk.1
Posts: 76
Joined: Wed Oct 10, 2007 7:37 pm

Post by mk.1 »

Mixing graphics isn't a wise thing to do. You should better write it yourself in irr.
BlindSide
Admin
Posts: 2821
Joined: Thu Dec 08, 2005 9:09 am
Location: NZ!

Post by BlindSide »

You don't have to use anything outside of Irrlicht, I think that will be inconvenient and more difficult anyway. For scaling, rotating, positioning of sprites in Irrlicht you can use this simple class I made:

Code: Select all

class CSprite
{
public:
	CSprite() : scale(irr::core::vector3df(1.0f, 1.0f, 0.0f))
	{
		Material.Lighting = false;
		Material.ZWriteEnable = false;

		Vertices[0] = irr::video::S3DVertex(-1.0f,-1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 
			irr::video::SColor(0xffffffff), 0.0f, 1.0f);
		Vertices[1] = irr::video::S3DVertex(-1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 
			irr::video::SColor(0xffffffff), 0.0f, 0.0f);
		Vertices[2] = irr::video::S3DVertex( 1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 
			irr::video::SColor(0xffffffff), 1.0f, 0.0f);
		Vertices[3] = irr::video::S3DVertex( 1.0f,-1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 
			irr::video::SColor(0xffffffff), 1.0f, 1.0f);
		Vertices[4] = irr::video::S3DVertex(-1.0f,-1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 
			irr::video::SColor(0xffffffff), 0.0f, 1.0f);
		Vertices[5] = irr::video::S3DVertex( 1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 
			irr::video::SColor(0xffffffff), 1.0f, 0.0f);
	}

	void setVertexAlpha(irr::u32 alpha)
	{
		for(irr::u32 i = 0;i < 6;++i)
			Vertices[i].Color.setAlpha(alpha);
	}

	void setVertexColor(irr::video::SColor color)
	{
		for(irr::u32 i = 0;i < 6;++i)
			Vertices[i].Color = color;
	}

	void render(irr::video::IVideoDriver* driver)
	{
		const irr::u16 indices[6] = {0, 1, 2, 3, 4, 5};

		irr::core::matrix4 mat;
		mat.setScale(scale);

		irr::core::matrix4 smat;
		smat.setRotationDegrees(rotation);
		mat *= smat;

		mat.setTranslation(position);

		driver->setMaterial(Material);
		driver->setTransform(irr::video::ETS_WORLD, mat);
		driver->setTransform(irr::video::ETS_VIEW, matrix4());
		driver->setTransform(irr::video::ETS_PROJECTION, matrix4());
		driver->drawIndexedTriangleList(&Vertices[0], 6, &indices[0], 2);
	}

	virtual irr::video::SMaterial& getMaterial()
	{
		return Material;
	}

	void setPosition(const irr::core::vector2df& positionIn)
	{
		position = irr::core::vector3df(positionIn.X * 2.0f, positionIn.Y * 2.0f, 0.0f)
			- irr::core::vector3df(1.0f, 1.0f, 0.0f);
	}

	void setRotation(const irr::f32 rotationIn)
	{
		rotation = irr::core::vector3df(0.0f, 0.0f, rotationIn);
	}

	void setScale(const irr::core::vector2df& scaleIn)
	{
		scale = irr::core::vector3df(scaleIn.X, scaleIn.Y, 0.0f);
	}

private:
	irr::video::S3DVertex Vertices[6];
	irr::video::SMaterial Material;
	irr::core::vector3df position, rotation, scale;
	
};
You can see a few vertices and a simple transformation matrix is all that's needed.

To use:

Code: Select all

CSprite mySprite;

mySprite.getMaterial().setTexture(0, driver->getTexture("TEXTURE")); // There is only one material anyway, so no parameter is taken in getMaterial().

mySprite.setPosition(vector2df(0.5, 0.5)); // Position is absolute, so 0.5 means the top-left corner of the sprite is going to be in the middle of the screen.

mySprite.setRotation(45.0f); // You can set rotation in degrees.

mySprite.setScale(vector2df(0.5f, 0.5f)); // And scale along the two axis.

### In your main loop: ###

driver->beginScene();
smgr->drawAll();
mySprite.render(driver); // You can do it after smgr->drawAll(), or before if you want background effects.
driver->endScene();
Cheers, I hope that was useful.
Last edited by BlindSide on Thu Jul 09, 2009 3:17 am, edited 1 time in total.
ShadowMapping for Irrlicht!: Get it here
Need help? Come on the IRC!: #irrlicht on irc://irc.freenode.net
geckoman
Posts: 143
Joined: Thu Nov 27, 2008 11:05 am
Location: Germany
Contact:

Post by geckoman »

@BlindSide: Thank you. That solution is not exactly what I searched for, as it does not return a resized image that can be used in the GUI. But if I don't need that functionality more often, I think I will use it, cause it's more easy. Anyway thanks a lot.
:D
Mel
Competition winner
Posts: 2292
Joined: Wed May 07, 2008 11:40 am
Location: Granada, Spain

Post by Mel »

Then render the result to a texture of the desired resolution. And that's all you need! :)

I'm working on a sort of class to do 2D effects, like scrolling, and stuff that return the results to a RTT. All of them in hardware, though, the calculations are done in software. Maybe in a not vere far future i try to do everything in the card, but for now, all i do is programming draw2Dimage sentences one oafter another
"There is nothing truly useless, it always serves as a bad example". Arthur A. Schmitt
geckoman
Posts: 143
Joined: Thu Nov 27, 2008 11:05 am
Location: Germany
Contact:

Post by geckoman »

I used BlindSides Method. It works quite good, the only thing that's annoying me is that I have to call the render method in my VERY clean main loop.

Thanks all.
sudi
Posts: 1686
Joined: Fri Aug 26, 2005 8:38 pm

Post by sudi »

well just make CSprite a children of ISceneNode and u should be fine....
We're programmers. Programmers are, in their hearts, architects, and the first thing they want to do when they get to a site is to bulldoze the place flat and build something grand. We're not excited by renovation:tinkering,improving,planting flower beds.
geckoman
Posts: 143
Joined: Thu Nov 27, 2008 11:05 am
Location: Germany
Contact:

Post by geckoman »

Do you mean inherit CSprite from ISceneNode?
sudi
Posts: 1686
Joined: Fri Aug 26, 2005 8:38 pm

Post by sudi »

Yes
We're programmers. Programmers are, in their hearts, architects, and the first thing they want to do when they get to a site is to bulldoze the place flat and build something grand. We're not excited by renovation:tinkering,improving,planting flower beds.
tonic
Posts: 69
Joined: Mon Dec 10, 2007 6:18 pm
Contact:

Post by tonic »

My 2D graphics/IMGUI library "Turska" supports also rendering with Irrlicht. It works in similar fashion with the method mentioned above, i.e. renders 3d polygons with custom projection.
http://jet.ro/turska/
geckoman
Posts: 143
Joined: Thu Nov 27, 2008 11:05 am
Location: Germany
Contact:

Post by geckoman »

Thank you, but I really do not want to change/patch Irrlicht. I want to use it "as is" so upgrading is much more easy!
emf2718
Posts: 4
Joined: Tue Feb 03, 2009 5:07 am

Can't get it to work right

Post by emf2718 »

I know this thread is a few days old, but I've been trying to get blindside's method to work. I can't get it to work quite right. I put it into the 2d demo that comes with Irrlicht and tried it out. Here's what I did:

I added

Code: Select all

CSprite mySprite;

mySprite.getMaterial().setTexture(0, driver->getTexture("../../media/earth.bmp")); // There is only one material anyway, so no parameter is taken in getMaterial().

mySprite.setPosition(vector2df(0.5, 0.5)); // Position is absolute, so 0.5 means the top-left corner of the sprite is going to be in the middle of the screen.

mySprite.setRotation(45.0f); // You can set rotation in degrees.

mySprite.setScale(vector2df(0.5f, 0.5f)); // And scale along the two axis.
after creating the driver; and I have "mySprite.render(driver);" in between the two imps. Unfortunately, I only see the usual dungeon scene. However, if I do this:

Code: Select all

      driver->setMaterial(Material);
      //driver->setTransform(irr::video::ETS_WORLD, mat);
      //driver->setTransform(irr::video::ETS_VIEW, matrix4());
      //driver->setTransform(irr::video::ETS_PROJECTION, matrix4());
      //driver->drawIndexedTriangleList(&Vertices[0], 6, &indices[0], 2);
The I see the earth map fill the window. If I do any resizing, rotating, or moving, then the first transform won't work right. Any ideas on what's going on?
BlindSide
Admin
Posts: 2821
Joined: Thu Dec 08, 2005 9:09 am
Location: NZ!

Post by BlindSide »

Can you paste the full altered example code? I'm happy to try it out and see what's going wrong.

Cheers
ShadowMapping for Irrlicht!: Get it here
Need help? Come on the IRC!: #irrlicht on irc://irc.freenode.net
fcloud
Posts: 5
Joined: Sun Oct 12, 2008 8:47 am

Post by fcloud »

BlindSide wrote:Can you paste the full altered example code? I'm happy to try it out and see what's going wrong.

Cheers
Hi, I tested your code. It works very nice. But I found that the picture has some deformation when the CSprite rotations. Why?
BlindSide
Admin
Posts: 2821
Joined: Thu Dec 08, 2005 9:09 am
Location: NZ!

Post by BlindSide »

How bad is the deformation? Can I see a screenshot?
ShadowMapping for Irrlicht!: Get it here
Need help? Come on the IRC!: #irrlicht on irc://irc.freenode.net
Post Reply