user2618681
user2618681

Reputation: 13

Monogame not drawing vertex buffered cubes, only drawing GraphicsDevice.Clear colour

My code is working perfectly on Windows Phone 7 and 8, but it isn't working at all on Monogame for Windows Store 8.

The code draws nothing on windows 8, except for the colour of graphicsDevice.Clear.

This issue starts occuring when I enter the game playing state, the game draws the menu fine, but the menu drawing code is no different to what I've included.

My code that sets up the blocks

   // 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);

My code that draws the blocks

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); 
        }


    }

My main class code that calls the block drawing method.

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();

        }
 }

Menu drawing code, for reference

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();
        }
     }

Upvotes: 0

Views: 1279

Answers (1)

user2618681
user2618681

Reputation: 13

Thanks for all the help guys.

I fixed the issue by changing

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

to

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

Upvotes: 1

Related Questions