C# - Drawing an image (sprite) with Direct3D

EFileTahi-A

Contributor
Joined
Aug 8, 2004
Messages
623
Location
Portugal / Barreiro
I just want to use DirectX.Direct3D for 2D graphics and I just want to know the esiest way to draw an image on x,y coords with less code as possible but using double buffer.

Ok the following code initializes the Direct3D:

Code:
public void InitializeGraphics()
{
    // Set our presentation parameters
    PresentParameters presentParams = new PresentParameters();
    presentParams.SwapEffect = SwapEffect.Discard;

    // Start up full screen
    Format current = Manager.Adapters[0].CurrentDisplayMode.Format;
 
    presentParams.Windowed = true;
    presentParams.BackBufferFormat = current;
    presentParams.BackBufferCount = 1;
    presentParams.BackBufferWidth = ScreenWidth;
    presentParams.BackBufferHeight = ScreenHeight;

    // Create our device
    device = new Device(0, DeviceType.Hardware, this,
        CreateFlags.SoftwareVertexProcessing, presentParams);
}

//just o quite the application
protected override void OnKeyUp(KeyEventArgs e)
{
    if (e.KeyCode == Keys.Escape) // Quit
        this.Close();

    base.OnKeyUp (e);
}

This initializes successfly the 3D device. There is also some things I add/change in my project as sugested by the book from which I learn all this in order to make Direct3D work fine like overriding the .NET builtin OnPaint method.

Anyway the all thing I want is to use Direct3D like GDI+, where I can create an image buffer to draw all the graphics there and then show it. Also I would like to manipulate the buffer image itself to performe some nice effects like, fade it, fade out, color paint etc...

Resuming:
Is this initialization of the DirectX.Direct3D fine for image buffering?
How can draw an image in Direct3D at x,y coords? (load image from HD, draw it to buffer picture and show buffer picture)
After drawing images on Direct3D device, how can I then grab the all screen (containing all the pics drawed (bufferPic)) so that I can then change its color?

Thanks
 
Last edited:
Machaira said:
I believe there's a Sprite class in D3D that you can use for drawing 2D images. It's pretty simple from what I remember.

Yeah, I know that. But I need to see some code in action to see how does it works... And, as always, if my post is not explicit enought drop me a line...
 
Last edited:
device = new Device(0, DeviceType.Hardware, this,
CreateFlags.SoftwareVertexProcessing, presentParams);

Try hardware vertex processing. Most graphics cards (excpet the ancient ones) support it... but oh boy does it help. If you're not sure if your card supports it, I beleive it's

dxDevice.supportsHWTransformAndLight

OR something like dxDevice.getDeviceCaps().supportsHWTransformAndLight;
 
thenerd said:
vbprogramming.8k.com

The pentium Guy's website. He has a bunch of d3d tutorials, and one that deals specifically with the d3d sprite class.

Hi, tks for the post... I already knew ThePentiums's site, unfortunately it's in VB.

Anyway, am still at the same step.. trying to figure out how double buffer works (is it automatic?), yet how to draw a simple sprite with less code as possible. So far I did this:

Code:
// Create our texture
spriteTexture = TextureLoader.FromFile(VAR.dxDevice, @"..\..\logo4.tga");
Surface s = spriteTexture.GetSurfaceLevel(0);
SurfaceDescription desc = s.Description;

textureSize = new Rectangle(0, 0, 256, 256);
		
sprite = new Sprite(VAR.dxDevice);

// ------------------------------------------------

VAR.dxDevice.BeginScene();
sprite.Begin(SpriteFlags.AlphaBlend);
sprite.Draw(spriteTexture, textureSize, new Vector3(0, 0, 0), new Vector3((e.X / 32) * 32,(e.Y / 32) * 32,0), Color.White);
sprite.End();
VAR.dxDevice.EndScene();
VAR.dxDevice.Present();

This works well, but transparency only works with the original image that camed with the pdf I downloaded...

I wonder why.. It's a .tga which haves a black background with the DX logo...
I do a picture with black background with some text, I save it as tga and nothing... the black background won't be transparent... This is truly irritating!
 
You will probably find the image that works has an alpha channel specfied which makes the black transparent. What image editor are you using to create your sprite? Does it allow you edit the Alpha channel of an image?
 
PlausiblyDamp said:
You will probably find the image that works has an alpha channel specfied which makes the black transparent. What image editor are you using to create your sprite? Does it allow you edit the Alpha channel of an image?

Yeah, I imediately remembered of tga supporting Alpha Channel... That was the problem. Now all is working fine...

Two questions:

1 - How can I use/load Targa (.tga) images as background on .NET controls?
2 - Is is possible to make an specific color go transparent of an BMP image (achieveing the same results of a .tga image by skiping the use of alpha channel that is)?
 
Last edited:
mutant said:
Yes it is.
Great!

Btw, how can I grab the contents of the backbuffer (all the drawn stuff) as a bitmap or sprite?

Am asking this so that in my game I can simulate day/night light by changing the color/brightness of all displayed graphics... Also, is there a simple way of acomplish all this?
 
You can use the Device.GetBackBuffer() method to get ahold of the backbuffer.

As for chaning day and night, I don't really know about that with sprites since I never used this object myself. For example using 3d graphics would be able to do that in Direct3D just by changing the ambient color of the scene. I would have to check if it works on sprites.
 
I just need to retrieve the the Backbuffer and convert it to Bitmap so that I can do the day/light effect. I just need to know how to use the "device.GetBackBuffer" and convert it to bitmap and vice-versa...
 
Well, I have found a way to darking the sprites without use of lights nor backbuffers. Now, I only need to know three things:

1 - How to load TGA images into memory and threat them as a Bitmap object.
2 - Using BMP textures in Direct3D with surface keyColor for transparency.
3 - The advantages and disavantages of TGA vs BMP.
 
1 - TextureLoader
2 - Lock it and set the alpha yourself or use System.Drawing.Bitmap to do it. (should use preset alpha)
3 - Bitmap is a bit more ubiquitous (but I wouldn't use either)
 
IngisKahn said:
1 - TextureLoader
I mean, loading TGA images with NO DirectX at all...(I was not explicit enough sorry)
2 - Lock it and set the alpha yourself or use System.Drawing.Bitmap to do it. (should use preset alpha)
Great! How do I do that?! (Am a DirectX noob, thus, learning all this requires both theory and practical examples (like almost everything))
3 - Bitmap is a bit more ubiquitous (but I wouldn't use either)
Could you develop this point of yours a bit further?
 
Back
Top