Monogame不绘制顶点缓冲立方体,只绘制GraphicsDevice.Clear颜色

时间:2013-09-03 12:35:09

标签: c# xna monogame

我的代码在Windows Phone 7和8上完美运行,但它在Monogame for Windows Store 8上完全没有用。

代码在Windows 8上绘制 nothing ,但graphicsDevice.Clear的颜色除外。

当我进入游戏状态时,这个问题就开始出现了,游戏画出的菜单很好,但是菜单绘图代码与我所包含的没有什么不同。

我设置块的代码

   // Calculate the position of the vertices on the top face.
        Vector3 topLeftFront = position + new Vector3(size.X, size.Y, size.Z);
        Vector3 topLeftBack = position + new Vector3(0,size.Y, size.Z);
        Vector3 topRightFront = position + new Vector3(size.X, size.Y, 0);
        Vector3 topRightBack = position + new Vector3(0,size.Y,0);

        // Calculate the position of the vertices on the bottom face.
        Vector3 btmLeftFront = position + new Vector3(size.X, 0, size.Z);
        Vector3 btmLeftBack = position + new Vector3(0, 0, size.Z);
        Vector3 btmRightFront = position + new Vector3(size.X,0,0);
        Vector3 btmRightBack = position; 

        // Normal vectors for each face (needed for lighting / display)
        Vector3 normalFront = new Vector3(0.0f, 0.0f, 1.0f) * size;
        Vector3 normalBack = new Vector3(0.0f, 0.0f, -1.0f) * size;
        Vector3 normalTop = new Vector3(0.0f, 1.0f, 0.0f) * size;
        Vector3 normalBottom = new Vector3(0.0f, -1.0f, 0.0f) * size;
        Vector3 normalLeft = new Vector3(-1.0f, 0.0f, 0.0f) * size;
        Vector3 normalRight = new Vector3(1.0f, 0.0f, 0.0f) * size;

        // UV texture coordinates
        Vector2 textureTopLeft = new Vector2(1.0f , 0.0f );
        Vector2 textureTopRight = new Vector2(0.0f , 0.0f );
        Vector2 textureBottomLeft = new Vector2(1.0f, 1.0f);
        Vector2 textureBottomRight = new Vector2(0.0f, 1.0f);

        // Add the vertices for the FRONT face.
        newVertices[0] = new VertexPositionNormalTexture(topLeftFront, normalFront, textureTopLeft);
        newVertices[1] = new VertexPositionNormalTexture(btmLeftFront, normalFront, textureBottomLeft);
        newVertices[2] = new VertexPositionNormalTexture(topRightFront, normalFront, textureTopRight);
        newVertices[3] = new VertexPositionNormalTexture(btmLeftFront, normalFront, textureBottomLeft);
        newVertices[4] = new VertexPositionNormalTexture(btmRightFront, normalFront, textureBottomRight);
        newVertices[5] = new VertexPositionNormalTexture(topRightFront, normalFront, textureTopRight);

        // Add the vertices for the BACK face.
        newVertices[6] = new VertexPositionNormalTexture(topLeftBack, normalBack, textureTopRight);
        newVertices[7] = new VertexPositionNormalTexture(topRightBack, normalBack, textureTopLeft);
        newVertices[8] = new VertexPositionNormalTexture(btmLeftBack, normalBack, textureBottomRight);
        newVertices[9] = new VertexPositionNormalTexture(btmLeftBack, normalBack, textureBottomRight);
        newVertices[10] = new VertexPositionNormalTexture(topRightBack, normalBack, textureTopLeft);
        newVertices[11] = new VertexPositionNormalTexture(btmRightBack, normalBack, textureBottomLeft);

        // Add the vertices for the TOP face.
        newVertices[12] = new VertexPositionNormalTexture(topLeftFront, normalTop, textureBottomLeft);
        newVertices[13] = new VertexPositionNormalTexture(topRightBack, normalTop, textureTopRight);
        newVertices[14] = new VertexPositionNormalTexture(topLeftBack, normalTop, textureTopLeft);
        newVertices[15] = new VertexPositionNormalTexture(topLeftFront, normalTop, textureBottomLeft);
        newVertices[16] = new VertexPositionNormalTexture(topRightFront, normalTop, textureBottomRight);
        newVertices[17] = new VertexPositionNormalTexture(topRightBack, normalTop, textureTopRight);

        // Add the vertices for the BOTTOM face. 
        newVertices[18] = new VertexPositionNormalTexture(btmLeftFront, normalBottom, textureTopLeft);
        newVertices[19] = new VertexPositionNormalTexture(btmLeftBack, normalBottom, textureBottomLeft);
        newVertices[20] = new VertexPositionNormalTexture(btmRightBack, normalBottom, textureBottomRight);
        newVertices[21] = new VertexPositionNormalTexture(btmLeftFront, normalBottom, textureTopLeft);
        newVertices[22] = new VertexPositionNormalTexture(btmRightBack, normalBottom, textureBottomRight);
        newVertices[23] = new VertexPositionNormalTexture(btmRightFront, normalBottom, textureTopRight);

        // Add the vertices for the LEFT face.
        newVertices[24] = new VertexPositionNormalTexture(topLeftFront, normalLeft, textureTopRight);
        newVertices[25] = new VertexPositionNormalTexture(btmLeftBack, normalLeft, textureBottomLeft);
        newVertices[26] = new VertexPositionNormalTexture(btmLeftFront, normalLeft, textureBottomRight);
        newVertices[27] = new VertexPositionNormalTexture(topLeftBack, normalLeft, textureTopLeft);
        newVertices[28] = new VertexPositionNormalTexture(btmLeftBack, normalLeft, textureBottomLeft);
        newVertices[29] = new VertexPositionNormalTexture(topLeftFront, normalLeft, textureTopRight);

        // Add the vertices for the RIGHT face. 
        newVertices[30] = new VertexPositionNormalTexture(topRightFront, normalRight, textureTopLeft);
        newVertices[31] = new VertexPositionNormalTexture(btmRightFront, normalRight, textureBottomLeft);
        newVertices[32] = new VertexPositionNormalTexture(btmRightBack, normalRight, textureBottomRight);
        newVertices[33] = new VertexPositionNormalTexture(topRightBack, normalRight, textureTopRight);
        newVertices[34] = new VertexPositionNormalTexture(topRightFront, normalRight, textureTopLeft);
        newVertices[35] = new VertexPositionNormalTexture(btmRightBack, normalRight, textureBottomRight);

