本文通过参考网上资源做的一个例子。

本程序的功能就是通过xna 将3d 图像显示到winfrom 对他进行旋转操作。

首先我们先准备好两个文件夹

model  文件夹放fbx文件,textures 放渲染文件,操作步骤都是添加现有项,准备好资源文件后,先检查下是否有以下引用

下面将定义Game类的实现方法

//本代码以网上参考稍作修改,使用请注明非本人原创,因未知是谁原创特此声明非本人原创
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System.Runtime.Remoting.Messaging; namespace _3DModel
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Microsoft.Xna.Framework.Game
{ #region 变量
GraphicsDeviceManager graphics; //声明一个图形设备管理器//define a graphics manage
SpriteBatch spriteBatch; //声明一个精灵集群.可以以相同的设置操纵(draw)一组精灵//define a SpriteBatch
Model myModel; //定义一个Model//define a model
float aspectRatio; //屏幕高宽比,控制3D世界的视图怎样转换成屏幕上的2D图象(投射)用到//control the screen,make the 3d model show in 2d sreen
Vector3 modelPosition = Vector3.Zero; //模型在屏幕上的位置(世界坐标系),屏幕中心为坐标原点//define a world coordinate,the origin is the center of screen
//float modelRotationY = 0.0f; //模型旋转角度 //rotate the angle
//float modelRotationX = 0.0f; //模型旋转角度 //rotate the angle
/*******************************************************************************
* +Y
* | -Z
* | /
* | /
* | /
* | /
* (0,0,0) ------------+X
/*******************************************************************************/
Vector3 cameraPosition = new Vector3(1000.0f, 0.0f, 5000.0f); //摄像机(眼睛)的位置和方向//vidicon location #endregion 变量
System.Windows.Forms.Form xnaWindow; //保存原始的窗体对象
MainForm mainform;
public Game1(MainForm form)
{ graphics = new GraphicsDeviceManager(this);
graphics.PreferredBackBufferHeight = 760;
graphics.PreferredBackBufferWidth = 1200;
Content.RootDirectory = "Content";
//窗体对换
this.mainform = form;
graphics.PreparingDeviceSettings += new EventHandler<PreparingDeviceSettingsEventArgs>(graphics_PreparingDeviceSettings);
xnaWindow = (System.Windows.Forms.Form)System.Windows.Forms.Control.FromHandle((this.Window.Handle));
xnaWindow.GotFocus += new EventHandler(delegate(object sender, EventArgs e)
{
((System.Windows.Forms.Form)sender).Visible = false;
form.TopMost = false;
}); } public Game1()
{
} private void graphics_PreparingDeviceSettings(object sender, PreparingDeviceSettingsEventArgs e)
{
e.GraphicsDeviceInformation.PresentationParameters.DeviceWindowHandle = mainform.Panel.Handle;
} SpriteBatch sprites; protected override void Initialize()
{ // backgroundTexture = Content.Load<Texture2D>("akqm"); sprites = new SpriteBatch(graphics.GraphicsDevice); base.Initialize();
} protected override void LoadContent()
{ //spriteBatch = new SpriteBatch(GraphicsDevice);
//myModel = Content.Load<Model>("Models\\"+mainform.filename); //素材管道载入3D模型//download the 3d model
//aspectRatio = (float)graphics.GraphicsDevice.Viewport.Width /(float)graphics.GraphicsDevice.Viewport.Height;
loade();
// TODO: use this.Content to load your game content here
} public void loade()
{ spriteBatch = new SpriteBatch(GraphicsDevice);
myModel = Content.Load<Model>("Models\\" + mainform.filename); //素材管道载入3D模型//download the 3d model
aspectRatio = (float)graphics.GraphicsDevice.Viewport.Width / (float)graphics.GraphicsDevice.Viewport.Height; } protected override void UnloadContent()
{
// TODO: Unload any non ContentManager content here
} /// <summary>
///
/// </summary>
/// <param name="gameTime"></param> protected override void Update(GameTime gameTime)
{
// Allows the game to exi
GamePadState gamePad = GamePad.GetState(PlayerIndex.One); KeyboardState keyboard = Keyboard.GetState();
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || keyboard.IsKeyDown(Keys.Escape))
this.Exit();
if (keyboard.IsKeyDown (Keys.Right ))
Program.modelRotationY += (float)gameTime.ElapsedGameTime.TotalMilliseconds * MathHelper.ToRadians(0.1f);
else if (keyboard.IsKeyDown (Keys.Left ))
Program.modelRotationY -= (float)gameTime.ElapsedGameTime.TotalMilliseconds * MathHelper.ToRadians(0.1f);
else if (keyboard.IsKeyDown(Keys.Up ))
Program.modelRotationX -= (float)gameTime.ElapsedGameTime.TotalMilliseconds * MathHelper.ToRadians(0.1f);
else if (keyboard.IsKeyDown(Keys.Down ))
Program.modelRotationX += (float)gameTime.ElapsedGameTime.TotalMilliseconds * MathHelper.ToRadians(0.1f);
//if (mainform.filename != mainform.filename)
//{
// base.Update(gameTime);
//} base.Update(gameTime);
} /// <summary>
/// /
/// </summary>
/// <param name="gameTime"></param>
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.Black);
Matrix[] transforms = new Matrix[myModel.Bones.Count];
myModel.CopyAbsoluteBoneTransformsTo(transforms); // 绘制模型
foreach (ModelMesh mesh in myModel.Meshes) //遍历模型mesh// for() all the mesh
{
//BasicEffect类可以简单的通过设置属性,包含光照、纹理等等就可以在“五分钟”内实现对一个物体的呈现。
string aaa = mesh.Name;
//if (aaa.ToString().IndexOf("矩形")>0 ) {
foreach (BasicEffect effect in mesh.Effects)
{ effect.EnableDefaultLighting(); //光照 //light
effect.World = transforms[mesh.ParentBone.Index] * Matrix.CreateRotationY(Program.modelRotationY) * Matrix.CreateRotationX(Program.modelRotationX) * Matrix.CreateTranslation(modelPosition); //使用World矩阵来改变模型在世界坐标系中的位置//use the World Matrix change the world coordinate
effect.View = Matrix.CreateLookAt(cameraPosition, Vector3.Zero, Vector3.Up);
effect.Projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(60.0f), aspectRatio, 1.0f, 10000.0f);
//Color[] colors = new Color[3]; //for (int i = 0; i < 3; i++)
//{
// colors.SetValue(new Color(0, 1, 0), i);
//} } mesh.Draw();
//}
} //sprites.Begin();
//sprites.Draw(backgroundTexture, Vector2.Zero, Color.White);
//sprites.End();
base.Draw(gameTime);
}
}
}

