Flip 2D images vertical?

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
fnolis
Posts: 10
Joined: Thu Jun 24, 2004 8:25 am
Location: Sweden

Flip 2D images vertical?

Post by fnolis »

Is there a way to flip 2D images (jpg/png) vertical (lika a mirror) so that I don't have to create both ways images of a character that runs to the left and right.
rogerborg
Admin
Posts: 3590
Joined: Mon Oct 09, 2006 9:36 am
Location: Scotland - gonnae no slag aff mah Engleesh
Contact:

Post by rogerborg »

Have you tried IVideoDriver::draw2DImage() and flipping the left and right sides of the sourceRect?
Please upload candidate patches to the tracker.
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
rogerborg
Admin
Posts: 3590
Joined: Mon Oct 09, 2006 9:36 am
Location: Scotland - gonnae no slag aff mah Engleesh
Contact:

Post by rogerborg »

Answering my own question: that doesn't work. You'll can make a slight modification to Irrlicht to support it though. I'd suggest adding a boolean flip parameter (or parameters, or bit flags, or an enumerated type, if you want to support vertical flipping) to one or all of the draw2DImage() methods. Then in the various implementations, you should just be able to swap the U (and/or V) texture co-ordinates in the vertices if the flag is set. That works fine in the CD39Driver anyway.
Please upload candidate patches to the tracker.
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
rogerborg
Admin
Posts: 3590
Joined: Mon Oct 09, 2006 9:36 am
Location: Scotland - gonnae no slag aff mah Engleesh
Contact:

Post by rogerborg »

By which I mean:

Code: Select all

Index: include/IVideoDriver.h
===================================================================
--- include/IVideoDriver.h	(revision 1023)
+++ include/IVideoDriver.h	(working copy)
@@ -423,7 +423,8 @@
 		used to draw the image.*/
 		virtual void draw2DImage(const 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) = 0;
+			SColor color=SColor(255,255,255,255), bool useAlphaChannelOfTexture=false,
+			bool flipHorizontally = false) = 0;
 
 		//! draws a set of 2d images, using a color and the alpha
 		/** channel of the texture if desired. The images are drawn
Index: source/Irrlicht/CD3D9Driver.cpp
===================================================================
--- source/Irrlicht/CD3D9Driver.cpp	(revision 1023)
+++ source/Irrlicht/CD3D9Driver.cpp	(working copy)
@@ -929,7 +929,8 @@
 				const core::position2d<s32>& pos,
 				const core::rect<s32>& sourceRect,
 				const core::rect<s32>* clipRect, SColor color,
-				bool useAlphaChannelOfTexture)
+				bool useAlphaChannelOfTexture,
+				bool flipHorizontally)
 {
 	if (!texture)
 		return;
@@ -1038,6 +1039,13 @@
 
 	setRenderStates2DMode(color.getAlpha()<255, true, useAlphaChannelOfTexture);
 
+	if(flipHorizontally)
+	{
+		f32 temp = tcoords.UpperLeftCorner.X;
+		tcoords.UpperLeftCorner.X = tcoords.LowerRightCorner.X;
+		tcoords.LowerRightCorner.X = temp;
+	}
+
 	S3DVertex vtx[4];
 	vtx[0] = S3DVertex((f32)(poss.UpperLeftCorner.X+xPlus) * xFact, (f32)(yPlus-poss.UpperLeftCorner.Y ) * yFact , 0.0f, 0.0f, 0.0f, 0.0f, color, tcoords.UpperLeftCorner.X, tcoords.UpperLeftCorner.Y);
 	vtx[1] = S3DVertex((f32)(poss.LowerRightCorner.X+xPlus) * xFact, (f32)(yPlus- poss.UpperLeftCorner.Y) * yFact, 0.0f, 0.0f, 0.0f, 0.0f, color, tcoords.LowerRightCorner.X, tcoords.UpperLeftCorner.Y);
Index: source/Irrlicht/CD3D9Driver.h
===================================================================
--- source/Irrlicht/CD3D9Driver.h	(revision 1023)
+++ source/Irrlicht/CD3D9Driver.h	(working copy)
@@ -65,7 +65,8 @@
 		//! draws an 2d image, using a color (if color is other then Color(255,255,255,255)) and the alpha channel of the texture if wanted.
 		virtual void draw2DImage(const 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);
+			SColor color=SColor(255,255,255,255), bool useAlphaChannelOfTexture=false,
+			bool flipHorizontally = false);
 
 		//! Draws a part of the texture into the rectangle.
 		virtual void draw2DImage(const video::ITexture* texture, const core::rect<s32>& destRect,
Start with that, then implement it in the other devices / draw2DImage() methods.
Please upload candidate patches to the tracker.
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
Post Reply