我绘制块的代码

public void Draw(Camera camera)
    {
        effect.View = camera.View;
        effect.Projection = camera.Projection;
        effect.World = rotationMatrix;
       // effect.EnableDefaultLighting();

        foreach (EffectPass pass in effect.CurrentTechnique.Passes)
        {
            pass.Apply();
            using (VertexBuffer buffer = new VertexBuffer(graphicsDevice, VertexPositionNormalTexture.VertexDeclaration, 
                36, BufferUsage.WriteOnly))
            {
                 // Load the buffer
                    buffer.SetData(newVertices);

                    // Send the vertex buffer to the device
                    graphicsDevice.SetVertexBuffer(buffer);

            }

        // Draw the primitives from the vertex buffer to the device as triangles
        graphicsDevice.DrawPrimitives(PrimitiveType.TriangleList, 0, 12); 
        }


    }

我的主类代码调用块绘制方法。

protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.AliceBlue);
        GraphicsDevice.SamplerStates[0] = SamplerState.LinearClamp; // need to do this on reach devices to allow non 2^n textures


        #region GamePlaying
        if (gameState == State.BREAKOUT || gameState == State.GAMEOVERBREAKOUT)
        {
            if (fifthBlocks.Count < 49)
            {
                foreach (Block block in lastBlocks)
                {

                    block.Draw(camera);
                }
            }
            if (fourthBlocks.Count < 49)
            {
                foreach (Block block in fifthBlocks)
                {
                    block.Draw(camera);
                }
            }
            if (thirdBlocks.Count < 49)
            {
                foreach (Block block in fourthBlocks)
                {
                    block.Draw(camera);
                }
            }
            if (secondBlocks.Count < 49)
            {
                foreach (Block block in thirdBlocks)
                {
                    block.Draw(camera);
                }
            }
            if (firstBlocks.Count < 49)
            {
                foreach (Block block in secondBlocks)
                {
                    block.Draw(camera);
                }
            }

            foreach (Block block in firstBlocks)
            {
                block.Draw(camera);
            }
            wallBlock.Draw(camera);

            ball.Draw(GraphicsDevice, camera.View, camera.Projection, spriteBatch);
            spriteBatch.Begin(SpriteSortMode.Immediate, null, null, d, rs);
            controls.Draw(spriteBatch);
            if (ball.scoreMultiplier <= 1)
            {
                spriteBatch.DrawString(scoreFont, "Score: " + scoreDiff + "\n Lives:" + ball.lives, new Vector2(100, 10), Color.Black);
            }
            else
            {
                spriteBatch.DrawString(scoreFont, "Score: " + scoreDiff + "x" + ball.scoreMultiplier + " multiplier!" + "\n Lives:" + ball.lives, new Vector2(100, 10), Color.Black);
            }
            spriteBatch.End();

        }
 }

菜单图纸代码,供参考

public void Draw(SpriteBatch spriteBatch)
    {
        if (gameState == State.MENU)
        {
            spriteBatch.Begin(SpriteSortMode.Immediate, null, null, d, rs);
            spriteBatch.Draw(imageMenu, Vector2.Zero, Color.White);
            spriteBatch.Draw(imageStart, recStart, Color.White);
            spriteBatch.Draw(imagePong, recPong, Color.White);
            spriteBatch.Draw(imageOptions, recOptions, Color.White);
            spriteBatch.Draw(imageJuggle, recJuggle, Color.White);
            spriteBatch.Draw(imageReview, recReview, Color.White);
            spriteBatch.Draw(imageContact, recContact, Color.White);
            spriteBatch.End();
        }
     }

1 个答案:

答案 0 :(得分:1)

感谢所有帮助人员。

我通过更改

解决了这个问题
graphicsDevice.DrawPrimitives(PrimitiveType.TriangleList, 0, 12);

graphicsDevice.DrawUserPrimitives(PrimitiveType.TriangleList, newVertices, 0, 12);