So I found some code on MSDN for creating these;
private RenderTarget2D CreateRenderTarget(GraphicsDevice device, int numberLevels, SurfaceFormat surface)
{
MultiSampleType type = device.PresentationParameters.MultiSampleType;
// If the card can't use the surface format
if (!GraphicsAdapter.DefaultAdapter.CheckDeviceFormat(
DeviceType.Hardware,
GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Format,
TextureUsage.None,
QueryUsages.None,
ResourceType.RenderTarget,
surface))
{
// Fall back to current display format
surface = device.DisplayMode.Format;
}
// Or it can't accept that surface format
// with the current AA settings
else if (!GraphicsAdapter.DefaultAdapter.CheckDeviceMultiSampleType(
DeviceType.Hardware, surface,
device.PresentationParameters.IsFullScreen, type))
{
// Fall back to no antialiasing
type = MultiSampleType.None;
}
int width, height;
// See if we can use our buffer size as our texture
CheckTextureSize(device.PresentationParameters.BackBufferWidth,
device.PresentationParameters.BackBufferHeight,
out width, out height);
// Create our render target
return new RenderTarget2D(device,
width, height, numberLevels, surface,
type, 0);
}
private bool CheckTextureSize(int width, int height, out int newwidth, out int newheight)
{
bool retval = false;
GraphicsDeviceCapabilities Caps;
Caps = GraphicsAdapter.DefaultAdapter.GetCapabilities(
DeviceType.Hardware);
// Check if Device requires Power2 textures
if (Caps.TextureCapabilities.RequiresPower2)
{
retval = true; // Return true to indicate the numbers changed
// Find the nearest base two log of the current width,
// and go up to the next integer
double exp = Math.Ceiling(Math.Log(width) / Math.Log(2));
// and use that as the exponent of the new width
width = (int)Math.Pow(2, exp);
// Repeat the process for height
exp = Math.Ceiling(Math.Log(height) / Math.Log(2));
height = (int)Math.Pow(2, exp);
}
if (Caps.TextureCapabilities.RequiresSquareOnly)
{
retval = true; // Return true to indicate numbers changed
width = Math.Max(width, height);
height = width;
}
newwidth = Math.Min(Caps.MaxTextureWidth, width);
newheight = Math.Min(Caps.MaxTextureHeight, height);
return retval;
}
private DepthStencilBuffer CreateDepthStencil(RenderTarget2D target)
{
return new DepthStencilBuffer(target.GraphicsDevice, target.Width,
target.Height, target.GraphicsDevice.DepthStencilBuffer.Format,
target.MultiSampleType, target.MultiSampleQuality);
}
The problem is that even with these methods my rendered model isn't showing up! :\
Here's my rendering code:
public override void Draw(SpriteBatch SBatch)
{
base.Draw(SBatch);
RenderTarget2D RenderTarget = CreateRenderTarget(m_Screen.ScreenMgr.GraphicsDevice, 1, SurfaceFormat.Depth32);
m_Screen.ScreenMgr.GraphicsDevice.SetRenderTarget(1, RenderTarget);
m_Screen.ScreenMgr.GraphicsDevice.DepthStencilBuffer = CreateDepthStencil(RenderTarget);
m_Effect.World = m_Screen.ScreenMgr.WorldMatrix;
m_Effect.View = m_Screen.ScreenMgr.ViewMatrix;
m_Effect.Projection = m_Screen.ScreenMgr.ProjectionMatrix;
if (m_HeadTexture != null)
{
m_Effect.Texture = m_HeadTexture;
m_Effect.TextureEnabled = true;
m_Effect.EnableDefaultLighting();
}
m_Effect.CommitChanges();
// Draw
m_Effect.Begin();
m_Effect.Techniques[0].Passes[0].Begin();
if (m_HeadVerticies != null)
{
foreach (Face Fce in m_CurrentHeadMesh.Faces)
{
VertexPositionNormalTexture[] Vertex = new VertexPositionNormalTexture[3];
Vertex[0] = m_HeadVerticies[Fce.AVertexIndex];
Vertex[1] = m_HeadVerticies[Fce.BVertexIndex];
Vertex[2] = m_HeadVerticies[Fce.CVertexIndex];
Vertex[0].TextureCoordinate = m_HeadVerticies[Fce.AVertexIndex].TextureCoordinate;
Vertex[1].TextureCoordinate = m_HeadVerticies[Fce.BVertexIndex].TextureCoordinate;
Vertex[2].TextureCoordinate = m_HeadVerticies[Fce.CVertexIndex].TextureCoordinate;
m_Screen.ScreenMgr.GraphicsDevice.DrawUserPrimitives<VertexPositionNormalTexture>(
PrimitiveType.TriangleList, Vertex, 0, 1);
}
}
m_Effect.Techniques[0].Passes[0].End();
m_Effect.End();
}
Here's the code for rendering a screen:
public virtual void Draw(SpriteBatch SBatch)
{
foreach (Texture2D Background in m_Backgrounds)
{
if (Background != null)
{
//Usually a screen only has 1 background, it should be drawn at 0, 0...
SBatch.Draw(Background, new Rectangle(0, 0, Background.Width, Background.Height),
Color.White);
}
}
//Networked UI elements are usually dialogs, so for now it is
//relatively safe to assume they can be drawn behind other elements.
foreach (NetworkedUIElement Element in m_NetUIElements)
Element.Draw(SBatch);
foreach (UIElement Element in m_UIElements)
{
if (Element.DrawingLevel == DrawLevel.DontGiveAFuck)
Element.Draw(SBatch);
}
foreach (UIElement Element in m_UIElements)
{
if (Element.DrawingLevel == DrawLevel.AlwaysOnTop)
Element.Draw(SBatch);
}
for (int i = 0; i < m_Popups.Count; i++)
m_Popups.Draw(SBatch);
}
What do I do to make my model show up?
I know that the rendering code works, because I've tested it in a tool I made.
So I'm thinking the reason the rendering isn't showing is because the model is rendered behind the textures...