XNA Creators Club Online
Page 1 of 1 (12 items)
Sort Posts: Previous Next

Noob question about 3D primitives

Last post 7/7/2009 11:51 PM by becreswell. 11 replies.
  • 7/4/2009 9:42 AM

    Noob question about 3D primitives

    Sorry if these are dumb questions, but I couldn't find any info from searching. I am attempting to make a kind of 3D board game. I'm drawing the blocks using DrawUserIndexedPrimitives. Its seems to be working correctly, but when you look at the tiles from some angles, they look like crap. The top surface of the blocks are not drawing completely or something. This is kind of hard to describe and it is a fair amount of code, so I'll post a pic to see if this is a common problem someone can recognize just by looking at it. I can post some code if needed.

    Image and video hosting by TinyPic


    Also, when i use a SpriteBatch to draw some stats relating to the camera at the top of the screen, all the blocks start overlapping each other. The blocks are being drawn in the order that they were created instead of according to their depth. When I comment out drawCameraStats() it goes back to normal. Anyone encountered that before or know how to get around it?


    Image and video hosting by TinyPic
  • 7/4/2009 10:18 AM In reply to

    Re: Noob question about 3D primitives

    This might help you. http://blogs.msdn.com/shawnhar/archive/2006/11/13/spritebatch-and-renderstates.aspx
    The spriteBatch messes around with some booleans and changes them, so when you draw 3D objects they appear drawn in the wrong order/way.
  • 7/4/2009 3:46 PM In reply to

    Re: Noob question about 3D primitives

    Cool! thanks for that tip. I enabled
    GraphicsDevice.RenderState.DepthBufferEnable = true;

    after I was done with the spirteBatch and it is drawing the text and drawing the blocks in the right order. I just gotta figure out how to make the boxes not look so glitchy now. I'm reading up on all those other renderStates.

    When the DepthBufferEnable = false it draws the boxes in the wrong order, but they don't look glitchy. Why is that, and how do i get it to not look glitchy when GraphicsDevice.RenderState.DepthBufferEnable = true?
  • 7/4/2009 5:17 PM In reply to

    Re: Noob question about 3D primitives

    The problem is likely your cull order. I think SpriteBatch sets culling to CW (Clockwise), and you generally want to have it CCW (Counter-clockwise).
    Jon Watte, Direct3D MVP
    Tweets, occasionally
    kW X-port 3ds Max .X exporter
    kW Animation source code
  • 7/4/2009 7:46 PM In reply to

    Re: Noob question about 3D primitives

    CullMode was already set counter clockwise. I tried changing it to clockwise and all i saw was the one back side of each box.

    Here is the draw code.


     
    protected override void Draw(GameTime gameTime) 
            { 
                graphics.GraphicsDevice.Clear(Color.White); 
     
                drawCameraStats(); 
     
                GraphicsDevice.RenderState.CullMode = CullMode.CullCounterClockwiseFace; 
                GraphicsDevice.RenderState.DepthBufferEnable = true
     
                graphics.GraphicsDevice.VertexDeclaration = vertexDeclaration; 
                effect.Projection = camera.Projection; 
                effect.View = camera.View; 
     
                foreach (Box box in boxes) 
                { 
                    box.Draw(effect); 
                } 
     
                base.Draw(gameTime); 
            } 
     
    public static void InitializeBox() 
            {             
                boxSide = Tactics.Game.Content.Load<Texture2D>("Box Side"); 
                boxTop = Tactics.Game.Content.Load<Texture2D>("Box Top"); 
                #region Initialize vertices 
                Vector3 position; 
                Vector2 textureCoordinates; 
                #region topVerticies 
                topVerticies = new VertexPositionNormalTexture[4]; 
     
                //top left 
                position = new Vector3(-5, 5, 0); 
                textureCoordinates = new Vector2(0, 0); 
                topVerticies[0] = new VertexPositionNormalTexture(position, Vector3.Forward, 
                    textureCoordinates); 
     
                //bottom right 
                position = new Vector3(5, -5, 0); 
                textureCoordinates = new Vector2(1, 1); 
                topVerticies[1] = new VertexPositionNormalTexture(position, Vector3.Forward, 
                    textureCoordinates); 
     
                //bottom left 
                position = new Vector3(-5, -5, 0); 
                textureCoordinates = new Vector2(0, 1); 
                topVerticies[2] = new VertexPositionNormalTexture(position, Vector3.Forward, 
                    textureCoordinates); 
     
                //top right 
                position = new Vector3(5, 5, 0); 
                textureCoordinates = new Vector2(1, 0); 
                topVerticies[3] = new VertexPositionNormalTexture(position, Vector3.Forward, 
                    textureCoordinates); 
                #endregion 
                #region topSideVertices 
                topSideVertices = new VertexPositionNormalTexture[4]; 
     
                //top left 
                position = new Vector3(-5, 5, 0); 
                textureCoordinates = new Vector2(0, 0); 
                topSideVertices[0] = new VertexPositionNormalTexture(position, Vector3.Forward, 
                    textureCoordinates); 
     
                //bottom right 
                position = new Vector3(5, 5, -1); 
                textureCoordinates = new Vector2(1, 1); 
                topSideVertices[1] = new VertexPositionNormalTexture(position, Vector3.Forward, 
                    textureCoordinates); 
     
                //bottom left 
                position = new Vector3(5, 5, 0); 
                textureCoordinates = new Vector2(0, 1); 
                topSideVertices[2] = new VertexPositionNormalTexture(position, Vector3.Forward, 
                    textureCoordinates); 
     
                //top right 
                position = new Vector3(-5, 5, -1); 
                textureCoordinates = new Vector2(1, 0); 
                topSideVertices[3] = new VertexPositionNormalTexture(position, Vector3.Forward, 
                    textureCoordinates); 
                #endregion 
                #region bottomSideVertices 
                bottomSideVertices = new VertexPositionNormalTexture[4]; 
     
                //top left 
                position = new Vector3(-5, -5, 0); 
                textureCoordinates = new Vector2(0, 0); 
                bottomSideVertices[0] = new VertexPositionNormalTexture(position, Vector3.Forward, 
                    textureCoordinates); 
     
                //bottom right 
                position = new Vector3(5, -5, -1); 
                textureCoordinates = new Vector2(1, 1); 
                bottomSideVertices[1] = new VertexPositionNormalTexture(position, Vector3.Forward, 
                    textureCoordinates); 
     
                //bottom left 
                position = new Vector3(-5, -5, -1); 
                textureCoordinates = new Vector2(0, 1); 
                bottomSideVertices[2] = new VertexPositionNormalTexture(position, Vector3.Forward, 
                    textureCoordinates); 
     
                //top right 
                position = new Vector3(5, -5, 0); 
                textureCoordinates = new Vector2(1, 0); 
                bottomSideVertices[3] = new VertexPositionNormalTexture(position, Vector3.Forward, 
                    textureCoordinates); 
                #endregion 
                
                #region leftSideVertices 
                leftSideVertices = new VertexPositionNormalTexture[4]; 
     
                //top left 
                position = new Vector3(-5, 5, 0); 
                textureCoordinates = new Vector2(0, 0); 
                leftSideVertices[0] = new VertexPositionNormalTexture(position, Vector3.Forward, 
                    textureCoordinates); 
     
                //bottom right 
                position = new Vector3(-5, -5, -1); 
                textureCoordinates = new Vector2(1, 1); 
                leftSideVertices[1] = new VertexPositionNormalTexture(position, Vector3.Forward, 
                    textureCoordinates); 
     
                //bottom left 
                position = new Vector3(-5, 5, -1); 
                textureCoordinates = new Vector2(0, 1); 
                leftSideVertices[2] = new VertexPositionNormalTexture(position, Vector3.Forward, 
                    textureCoordinates); 
     
                //top right 
                position = new Vector3(-5, -5, 0); 
                textureCoordinates = new Vector2(1, 0); 
                leftSideVertices[3] = new VertexPositionNormalTexture(position, Vector3.Forward, 
                    textureCoordinates); 
                #endregion 
                #region rightSideVertices 
                rightSideVertices = new VertexPositionNormalTexture[4]; 
     
                //top left 
                position = new Vector3(5, -5, 0); 
                textureCoordinates = new Vector2(0, 0); 
                rightSideVertices[0] = new VertexPositionNormalTexture(position, Vector3.Forward, 
                    textureCoordinates); 
     
                //bottom right 
                position = new Vector3(5, 5, -1); 
                textureCoordinates = new Vector2(1, 1); 
                rightSideVertices[1] = new VertexPositionNormalTexture(position, Vector3.Forward, 
                    textureCoordinates); 
     
                //bottom left 
                position = new Vector3(5, -5, -1); 
                textureCoordinates = new Vector2(0, 1); 
                rightSideVertices[2] = new VertexPositionNormalTexture(position, Vector3.Forward, 
                    textureCoordinates); 
     
                //top right 
                position = new Vector3(5, 5, 0); 
                textureCoordinates = new Vector2(1, 0); 
                rightSideVertices[3] = new VertexPositionNormalTexture(position, Vector3.Forward, 
                    textureCoordinates); 
                #endregion 
                #endregion 
                #region Initialize indices 
                //6 vertices make up 2 triangles which make up our rectangle 
                indices = new short[6]; 
     
                //triangle 1 (bottom portion) 
                indices[0] = 0; // top left 
                indices[1] = 1; // bottom right 
                indices[2] = 2; // bottom left 
     
                //triangle 2 (top portion) 
                indices[3] = 0; // top left 
                indices[4] = 3; // top right 
                indices[5] = 1; // bottom right 
                #endregion 
            } 
     
    //Box.Draw() 
     public void Draw(BasicEffect effect) 
            { 
                effect.World = world; 
                effect.TextureEnabled = true
     
                if (RenderTop) 
                { 
                    effect.Texture = boxTop; 
                    effect.Begin(); 
                    effect.CurrentTechnique.Passes[0].Begin();                 
     
                    //top 
                    Tactics.Game.GraphicsDevice.DrawUserIndexedPrimitives( 
                        PrimitiveType.TriangleList, 
                        topVerticies, 0, topVerticies.Length, 
                        indices, 0, indices.Length / 3); 
     
                    effect.CurrentTechnique.Passes[0].End(); 
                    effect.End(); 
                } 
     
                effect.Texture = boxSide; 
                effect.Begin(); 
                effect.CurrentTechnique.Passes[0].Begin(); 
     
                //top Side 
                Tactics.Game.GraphicsDevice.DrawUserIndexedPrimitives( 
                    PrimitiveType.TriangleList, 
                    topSideVertices, 0, topSideVertices.Length, 
                    indices, 0, indices.Length / 3); 
     
                //bottom Side 
                Tactics.Game.GraphicsDevice.DrawUserIndexedPrimitives( 
                    PrimitiveType.TriangleList, 
                    bottomSideVertices, 0, bottomSideVertices.Length, 
                    indices, 0, indices.Length / 3); 
     
                //left Side 
                Tactics.Game.GraphicsDevice.DrawUserIndexedPrimitives( 
                    PrimitiveType.TriangleList, 
                    leftSideVertices, 0, leftSideVertices.Length, 
                    indices, 0, indices.Length / 3); 
     
                //right Side 
                Tactics.Game.GraphicsDevice.DrawUserIndexedPrimitives( 
                    PrimitiveType.TriangleList, 
                    rightSideVertices, 0, rightSideVertices.Length, 
                    indices, 0, indices.Length / 3); 
     
                effect.CurrentTechnique.Passes[0].End(); 
                effect.End(); 
            } 

  • 7/5/2009 9:03 AM In reply to

    Re: Noob question about 3D primitives

    I ended up setting RenderState.DepthBufferEnable = falseand now i'm calculating each piece's depth and sorting them and drawing them in the correct order during each Draw(). This is obviously not a great solution, but it works. This has been very discouraging. Anyone else have any ideas why the 3d primitives look so glitchy?

    I have spent a lot of time trying to figure out how to make my own .fbx models for free instead of 3D primitives, using something like google sketchup, but no luck so far. Anyone have any tips on getting sketchup models into an xna game?
  • 7/5/2009 12:26 PM In reply to

    Re: Noob question about 3D primitives

    It looks like you've just got some problems with indices/vertices, with so much code it's easy to make a mistake. I haven't looked through it all to check it all, but I suggest you comment out blocks of code to just render a single face. Check it renders as you expect, if it does move on to the next face, and so on until you narrow down where the problem is.
  • 7/5/2009 4:01 PM In reply to

    Re: Noob question about 3D primitives

    SpaceDude:
    It looks like you've just got some problems with indices/vertices, with so much code it's easy to make a mistake. I haven't looked through it all to check it all, but I suggest you comment out blocks of code to just render a single face. Check it renders as you expect, if it does move on to the next face, and so on until you narrow down where the problem is.


    I'll agree with this. It visually looks very similar to overlapping faces / sides from my 3d mapping & modelling days. Cant say for sure - but like mentioned, I'd remove it all and slowly add back until the bug is present again. It will limit the amount of code you need to trudge through to determine the error at hand.

    I'd be willing to be you are overlapping something just baaarely unintentionally - that will somewhat typically result in it looking fine until the right angle is hit.

    Good luck.
  • 7/5/2009 5:29 PM In reply to

    Re: Noob question about 3D primitives

    It looks to me that your index buffer is shared by all of your vertex buffers?

    There is definitely a culling issue here.  You may need to create a separate index buffer for each vertex buffer - or - carefully change the order of your vertices so that the index buffer can map them all in the right order.

    I had this problem at one point in my project too.

    Regards,
    -db
    Terra-Infinitum
  • 7/7/2009 12:38 AM In reply to

    Re: Noob question about 3D primitives

    It seems to me that if it were a culling issue then it would be all or nothing right? A given triangle would either draw or it wouldn't. But I am getting parts of triangles bleeding through depending on the angle of the camera. Two overlapping triangles seems much more likely i think. I'll commenting everything out and then adding it back one at a time.
  • 7/7/2009 12:55 AM In reply to

    Re: Noob question about 3D primitives

    I tried only drawing 1 triangle per box instead of the normal 10 and this glitch it still happening. Given this, I am guessing that there is a setting somewhere that is causing this.

    Any ideas? Thanks for all the input so far. Glad to see XNA has a good community :)
  • 7/7/2009 11:51 PM In reply to

    Re: Noob question about 3D primitives

    I figured it out.

    Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, aspectRatio, 0.0001f, 1000.0fout projection); 


    That is way too big of a range for the clipping planes apparently. I changed it to 1, 150 and all my problems went away.
Page 1 of 1 (12 items) Previous Next