質問

I am trying to draw text on to the screen using a spritefont in XNA. I can get the text to appear, but no matter what I try there is always something going wrong with something else when I draw the text.

I am trying to display the FPS. I have tried it in a few different ways and in various different XNA projects I have made. Some of the things that happen when drawing the text to the screen include - Vertices being drawn further away and in the wrong spots, vertices not being drawn at all, wire frame mode not being able to be turned on. Just depending on how I try to render the text I always end up with one of these. And then if I comment out the part that draws the text, everything will be back to normal.

Here is some code My variables at the top of the code are -

    GraphicsDeviceManager graphics;
    SpriteFont font;
    SpriteBatch spriteBatch;

In my LoadContent I have

        font = Content.Load<SpriteFont>("SpriteFont1");
        RasterizerState rs = new RasterizerState();
        rs.FillMode = FillMode.WireFrame;
        GraphicsDevice.RasterizerState = rs;
        spriteBatch = new SpriteBatch(GraphicsDevice);

And here is my entire Draw method -

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

        effect.Parameters["View"].SetValue(cam.viewMatrix);
        effect.Parameters["Projection"].SetValue(projectionMatrix);
        effect.CurrentTechnique = effect.Techniques["Technique1"];
        effect.CurrentTechnique.Passes[0].Apply();
        GraphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, vertices.Count, 0, indices.Length / 3);

        spriteBatch.Begin();
        spriteBatch.DrawString(font, frameRate.ToString(), new Vector2(1, 1), Color.Black);
        spriteBatch.End();

        frameCounter++;
        base.Draw(gameTime);
    }

This is the closest way I have foundto working yet, but for some reason it makes wireframe not work no matter what I try to do, and I really need wire frame.

EDIT: I debugged the fill mode as the program runs and it sets it back to solid on its own for some reason when using the sprite font. I can reset the the fill mode every frame before the vertices are drawn, but I'd really rather not have to do that.

役に立ちましたか?

解決

You should apply your rasterizer state just before drawing somthing win wireframe mode, not only in the load method... because batch.Begin() method will set a default rasterizer state

if you want use other rasterizer state for spritebatch, you should provide one

spritebatch.Begin(SortMode,...,,.., rs );

your code should be changed this way:

static RasterizerState rs = new RasterizerState() { FillMode = FillMode.Wireframe; }

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

    GraphicsDevice.RasterizerState = rs;

    effect.Parameters["View"].SetValue(cam.viewMatrix);
    effect.Parameters["Projection"].SetValue(projectionMatrix);
    effect.CurrentTechnique = effect.Techniques["Technique1"];
    effect.CurrentTechnique.Passes[0].Apply();
    GraphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, vertices.Count, 0, indices.Length / 3);
    ....

他のヒント

Using a SpriteBatch changes the render state to the point that 3D graphics no longer work unless you change the render state back to a 3D-friendly state. See this article for more details.

http://blogs.msdn.com/b/shawnhar/archive/2006/11/13/spritebatch-and-renderstates.aspx

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top