Hello, I've found the Irrlicht Lime wrapper to be quite excellent!
I'm having trouble converting this 2D image with rotate method, posted here, into the C# Lime equivalent:
http://irrlicht.sourceforge.net/forum/v ... =9&t=32388
I've attempted to extend this 2D example into my application.
Here is what I have so far, but it doesn't draw anything to the screen, where the standard Draw2d
Code: Select all
public class Sprite
{
public Texture BaseTexture { get; set; }
public float Rotation { get; set; }
public Vector2Di Position { get; set; }
public Vector2Di RotationPoint { get; set; }
public Vector2Df Scale { get; set; }
public Recti SourceRect { get; set; }
public Sprite()
{
BaseTexture = null;
Rotation = 0;
Position = new Vector2Di();
RotationPoint = new Vector2Di();
Scale = new Vector2Df();
SourceRect = new Recti();
}
// Works fine
public void DrawSimple(VideoDriver driver)
{
driver.Draw2DImage(BaseTexture, Position);
}
// This is the trouble method //
public void DrawRotate(VideoDriver driver, Color color, bool UseAlphaChannel)
{
// Store and clear the projection matrix
Matrix oldProjMat = driver.GetTransform(TransformationState.Projection);
driver.SetTransform(TransformationState.Projection, new Matrix());
// Store and clear the view matrix
Matrix oldViewMat = driver.GetTransform(TransformationState.View);
driver.SetTransform(TransformationState.View, new Matrix());
// Store and clear the world matrix
Matrix oldWorldMat = driver.GetTransform(TransformationState.World);
driver.SetTransform(TransformationState.World, new Matrix());
// Find horizontal and vertical axes after rotation
float c = (float)Math.Cos(-Rotation * 0.017453f);
float s = (float)Math.Sin(-Rotation * 0.017453f);
Vector2Df horizontalAxis = new Vector2Df(c, s);
Vector2Df verticalAxis = new Vector2Df(s, -c);
// First, we'll find the offset of the center and then where the center would be after rotation
Vector2Df centerOffset = new Vector2Df(Position.X + SourceRect.Width / 2.0f * Scale.X - RotationPoint.X, Position.Y + SourceRect.Height / 2.0f * Scale.Y - RotationPoint.Y);
Vector2Df center = new Vector2Df( (horizontalAxis.X * centerOffset.X) - (verticalAxis.X * centerOffset.Y), (horizontalAxis.Y * centerOffset.X) - (verticalAxis.Y * centerOffset.Y) );
center.X += RotationPoint.X;
center.Y += RotationPoint.Y;
// Now find the corners based off the center
Vector2Df cornerOffset = new Vector2Df(SourceRect.Width * Scale.X / 2.0f, SourceRect.Height * Scale.Y / 2.0f);
verticalAxis *= cornerOffset.Y;
horizontalAxis *= cornerOffset.X;
Vector2Df[] corner = new Vector2Df[4];
corner[0] = center + verticalAxis - horizontalAxis;
corner[1] = center + verticalAxis + horizontalAxis;
corner[2] = center - verticalAxis - horizontalAxis;
corner[3] = center - verticalAxis + horizontalAxis;
// Find the uv coordinates of the sourceRect
Vector2Df textureSize = new Vector2Df(BaseTexture.Size.Width, BaseTexture.Size.Height);
Vector2Df[] uvCorner = new Vector2Df[4];
uvCorner[0] = new Vector2Df(SourceRect.UpperLeftCorner.X, SourceRect.UpperLeftCorner.Y);
uvCorner[1] = new Vector2Df(SourceRect.LowerRightCorner.X, SourceRect.UpperLeftCorner.Y);
uvCorner[2] = new Vector2Df(SourceRect.UpperLeftCorner.X, SourceRect.LowerRightCorner.Y);
uvCorner[3] = new Vector2Df(SourceRect.LowerRightCorner.X, SourceRect.LowerRightCorner.Y);
for(int i = 0; i < 4; i++)
{
uvCorner[i] /= textureSize;
}
// Vertices for the image
Vertex3D []vertices = new Vertex3D[4];
ushort []indices = { 0, 1, 2, 3 ,2 ,1 };
// Convert pixels to world coordinates
Vector2Df screenSize = new Vector2Df(driver.ViewPort.Width, driver.ViewPort.Height);
for (int i = 0; i < 4; i++)
{
vertices[i] = new Vertex3D();
vertices[i].Position = new Vector3Df(((corner[i].X/screenSize.X)-0.5f)*2.0f,((corner[i].Y/screenSize.Y)-0.5f)*-2.0f,1);
vertices[i].TCoords = uvCorner[i];
vertices[i].Color = color;
}
// Create the material
Material material = new Material();
material.BlendOperation = BlendOperation.Add;
material.Lighting = false;
material.ZWrite = false;
material.ZBuffer = ComparisonFunc.Disabled;
material.BackfaceCulling = false;
material.SetTexture(0, this.BaseTexture);
/* can't find lime equivalent
material.MaterialTypeParam =
irr::video::pack_texureBlendFunc(irr::video::EBF_SRC_ALPHA,
irr::video::EBF_ONE_MINUS_SRC_ALPHA,
irr::video::EMFN_MODULATE_1X,
irr::video::EAS_TEXTURE | irr::video::EAS_VERTEX_COLOR);
*/
if(UseAlphaChannel)
material.Type = MaterialType.OneTextureBlend;
else
material.Type = MaterialType.Solid;
driver.SetMaterial(material);
driver.Draw2DVertexPrimitiveList(vertices, indices);
// Restore projection, world, and view matrices
driver.SetTransform(TransformationState.Projection, oldProjMat);
driver.SetTransform(TransformationState.View ,oldViewMat);
driver.SetTransform(TransformationState.World, oldWorldMat);
}
}
If I'm totally missing something, or there is a much easier way to rotate a 2d image, let me know. My previous games were done with the Haaf's Game engine. Unfortunately I can't mix 3D in with it, and Irrlicht Lime is otherwise awesome except not being able to rotate a 2d image intuitively.
Thanks.