3d 新华社动画版

3d 新华社动画版,3d,xna,3d,Xna,我想画3轴X Y Z和一个模型 看看它在空间中的位置和移动方式 当我用简单的旋转画立方体时 一切都好 但当我添加轴绘制方法时 撕裂不正常 using System; using System.Collections.Generic; using System.Linq; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Audio; using Microsoft.Xna.Framework.Content; using M

我想画3轴X Y Z和一个模型 看看它在空间中的位置和移动方式

当我用简单的旋转画立方体时 一切都好

但当我添加轴绘制方法时

撕裂不正常

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace WindowsGame2
{

    public class Game1 : Microsoft.Xna.Framework.Game
    {
    //Some declaration 
    float x = 30f;
    float y = 0f;
    float z = 30f;

    //GraphicsDevice device;
    VertexBuffer vertexBuffer;
    VertexPositionColor[] lines;

    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;
    Model model;
    Matrix view;
    Matrix progection;
    float xTans = 0.0f;
    float yTans = 0.0f;
    float zTans = 0.0f;
    public Game1()
    {
        graphics = new GraphicsDeviceManager(this);
        Content.RootDirectory = "Content";
    }

    protected override void Initialize()
    {

        //device = graphics.GraphicsDevice;
        base.Initialize();
        this.IsMouseVisible = true;
    }

    protected override void LoadContent()
    {
        // Create a new SpriteBatch, which can be used to draw textures.

        spriteBatch = new SpriteBatch(GraphicsDevice);
        model = Content.Load<Model>("cube");

        DrawLines();

        createCamera();

    }

    protected override void Update(GameTime gameTime)
    {
        // Allows the game to exit
        if (GamePad.GetState(PlayerIndex.One).Buttons.Back == 
                ButtonState.Pressed)
            this.Exit();

        // TODO: Add your update logic here

        base.Update(gameTime);

        //update camera
    }

    protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.Azure);

        foreach (ModelMesh mesh in model.Meshes)
        {
            foreach (BasicEffect fx in mesh.Effects)
            {
                fx.View = view;

                //fx.World = Matrix.CreateRotationX(xTans);
                fx.World = Matrix.CreateRotationY(yTans);
                //fx.World = Matrix.CreateRotationZ(zTans);

                fx.Projection = progection;
                fx.EnableDefaultLighting();

            }
            mesh.Draw();

        }
        //xTans += 0.01f;
        yTans += 0.01f;
        //zTans += 0.01f;

        BasicEffect effect = new BasicEffect(graphics.GraphicsDevice);
        effect.VertexColorEnabled = true;

        effect.View = view;
        effect.Projection = progection;

        effect.World = Matrix.Identity;

        foreach (EffectPass pass in effect.CurrentTechnique.Passes)
        {
            // Begin Drawing
            pass.Apply();
            // Set the vertex buffer to graphic device
            graphics.GraphicsDevice.SetVertexBuffer(vertexBuffer, 0);
            // Draw the axes with LineList Type
            graphics.GraphicsDevice.DrawUserPrimitives<VertexPositionColor>(
            PrimitiveType.LineList, lines, 0, 3);
            //pass.Apply();
        }
        base.Draw(gameTime);
    }

    protected void DrawLines()
    {
        lines = new VertexPositionColor[6];

        //Setting Size and Width and Color of the X axis
        lines[0] = new VertexPositionColor(new Vector3(50, 0, 0), Color.Red);
        lines[1] = new VertexPositionColor(new Vector3(-50, 0, 0), Color.Red);

        //Setting Size and Width and Color of the Y axis
        lines[2] = new VertexPositionColor(new Vector3(0, 50, 0), Color.Green);
        lines[3] = new VertexPositionColor(new Vector3(0, -50, 0), Color.Green);

        //Setting Size and Width and Color of the Z axis
        lines[4] = new VertexPositionColor(new Vector3(0, 0, 50), Color.Blue);
        lines[5] = new VertexPositionColor(new Vector3(0, 0, -50), Color.Blue);

        vertexBuffer = new VertexBuffer(graphics.GraphicsDevice,
            VertexPositionColor.VertexDeclaration, 6, BufferUsage.None);
        vertexBuffer.SetData<VertexPositionColor>(lines);
    }

    protected void createCamera()
    {
        view = Matrix.CreateLookAt(
            new Vector3(0f, 60.0f, 60f), 
            Vector3.Zero, 
            Vector3.Up);
        progection = Matrix.CreatePerspectiveFieldOfView(
            MathHelper.ToRadians(8.0f), 
            graphics.GraphicsDevice.Viewport.AspectRatio, 
            10.0f, 
            1000.0f);
    }        
} //end game class
} //end namespace
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用Microsoft.Xna.Framework;
使用Microsoft.Xna.Framework.Audio;
使用Microsoft.Xna.Framework.Content;
使用Microsoft.Xna.Framework.GamerServices;
使用Microsoft.Xna.Framework.Graphics;
使用Microsoft.Xna.Framework.Input;
使用Microsoft.Xna.Framework.Media;
命名空间WindowsGame2
{
公共类游戏1:Microsoft.Xna.Framework.Game
{
//一些声明
浮动x=30f;
浮动y=0f;
浮动z=30f;
//图形设备;
顶点缓冲区顶点缓冲区;
顶点颜色[]行;
图形管理器图形;
SpriteBatch SpriteBatch;
模型;
矩阵视图;
矩阵投影;
浮动xTans=0.0f;
浮动yTans=0.0f;
浮动zTans=0.0f;
公共游戏1()
{
graphics=新的GraphicsDeviceManager(此);
Content.RootDirectory=“Content”;
}
受保护的覆盖无效初始化()
{
//设备=graphics.GraphicsDevice;
base.Initialize();
this.IsMouseVisible=true;
}
受保护的覆盖void LoadContent()
{
//创建一个新的SpriteBatch,可用于绘制纹理。
spriteBatch=新spriteBatch(图形设备);
模型=Content.Load(“多维数据集”);
抽绳();
createCamera();
}
受保护覆盖无效更新(游戏时间游戏时间)
{
//允许游戏退出
if(GamePad.GetState(PlayerIndex.One).Buttons.Back==
按钮状态(按下)
这是Exit();
//TODO:在此处添加更新逻辑
更新(游戏时间);
//更新摄像机
}
受保护覆盖无效绘制(游戏时间游戏时间)
{
图形设备。清晰(颜色为蓝色);
foreach(model.mesh中的ModelMesh网格)
{
foreach(网格效果中的基本效果fx)
{
视图=视图;
//fx.World=Matrix.CreateRotationX(xTans);
fx.World=Matrix.CreateRotationY(yTans);
//fx.World=Matrix.CreateRotationZ(zTans);
fx.预测=预测;
fx.EnableDefaultLighting();
}
mesh.Draw();
}
//xTans+=0.01f;
yTans+=0.01f;
//zTans+=0.01f;
基本效果=新的基本效果(graphics.GraphicsDevice);
effect.VertexColorEnabled=真;
效果。视图=视图;
效果:投影=投影;
效果。世界=矩阵。同一性;
foreach(EffectPass-in-effect.currentTechnology.passs)
{
//开始画画
pass.Apply();
//将顶点缓冲区设置为图形设备
graphics.GraphicsDevice.SetVertexBuffer(vertexBuffer,0);
//使用线列表类型绘制轴
graphics.GraphicsDevice.DrawUserPrimitives(
PrimitiveType.LineList,行,0,3);
//pass.Apply();
}
基础。抽签(游戏时间);
}
受保护的空心抽绳()
{
线条=新的顶点曝光颜色[6];
//设置X轴的大小、宽度和颜色
线条[0]=新的顶点曝光颜色(新的矢量3(50,0,0),颜色为红色);
线条[1]=新的顶点曝光颜色(新矢量3(-50,0,0),颜色为红色);
//设置Y轴的大小、宽度和颜色
线条[2]=新的顶点曝光颜色(新的向量3(0,50,0),颜色为绿色);
线条[3]=新的顶点曝光颜色(新的向量3(0,-50,0),颜色为绿色);
//设置Z轴的大小、宽度和颜色
线条[4]=新的顶点曝光颜色(新的矢量3(0,0,50),颜色为.Blue);
线条[5]=新的顶点曝光颜色(新的向量3(0,0,-50),颜色为.Blue);
vertexBuffer=新的vertexBuffer(graphics.GraphicsDevice,
VertexDeclaration,6,BufferUsage.None);
vertexBuffer.SetData(行);
}
受保护的void createCamera()
{
view=Matrix.CreateLookAt(
新矢量3(0f,60.0f,60f),
向量3,零,
向量3.向上);
Projection=Matrix.CreatePerspectiveFieldOfView(
托拉迪安(8.0f),
graphics.GraphicsDevice.Viewport.AspectRatio,
10.0f,
1000.0华氏度);
}        
}//结束游戏类
}//结束命名空间
绘制轴的函数是drawLines()

在方法draw()中

我有这个来画斧子

BasicEffect effect = new BasicEffect(graphics.GraphicsDevice);
effect.VertexColorEnabled = true;

effect.View = view;
effect.Projection = progection;

effect.World = Matrix.Identity;

foreach (EffectPass pass in effect.CurrentTechnique.Passes)
{
    // Begin Drawing
    pass.Apply();
    // Set the vertex buffer to graphic device
    graphics.GraphicsDevice.SetVertexBuffer(vertexBuffer, 0);
    // Draw the axes with LineList Type
    graphics.GraphicsDevice.DrawUserPrimitives<VertexPositionColor>(
    PrimitiveType.LineList, lines, 0, 3);
    //pass.Apply();
}
basicfect效果=新的basicfect(graphics.GraphicsDevice);
effect.VertexColorEnabled=真;
效果。视图=视图;
效果:投影=投影;
效果。世界=矩阵。同一性;
foreach(EffectPass-in-effect.currentTechnology.passs)
{
//开始画画
pass.Apply();
//将顶点缓冲区设置为图形设备
graphics.GraphicsDevice.SetVertexBuffer(vertexBuffer,0);
//使用线列表类型绘制轴
graphics.GraphicsDevice.DrawUserPrimitives(
PrimitiveType.LineList,行,0,3);
//pass.Apply();
}

我不知道为什么旋转的速度慢

有好消息,也有坏消息。好消息是您的项目可以顺利运行。你发布了足够的代码来构建你的项目,只要构建者像我一样有一个fbx模型。这是必要的改变

// take this out of your Draw() method.
BasicEffect effect = new BasicEffect(graphics.GraphicsDevice);

// put this in your class variables
BasicEffect effect;

//and put this at the end of LoadContent()
effect = new BasicEffect(graphics.GraphicsDevice);
在文章的末尾解释

坏消息是你有一大堆不好的代码。它会编译,也会运行,但不会很好地运行。我假设您对编写XNA项目有点陌生,这意味着您可能对游戏程序的组件缺乏了解

虽然我要去
protected override void LoadContent()
    ...
    DrawLines();
    ...
}
protected void DrawLines()
{
    lines = new VertexPositionColor[6];
    lines[0] = new VertexPositionColor(new Vector3(50, 0, 0), Color.Red);
    // ...

    vertexBuffer = new VertexBuffer(graphics.GraphicsDevice,
        VertexPositionColor.VertexDeclaration, 6, BufferUsage.None);
    vertexBuffer.SetData<VertexPositionColor>(lines);
    ...
graphics.GraphicsDevice.SetVertexBuffer(vertexBuffer, 0);