Sign in to follow this  
TScrypter

Can't render sprite in SlimDX using SlimDX.Direct3D10

Recommended Posts

TScrypter    108
I'm attempting to build a Sprite animation creator for the artists on a project I'm currently working on. I am using SlimDX for rendering to a Panel which serves as a viewport on a form. The application does not throw an error, but I cannot get the sprite I'm attempting to load to be rendered. I know the device and swapchain are set up correctly because I can clear the backbuffer and present. I feel like I'm missing one crucial step, but I can't figure out what it is. I'm basing a lot off this off of an example from [u]Beginning DirectX 10 Game Programming[/u] by Wendy Jones. I've attached a screenshot to show the result that I'm getting. All I'm asking is that someone can help me figure out what I'm missing to get my sprite to be rendered to the screen.


[source lang="csharp"]using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using SlimDX;
using SlimDX.Direct3D10;
using SlimDX.DXGI;

using Device = SlimDX.Direct3D10.Device;
using Resource = SlimDX.Direct3D10.Resource;
using SlimDX.Windows;

namespace Editor
{
public partial class Form1 : Form
{

#region DirectX Fields
/// <summary>
/// Properties to represent objects necessary just get Direct3D running
/// </summary>
Device device;
SwapChainDescription swapChainDescription;
SwapChain swapChain;
RenderTargetView BackBuffer;


/// <summary>
/// Properties to represent drawing a sprite
/// </summary>
Sprite sprite;
List<SpriteInstance> sprites = new List<SpriteInstance>();
#endregion

public Form1()
{
InitializeComponent();
}

/// <summary>
/// Load this form, then create all
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Form1_Load(object sender, EventArgs e)
{
// Initialize the SwapChainDescription
swapChainDescription = new SwapChainDescription()
{
BufferCount = 1,
IsWindowed = true,
Flags = SwapChainFlags.AllowModeSwitch,
ModeDescription = new ModeDescription(0,0, new Rational(60,1), Format.R8G8B8A8_UNorm),
OutputHandle = pnlOutput.Handle,
SampleDescription = new SampleDescription(1, 0),
SwapEffect = SwapEffect.Discard,
Usage = Usage.RenderTargetOutput
};

// Create a Device instance and a SwapChain instance
Device.CreateWithSwapChain(null,
DriverType.Hardware,
DeviceCreationFlags.Debug,
swapChainDescription,
out device,
out swapChain);

// Get a reference to the BackBuffer
using (var resource = Resource.FromSwapChain<Texture2D>(swapChain, 0))
{
BackBuffer = new RenderTargetView(device, resource);
};

InitializeSpriteObject();

// Run the renderer on its own thread using the MessagePump class
MessagePump.Run(this, Render);
}

/// <summary>
/// Render the current frame to the backbuffer and present it
/// </summary>
private void Render()
{
device.ClearRenderTargetView(BackBuffer, new Color4(0.5f, 0.0f, 0.75f));


if (sprites.Count > 0)
{
sprite.Begin(SpriteFlags.SortBackToFront);

sprite.DrawImmediate(sprites.ToArray());

sprite.End();
}

swapChain.Present(0, PresentFlags.None);
}

/// <summary>
/// Initialize the SpriteObject (acts similarly to a SpriteBatch in XNA)
/// </summary>
private void InitializeSpriteObject()
{
sprite = new Sprite(device, 0);
sprite.ProjectionTransform = Matrix.OrthoOffCenterLH(0.0f, 600.0f, 400.0f, 0.0f, 0.1f, 10.0f);
sprite.ViewTransform = Matrix.Scaling(1.0f,1.0f,1.0f) * Matrix.Translation(0.0f, 0.0f, 0.0f);
}

/// <summary>
/// Get an image for the sprite and render it
/// </summary>
private void LoadTexture(string ImagePath)
{
// MessageBox.Show(ImagePath);
Texture2D texture = Texture2D.FromFile(device, ImagePath);
Texture2DDescription textureDesc = texture.Description;

ShaderResourceViewDescription srvd = new ShaderResourceViewDescription()
{
Dimension = ShaderResourceViewDimension.Texture2D,
Format = Format.R8G8B8A8_UNorm,
MipLevels = textureDesc.MipLevels
};
ShaderResourceView srv = new ShaderResourceView(device, texture, srvd);

SpriteInstance instance = new SpriteInstance(srv, new Vector2(300, 200), new Vector2(600, 400));
instance.Transform = Matrix.Scaling(1.0f, 1.0f, 1.0f) * Matrix.Translation(300, 200, 0.1f);

sprites.Add(instance);
}

/// <summary>
/// Open a new item (a texture)
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
OpenFileDialog dialog = new OpenFileDialog();
if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
if (dialog.FileName.EndsWith("png") || dialog.FileName.EndsWith("bmp"))
{
LoadTexture(dialog.FileName);
}
}
}
}
}[/source]

Share this post


Link to post
Share on other sites
unbird    8338
Couple of issues:
[list]
[*]You clear your render target view, but don't set it as render target in the output merger stage.
[*]Neither do you set a viewport in the rasterizer stage.
[*]You don't set a rasterizer state explicitly. The default has culling enabled, which might cull your quad, depending on the transformation. For now I'd disable it altogether.
[*]The SpriteInstance constructors second and third argument are in texture space, which is normalized (goes from 0 to 1 in both dimensions). E.g. use something like (0,0) and (1,1) respectively.
[*]The SpriteInstances transformation means the world transformation. This has to play along with your projection and view. Since you are currently using a ortho projection in pixel/screen space, a scaling of (1,1,1) will result in a 1x1 pixel, unlikely what you want.
[/list]

I hope that helps.

Share this post


Link to post
Share on other sites

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