Sign in to follow this  
whereisumar

C# Direct3D 9.0

Recommended Posts

whereisumar    122
Hi everyone, I have a question. I have started using direct3d with C#. I don't understand why my rendering is not displaying things in order. For instance, a box that is behind a pyramid, is actually being displayed as being in front, the geometry is not hiding what's behind it. I have enabled the zbuffer with device.RenderStates.ZBufferEnable = true in the initialization, it still is not working!! I'm so frustrated. Someone please help me out. Am I missing something? I've worked for a month on a pretty decent ASE file loader loading transforms, meshanimation, camera and light controls etc. This one thing is just messing it up. Thanks.

Share this post


Link to post
Share on other sites
u235    359
You didn't say if you set a depth buffer format so try this instead:

PresentParameters pp = new PresentParameters();
pp.AutoDepthStencil = DepthBufferFormat.D16
pp.EnableAutoDepthStencil = true;


I don't remember the exact names for the property and enum that hold the depth buffer formats, but it's something to that effect, so it shouldn't be too hard to find. Hope this helps.

-AJ

Share this post


Link to post
Share on other sites
sirob    1181
Hi,

You'll need to provide a proper format under the AutoDepthStencilFormat in the PresentationParameters, and also set AutoDepthStencil to true.

Valid formats would be any depth/stencil format (namely, D16, D32, or D24S8).

Hope this helps.

Edit: beaten, oh the noes.

Share this post


Link to post
Share on other sites
whereisumar    122
Hi, well, i added the following lines as suggested

presentParams.AutoDepthStencilFormat = DepthFormat.D16; presentParams.EnableAutoDepthStencil = true;

But it still is not working, there was no difference.

Is there a specific DepthFormat I have to use, or can any one work?

Is it a problem with c#? This may sound like a foolish question, but are there any bugs or incomplete sections for the Direct3D API for C#?

Share this post


Link to post
Share on other sites
u235    359
Are you sure the location of the objects is correct. Let's see some code to [smile].

-AJ

Share this post


Link to post
Share on other sites
whereisumar    122
Hi, the location of the objects is correct, i'm importing straight from 3d studio .ASE format. I double check the order by moving the camera around in my engine, so i know what is infront and behind.

Does the order of the device.DrawPrimitives have to be in order too, or just once per output direct3d checks the buffer after all the device.drawprimitives have been called for that frame?

What type of code would help you help me?

Again, thanks a log guys.

Share this post


Link to post
Share on other sites
u235    359
Quote:
Original post by whereisumar
Hi, the location of the objects is correct, i'm importing straight from 3d studio .ASE format. I double check the order by moving the camera around in my engine, so i know what is infront and behind.

Does the order of the device.DrawPrimitives have to be in order too, or just once per output direct3d checks the buffer after all the device.drawprimitives have been called for that frame?

What type of code would help you help me?

Again, thanks a log guys.


The order of device.DrawPrimitives shouldn't matter, I don't think, but it helps for efficiency's sake. The code that would help out would probably be any rendering code and code that handles positioning of the objects in the world.

-AJ

Share this post


Link to post
Share on other sites
whereisumar    122
Hi, well below is some related code to my problem.

PresentParameters presentParams = new PresentParameters();
presentParams.Windowed = true;
presentParams.SwapEffect = SwapEffect.Discard;

device = new Device(0, DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, presentParams);
device.RenderState.Lighting = true;
presentParams.AutoDepthStencilFormat = DepthFormat.D16;
presentParams.EnableAutoDepthStencil = true;
device.RenderState.ZBufferEnable = true;

device.RenderState.CullMode = Cull.CounterClockwise;

That's all in the initialization. Below is code regarding positioning, this may be a bit confusing and an overload, because it's taking transformations into account.

while (!CorrectKeyFrameFound)
{
if (Ticks >= TMAnimation.PosTrack[KeyFrame].Frame && Ticks < TMAnimation.PosTrack[KeyFrame+1].Frame)
{
CorrectKeyFrameFound = true;
CurrentFrame = KeyFrame;
}
KeyFrame++;
}
Magnitude = (Ticks - TMAnimation.PosTrack[CurrentFrame].Frame) / 160;
Pos.X = TMAnimation.PosTrackI[CurrentFrame].Pos.X;
Pos.Y = TMAnimation.PosTrackI[CurrentFrame].Pos.Y;
Pos.Z = TMAnimation.PosTrackI[CurrentFrame].Pos.Z;

M.M41 += Pos.X;
M.M42 += Pos.Y;
M.M43 += Pos.Z;

device.Transform.World = M;

The above code is fragments from the .Draw() function. Each object has it's own draw method.

Hope this helps. I really still can't see what i'm doin wrong

Share this post


