【问题标题】:Monogame not drawing vertex buffered cubes, only drawing GraphicsDevice.Clear colourMonogame 不绘制顶点缓冲立方体,只绘制 GraphicsDevice.Clear 颜色
【发布时间】:2013-09-03 12:35:09
【问题描述】:

我的代码在 Windows Phone 7 和 8 上运行良好,但在 Monogame for Windows Store 8 上根本无法运行。

代码在 windows 8 上什么都没有,除了 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();
        }
     }

【问题讨论】:

  • 尝试在你的 Draw 调用中声明 buffer.SetData(newVertices);
  • 刚刚试过,有点帮助。现在正在绘制球,但没有任何积木。
  • 尝试在foreach (EffectPass..)之前调用graphicsDevice.SetVertexBuffer(buffer);
  • 解决了“未绘制”的问题。然而,现在块的所有面都被奇怪地映射了。只有每个立方体的正面被正确绘制,其余的被串起来。
  • 抱歉,我不明白你所说的“串烧”是什么意思。如果您可以加载可以提供帮助的图像。也许是因为您为每个面使用了 6 个顶点。

标签: c# xna monogame


【解决方案1】:

感谢大家的帮助。

我通过更改解决了这个问题

graphicsDevice.DrawPrimitives(PrimitiveType.TriangleList, 0, 12);

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-19
    • 1970-01-01
    • 2017-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多