Sign in to follow this  

VertexBuffer not setting properly?

This topic is 1970 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

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.

[CODE]
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();
}
[/CODE]

SpriteClass Draw() looks like this

[CODE]
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);
}
}
[/CODE]

Any reason that the wrong buffer would be used like that? I'm running out of things to check : / Edited by ChaseRLewis73003

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
[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] Edited by majorbrot

Share this post


Link to post
Share on other sites

This topic is 1970 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