1

我有什么:我的船相机类的游戏类模型类

当我按 F5 时,我的游戏打开了,它显示为蓝色,但我的模型没有出现,有人对为什么会发生这种情况有任何建议吗?

如果有人能够提供帮助,我将不胜感激。


    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 test1
    {
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Microsoft.Xna.Framework.Game
{
    GraphicsDeviceManager graphics;



    //Visual components
    Ship ship = new Ship();
    Camera _camera;

    public Game1()
    {
        graphics = new GraphicsDeviceManager(this);
        Content.RootDirectory = "Content";


    }

   protected override void Initialize()
    {
        this.graphics.ToggleFullScreen(); 
        this._camera = new Camera(graphics.GraphicsDevice.Viewport);
        this._camera.LookAt = new Vector3(0.0f, 0.0f, -1.0f);


        base.Initialize();
    }


    protected override void LoadContent()
    {
        ship.Model = Content.Load<Model>("Models/p1_wedge");
        ship.Transforms = _camera.SetupEffectDefaults(ship.Model);


    }


    protected override void UnloadContent()
    {
        // TODO: Unload any non ContentManager content here
    }


    protected override void Update(GameTime gameTime)
    {

        // Allows the game to exit
        if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed ||
            Keyboard.GetState().IsKeyDown(Keys.Escape))
            this.Exit();

        // Get some input.
        UpdateInput();



        base.Update(gameTime);
    }

    protected void UpdateInput()
    {
        // Get the game pad state.
        GamePadState currentState = GamePad.GetState(PlayerIndex.One);
        KeyboardState currentKeyState = Keyboard.GetState();

            ship.Update(currentState);

        }


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

        //camera
        this._camera.Update();

        _camera.LookAt = ship.Position;

        Matrix shipTransformMatrix = ship.RotationMatrix
                * Matrix.CreateTranslation(ship.Position);
        DrawModel(ship.Model, shipTransformMatrix, ship.Transforms);
        base.Draw(gameTime);

    }


    public static void DrawModel(Model model, Matrix modelTransform,
Matrix[] absoluteBoneTransforms)
    {
        //Draw the model, a model can have multiple meshes, so loop

        foreach (ModelMesh mesh in model.Meshes)
        {
            //This is where the mesh orientation is set
            foreach (BasicEffect effect in mesh.Effects)
            {

                effect.World =
                    absoluteBoneTransforms[mesh.ParentBone.Index] *
                    modelTransform;
            }

            mesh.Draw();


        }
    }


}
    }

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;


namespace test1
{
class Ship
{
    public Model Model;
    public Matrix[] Transforms;

    //Position of the model in world space
    public Vector3 Position = Vector3.Zero;

    //Velocity of the model, applied each frame to the model's position
    public Vector3 Velocity = Vector3.Zero;
    private const float VelocityScale = 5.0f;

    public Matrix RotationMatrix =
 Matrix.CreateRotationX(MathHelper.PiOver2);

    private float rotation;

    public float Rotation
    {
        get { return rotation; }
        set
        {
            float newVal = value;
            while (newVal >= MathHelper.TwoPi)
            {
                newVal -= MathHelper.TwoPi;
            }
            while (newVal < 0)
            {
                newVal += MathHelper.TwoPi;
            }

            if (rotation != value)
            {
                rotation = value;
                RotationMatrix =
                    Matrix.CreateRotationX(MathHelper.PiOver2) *
                    Matrix.CreateRotationZ(rotation);
            }

        }
    }

