3D C#XNA模型在旋转之前不绘图(和透明度问题)

时间:2014-12-06 14:30:22

标签: c# model xna transparency draw

问题1:模型在旋转前不绘图: 所以这可能非常简单,我无法弄明白...... 我的模型绘制得很好,但只有在我旋转之后。否则不会画出来。 以下是我的代码的一些摘录:

public class Game1 : Microsoft.Xna.Framework.Game
{
    Model spaceShip;
    Matrix world;         //object world transformation
    SpaceShip player;

    Matrix view, proj;

    public override void Initialize()
    {
        view = Matrix.CreateLookAt(new Vector3(0, 30000, 0), Vector3.Zero, Vector3.Forward);
        proj = Matrix.CreatePerspectiveFieldOfView((float)Math.PI / 4.0f, (float)graphics.GraphicsDevice.Viewport.Width / (float)graphics.GraphicsDevice.Viewport.Height, 0.1f, 100000);
        //world = Matrix.CreateTranslation(Vector3.Zero);    //This isn't part of original code. I tried adding this here to see if it would solve the problem

        base.Initialize();

        player = new SpaceShip(spaceShip, world);
    }

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

        player.Update(gameTime);
    }
    protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.CornflowerBlue);

        //Fix transparency of ship
        GraphicsDevice.BlendState = BlendState.AlphaBlend;
        GraphicsDevice.DepthStencilState = DepthStencilState.None;
        GraphicsDevice.RasterizerState = RasterizerState.CullCounterClockwise;
        GraphicsDevice.SamplerStates[0] = SamplerState.LinearClamp;
        GraphicsDevice.BlendState = BlendState.Opaque;
        GraphicsDevice.DepthStencilState = DepthStencilState.Default;

        //Draw ship
        player.Draw(spriteBatch, view, proj);

        base.Draw(gameTime);
    }
}

太空飞船课程:

class Spaceship
{
    Model ship;
    Vector3 pos;
    Matrix world, translation, yRotation;
    float yYaw;             //value to rotate by
    float totalYRot;        //total rotation

    public SpaceShip(Model model, Matrix transformation)
    {
        ship = model;
        world = transformation;
        pos = Vector3.Zero;
        translation = Matrix.CreateTranslation(pos);
        yYaw = MathHelper.ToRadians(2);
    }

    public void Update(GameTime gameTime)
    {
        //KEYBOARD INPUTS
        //L/R
        if (Keyboard.GetState().IsKeyDown(Keys.Left))
        {
            totalYRot += yYaw;
            yRotation = Matrix.CreateRotationY(totalYRot);
        }
        if (Keyboard.GetState().IsKeyDown(Keys.Right))
        {
            totalYRot -= yYaw;
            yRotation = Matrix.CreateRotationY(totalYRot);
        }

        //Update world transformation
        world = yRotation * translation;
    }

    public Draw(SpriteBatch spriteBatch, Matrix view, Matrix proj)
    {
        foreach (ModelMesh mesh in ship.Meshes)
        {
            foreach (BasicEffect effect in mesh.Effects)
            {
                //Effect Settings Goes here
                effect.LightingEnabled = false;
                effect.World = world;
                effect.Projection = proj;
                effect.View = view;
            }
            mesh.Draw();
        }
    }
}

问题2:透明度问题: 从Game1.cs中的Draw()方法可以看出,我已经实现了一些我在stackoverflow上找到的代码来修复我的透明度问题。但是,我不明白为什么这是必要的。我在大学里,大多数同学都没有这个问题。我肯定在某个地方出了问题。

非常感谢。无法在这里找到解决方案,所以我决定写下这个问题。希望将来对其他人有用。

0 个答案:

没有答案