Jump to content
  • Advertisement
Sign in to follow this  
Maddius

Managed Direct3D C#

This topic is 3033 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi all,

Ok i'm pretty stumped here. I just started work on my 3D level editor and I am putting the boiler plate code down for getting up a d3d window and trying to draw a cube. I have an entire engine built in directx 10 in C++ so I have a fair understanding on what needs to be in place for this to work, but for the life of me I cannot see why I cannot see the cube!

I have a picturebox on my C# form which is my directx window, I seem to think I have everything in place to draw a cube, But nothing appears, I have tried all the obvious and non obvious intial debugs, but nothing has worked, any help is much much appreciated. Cheers.

I have this shader:

float4x4 gWorldViewProjMatrix;
float4x4 gTextureMatrix;
Texture gColoredTexture;

sampler gSampler = sampler_state
{
texture = <gColoredTexture>;
magfilter = LINEAR;
minfilter = LINEAR;
mipfilter = LINEAR;
AddressU = mirror;
AddressV = mirror;
};

struct VS_IN_OUT
{
float4 mPosition : POSITION;
float2 mTexCoords : TEXCOORD0;
};

struct PS_OUT
{
float4 mColor : COLOR0;
};

VS_IN_OUT VS(VS_IN_OUT pVertexIn)
{
VS_IN_OUT wVertexOut;

wVertexOut.mPosition = mul(pVertexIn.mPosition, gWorldViewProjMatrix);
wVertexOut.mTexCoords = mul(float4(pVertexIn.mTexCoords, 0.0f, 1.0f), gTextureMatrix);

return wVertexOut;
}

PS_OUT PS(VS_IN_OUT pPixelIn)
{
PS_OUT wPixelOut;

wPixelOut.mColor = float4(1.0f, 1.0f, 1.0f, 1.0f); //= tex2D(gSampler, pPixelIn.mTexCoords);

return wPixelOut;
}

technique AmbientTextured
{
pass Pass0
{
VertexShader = compile vs_2_0 VS();
PixelShader = compile ps_2_0 PS();
}
}


Which I have even hacked to just display white for all pixels of the cube, because I thought this might be a texture issue, but to no avail.

I setup the view and proj matricies like so:

mPosition = new Vector3(0.0f, 0.0f, -50.0f);
mFocusPoint = new Vector3(0.0f, 0.0f, 0.0f);
mViewMatrix = Matrix.LookAtLH(mPosition, mFocusPoint, mUpVector);
mProjMatrix = Matrix.PerspectiveFovLH((float)Math.PI * 0.5f, Globals.gFrmMain.pbDxWindow.Width /
Globals.gFrmMain.pbDxWindow.Height, 1.0f, Globals.gViewRange);


My cube is then created through code:

mVertexCount = 24;
mIndexCount = 36;
mTriangleCount = 12;

mVertexBuffer = new VertexBuffer(typeof(Verts.VertexPosTex), mVertexCount, Globals.gDevice, Usage.WriteOnly, VertexFormats.Position | VertexFormats.Texture0, Pool.Managed);
mIndexBuffer = new IndexBuffer(Globals.gDevice, sizeof(int) * mIndexCount, Usage.WriteOnly, Pool.Managed, false);

int[] wIndicies = new int[mIndexCount];
Verts.VertexPosTex[] wVerticies = new Verts.VertexPosTex[mVertexCount];

#region Create Verticies
// Fill in the front face vertex data.
wVerticies[0] = new Verts.VertexPosTex(new Vector3(-1.0f, -1.0f, -1.0f), new Vector2(0.0f, 1.0f));
wVerticies[1] = new Verts.VertexPosTex(new Vector3(-1.0f, 1.0f, -1.0f), new Vector2(0.0f, 0.0f));
wVerticies[2] = new Verts.VertexPosTex(new Vector3( 1.0f, 1.0f, -1.0f), new Vector2(1.0f, 0.0f));
wVerticies[3] = new Verts.VertexPosTex(new Vector3( 1.0f, -1.0f, -1.0f), new Vector2(1.0f, 1.0f));
// Fill in the back face vertex data.
wVerticies[4] = new Verts.VertexPosTex(new Vector3(-1.0f, -1.0f, 1.0f), new Vector2(1.0f, 1.0f));
wVerticies[5] = new Verts.VertexPosTex(new Vector3( 1.0f, -1.0f, 1.0f), new Vector2(0.0f, 1.0f));
wVerticies[6] = new Verts.VertexPosTex(new Vector3( 1.0f, 1.0f, 1.0f), new Vector2(0.0f, 0.0f));
wVerticies[7] = new Verts.VertexPosTex(new Vector3(-1.0f, 1.0f, 1.0f), new Vector2(1.0f, 0.0f));
// Fill in the top face vertex data.
wVerticies[8] = new Verts.VertexPosTex(new Vector3(-1.0f, 1.0f, -1.0f), new Vector2(0.0f, 1.0f));
wVerticies[9] = new Verts.VertexPosTex(new Vector3(-1.0f, 1.0f, 1.0f), new Vector2(0.0f, 0.0f));
wVerticies[10] = new Verts.VertexPosTex(new Vector3( 1.0f, 1.0f, 1.0f), new Vector2(1.0f, 0.0f));
wVerticies[11] = new Verts.VertexPosTex(new Vector3( 1.0f, 1.0f, -1.0f), new Vector2(1.0f, 1.0f));
// Fill in the bottom face vertex data.
wVerticies[12] = new Verts.VertexPosTex(new Vector3(-1.0f, -1.0f, -1.0f), new Vector2(1.0f, 1.0f));
wVerticies[13] = new Verts.VertexPosTex(new Vector3( 1.0f, -1.0f, -1.0f), new Vector2(0.0f, 1.0f));
wVerticies[14] = new Verts.VertexPosTex(new Vector3( 1.0f, -1.0f, 1.0f), new Vector2(0.0f, 0.0f));
wVerticies[15] = new Verts.VertexPosTex(new Vector3(-1.0f, -1.0f, 1.0f), new Vector2(1.0f, 0.0f));
// Fill in the left face vertex data.
wVerticies[16] = new Verts.VertexPosTex(new Vector3(-1.0f, -1.0f, 1.0f), new Vector2(0.0f, 1.0f));
wVerticies[17] = new Verts.VertexPosTex(new Vector3(-1.0f, 1.0f, 1.0f), new Vector2(0.0f, 0.0f));
wVerticies[18] = new Verts.VertexPosTex(new Vector3(-1.0f, 1.0f, -1.0f), new Vector2(1.0f, 0.0f));
wVerticies[19] = new Verts.VertexPosTex(new Vector3(-1.0f, -1.0f, -1.0f), new Vector2(1.0f, 1.0f));
// Fill in the right face vertex data.
wVerticies[20] = new Verts.VertexPosTex(new Vector3( 1.0f, -1.0f, -1.0f), new Vector2(0.0f, 1.0f));
wVerticies[21] = new Verts.VertexPosTex(new Vector3( 1.0f, 1.0f, -1.0f), new Vector2(0.0f, 0.0f));
wVerticies[22] = new Verts.VertexPosTex(new Vector3( 1.0f, 1.0f, 1.0f), new Vector2(1.0f, 0.0f));
wVerticies[23] = new Verts.VertexPosTex(new Vector3( 1.0f, -1.0f, 1.0f), new Vector2(1.0f, 1.0f));

