1

I have been learning DirectX for a while now & I got to the point of drawing multiple objects to the screen from .obj files. My problem is that if I draw 2 objects, the 2nd draw will be on top of the 1st.

Example:
Obj1 = Cardboard box
Obj2 = Cube

loadStuff_&_draw(Obj1);  
loadStuff_&_draw(Obj2);

That would draw the cube outside/front the box even if it is in/behind.

How would I make multiple objects draw together so that they overlap correctly?

The only drawing things I know of are:

  • Load vertex, index, constant buffers
  • updateSubresource()
  • drawIndexed()

Edit:
Here is a picture of a cube in a box. It shows that the cube is behind the box rather than inside. It also shows that the rim of the box clips behind the box. Not sure what happened.

I drew the cube, then drew the hollow box.

Render of a cube in a box

4

2 回答 2

1

This is what the depth buffer (sometimes called the z-buffer) is for. When you write a pixel during the drawing of one object, it records the distance from the viewpoint for that pixel in the depth buffer. Then when you draw a second object that would also write to that pixel, it can compare the new object's depth value at that pixel with the buffered value (from previous objects) and only overwrites the pixel if the new value would be nearer than the old one. If the pixel gets drawn, then the depth value is updated to reflect the closer value.

Some links that might give you some ideas on how to implement this:

http://msdn.microsoft.com/en-gb/library/windows/desktop/bb205074%28v=vs.85%29.aspx

http://www.rastertek.com/dx11tut35.html

https://stackoverflow.com/a/8641210/28875

于 2012-11-06T23:30:26.837 回答
-1

This depends on how do you set the world matrix of the object, If you want to second object behind the first object, then set the correct world matrix before drawing. suppose object 1 place at origin(0, 0, 0), then you can translate object2 to (0, 0, -10) to make it behind object1. and translate to (0, 0, 10) will make it in front of object1.

于 2012-11-02T01:39:18.527 回答