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

本程序的功能就是通过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. 我喜欢 amy 同学的腿……

    1.我是人间之屑,这点我先承认大约不会错,但并不代表我的一切都是 hitonokuzu 为了防止对成语的吴勇(迫真),这就是人渣的意思. 2.看来亚里士多德的观点还是没有过时,老祖宗说的对.老祖宗指的 ...

  2. Luogu P11233 CSP-S2024 染色 题解 [ 蓝 ] [ 线性 dp ] [ 前缀和优化 ]

    染色:傻逼题. 赛时没切染色的都是唐氏!都是唐氏!都是唐氏!都是唐氏!都是唐氏!都是唐氏!都是唐氏! 包括我. 真的太傻逼了这题. 我今晚心血来潮一打这题,随便优化一下,就 AC 了. 怎么做到这么蠢 ...

  3. getDerivedStateFromProps 详解

    getDerivedStateFromProps 是 React 生命周期中的一个静态方法,主要用于在组件接收到新的 props 时更新 state.这个方法在组件的初始渲染和后续的每次更新(即每次接 ...

  4. 【ABP】项目示例(2)——聚合根和实体

    聚合根和实体 在上一章节中,已经完成了项目搭建的前置准备,在这一章节中,实现领域层的聚合根和实体 创建名称为General.Backend.Domain的标准类库,分别新建名称为Entities.Se ...

  5. [CERC2014] Parades 题解

    感觉长脑子了. 考虑在路线两端点的 \(lca\) 计算贡献,那么线段可以分两类: \(u\) 为 \(v\) 祖先. \(u,v\) 互不为祖先. 设 \(dp_i\) 表示只考虑 \(i\) 子树 ...

  6. [Ynoi2015] 盼君勿忘 题解

    CSP 前学习珂学,祝自己 \(while(1)\ rp++\). 考虑求解出每种数对答案的贡献. 设 \(t=r-l+1,k_x=\sum\limits_{i=l}^r [a_i=x]\),由容斥得 ...

  7. nacos(七): gateway(单体)

    这篇文章将从gateway的搭建.自动路由匹配.路由数组.跨域和路由过滤器五个方面对gateway项目展开讨论. 1.gateway的搭建 gateway的项目基本的搭建过程与消费者的搭建过程基本一致 ...

  8. 【攻防世界】wzsc_文件上传

    wzsc_文件上传 题目来源 攻防世界 NO.GFSJ0997 题目描述 经典上传页面 用御剑扫出upload文件夹 /upload路径下是上传的文件 题解 新建几个空文件,发现后缀为txt的文件可以 ...

  9. 单元测试三部曲-AAA模式

    AAA 指的是 "Arrange, Act, Assert",这是一种通用的单元测试模式. 在测试方法中, 1.首先对测试对象进行准备(Arrange), 2.然后调用要测试的方法 ...

  10. Redmine 中,如何新增一个字段名,比如"模块名称":

    why: 用于编写测试报告时能够直接根据模块名称进行统计,不对excel 表格进行自定义拆分-----规范性 登录到 Redmine 平台,并进入你的项目页面. 在项目页面上方的导航栏中,点击 &qu ...