【练手小程序】C++/CLI纯代码编写的WPF 3D程序
acmilan2016/02/05软件综合 IP:天津
程序很简单,一个旋转的立方体。没有使用任何的XAML,纯代码编写。

运行界面:

wpf3d.png

下载:
attachment icon wpf3dcli.zip 1.66MB ZIP 25次下载

程序代码:
<code class="lang-cpp">// 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></code>
来自:计算机科学 / 软件综合
3
已屏蔽 原因:{{ notice.reason }}已屏蔽
{{notice.noticeContent}}
~~空空如也
acmilan 作者
8年4个月前 修改于 8年4个月前 IP:天津
807164
使用VS2008开发,使用新版本打开会进行版本升级,按提示操作即可。
程序需要.NET 3.5支持,可以自己添加XXXXXXnfig以兼容.NET 4.x,以便在Win8+免运行库,以前发过,这里不再赘述。
引用
评论
加载评论中,请稍候...
200字以内,仅用于支线交流,主线讨论请采用回复功能。
折叠评论
acmilan作者
8年4个月前 IP:江苏
808160
旋转动画除了使用DispatcherTimer实现之外,还可以使用DoubleAnimation或Rendering事件来实现。。。
引用
评论
加载评论中,请稍候...
200字以内,仅用于支线交流,主线讨论请采用回复功能。
折叠评论
acmilan作者
8年4个月前 修改于 8年4个月前 IP:江西
808165
使用DoubleAnimation实现动画:
<code class="lang-cpp">// 使用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);</code>
引用
评论
加载评论中,请稍候...
200字以内,仅用于支线交流,主线讨论请采用回复功能。
折叠评论

想参与大家的讨论?现在就 登录 或者 注册

所属专业
所属分类
上级专业
同级专业
acmilan
进士 学者 笔友
文章
461
回复
2934
学术分
4
2009/05/30注册,5年4个月前活动
暂无简介
主体类型:个人
所属领域:无
认证方式:邮箱
IP归属地:未同步
文件下载
加载中...
{{errorInfo}}
{{downloadWarning}}
你在 {{downloadTime}} 下载过当前文件。
文件名称:{{resource.defaultFile.name}}
下载次数:{{resource.hits}}
上传用户:{{uploader.username}}
所需积分:{{costScores}},{{holdScores}}下载当前附件免费{{description}}
积分不足,去充值
文件已丢失

当前账号的附件下载数量限制如下:
时段 个数
{{f.startingTime}}点 - {{f.endTime}}点 {{f.fileCount}}
视频暂不能访问,请登录试试
仅供内部学术交流或培训使用,请先保存到本地。本内容不代表科创观点,未经原作者同意,请勿转载。
音频暂不能访问,请登录试试
支持的图片格式:jpg, jpeg, png
插入公式
评论控制
加载中...
文号:{{pid}}
投诉或举报
加载中...
{{tip}}
请选择违规类型:
{{reason.type}}

空空如也

加载中...
详情
详情
推送到专栏从专栏移除
设为匿名取消匿名
查看作者
回复
只看作者
加入收藏取消收藏
收藏
取消收藏
折叠回复
置顶取消置顶
评学术分
鼓励
设为精选取消精选
管理提醒
编辑
通过审核
评论控制
退修或删除
历史版本
违规记录
投诉或举报
加入黑名单移除黑名单
查看IP
{{format('YYYY/MM/DD HH:mm:ss', toc)}}