    public void Update(GamePadState controllerState)
    {

        KeyboardState currentKeyState = Keyboard.GetState();
        if (currentKeyState.IsKeyDown(Keys.A))
            Rotation += 0.10f;
        else
        // Rotate the model using the left thumbstick, and scale it down.
        Rotation -= controllerState.ThumbSticks.Left.X * 0.10f;

        if (currentKeyState.IsKeyDown(Keys.D))
            Rotation -= 0.10f;

        if (currentKeyState.IsKeyDown(Keys.W))
            Velocity += RotationMatrix.Forward * VelocityScale;
        else
        // Finally, add this vector to our velocity.
        Velocity += RotationMatrix.Forward * VelocityScale *
         controllerState.Triggers.Right;

        // In case you get lost, press A to warp back to the center.
        if  (currentKeyState.IsKeyDown(Keys.Enter))
        {
            Position = Vector3.Zero;
            Velocity = Vector3.Zero;
            Rotation = 0.0f;

        }

        Position += Velocity;
        Velocity *= 0.95f;
    }



}
}

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

namespace test1
{
public class Camera
{

    private Vector3 _position;
    private Vector3 _lookAt;
    private Matrix _viewMatrix;
    private Matrix _projectionMatrix;
    private float _aspectRatio;


public Camera(Viewport viewport)
{
    this._aspectRatio = ((float)viewport.Width) / ((float)viewport.Height);
    this._projectionMatrix = Matrix.CreatePerspectiveFieldOfView(
                                MathHelper.ToRadians(40.0f),
                                this._aspectRatio,
                                20000.0f,
                                30000.0f);
}
public Vector3 Position
{
    get { return this._position; }
    set { this._position = value; }
}
public Vector3 LookAt
{
    get { return this._lookAt; }
    set { this._lookAt = value; }
}
public Matrix ViewMatrix
{
    get { return this._viewMatrix; }
}
public Matrix ProjectionMatrix
{
    get { return this._projectionMatrix; }
}
public void Update()
{
    this._viewMatrix = 
        Matrix.CreateLookAt(this._position, this._lookAt, Vector3.Up);

}

public Matrix[] SetupEffectDefaults(Model myModel)
{
    Matrix[] absoluteTransforms = new Matrix[myModel.Bones.Count];
    myModel.CopyAbsoluteBoneTransformsTo(absoluteTransforms);

    foreach (ModelMesh mesh in myModel.Meshes)
    {
        foreach (BasicEffect effect in mesh.Effects)
        {
            effect.EnableDefaultLighting();
            effect.Projection = ProjectionMatrix;
            effect.View = ViewMatrix;
        }
    }
    return absoluteTransforms;
}

}
}

编辑

public static void DrawModel(Model model, Matrix modelTransform, 
Matrix[] absoluteBoneTransforms)
{
//Draw the model, a model can have multiple meshes, so loop
foreach (ModelMesh mesh in model.Meshes)
{
    //This is where the mesh orientation is set
    foreach (BasicEffect effect in mesh.Effects)
    {
        effect.World = 
            absoluteBoneTransforms[mesh.ParentBone.Index] * 
            modelTransform;
        effect.Projection = _camera.ProjectionMatrix;
        effect.View = _camera.ViewMatrix; 
    }


    //Draw the mesh, will use the effects set above.
    mesh.Draw();
}   
4

2 回答 2

1

我认为您的代码有两个问题:

Look At您似乎没有更新相机的 Look At 以指向您的宇宙飞船,要么通过给它一个引用来让您的相机“跟踪”宇宙飞船,并在Update更新时查看位置或手动更新外观从你的位置Game1

视图/投影矩阵即使您的相机具有视图矩阵和投影矩阵,您也不会在DrawModel. 分配世界时,请考虑添加以下两行

effect.Projection = _camera.ProjectionMatrix;
effect.View = _camera.ViewMatrix; 
于 2012-11-26T02:32:10.773 回答
0

您的代码中可能存在几个问题,使其无法按预期工作。两个突然出现在我面前的是 1.) 你从来没有设置相机位置,所以它默认为 0,0,0,它与船模型并置。因此,您的相机实际上位于模型内部,并且如果 2 出现在其视图中的任何三角形都将被剔除为背面三角形。)由于您的近平面设置为 20000,它还没有被截断截体。

于 2012-11-26T20:02:10.603 回答