Link to post
Share on other sites
whereisumar    122
protected virtual void Render()
{
if (device != null) {
device.Clear(ClearFlags.Target, Color.Black, 1.0f, 0);
device.Clear(ClearFlags.ZBuffer, Color.Black, 1.0f, 0);
device.BeginScene();
GameObject.Draw(device, Ticks);
DateTime StartTime = DateTime.Now;
while (DateTime.Now.Ticks < StartTime.Ticks + 191325);
Ticks+=160;
device.EndScene();
device.Present();
}
}

Hi, well i added this line, i'm almost 100% sure i'm doing this wrong, this is the render method. Is this what you meant? Also, now when i run it, it just gives me an error and does not start at all.

How do you hide geometry!?!?!? i'm goin crazy.

Share this post


Link to post
Share on other sites
Instruo    373
Well, you really only need to call clear once.

device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.Black, 1.0f, 0);

Share this post


Link to post
Share on other sites
whereisumar    122
Hi, well i tried it that way. I'm still getting an error, the error message is "INVALID CALL",

and it specifies the line with ClearFlags in it.

please help me through this rough time in my life where i am questioning my 3d abilities! lol.

Share this post


Link to post
Share on other sites
Instruo    373
Well, it looks to me that indicates back to something being incorrect with the way you're setting up your device. Note what it says at the bottom of this, basically that it'll throw that exception if you don't have an attached depth buffer.

Share this post


Link to post
Share on other sites
Instruo    373
Looking at your code...

Quote:
PresentParameters presentParams = new PresentParameters();
presentParams.Windowed = true;
presentParams.SwapEffect = SwapEffect.Discard;

device = new Device(0, DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, presentParams);
device.RenderState.Lighting = true;
presentParams.AutoDepthStencilFormat = DepthFormat.D16;
presentParams.EnableAutoDepthStencil = true;
device.RenderState.ZBufferEnable = true;


one thing that sticks out as odd is that you're creating your device BEFORE you've finished setting all the properties of your presentation parameters...

Share this post


Link to post
Share on other sites
DXnut    262
From what you posted, it looks like you are setting some of the PresentParameters AFTER creating the device. You need to set them all BEFORE creating the device with it.

Edit - oops. Someone beat me to it!

Share this post


Link to post
Share on other sites
whereisumar    122
Yes, you were right, quite a retarded mistake of mine indeed.

However, now it's back to what it was in the beginning, it's still not hiding objects that should be behind other objects.

What in the world is goin on here? Do i have to clear the Stencil too like ClearFlags.Stencil?

Share this post


Link to post
Share on other sites
whereisumar    122
The Blue box is actually supposed to be in front of all of them...The geometry is 100% right, i'm not messing that up. How do i get the hiding of geometry to work?



[Edited by - whereisumar on May 20, 2006 12:04:27 PM]

Share this post


Link to post
Share on other sites
whereisumar    122
Oh just an update, ok now i changed the depth format to D24S8, so what is happening now is that every 3rd frame or so, the blue box successfully comes in front! But 2/3 frames it's behind...what a mystery. So how do i fix this fully? Again the code is below

PresentParameters presentParams = new PresentParameters();
presentParams.Windowed = true;
presentParams.SwapEffect = SwapEffect.Discard;
presentParams.AutoDepthStencilFormat = DepthFormat.D24S8;
presentParams.EnableAutoDepthStencil = true;

device = new Device(0, DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, presentParams);
device.RenderState.ZBufferEnable = true;
device.RenderState.Lighting = true;

Share this post


Link to post
Share on other sites
Enselic    829
Quote:
Original post by whereisumar
The Blue box is actually supposed to be in front of all of them...The geometry is 100% right, i'm not messing that up. How do i get the hiding of geometry to work?

If you rotate the scene, is it still messed up? Maybe the view is from the "wrong" point? (Some modeling softwares uses different coordinates systems.)

Share this post


Link to post
Share on other sites
whereisumar    122
hi, yep, as i stated i have rotated the scene too. 3DS does in fact use a different coordinate system, but it's reflected on the X axis, the depth is not affected (as far as I know).

I've dragged objects from behind, and through to the front of other objects, it is still not working.

Share this post


Link to post
Share on other sites
parnell    122
Where is your view transform? I'm sure whatever that is, is the problem.

and this:
Quote:

while (DateTime.Now.Ticks < StartTime.Ticks + 191325);

is just burning cpu up...

you might try doing this...

Quote:

while(DateTime.Now.Ticks < StartTime.Ticks + 191325) System.Threading.Thread.Sleep(1);


because that while loop is just churning cpu the whole time until the comparison is true.

Share this post


Link to post
Share on other sites
whereisumar    122
the device.present is called once every frame :)

Still not working.

how do you clear the stencil buffer every frame, is it with the device.clear(clearflags.target | clearflags.zbuffer....)?

Or is the zbuffer and stencil buffer two different things, and if so, how do i clear the stencil buffer?

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