然后进行  winfrom  的编写

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Windows.Forms; namespace _3DModel
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
} //xna form 接入
public Control Panel
{
get { return XnaPanel; }
} public string strRotate="";
private void bt_Up_MouseDown(object sender, MouseEventArgs e)
{
Button bt = (Button)sender;
strRotate = bt.Text;
timer1.Enabled = true;
} private void bt_Up_MouseUp(object sender, MouseEventArgs e)
{
strRotate = "";
timer1.Enabled = false ;
} private void timer1_Tick(object sender, EventArgs e)
{if(strRotate =="Up")
Program.modelRotationX += (float)(-0.03);
else if(strRotate =="Down")
Program.modelRotationX -= (float)(-0.03);
else if (strRotate == "Left")
Program.modelRotationY -= (float)(-0.03);
else if (strRotate == "Right")
Program.modelRotationY += (float)(-0.03);
} private void bt_Left_Click(object sender, EventArgs e)
{ }
public static MainForm form;
static Game1 game=new Game1(form); public void button1_Click(object sender, EventArgs e)
{
OpenFileDialog fileDialog = new OpenFileDialog(); game.Exit();
fileDialog.Title = "Load Model"; fileDialog.Filter = "Model Files (*.fbx;*.x)|*.fbx;*.x|" +
"FBX Files (*.fbx)|*.fbx|" +
"X Files (*.x)|*.x|" +
"All Files (*.*)|*.*"; if (fileDialog.ShowDialog() == DialogResult.OK)
{ string[] strName = fileDialog.SafeFileName.Split('.');
filename = strName[0];
game.loade();
} } public string filename = "1"; }
}

  效果如下

注:本文代码只做学习参考不得用作商业用途。违反引起的法律责任将由违反本声明的承担

