How to make primitives correctly overlay sprites in DirectX (2D)
I've got a problem with rendering sprites and primitives in Direct3D9. I'm trying to make a simple 2D game using sprites, however I've found out that I'll also need primitives to draw simple squares, lines, etc. The problem is with overlaying.
If I only draw primitives using Device.DrawUserPrimitives()
, everything is fine, primitives are rendered correctly. If I only draw sprites using Sprite.Draw()
, it's also fine. However if I want to draw both sprites and primitives, all sprites automatically overlays primitives or make them black.
I've tried disabling Z buffer, disabling writing in Z buffer while rendering sprites, etc. but it was always same. I managed to make primitives overlay sprites by changing their z position, but then all primitives were black... :(
Note, please, that I've tried rendering primitives both before and after drawing sprites. It didn't help.
I'll post here some snippets of drawing sprites and primitives (variable names changed so you understand what I'm drawing even if you don't know anything about C# and SlimDX):
mSprite.Begin(SpriteFlags.ObjectSpace | SpriteFlags.DoNotModifyRenderState);
mSprite.Draw(texture, centerVector, positionVector, color); // positionVector = Vector3.Zero in my example
mSprite.Flush();
(...)
device.VertexDeclaration = vertexDecl;
device.DrawUserPrimitives(PrimitiveType.TriangleFan, m_fan1.Length - 2, m_fan1);
...where m_fan1 is a structure containing Vector4 position
and int color
and vertexDecl declares these vertex elements.
m_fan1[0] = new TransformedColored(new Vector4(100.0f, 100.0f, 1.0f, 1.0f), Color.Green);
m_fan1[1] = new TransformedColored(new Vector4(0.0f, 0.0f, 1.0f, 1.0f), Color.Green);
m_fan1[2] = new TransformedColored(new Vector4(200.0f, 0.0f, 1.0f, 1.0f), Color.Green);
m_fan1[3] = new TransformedColored(new Vector4(200.0f, 200.0f, 1.0f, 1.0f), Color.Green);
m_fan1[4] = new TransformedColored(new Vector4(0.0f, 200.0f, 1.0f, 1.0f), Color.Yellow);
m_fan1[5] = new TransformedColored(new Vector4(0.0f, 0.0f, 1.0f, 1.0f), Color.Green);
By the way:
- If I change fill mode to wireframe, the primitives' wiref开发者_StackOverflowrames are drawn correctly.
- If the primitives overlay sprites, the primitives are almost black, and the more sprites I draw, the darker primitives are. Maybe it has something to do with alpha?
- All primitives are rendered correctly until I draw a sprite. But even if I stop drawing sprites, primitives will stay dark / black until I reset the application.
If you need some other snippets of my source code, just write it here, please, and I'll update this.
I solved this with the use of sprite.Transform();
sprite.Transform = Matrix.Scaling(1, 1, 1) * Matrix.Translation(x,y, 0); // works
compared to
sprite.Transform = Matrix.Scaling(1, 1, 1) * Matrix.Translation(x,y, 1); // doesn't work
I hope this helps anyone else who encounters this problem.
I don't see a sprite.End() being called. From the documentation here (http://msdn.microsoft.com/en-us/library/ms881042.aspx), Sprite.End() "Restores the device to the state it was in before Sprite.Begin was called."
I would also do a device.EndScene() after drawing the user primitives.
Hope that helps!
Without seeing your complete code, it's not realy possible to find the error. I would check the z-buffer values of the sprite and our primitives. If that part is correct, there is no reason your primitives should'nt over draw the backbuffer. Do you set the renderstates needed for the primitives each render cycle? The d3dx sprite sets its own renderstates by default, maybe that is messing up your primitives. And, there is a flag D3DXSPRITE_DONOTMODIFY_RENDERSTATE when drawing a sprite.
->When your primitives darken when you change the z-value, i think this is caused by your light sources (range?)
精彩评论