Sign in to follow this  

[XNA] [solved]Screwy things happening when using DrawUserIndexedPrimitives...

This topic is 3595 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

EDIT - Solved. See solution at end of post. So I am trying to whip up a small and simple skybox class that renders a simple skybox using the BasicEffect (I still haven't taken the dive into shaders, yet). I'm at the point where I have my index and vertex buffers set up for part of the skybox (just 2 sides at this point), but there are some incredibly screwy things going on. I am able to draw one side of the skybox without any problems. The problems start when I make a second call to DrawUserIndexedPrimitives. I don't get any errors (or warnings), but what I do get is some strange results. The geometry that is drawn in the second DrawUserIndexedPrimitives isn't drawn at all. Initially, after 8 to 10 seconds of the app running, the first geometry's texture suddenly switches to the texture that is supposed to be on the second set of geometry. Approximately every 8 to 10 seconds after that, I see a flash of geometry being drawn with the second texture in some random screwed up location. This only happens for a single frame every 8 to 10 seconds. I'm pretty sure I've got my index and vertex buffers set up properly. I really have no idea what is causing the second call to DrawUserIndexedPrimitives to not draw anything, and I have no idea why I'm getting screwed up geometry being drawn every 8 to 10 seconds. This has got me totally stumped. Here's some of the code I am using: Vertex/Index Buffer declaration
		VertexPositionTexture[] vertices = new VertexPositionTexture[8];
		VertexDeclaration vertexDeclaration;
		int[] indices = new int[12];



Setting up vertex and index buffers:

			//vertex positions
			Vector3 topRightFront = new Vector3(skyboxSize, skyboxSize, -skyboxSize);
			Vector3 topLeftFront = new Vector3(-skyboxSize, skyboxSize, -skyboxSize);
			Vector3 bottomRightFront = new Vector3(skyboxSize, -skyboxSize, -skyboxSize);
			Vector3 bottomLeftFront = new Vector3(-skyboxSize, -skyboxSize, -skyboxSize);
			Vector3 topRightRear = new Vector3(skyboxSize, skyboxSize, skyboxSize);
			Vector3 topLeftRear = new Vector3(-skyboxSize, skyboxSize, skyboxSize);
			Vector3 bottomRightRear = new Vector3(skyboxSize, -skyboxSize, skyboxSize);
			Vector3 bottomLeftRear = new Vector3(-skyboxSize, -skyboxSize, skyboxSize);

			//texture coordinates
			Vector2 topLeft = new Vector2(0.0f, 0.0f);
			Vector2 topRight = new Vector2(1.0f, 0.0f);
			Vector2 bottomLeft = new Vector2(0.0f, 1.0f);
			Vector2 bottomRight = new Vector2(1.0f, 1.0f);
			//Front
			vertices[0].Position = topLeftFront;
			vertices[0].TextureCoordinate = topLeft;

			vertices[1].Position = topRightFront;
			vertices[1].TextureCoordinate = topRight;

			vertices[2].Position = bottomRightFront;
			vertices[2].TextureCoordinate = bottomRight;

			vertices[3].Position = bottomLeftFront;
			vertices[3].TextureCoordinate = bottomLeft;

			//Bottom
			vertices[4].Position = bottomLeftFront;
			vertices[4].TextureCoordinate = topLeft;

			vertices[5].Position = bottomRightFront;
			vertices[5].TextureCoordinate = topRight;

			vertices[6].Position = bottomRightRear;
			vertices[6].TextureCoordinate = bottomRight;

			vertices[7].Position = bottomLeftRear;
			vertices[7].TextureCoordinate = bottomLeft;

			//Set up the indices

			//front
			indices[0] = 0;
			indices[1] = 1;
			indices[2] = 3;
			indices[3] = 1;
			indices[4] = 2;
			indices[5] = 3;

			//bottom
			indices[6] = 4;
			indices[7] = 5;
			indices[8] = 7;
			indices[9] = 5;
			indices[10] = 6;
			indices[11] = 7;

			vertexDeclaration = new VertexDeclaration(this.GraphicsDevice, 
				VertexPositionTexture.VertexElements);




My Draw method:
        /// <summary>
        /// Draw method
        /// </summary>
        /// <param name="gameTime"></param>
        public override void Draw(GameTime gameTime)
        {
			this.GraphicsDevice.VertexDeclaration = vertexDeclaration;

						this.GraphicsDevice.RenderState.DepthBufferEnable = true;
			
			basicEffect.LightingEnabled = false;
			basicEffect.Begin();

			basicEffect.Projection = projectionMatrix;
			basicEffect.View = viewMatrix;

			foreach (EffectPass pass in basicEffect.CurrentTechnique.Passes)
			{
				pass.Begin();

				
				basicEffect.Texture = frontTexture;
				basicEffect.CommitChanges();

				this.GraphicsDevice.DrawUserIndexedPrimitives<VertexPositionTexture>(PrimitiveType.TriangleList,
					vertices, 0, 4, indices, 0, 2);
				

				basicEffect.Texture = bottomTexture;
				basicEffect.CommitChanges();

				this.GraphicsDevice.DrawUserIndexedPrimitives<VertexPositionTexture>(PrimitiveType.TriangleList,
					vertices, 4, 4, indices, 6, 2);
				
				pass.End();
			}
		
			basicEffect.End();


            base.Draw(gameTime);
        }




The BasicEffect view and projection matrices are being properly set from values pulled in from another place (my camera class, which works fine with some other lines I am drawing). Can anyone spot anything that might be casuing this to go wonky? It's got me stumped... EDIT: Dang, I hate it when I solve my own problems. It turns out the second draw call had some bad parameters. It should have been something like the following:
			foreach (EffectPass pass in basicEffect.CurrentTechnique.Passes)
			{
				pass.Begin();

				
				basicEffect.Texture = frontTexture;
				basicEffect.CommitChanges();

				this.GraphicsDevice.DrawUserIndexedPrimitives<VertexPositionTexture>(PrimitiveType.TriangleList,
					vertices, 0, 4, indices, 0, 2);
				

				basicEffect.Texture = bottomTexture;
				basicEffect.CommitChanges();

				this.GraphicsDevice.DrawUserIndexedPrimitives<VertexPositionTexture>(PrimitiveType.TriangleList,
					vertices, 0, 8, indices, 6, 2);
				
				pass.End();
			}

On a similar note, I do still have one question. Is there any way of avoiding the extra calls to BasicEffect.CommitChanges()? Is it necessary to call it every time I need to switch textures? If I don't call it, it seems as though things get drawn with the wrong textures on (ie: the last texture to be set). [Edited by - Moe on February 14, 2008 7:38:45 PM]

Share this post


Link to post
Share on other sites

This topic is 3595 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this