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

本程序的功能就是通过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. RowCellMenuCustomizations 实现 Command 绑定

    给GridControl的行数据单元格添加菜单项,并通过Command绑定的方式实现菜单点击动作触发: <dxg:GridControl x:Name="mainGrid" ...

  2. nginx出现: [error] open() "/usr/local/nginx/logs/nginx.pid" failed错误

    问题情况 登陆服务器之后进到nginx使用./nginx -s reload重新读取配置文件,发现报==nginx: [error] open() "/usr/local/nginx/log ...

  3. Android Service后台服务进程意外被kill掉之后如何重启

    Service组件在android开发中经常用到,经常作为后台服务,需要一直保持运行,负责处理一些不必展示的任务.而一些安全软件,会有结束进程的功能,如果不做Service的保持,就会被其杀掉. 那么 ...

  4. Qt QString的格式化与QString的拼接

    1. QString 与 QString 直接用 + 号也可以 QString date = "昨晚"; QString msg = "你真棒": QStrin ...

  5. Markdown 语法深度详解与实战演示

    一.引言 在当今数字化的时代,高效地处理和呈现文本信息变得至关重要.Markdown 作为一种轻量级标记语言,因其简洁.易读.易写的特点,受到了广大开发者.写作者和内容创作者的喜爱.无论您是撰写博客. ...

  6. Go实现动态开点线段树

    1.线段树介绍 线段树是一种用于高效处理区间查询和区间更新的数据结构,当我们需要解决一个频繁更新区间值的问题的时候,就可以采用线段树的结构进行解决.线段树的核心思想是将区间分为多个子区间进行管理,越往 ...

  7. DW - 问题

    数据库三范式 1NF(First Normal Form):一个关系模式符合 1NF 的定义,则该关系模式是简单的.简单的意思就是不存在从属或重复的属性,即每个属性都是原子性的. 2NF(Second ...

  8. centos 8 编译*.cpp文件

    1.安装g++ yum -y install gcc-c++ 2.编译*.cpp文件 g++ -o test_app_name test_source_file.cpp 3.运行编译结果 ./test ...

  9. DeFi(去中心化金融)的硬核知识

    1. ​DeFi流动性挖矿:躺着赚利息的"矿工"​ 简单来说,流动性挖矿就像你往银行存钱赚利息,但这里存的是加密货币,利息更高,还能随时提现.比如你往Uniswap这样的去中心化交 ...

  10. Visual Studio 好用的主题+字体推荐!!!

    Vs2022主题+字体 Visual Studio(VS)是一款功能强大的集成开发环境(IDE),可以用于开发各种类型的应用程序,包括桌面应用.Web应用.移动应用等.它提供了许多主题设置和字体选项, ...