Support for 2D transform

Post your questions, suggestions and experiences regarding to Image manipulation, 3d modeling and level editing for the Irrlicht engine here.
Post Reply
WuZhenwei
Posts: 5
Joined: Fri Apr 18, 2014 11:08 am

Support for 2D transform

Post by WuZhenwei »

With current version of Irrlicht, it's quite difficult to rotate a 2D sprite. Although some developers have provided codes that can do this, it's still not very flexible. I think it would be better if irrlicht video driver supports setting a transform matrix in 2D mode. For example, IVideoDriver will have a new function `setTransform2D' and in `setRenderStates2DMode' function, use that matrix instead of identity matrix. Although it involves all kinds of drivers, for each driver it's not a big change. Is that possible and is there any problems with that solution?
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Support for 2D transform

Post by CuteAlien »

It's under discussion regulalry. We need some solution for that, but couldn't agree yet on an implementation.
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
chronologicaldot
Competition winner
Posts: 688
Joined: Mon Sep 10, 2012 8:51 am

Re: Support for 2D transform

Post by chronologicaldot »

mm...
I think it'd be nice if images were drawn in the same way as mesh textures in the world, and then you could call something like draw2DImage( texture, matrix=identity ), and thus by default, it draws according to the identity matrix, but otherwise, it changes the drawing orientation. Seems to me it could take advantage of pre-existing parts of the engine and could draw images as it does now if the matrix is the identity matrix.

... Otherwise, we could just create a class that transforms the image for us, but that would create textures and eventually become a memory hog.

Notably, I forgot which thread we were discussing the image transformation in, otherwise I might be more informed.
Mel
Competition winner
Posts: 2292
Joined: Wed May 07, 2008 11:40 am
Location: Granada, Spain

Re: Support for 2D transform

Post by Mel »

Rotating sprites isn't exactly complex, actually, but it is tricky because the amount of options it may involve, for instance, you can rotate a image changing its texture matrix. Also, currently all the 2D drawing options perform clipping in software to help saving renderstate changes, something that may not happen while rotating images, and such.

I'd go for creating a set of rectangular polygons, rotate them manually, and placing them in the proper coordinates, later you set up a proper view and projection matrix and draw the whole set of quads as a single meshbuffer
"There is nothing truly useless, it always serves as a bad example". Arthur A. Schmitt
Max Power
Posts: 98
Joined: Wed Mar 14, 2012 10:09 am

Re: Support for 2D transform

Post by Max Power »

Can someone show me some example code how to deal with rotating sprites in a 2d environment? If I just change texture coordinates, won't the rectangle be too small to display the whole sprite? So I would need enough empty transparent space around every image... How do I even manipulate tex coords when drawing a 2d picture without(?) actual vertices? Custom material shader?..

I'd like to see better 2d support, too. It seems atm Irrlicht is a pretty bad choice for it, but I've learned too much about the engine to switch now. I rather stick with it.
chronologicaldot
Competition winner
Posts: 688
Joined: Mon Sep 10, 2012 8:51 am

Re: Support for 2D transform

Post by chronologicaldot »

Late reply, sorry.

Here's the simple way:

Code: Select all

 
// Assuming sceneManager is a pointer to ISceneManager...
ISceneNode* node = sceneManager->addCubeSceneNode();
 
// Assuming videoDriver is a pointer to IVideoDriver
ITexture* texture = videoDriver->getTexture( path_to_image ); // io::path path_to_image
 
node->setMaterialTexture(0, texture );
 
node->setRotation( /* radians */ vector3df( core::degToRad(45.f), 0.f, 0.f ) );
 
And if you only want it in a GUI element, you'd grab() it, clear the scene with sceneManager->clear(), and only draw the node in your GUI element draw() function.
Mel
Competition winner
Posts: 2292
Joined: Wed May 07, 2008 11:40 am
Location: Granada, Spain

Re: Support for 2D transform

Post by Mel »

I think it wouldn't harm to have a sort of "screen space aligned quad", like a billboard but that you place in screen coordinates, not in 3D coordinates, that you can rotate, scale and translate. That would simplify enormously the task of drawing 2D rotated and scaled textures.
"There is nothing truly useless, it always serves as a bad example". Arthur A. Schmitt
Post Reply