c# Texturemap matrixtransform problem

Jan 22, 2008 at 12:47 PM
Edited Jan 22, 2008 at 12:48 PM

I've been trying to add texturemapping to the 3dcube sample you've posted. but I'm running into a problem and am wondering if you're having the same difficulty.
Since there haven't been new posts sinds last year regarding texturemapping, I'm guessing you havent finished yet :P

I've got a texture mapping routine in Flash which subdivides an image in triangles (with custom subdivision level), and performs matrix translations on those triangles to approximate texturemapping. (I think papervision 3d and the like also use techniques like this)

I see there's also a newer version, I used an old one :)

The javascript from the Vista demo page (http://www.windowsvista.si/js/object_3d.js) also uses triangles to do the texturemapping, but in a different way. (zoom in on the logo and you can see the deformations :)

When I ported the code to Silverlight, it appears that the Matrices don't work they way they should when you apply them to an image brush. (All other c# silverlight examples i found all use matrices from XAML and apply them to polys, and not on the imagebrush itself)

The matrices often use non-integer values. but eventhough the matrix parameters in c# are all doubles, they won't work when you use non rounded figures.

The following example won't draw anything unless intQ is 10 or higher.
(Nevermind the neatness of the code, I grabbed it from a work-in-progress )

double x0 = 100;
double y0 = 0;
double x1 = 800;
double y1 = 0;
double x2 = 110;
double y2 = 800;

Polygon myPoly = new Polygon();
Point[] arrPoints = new Point[]{
new Point(Convert.ToInt16(x0), Convert.ToInt16(y0)),
new Point(Convert.ToInt16(x1), Convert.ToInt16(y1)),
new Point(Convert.ToInt16(x2), Convert.ToInt16(y2))

myPoly.Points = arrPoints;


Matrix myMatrix = new Matrix();
myMatrix.M11 = 1;
// this should gradually scale the image, but it just jumps (what seems like) whole values
// and for values between 0 and 1 it also wont draw anything
myMatrix.M12 = intQ / 10.0;
myMatrix.M21 = intQ / 10.0;
myMatrix.M22 = 1;
myMatrix.OffsetX = 0;
myMatrix.OffsetY = 0;

MatrixTransform myMatrixTransform = new MatrixTransform();
myMatrixTransform.Matrix = myMatrix;

ImageBrush myBrush = new ImageBrush();
myBrush.Stretch = Stretch.None;
myBrush.AlignmentX = AlignmentX.Left;
myBrush.AlignmentY = AlignmentY.Top;

Uri imgUri = new Uri("images/gridimage.jpg", UriKind.RelativeOrAbsolute);

myPoly.Fill = myBrush;
myPoly.Fill.SetValue(ImageBrush.ImageSourceProperty, imgUri);
myPoly.Fill.SetValue(ImageBrush.TransformProperty, myMatrixTransform);


Have you encountered problems like this? and if so, (how) did you solve them? :P