使用VS2008开发,使用新版本打开会进行版本升级,按提示操作即可。
程序需要.NET 3.5支持,可以自己添加XXXXXXnfig以兼容.NET 4.x,以便在Win8+免运行库,以前发过,这里不再赘述。
程序需要.NET 3.5支持,可以自己添加XXXXXXnfig以兼容.NET 4.x,以便在Win8+免运行库,以前发过,这里不再赘述。
// wpf3dcli.cpp: 主项目文件。
#include "stdafx.h"
#pragma comment(linker, "/SUBSYSTEM:WINDOWS /ENTRY:main")
using namespace System;
using namespace System::Windows;
using namespace System::Windows::Controls;
using namespace System::Windows::Media;
using namespace System::Windows::Media::Media3D;
using namespace System::Windows::Threading;
// 需要添加以下三个引用:
// PresentationCore
// PresentationFramework
// WindowBase
ref class MainWindow : public Window
{
public:
AxisAngleRotation3D ^rotation;
DispatcherTimer ^timer;
void timer_Tick(Object ^sender, EventArgs ^args)
{
// 每次自动转动18度
rotation->Angle += Math::PI * 0.1;
}
MainWindow()
{
this->Width = 500;
this->Height = 500;
this->Title = L"WPF 3D 示例";
// Window
// +-Viewport3D --------------------------------------------- 三维视口
// +-Camera=OrthographicCamera --------------------------- 使用平行投影
// +-ModelVisual3D [light] ------------------------------- 灯光
// | +-AmbientLight ------------------------------------- 围绕灯光
// +-ModelVisual3D [cube] -------------------------------- 立方体
// +-Transform=RotateTransform3D ---------------------- 使用旋转变换
// | +-Rotation=AxisAngelRotation3D -------- 轴-角旋转(用于随Timer自动旋转)
// +-Model3DGroup ------------------------------------- 三维几何体的组合
// +-GeometryModel3D (*3) -------------------------- 三维几何体*3(表示三个相对侧面)
// +-Material=DiffuseMaterial ------------------- 散射材质
// | +-Brush=LinearGradientBrush ------ 渐变画刷
// +-MeshGeometry3D ----------------------------- 坐标网格
// +-Position=... ---------------------------- 顶点数据
// +-TriangleIndices=... --------------------- 三角面声明(应符合右手螺旋定则)
// +-TextureCoordinates=... ------------------ 纹理坐标映射(对渐变画刷很重要)
// 创建3D视口控件
Viewport3D ^viewport = gcnew Viewport3D();
this->Content = viewport;
// 使用平行投影的摄像机
viewport->Camera = gcnew OrthographicCamera(Point3D(0, 5, 5), Vector3D(0, -1, -1), Vector3D(0, 1, 0), 5);
// 添加一个围绕灯光(AmbientLight)
ModelVisual3D ^light = gcnew ModelVisual3D();
light->Content = gcnew AmbientLight();
viewport->Children->Add(light);
// 添加一个立方体
ModelVisual3D ^cube = gcnew ModelVisual3D();
viewport->Children->Add(cube);
// 建立旋转对象(Timer中用来实现立方体的旋转),并设置立方体的旋转变换
rotation = gcnew AxisAngleRotation3D(Vector3D(0, 1, 0), 0);
cube->Transform = gcnew RotateTransform3D(rotation);
// 立方体的内容为三维几何体的组合
Model3DGroup ^cubegroup = gcnew Model3DGroup();
cube->Content = cubegroup;
// 将要重复使用的变量
GeometryModel3D ^dblrect; // 立方体相对的侧面
MeshGeometry3D ^geometry; // 立方体相对的侧面的坐标网格
// 相对侧面的三角面声明(应符合右手螺旋定则)
Int32Collection ^index = gcnew Int32Collection();
index->Add(0);
index->Add(1);
index->Add(2);
index->Add(0);
index->Add(2);
index->Add(3);
index->Add(4);
index->Add(6);
index->Add(5);
index->Add(4);
index->Add(7);
index->Add(6);
// 侧面纹理的坐标映射(对渐变画刷很重要)
PointCollection ^txtcoord = gcnew PointCollection();
txtcoord->Add(Point(1, 0));
txtcoord->Add(Point(1, 1));
txtcoord->Add(Point(0, 1));
txtcoord->Add(Point(0, 0));
txtcoord->Add(Point(1, 0));
txtcoord->Add(Point(1, 1));
txtcoord->Add(Point(0, 1));
txtcoord->Add(Point(0, 0));
// 第一个相对侧面(渐变红蓝)
dblrect = gcnew GeometryModel3D();
dblrect->Material = gcnew DiffuseMaterial(gcnew LinearGradientBrush(Colors::Red, Colors::Blue, Point(0, 0), Point(1, 1))); // 设置渐变画刷
cubegroup->Children->Add(dblrect);
// 第一个相对侧面的坐标网格
geometry = gcnew MeshGeometry3D();
geometry->Positions = gcnew Point3DCollection(); // 设置顶点
geometry->Positions->Add(Point3D(1, 1, 1));
geometry->Positions->Add(Point3D(-1, 1, 1));
geometry->Positions->Add(Point3D(-1, -1, 1));
geometry->Positions->Add(Point3D(1, -1, 1));
geometry->Positions->Add(Point3D(1, 1, -1));
geometry->Positions->Add(Point3D(-1, 1, -1));
geometry->Positions->Add(Point3D(-1, -1, -1));
geometry->Positions->Add(Point3D(1, -1, -1));
geometry->TriangleIndices = index; // 设置三角面声明
geometry->TextureCoordinates = txtcoord; // 设置纹理坐标映射
dblrect->Geometry = geometry;
// 第二个相对侧面(渐变绿蓝)
dblrect = gcnew GeometryModel3D();
dblrect->Material = gcnew DiffuseMaterial(gcnew LinearGradientBrush(Colors::Green, Colors::Blue, Point(0, 0), Point(1, 1)));
cubegroup->Children->Add(dblrect);
// 第二个相对侧面的坐标网格
geometry = gcnew MeshGeometry3D();
geometry->Positions = gcnew Point3DCollection();
geometry->Positions->Add(Point3D(1, 1, 1));
geometry->Positions->Add(Point3D(1, 1, -1));
geometry->Positions->Add(Point3D(-1, 1, -1));
geometry->Positions->Add(Point3D(-1, 1, 1));
geometry->Positions->Add(Point3D(1, -1, 1));
geometry->Positions->Add(Point3D(1, -1, -1));
geometry->Positions->Add(Point3D(-1, -1, -1));
geometry->Positions->Add(Point3D(-1, -1, 1));
geometry->TriangleIndices = index;
geometry->TextureCoordinates = txtcoord;
dblrect->Geometry = geometry;
// 第三个相对侧面(渐变红绿)
dblrect = gcnew GeometryModel3D();
dblrect->Material = gcnew DiffuseMaterial(gcnew LinearGradientBrush(Colors::Red, Colors::Green, Point(0, 0), Point(1, 1)));
cubegroup->Children->Add(dblrect);
// 第三个相对侧面的坐标网格
geometry = gcnew MeshGeometry3D();
geometry->Positions = gcnew Point3DCollection();
geometry->Positions->Add(Point3D(1, 1, 1));
geometry->Positions->Add(Point3D(1, -1, 1));
geometry->Positions->Add(Point3D(1, -1, -1));
geometry->Positions->Add(Point3D(1, 1, -1));
geometry->Positions->Add(Point3D(-1, 1, 1));
geometry->Positions->Add(Point3D(-1, -1, 1));
geometry->Positions->Add(Point3D(-1, -1, -1));
geometry->Positions->Add(Point3D(-1, 1, -1));
geometry->TriangleIndices = index;
geometry->TextureCoordinates = txtcoord;
dblrect->Geometry = geometry;
// 使用Timer进行自动旋转
timer = gcnew DispatcherTimer();
timer->Interval = TimeSpan(10000);
timer->Tick += gcnew EventHandler(this, &MainWindow::timer_Tick);
timer->Start();
}
};
[STAThreadAttribute]
int main(array<system::string ^> ^args)
{
Application ^app = gcnew Application();
app->MainWindow = gcnew MainWindow();
app->MainWindow->Show();
app->Run();
return 0;
}</system::string>
// 使用DoubleAnimation进行自动旋转
DoubleAnimation ^anim = gcnew DoubleAnimation();
//anim->IsCumulative = true;
anim->From = 0;
anim->To = 360;
anim->Duration = Duration(TimeSpan(0, 0, 6));
anim->RepeatBehavior = RepeatBehavior::Forever;
rotation->BeginAnimation(rotation->AngleProperty, anim);
200字以内,仅用于支线交流,主线讨论请采用回复功能。