mVertexBuffer.SetData(wVerticies, 0, LockFlags.None);
#endregion

#region Create Indicies
// Fill in the front face index data
wIndicies[0] = 0; wIndicies[1] = 1; wIndicies[2] = 2;
wIndicies[3] = 0; wIndicies[4] = 2; wIndicies[5] = 3;

// Fill in the back face index data
wIndicies[6] = 4; wIndicies[7] = 5; wIndicies[8] = 6;
wIndicies[9] = 4; wIndicies[10] = 6; wIndicies[11] = 7;

// Fill in the top face index data
wIndicies[12] = 8; wIndicies[13] = 9; wIndicies[14] = 10;
wIndicies[15] = 8; wIndicies[16] = 10; wIndicies[17] = 11;

// Fill in the bottom face index data
wIndicies[18] = 12; wIndicies[19] = 13; wIndicies[20] = 14;
wIndicies[21] = 12; wIndicies[22] = 14; wIndicies[23] = 15;

// Fill in the left face index data
wIndicies[24] = 16; wIndicies[25] = 17; wIndicies[26] = 18;
wIndicies[27] = 16; wIndicies[28] = 18; wIndicies[29] = 19;

// Fill in the right face index data
wIndicies[30] = 20; wIndicies[31] = 21; wIndicies[32] = 22;
wIndicies[33] = 20; wIndicies[34] = 22; wIndicies[35] = 23;

mIndexBuffer.SetData(wIndicies, 0, LockFlags.None);
#endregion



Then the draw loop, this is called between Clear, BeginScene, EndScene and Present.


foreach (Cube wObject in mCubes)
{
wObject.PreDraw();

Globals.gAmbientTextured.Technique = "AmbientTextured";
Globals.gAmbientTextured.SetValue("gWorldViewProjMatrix", wObject.WorldMatrix * Camera.ViewProjMatrix);
Globals.gAmbientTextured.SetValue("gTextureMatrix", wObject.TextureMatrix);
Globals.gAmbientTextured.SetValue("gColoredTexture", wObject.Texture);

int wPasses = Globals.gAmbientTextured.Begin(0);

for (int i = 0; i < wPasses; i++)
{
Globals.gAmbientTextured.BeginPass(i);
wObject.Draw();
Globals.gAmbientTextured.EndPass();
}

Globals.gAmbientTextured.End();
}


Draw calls:

public virtual void PreDraw()
{
Globals.gDevice.SetStreamSource(0, mVertexBuffer, 0);
Globals.gDevice.Indices = mIndexBuffer;
Globals.gDevice.VertexDeclaration = Globals.gVertexDec;
}

public virtual void Draw()
{
Globals.gDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, mVertexCount, 0, mTriangleCount);
}


I know this is a massive post, and I will be more than grateful for people to even just read this, but for the life of me I cannot see what is not in place, the verticies and indicies has been built and set. The shader is created and should just return white as the colour of the object.

The camera is also moved backwards and the cube scaled up, so there should be no embarassing out of view shenanigans going on. In truth I have no prior experience with managed direct3d, and any help would be a massive help more than happy to post up any more code people would like to see.

Thank you all!

Share this post


Link to post
Share on other sites
Advertisement
Forget this topic guys, I feel like a grade A <insert explitive here>, it was just a problem with my matricies, sorry to waste peoples time who had to read this!

I thought I checked them, obviously not well enough. Oh well!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!