In this demonstration I am trying to create 3-D cube in Windows Presentation Foundation (WPF) and this 3-D cube will be rotate on its axis as shown below.
When we create a new WPF Application project in VisualStudio, several files are generated, one of them file is App.xaml, App.xaml file that provides a default definition of each Style resource, in this demonstration App.xaml file contained references of Images in ImageBrush tags with keys as shown below:
Code: App.xaml
<Application x:Class="Create_Cube.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml">
<Application.Resources>
<ImageBrush x:Key="Chrysanthemum"
ImageSource="Chrysanthemum.jpg" Stretch="Fill" TileMode="FlipXY" />
<ImageBrush x:Key="Desert"
ImageSource="Desert.jpg" Stretch="Fill" TileMode="FlipXY" />
<ImageBrush x:Key="Jellyfish"
ImageSource="Jellyfish.jpg" Stretch="Fill" TileMode="FlipXY" />
<ImageBrush x:Key="Koala"
ImageSource="Koala.jpg" Stretch="Fill" TileMode="FlipXY" />
<ImageBrush x:Key="Penguins"
ImageSource="Penguins.jpg" Stretch="Fill" TileMode="FlipXY" />
<ImageBrush x:Key="Tulips"
ImageSource="Tulips.jpg" Stretch="Fill" TileMode="FlipXY" />
</Application.Resources>
</Application>
Code: MainWindow.xaml
<Window x:Class="Create_Cube.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="312" Width="559"
Title="create_cube"
Loaded="WindowLoaded"
Name="mainWindow">
<Window.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="Black" Offset="0" />
<GradientStop Color="#FFD2D3D4" Offset="1" />
</LinearGradientBrush>
</Window.Background>
</Window>
Code: MainWindow.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
//Adding namespaces in application as shown below
using System.Windows.Media.Media3D;
using System.Windows.Media.Animation;
namespace Create_Cube
{
publicpartialclassMainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
Viewport3D Viewport = newViewport3D(); //Viewport3D class provides a rendering surface for 3-D visual content
GeometryModel3D surface1 = newGeometryModel3D();//GeometryModel3D class object provides generalized transformation support for 3-D objects
GeometryModel3D surface2 = newGeometryModel3D();
GeometryModel3D surface3 = newGeometryModel3D();
GeometryModel3D surface4 = newGeometryModel3D();
GeometryModel3D surface5 = newGeometryModel3D();
GeometryModel3D surface6 = newGeometryModel3D();
PerspectiveCamera Camera = newPerspectiveCamera();//PerspectiveCamera class object represents a perspective projection camera
DirectionalLight myDLight = newDirectionalLight();//DirectionalLight class object provides effect along a direction
AmbientLight AmLight = newAmbientLight();//AmbientLight class objects provides Lights every surface uniformly a bright AmbientLight creates because of lack of shading, but a low-intensity AmbientLight approximatesthe effect of light that has been scattered by reflecting between diffuse surfaces in the scene.
MaterialGroup myMaterials = newMaterialGroup();//MaterialGroup class objects applies multiple Materials to a model each Material is rendered in order, with the last Material in the group appearing on top.
Transform3DGroup Transgroup = newTransform3DGroup();//Transform3DGroup class contains a collection of Transform3Ds.
Model3DGroup Modelgroup = newModel3DGroup();//The Model3DGroup class is itself a Model3D and is often used to group multiple GeometryModel3Ds
Model3DGroup cube = newModel3DGroup();
MeshGeometry3D surface1Plane = newMeshGeometry3D();// MeshGeometry3D class represents a set of 3D surfaces
MeshGeometry3D surface2Plane = newMeshGeometry3D();
MeshGeometry3D surface3Plane = newMeshGeometry3D();
MeshGeometry3D surface4Plane = newMeshGeometry3D();
MeshGeometry3D surface5Plane = newMeshGeometry3D();
MeshGeometry3D surface6Plane = newMeshGeometry3D();
DiffuseMaterial surface1Material = newDiffuseMaterial();//DiffuseMaterial class provides scatters light striking the surface in all directions,
DiffuseMaterial surface2Material = newDiffuseMaterial();
DiffuseMaterial surface3Material = newDiffuseMaterial();
DiffuseMaterial surface4Material = newDiffuseMaterial();
DiffuseMaterial surface5Material = newDiffuseMaterial();
DiffuseMaterial surface6Material = newDiffuseMaterial();
ModelVisual3D ModelVisualD = newModelVisual3D();//ModelVisual3D class contains 3-D models
privatevoid WindowLoaded(object sender, RoutedEventArgs e)
{
Camera.Position = newPoint3D(-5, 2, 3);//setting the camera position in world coordinates
Camera.LookDirection = newVector3D(5, -2, -3);//defininig the direction in which the camera looking in world coordinates
AmLight.Color = Colors.White;//setting the color of light
//set Geometry property of MeshGeometry3D
surface1.Geometry = surface1Plane;
surface2.Geometry = surface2Plane;
surface3.Geometry = surface3Plane;
surface4.Geometry = surface4Plane;
surface5.Geometry = surface5Plane;
surface6.Geometry = surface6Plane;
Vector3D vecD = newVector3D(-50, -90, 0);
Rotation3D RD = new AxisAngleRotation3D(vecD, 1);
RotateTransform3D RotateTrans = new RotateTransform3D(RD);//RotateTransform3D class specify rotation transformation that is parameterized
//Define an animation for the rotation
DoubleAnimation myAnimation = newDoubleAnimation();
myAnimation.From = 1;
myAnimation.To = 361;
myAnimation.Duration = newDuration(TimeSpan.FromMilliseconds(3000));//Defining time in second for cube that will be rotate
myAnimation.RepeatBehavior = RepeatBehavior.Forever;
RotateTrans.Rotation.BeginAnimation(AxisAngleRotation3D.AngleProperty, myAnimation);
//Add transformation to the model
Transgroup.Children.Add(RotateTrans);
cube.Children.Add(surface1);
cube.Children.Add(surface2);
cube.Children.Add(surface3);
cube.Children.Add(surface4);
cube.Children.Add(surface5);
cube.Children.Add(surface6);
Modelgroup.Transform = Transgroup;
Modelgroup.Children.Add(cube);
Modelgroup.Children.Add(AmLight);
Viewport.Camera = Camera;
ModelVisualD.Content = Modelgroup;
Viewport.Children.Add(ModelVisualD);
//Defining surface position in world coordinates
-----------------------surface1------------------------
surface1Plane.Positions.Add(newPoint3D(-0.5, -0.5, -0.5));
surface1Plane.Positions.Add(newPoint3D(-0.5, 0.5, -0.5));
surface1Plane.Positions.Add(newPoint3D(0.5, 0.5, -0.5));
surface1Plane.Positions.Add(newPoint3D(0.5, 0.5, -0.5));
surface1Plane.Positions.Add(newPoint3D(0.5, -0.5, -0.5));
surface1Plane.Positions.Add(newPoint3D(-0.5, -0.5, -0.5));
//TriangleIndices—Describes the connections between the vertices to form triangles if TriangleIndices is not specified, it is implied that the positions should beconnected in the order they appear: 0 1 2, then 3 4 5, and so on.
surface1Plane.TriangleIndices.Add(0);
surface1Plane.TriangleIndices.Add(1);
surface1Plane.TriangleIndices.Add(2);
surface1Plane.TriangleIndices.Add(3);
surface1Plane.TriangleIndices.Add(4);
surface1Plane.TriangleIndices.Add(5);
surface1Plane.Normals.Add(newVector3D(0, 0, -1));
surface1Plane.Normals.Add(newVector3D(0, 0, -1));
surface1Plane.Normals.Add(newVector3D(0, 0, -1));
surface1Plane.Normals.Add(newVector3D(0, 0, -1));
surface1Plane.Normals.Add(newVector3D(0, 0, -1));
surface1Plane.Normals.Add(newVector3D(0, 0, -1));
surface1Plane.TextureCoordinates.Add(newPoint(1, 0));
surface1Plane.TextureCoordinates.Add(newPoint(1, 1));
surface1Plane.TextureCoordinates.Add(newPoint(0, 1));
surface1Plane.TextureCoordinates.Add(newPoint(0, 1));
surface1Plane.TextureCoordinates.Add(newPoint(0, 0));
surface1Plane.TextureCoordinates.Add(newPoint(1, 0));
-----------------------surface2------------------------
surface2Plane.Positions.Add(newPoint3D(-0.5, -0.5, 0.5));
surface2Plane.Positions.Add(newPoint3D(0.5, -0.5, 0.5));
surface2Plane.Positions.Add(newPoint3D(0.5, 0.5, 0.5));
surface2Plane.Positions.Add(newPoint3D(0.5, 0.5, 0.5));
surface2Plane.Positions.Add(newPoint3D(-0.5, 0.5, 0.5));
surface2Plane.Positions.Add(newPoint3D(-0.5, -0.5, 0.5));
surface2Plane.TriangleIndices.Add(0);
surface2Plane.TriangleIndices.Add(1);
surface2Plane.TriangleIndices.Add(2);
surface2Plane.TriangleIndices.Add(3);
surface2Plane.TriangleIndices.Add(4);
surface2Plane.TriangleIndices.Add(5);
surface2Plane.Normals.Add(newVector3D(0, 0, 1));
surface2Plane.Normals.Add(newVector3D(0, 0, 1));
surface2Plane.Normals.Add(newVector3D(0, 0, 1));
surface2Plane.Normals.Add(newVector3D(0, 0, 1));
surface2Plane.Normals.Add(newVector3D(0, 0, 1));
surface2Plane.Normals.Add(newVector3D(0, 0, 1));
surface2Plane.TextureCoordinates.Add(newPoint(0, 0));
surface2Plane.TextureCoordinates.Add(newPoint(1, 0));
surface2Plane.TextureCoordinates.Add(newPoint(1, 1));
surface2Plane.TextureCoordinates.Add(newPoint(1, 1));
surface2Plane.TextureCoordinates.Add(newPoint(0, 1));
surface2Plane.TextureCoordinates.Add(newPoint(0, 0));
-----------------------surface3------------------------
surface3Plane.Positions.Add(newPoint3D(-0.5, -0.5, -0.5));
surface3Plane.Positions.Add(newPoint3D(0.5, -0.5, -0.5));
surface3Plane.Positions.Add(newPoint3D(0.5, -0.5, 0.5));
surface3Plane.Positions.Add(newPoint3D(0.5, -0.5, 0.5));
surface3Plane.Positions.Add(newPoint3D(-0.5, -0.5, 0.5));
surface3Plane.Positions.Add(newPoint3D(-0.5, -0.5, -0.5));
surface3Plane.TriangleIndices.Add(0);
surface3Plane.TriangleIndices.Add(1);
surface3Plane.TriangleIndices.Add(2);
surface3Plane.TriangleIndices.Add(3);
surface3Plane.TriangleIndices.Add(4);
surface3Plane.TriangleIndices.Add(5);
surface3Plane.Normals.Add(newVector3D(0, -1, 0));
surface3Plane.Normals.Add(newVector3D(0, -1, 0));
surface3Plane.Normals.Add(newVector3D(0, -1, 0));
surface3Plane.Normals.Add(newVector3D(0, -1, 0));
surface3Plane.Normals.Add(newVector3D(0, -1, 0));
surface3Plane.Normals.Add(newVector3D(0, -1, 0));
surface3Plane.TextureCoordinates.Add(newPoint(0, 0));
surface3Plane.TextureCoordinates.Add(newPoint(1, 0));
surface3Plane.TextureCoordinates.Add(newPoint(1, 1));
surface3Plane.TextureCoordinates.Add(newPoint(1, 1));
surface3Plane.TextureCoordinates.Add(newPoint(0, 1));
surface3Plane.TextureCoordinates.Add(newPoint(0, 0));
-----------------------surface4------------------------
surface4Plane.Positions.Add(newPoint3D(0.5, -0.5, -0.5));
surface4Plane.Positions.Add(newPoint3D(0.5, 0.5, -0.5));
surface4Plane.Positions.Add(newPoint3D(0.5, 0.5, 0.5));
surface4Plane.Positions.Add(newPoint3D(0.5, 0.5, 0.5));
surface4Plane.Positions.Add(newPoint3D(0.5, -0.5, 0.5));
surface4Plane.Positions.Add(newPoint3D(0.5, -0.5, -0.5));
surface4Plane.TriangleIndices.Add(0);
surface4Plane.TriangleIndices.Add(1);
surface4Plane.TriangleIndices.Add(2);
surface4Plane.TriangleIndices.Add(3);
surface4Plane.TriangleIndices.Add(4);
surface4Plane.TriangleIndices.Add(5);
surface4Plane.Normals.Add(newVector3D(1, 0, 0));
surface4Plane.Normals.Add(newVector3D(1, 0, 0));
surface4Plane.Normals.Add(newVector3D(1, 0, 0));
surface4Plane.Normals.Add(newVector3D(1, 0, 0));
surface4Plane.Normals.Add(newVector3D(1, 0, 0));
surface4Plane.Normals.Add(newVector3D(1, 0, 0));
surface4Plane.TextureCoordinates.Add(newPoint(1, 0));
surface4Plane.TextureCoordinates.Add(newPoint(1, 1));
surface4Plane.TextureCoordinates.Add(newPoint(0, 1));
surface4Plane.TextureCoordinates.Add(newPoint(0, 1));
surface4Plane.TextureCoordinates.Add(newPoint(0, 0));
surface4Plane.TextureCoordinates.Add(newPoint(1, 0));
-----------------------surface5------------------------
surface5Plane.Positions.Add(newPoint3D(0.5, 0.5, -0.5));
surface5Plane.Positions.Add(newPoint3D(-0.5, 0.5, -0.5));
surface5Plane.Positions.Add(newPoint3D(-0.5, 0.5, 0.5));
surface5Plane.Positions.Add(newPoint3D(-0.5, 0.5, 0.5));
surface5Plane.Positions.Add(newPoint3D(0.5, 0.5, 0.5));
surface5Plane.Positions.Add(newPoint3D(0.5, 0.5, -0.5));
surface5Plane.TriangleIndices.Add(0);
surface5Plane.TriangleIndices.Add(1);
surface5Plane.TriangleIndices.Add(2);
surface5Plane.TriangleIndices.Add(3);
surface5Plane.TriangleIndices.Add(4);
surface5Plane.TriangleIndices.Add(5);
surface5Plane.Normals.Add(newVector3D(0, 1, 0));
surface5Plane.Normals.Add(newVector3D(0, 1, 0));
surface5Plane.Normals.Add(newVector3D(0, 1, 0));
surface5Plane.Normals.Add(newVector3D(0, 1, 0));
surface5Plane.Normals.Add(newVector3D(0, 1, 0));
surface5Plane.Normals.Add(newVector3D(0, 1, 0));
surface5Plane.TextureCoordinates.Add(newPoint(1, 1));
surface5Plane.TextureCoordinates.Add(newPoint(0, 1));
surface5Plane.TextureCoordinates.Add(newPoint(0, 0));
surface5Plane.TextureCoordinates.Add(newPoint(0, 0));
surface5Plane.TextureCoordinates.Add(newPoint(1, 0));
surface5Plane.TextureCoordinates.Add(newPoint(1, 1));
-----------------------surface6------------------------
surface6Plane.Positions.Add(newPoint3D(-0.5, 0.5, -0.5));
surface6Plane.Positions.Add(newPoint3D(-0.5, -0.5, -0.5));
surface6Plane.Positions.Add(newPoint3D(-0.5, -0.5, 0.5));
surface6Plane.Positions.Add(newPoint3D(-0.5, -0.5, 0.5));
surface6Plane.Positions.Add(newPoint3D(-0.5, 0.5, 0.5));
surface6Plane.Positions.Add(newPoint3D(-0.5, 0.5, -0.5));
surface6Plane.TriangleIndices.Add(0);
surface6Plane.TriangleIndices.Add(1);
surface6Plane.TriangleIndices.Add(2);
surface6Plane.TriangleIndices.Add(3);
surface6Plane.TriangleIndices.Add(4);
surface6Plane.TriangleIndices.Add(5);
surface6Plane.Normals.Add(newVector3D(-1, 0, 0));
surface6Plane.Normals.Add(newVector3D(-1, 0, 0));
surface6Plane.Normals.Add(newVector3D(-1, 0, 0));
surface6Plane.Normals.Add(newVector3D(-1, 0, 0));
surface6Plane.Normals.Add(newVector3D(-1, 0, 0));
surface6Plane.Normals.Add(newVector3D(-1, 0, 0));
surface6Plane.TextureCoordinates.Add(newPoint(0, 1));
surface6Plane.TextureCoordinates.Add(newPoint(0, 0));
surface6Plane.TextureCoordinates.Add(newPoint(1, 0));
surface6Plane.TextureCoordinates.Add(newPoint(1, 0));
surface6Plane.TextureCoordinates.Add(newPoint(1, 1));
surface6Plane.TextureCoordinates.Add(newPoint(0, 1));
//Accessing ImageBrush tags from App.xaml file with their keys name
DiffuseMaterial surface1Material = newDiffuseMaterial((Brush)Application.Current.Resources["Jellyfish"]);
DiffuseMaterial surface2Material = newDiffuseMaterial((Brush)Application.Current.Resources["Chrysanthemum"]);
DiffuseMaterial surface3Material = newDiffuseMaterial((Brush)Application.Current.Resources["Koala"]);
DiffuseMaterial surface4Material = newDiffuseMaterial((Brush)Application.Current.Resources["Penguins"]);
DiffuseMaterial surface5Material = newDiffuseMaterial((Brush)Application.Current.Resources["Tulips"]);
DiffuseMaterial surface6Material = newDiffuseMaterial((Brush)Application.Current.Resources["Desert"]);
surface2.Material = surface2Material;
surface6.Material = surface6Material;
surface1.Material = surface1Material;
surface3.Material = surface3Material;
surface4.Material = surface4Material;
surface5.Material = surface5Material;
mainWindow.Content = Viewport;
}
}
}
Anonymous User
27-Jun-2012srinivas dyasani
26-Jun-2012