3d xna fbx winfrom 读取的更多相关文章

  1. 【学习笔记】tensorflow图片读取

    目录 图像基本概念 图像基本操作 图像基本操作API 图像读取API 狗图片读取 CIFAR-10二进制数据读取 TFRecords TFRecords存储 TFRecords读取方法 图像基本概念 ...

  2. 基于FBX SDK的FBX模型解析与加载 -(三)

    http://blog.csdn.net/bugrunner/article/details/7229416 6. 加载Camera和Light 在FBX模型中除了几何数据外较为常用的信息可能就是Ca ...

  3. Python之TensorFlow的数据的读取与存储-2

    一.我们都知道Python由于GIL的原因导致多线程并不是真正意义上的多线程.但是TensorFlow在做多线程使用的时候是吧GIL锁放开了的.所以TensorFlow是真正意义上的多线程.这里我们主 ...

  4. 3D打印如何重组制造格局?

    ​全球化的竞争正变得毫无底线,国与国之间只有利益,没有同情,也就是说美国品牌想把自己的工厂移回本土,是不会考虑中国工人的生存现状的,更不会顾及这里的GDP和环境问题,甚至还会依靠经济能力去奴役其他国家 ...

  5. OpenSCAD:一款用于创建实体3D CAD对象的软件

    推荐:使用 NSDT场景设计器 快速搭建 3D场景. 原文链接:https://www.mvrlink.com/openscad/ OpenSCAD是一个用于创建实体3D CAD对象的软件.它是免费软 ...

  6. [Unity3D]Unity资料大全免费分享

     都是网上找的连七八糟的资料了,整理好分享的,有学习资料,视频,源码,插件……等等 东西比较多,不是所有的都是你需要的,可以按  ctrl+F 来搜索你要的东西,如果有广告,不用理会,关掉就可以了,如 ...

  7. Windows phone 8 学习笔记(1) 触控输入(转)

    Windows phone 8 的应用 与一般的Pc应用在输入方式上最大的不同就是:Windows phone 8主要依靠触控操作.因此在输入方式上引入一套全新的触控操作方式,我们需要重新定义相关的事 ...

  8. Windows phone 8 学习笔记(1) 触控输入

    原文:Windows phone 8 学习笔记(1) 触控输入 Windows phone 8 的应用 与一般的Pc应用在输入方式上最大的不同就是:Windows phone 8主要依靠触控操作.因此 ...

  9. WPS客户端更新日志留着备用

    WPS Office (10.1.0.7520)==========================================新增功能列表------------WPS文字1 拼写检查:新增“中 ...

  10. 16个富有创意的HTML5 Canvas动画特效集合

    HTML5技术正在不断的发展和更新,越来越多的开发者也正在加入HTML5阵营,甚至在移动开发上HTML5的地位也是越来越重要了.HTML5中的大部分动画都是通过Canvas实现,因为Canvas就像一 ...

随机推荐

  1. 硬件设计:逻辑电平--差分信号(PECL、LVDS、CML)电平匹配

    参考资料:逻辑电平设计规范 PECL电平匹配设计指南 CML信号与LVPECL信号的连接 硬件设计:逻辑电平--CML 硬件设计:逻辑电平--ECL/PECL/LVPECL 硬件设计:逻辑电平--LV ...

  2. 表治理-Iceberg元数据合并-metadata.json文件

    一.背景描述 元数据文件随时间增多,导致查询变慢.通过如下方式可以指定metadata个数,超过指定数量自动清理. metadata文件对应Iceberg概念是Snapshots 二.解决方案 1.在 ...

  3. IDEA中使用Yapi上传接口

    一.Idea下载插件YapiUpload 二.修改该项目的隐藏文件夹idea .idea文件下修改misc.xml增加如下配置  <component name="yapi" ...

  4. DeepSeek处理自有业务的案例:让AI给你写一份小众编辑器(EverEdit)的语法着色文件

    1 DeepSeek处理自有业务的案例:让AI给你写一份小众编辑器(EverEdit)的语法着色文件 1.1 背景   AI能力再强,如果不能在企业的自有业务上产生助益,那基本也是一无是处.将企业的自 ...

  5. QT5笔记:7. 自定义类、自定义信号及类的元对象信息

    自定义的QPerson类,需要继承 QObject类 qperson.h头文件 #ifndef QPERSON_H #define QPERSON_H #include <QObject> ...

  6. Netty - [01] 概述

    题记部分 一.介绍 Netty 是由JBOSS提供的一个Java开源框架,现为Github上的独立项目. Netty是一个异步的.基于事件驱动的网络应用框架,用以快速开发高性能.高可靠性的网络I/O程 ...

  7. Flink学习(六) 常用DataStreaming API

    曾经提到过,Flink 很重要的一个特点是"流批一体",然而事实上 Flink 并没有完全做到所谓的"流批一体",即编写一套代码,可以同时支持流式计算场景和批量 ...

  8. C#(面向对象的托管语言)类库(区别于应用程序)的异常处理思路

    1.不要做出任何应用程序才需要考虑抉择策略,不能想当然的决定一些错误情形.具体的一个体现形式是什么异常都捕获.这不是类库的职责,因为无法掌握所有的调用者的使用情形,这些不确定性是委托.虚方法.接口等特 ...

  9. 浅谈processing-java.exe应用程序的使用(与PowerShell的联合)

    简单总结一下processing-java.exe的使用,以及和PowerShell结合,如何互相调用和传参. Processing-java 这是 processing-java.exe 的官方说明 ...

  10. redux vs redux-toolkit 及源码实现

    我们是袋鼠云数栈 UED 团队,致力于打造优秀的一站式数据中台产品.我们始终保持工匠精神,探索前端道路,为社区积累并传播经验价值. 本文作者:霜序 前言 为何讲这个内容?以为后续大家会使用 redux ...