VertexBuffer not setting properly?

Started by
1 comment, last by majorbrot 11 years, 9 months ago
So I'm looking into porting a unity project to XNA (familiar with directX, just not XNA).

Testing a custom mesh type that I'm using by rendering some simple quads and have ran into a problem.

Edit: I think I'm just misunderstanding how this whole drawCall thing works in XNA. Messing with different commenting out arrangements is given me a lot of results compared to what I expected. Does it have something to do with Pass? Is there a way to like commit it?

First frame rendered looks this:

[attachment=10271:Frame1.PNG]

All others this:

[attachment=10272:OtherFrames.PNG]

Well first off all the red color data is in the otherchild item or the one that doesn't render the first time through for some reason. If I remove the otherchild draw call it becomes white like it should be. If I draw only 1 of the buffers it works fine, but not with 2.

Evidence that otherchild should be Red but not sprite is in the code below. I have also thoroughly verified with a debugger that the data in the VertexBuffer when I call SetData() is correct. So I am positive the issue is with how I am setting the buffers because it is rendering each sprite with the wrong buffer and their is no reference that exists between the 2 sprites that exists.


Sprite sprite = new Sprite();
Sprite otherchild = new Sprite();
Effect basic;
Camera2D camera;
protected override void Initialize()
{
new GameEngine(this, graphics);
spriteBatch = new SpriteBatch(GraphicsDevice);
font = Content.Load<SpriteFont>("SegoeUIMono");
GameEngine.settings = Content.Load<Settings>("XMLFile1");
basic = Content.Load<Effect>("Basic");
sprite.quad.SetData();
otherchild.quad.color = Color.Red;
otherchild.quad.SetData();
sprite.transform.AddChild(otherchild.transform);
sprite.transform.scale = new Vector2(2.5f, 2.5f);
otherchild.transform.scale = new Vector2(2.0f,1.0f);
otherchild.transform.localPosition = new Vector3(3.0f, 0.0f, 0.0f);
camera = new Camera2D(0.1f,100.0f,50.0f,50.0f);
camera.transform.localPosition = new Vector3(0.0f, 0.0f, -10.0f);
}

protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(ClearOptions.Target | ClearOptions.DepthBuffer, Color.Gray,1.0f,0);
basic.CurrentTechnique.Passes[0].Apply();

basic.Parameters["World"].SetValue(sprite.transform.world);
basic.Parameters["ViewProjection"].SetValue(camera.ViewProjectionMatrix);
sprite.Draw();
basic.CurrentTechnique.Passes[0].Apply();
basic.Parameters["World"].SetValue(otherchild.transform.world);
basic.Parameters["ViewProjection"].SetValue(camera.ViewProjectionMatrix);
otherchild.Draw();
GraphicsDevice.Present();
}


SpriteClass Draw() looks like this


public void Draw()
{
if (quad.hasBuffers)
{
GameEngine.graphicsDevice.Indices = quad.IndexBuffer;
GameEngine.graphicsDevice.SetVertexBuffer(quad.VertexBuffer);
GameEngine.graphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0,quad.vertices.Length, 0,2);
}
}


Any reason that the wrong buffer would be used like that? I'm running out of things to check : /
Advertisement
Hi there welcome to gamedev.

I notice your using a custom effect file called Basic, however the code above doesn't show how you tell the shader to use the colour for each quad. Also I'm wondering why your calling GraphicsDevice.Present();

Could you show us the code for your Sprite class, it may help us better answer your question.

Aimee

We are now on Tumblr, so please come and check out our site!

http://xpod-games.com

[sub]its just a.wrong order. try this:[/sub]
[sub]-set effect params[/sub]
[sub]-call Pass.Apply()[/sub]
[sub]-Draw()[/sub]
[sub]and you dont have to call present as this is done in base.Draw().[/sub]

[sub]Edit: [/sub]
[sub]its enaugh to set the params one time per frame, Apply() and draw two or more times, if all params stay the same. otherwise you can update only the per object changing values, Apply again and draw. [/sub]

This topic is closed to new replies.

Advertisement