Click here to Skip to main content
15,889,852 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hello guys!

With the following code I create my ModelVisual3D model.
C#
private ImageBrush texture = new ImageBrush(new BitmapImage(new Uri("Images//texture.png", UriKind.RelativeOrAbsolute))) { ViewboxUnits = BrushMappingMode.Absolute };

model = new ModelVisual3D();
Point3D pA = new Point3D(pos.X +  0.86602 * size, pos.Y, pos.Z +  0.50000 * size);
Point3D pB = new Point3D(pos.X +  0.86602 * size, pos.Y, pos.Z + -0.50000 * size);
Point3D pC = new Point3D(pos.X, pos.Y, pos.Z + size);
Point3D pD = new Point3D(pos.X + -0.86602 * size, pos.Y, pos.Z + -0.50000 * size);
Point3D pE = new Point3D(pos.X + -0.86602 * size, pos.Y, pos.Z +  0.50000 * size);
Point3D pF = new Point3D(pos.X, pos.Y, pos.Z - size);
Model3DGroup grp = new Model3DGroup();
grp.Children.Add(CreateTriangleModel(new DiffuseMaterial(texture), pB, pF, pD, new Point(0, 0), new Point(0, 100), new Point(100, 100)));
grp.Children.Add(CreateTriangleModel(new DiffuseMaterial(texture), pD, pE, pA, new Point(0, 0), new Point(0, 0.5), new Point(0.5, 0.5)));
grp.Children.Add(CreateTriangleModel(new DiffuseMaterial(texture), pB, pD, pA, new Point(0, 0), new Point(100, 0), new Point(100, 100)));
grp.Children.Add(CreateTriangleModel(new DiffuseMaterial(texture), pA, pE, pC, new Point(0, 0), new Point(0, 100), new Point(100, 100)));

I use this method to create my triangle models:
public static Model3DGroup CreateTriangleModel(Material material, Point3D p0, Point3D p1, Point3D p2, Point tc0, Point tc1, Point tc2)
{
    MeshGeometry3D mesh = new MeshGeometry3D();
    Vector3D normal = CalculateNormal(p0, p1, p2);
    mesh.Positions.Add(p0);
    mesh.Positions.Add(p1);
    mesh.Positions.Add(p2);
    mesh.TriangleIndices.Add(0);
    mesh.TriangleIndices.Add(1);
    mesh.TriangleIndices.Add(2);
    mesh.TextureCoordinates.Add(tc0);
    mesh.TextureCoordinates.Add(tc1);
    mesh.TextureCoordinates.Add(tc2);
    mesh.Normals.Add(normal);
    mesh.Normals.Add(normal);
    mesh.Normals.Add(normal);
    GeometryModel3D model = new GeometryModel3D(mesh, material);
    Model3DGroup group = new Model3DGroup();
    group.Children.Add(model);
    return group;
}


well the problem is, the image is not displayed correctly. it always shows the full image, no matter what points I use for TextureCoordinates. And I want to select a special triangle on my image for every triangleModel... thats why i use "ViewboxUnits = BrushMappingMode.Absolute"... but somehow this doesnt work. And if i set ViewboxUNits to AccordingToBoundingBox and use Points like (0, 0.5) it doesnt work either. It always shows the full image on my triangleModel and not only a part of it...

regards
Posted
Updated 20-Aug-12 14:11pm
v2

 
Share this answer
 
v2
Comments
abfl 20-Aug-12 20:06pm    
well the problem is, the image is not displayed correctly. it always shows the full image, no matter what points I use for TextureCoordinates. And I want to select a special triangle on my image for every triangleModel... thats why i use "ViewboxUnits = BrushMappingMode.Absolute"... but somehow this doesnt work. And if i set ViewboxUNits to AccordingToBoundingBox and use Points like (0, 0.5) it doesnt work either. It always shows the full image on my triangleModel and not only a part of it...
Kenneth Haugland 20-Aug-12 20:09pm    
tHen you should update your question, how should I have known this?
abfl 21-Aug-12 6:46am    
Thx, but I've read this tutorial for about 10 times now and it doesn't help me.
When I use e.g. (0, 0), (0.5, 0.5), (0.5, 0) for the TextureCoordinates it shows me exactly the same Image like (0, 0), (1, 1), (1, 0) with this coordinates.
And if I set the ViewboxUnits to Absolute, coordinates like this (0, 0), (0, 100), (100, 100) show me the same Image like coordinates like this (0, 0), (0, 1), (1, 1)... I don't understand why it doesn't work like in the Tutorial...!?
Ok, I managed to find a solution:
model = new ModelVisual3D();
Point3D pD = new Point3D(pos.X +  0.86602 * size, pos.Y, pos.Z +  0.50000 * size);
Point3D pE = new Point3D(pos.X +  0.86602 * size, pos.Y, pos.Z + -0.50000 * size);
Point3D pF = new Point3D(pos.X, pos.Y, pos.Z - size);
Point3D pA = new Point3D(pos.X + -0.86602 * size, pos.Y, pos.Z + -0.50000 * size);
Point3D pB = new Point3D(pos.X + -0.86602 * size, pos.Y, pos.Z +  0.50000 * size);
Point3D pC = new Point3D(pos.X, pos.Y, pos.Z + size);
MeshGeometry3D mesh = new MeshGeometry3D();
Vector3D normal = Model3DHelper.CalculateNormal(pA, pB, pC);
mesh.Positions.Add(pA);
mesh.Positions.Add(pB);
mesh.Positions.Add(pC);
mesh.Positions.Add(pD);
mesh.Positions.Add(pE);
mesh.Positions.Add(pF);
mesh.Normals.Add(normal);
mesh.Normals.Add(normal);
mesh.Normals.Add(normal);
mesh.Normals.Add(normal);
mesh.Normals.Add(normal);
mesh.Normals.Add(normal);
mesh.TextureCoordinates.Add(new Point(0.25, 1));
mesh.TextureCoordinates.Add(new Point(0.75, 1));
mesh.TextureCoordinates.Add(new Point(1, 0.5));
mesh.TextureCoordinates.Add(new Point(0.75, 0));
mesh.TextureCoordinates.Add(new Point(0.25, 0));
mesh.TextureCoordinates.Add(new Point(0, 0.5));
mesh.TriangleIndices.Add(0); mesh.TriangleIndices.Add(4); mesh.TriangleIndices.Add(5);
mesh.TriangleIndices.Add(0); mesh.TriangleIndices.Add(1); mesh.TriangleIndices.Add(3);
mesh.TriangleIndices.Add(3); mesh.TriangleIndices.Add(4); mesh.TriangleIndices.Add(0);
mesh.TriangleIndices.Add(1); mesh.TriangleIndices.Add(2); mesh.TriangleIndices.Add(3);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900