Direct3D in C#

Nerseus

Danner
Joined
Oct 22, 2002
Messages
2,547
Location
Arizona, USA
I have sample projects of Direct3D in C# if anyone would care to take a look. They utilize Vertex Buffers for some 3D work. They also use textures and sprites. It uses the VB DirectX COM DLL so you'll need the DirectX SDK installed (DirectX 8). Other than calling into the DirectX DLL, there is no unmanaged code.

If enough people would like to see it, I can post the two projects here.

-ner
 
Go ahead and post them (source, not binaries, of course). It will be interesting to compare them when managed DX9 comes out.
 
I've attached the first project, DX8Test. It contains 3 samples, based loosely on the MS tutorials that came with the SDK.

Of note are the following:
1. It ONLY utilizes VertexBuffers. I will post my sample that uses DrawPrimitiveUP tomorrow.
2. The texture sample has a bit of "extras" in it. At the very bottom of the form's source, you'll see the ResetDevice function. You can read the comment but it basically talks about how to get around .NET's non-deterministic finalization. I wish there were a better way for that, but I can't find it :)

The main form just launches one of three sample forms. The first, triangletest, shows a simple triangle with each corner a different color. The second shows a rotating cube. The last shows a rotating, textured cylinder with the aforementioned ability to reset the managed objects (the texture and the vertex buffer).

The alternative is to create all objects in the managed pool (change D3DPOOL_DEFAULT to D3DPOOL_MANAGED). This also means you'll have to create the texture using CreateTextureFromFileEx instead of CreateTextureFromFile since the Ex version allows you to specify the pool for the texture.

Have fun!

-ner
 

Attachments

Here's the second sample project. It's a 2D sample using "sprites" in DirectX8. It's a rewrite of the MS sample Donuts that came with the SDK, enhanced slightly (slightly better organized code).

The main points to notice are:
1. Uses a GCHandle object to get a pointer to an array of vertices. This GCHandle can be passed to DrawPrimitiveUP as an IntPtr.
2. Pressing Alt-Enter will toggle Fullscreen/Windowed mode.
3. This code looks for a suitable fullscreen mode.
4. The whole project, in general, has decent error checking on the DirectX calls. Definitely far from 100%, but there are a lot of try/catch blocks to handle most common problems.
5. Use of CreateTextureFromFileEx to load a texture and apply transparency.

NOTE: The setting of m_transColorKey can be made quite a bit simpler through the use of a color object. In the sample, black is used with a full white alpha (0xFF). This can be accomplished with:
m_transColorKey = Color.Black.ToArgb();

Enjoy!

-nerseus
 

Attachments

I love that sample! Shows the true power of VB, even when doing lots of math and looping.

On a side note... The following code snippet can be used instead of the CopyVerts call in the DX8Test sample code. CopyVerts is an alias for RtlMoveMemory which does a pure memory copy. For those that would rather use pointers to copy the verts manually, here's some unsafe code to do the same thing (thanks to Vincent Demongodin who sent me this awhile back).

For the texturetest sample, replace the line:
DXWrapper.CopyVerts(pData, m_vertices, m_VertexSize * m_vertices.Length);
with the following:

C#:
unsafe 
{
	System.IntPtr ptrData = new IntPtr(pData);
	TextureVertex* ptrVertBuffer = (TextureVertex*)ptrData.ToPointer();
	fixed(TextureVertex* ptrVerts = &m_vertices[0])
	{
		for(int i=0; i<m_vertices.Length; i++)
			ptrVertBuffer[i] = ptrVerts[i];
	}
}

Note that you'll have to change the project properties to allow unsafe code blocks for this to run (part of Configuration Options -> Build).

It may not be as fast, but it might be more clear and maybe less error prone than using aliased version of RtlMoveMemory. :)

Enjoy!
-Nerseus
 
Last edited by a moderator:
Back
Top