|
 | Sprit Batch Issue | |  |
| Poster | : garfinkle | | Posts | : 7 | | Country | : England | | City | : Manchester |
| | | | Posted by garfinkle on 22/12/2008 at 14:28:16
| | Hi All
I've built the terrain and water system and made them into an framework based system. If am drawing the terrain and water as follows
protected override void Draw(GameTime gameTime)
{
SetupWater();
water.DrawRefractionMap();
water.DrawReflectionMap();
float time = (float)gameTime.TotalGameTime.TotalMilliseconds / 100000.0f;
device.RenderState.CullMode = CullMode.None;
device.Clear(ClearOptions.Target | ClearOptions.DepthBuffer, Color.Black, 1.0f, 0);
SetupSkyDome();
skyDome.DrawSkyDome();
SetupTerrain();
terrain.DrawTerrain();
water.DrawWater(time);
base.Draw(gameTime);
}
|
Everything at this point is loading fine.
I want to use a SpriteBatch to load images and text onto the screen for an interface, however when ever I add the following code to the draw() method
spriteBatch.Begin();
int i = 0;
foreach (string entry in log)
{
spriteBatch.DrawString(myFont, entry, new Vector2(20, 20 * ++i), Color.White);
}
foreach (string gamerTime in gamerTimes.Values)
{
spriteBatch.DrawString(myFont, gamerTime, new Vector2(graphics.GraphicsDevice.Viewport.Width - 150, 20), Color.Red);
}
spriteBatch.End();
|
The graphics go crazy. The terrain becomes semi-transparent and the water plane overlays the whole terrain.
Any idea why the spritebatch is causing this problem.
P.S. The sprite batch causes the issue no matter where in the draw method i put it.
Thanks in advance.
Garfinkle | |
|
| | | | | | Poster | : garfinkle | | Posts | : 7 | | Country | : England | | City | : Manchester |
| | | | Posted by garfinkle on 22/12/2008 at 14:42:28
| | Fixed it,
Here is the issue and how to solve it for anyone else who may have this problem.
When you use a SpriteBatch, it resets several settings that affect 3D drawing before the spritebatch.begin() method starts.
To fix the issue, you just need to reset the changed variables just before the 3D objects are drawn.
The settings that need to be reset are as follows. I have written them in a function so that it can be used easier in draw methods
void prepare3d()
{
GraphicsDevice.RenderState.DepthBufferEnable = true;
GraphicsDevice.RenderState.AlphaBlendEnable = false;
GraphicsDevice.RenderState.AlphaTestEnable = false;
GraphicsDevice.SamplerStates[0].AddressU = TextureAddressMode.Wrap;
GraphicsDevice.SamplerStates[0].AddressV = TextureAddressMode.Wrap;
}
|
so if you call the prepare3d() method in draw() before you start any 3D drawing, there will be no issues when dealing with Spritebatches.
Hope this helps anyone | |
|
|
 | | |  |
|
|
|
If you appreciate the amount of time I spend creating and updating these pages, feel free to donate -- any amount is welcome !
|
- Website design & DirectX code : Riemer Grootjans - ©2006 Riemer Grootjans
|
|