Jump to content

  • Log In with Google      Sign In   
  • Create Account


Member Since 11 Jul 2013
Offline Last Active Today, 01:57 PM

Topics I've Started

Stencil Buffer

03 September 2014 - 02:45 PM



Here's a greatly simplified version of what I'm trying to accomplish: 


Imagine I have a gently sloping terrain (triangle strip).  At an arbitrary position on the terrain I want to put a cardboard box (with no lid).  The box will live at the base-Y position of the terrain, but the terrain might be slightly higher at this point, making the box appear to be partially buried.  This is exactly what I want.  Now imagine the camera looks down into the box.  I want to show the box contents, or simply just the bottom of the box.


I thought the best way to accomplish this would be with a stencil buffer, however I'm struggling to get it to work (following Chapter 13 in Introduction to 3D Game Programming).  Here's what I'm doing:


Step 1:  Set up the stencil buffer

   gpDeviceInterface->SetRenderState(D3DRS_STENCILENABLE, true);
   gpDeviceInterface->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_ALWAYS);
   gpDeviceInterface->SetRenderState(D3DRS_STENCILREF, 0x1);
   gpDeviceInterface->SetRenderState(D3DRS_STENCILMASK, 0xFFFFFFFF);
   gpDeviceInterface->SetRenderState(D3DRS_STENCILWRITEMASK, 0xFFFFFFFF);
   gpDeviceInterface->SetRenderState(D3DRS_ZWRITEENABLE, false);
   gpDeviceInterface->SetRenderState(D3DRS_ALPHABLENDENABLE, true);
   gpDeviceInterface->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ZERO);
   gpDeviceInterface->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE);

Step 2:  Build a polygon to represent the top plane of the box.  Render the polygon.


Step 3:  Render the terrain.

// According to Luna, the stencil buffer should be filled with 0x1 where the box "top" was
// rendered and 0x0 everywhere else.  So, LESSEQUAL should perform the stencil test on
// the entire scene/target
gpDeviceInterface->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_LESSEQUAL);

// If the test failed, replace the 0x0 with 0x1 so the terrain pixel will render

// If the test passed, keep the pixel
gpDeviceInterface->SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_KEEP);

// If the depth test failed, the pixel is behind the stencil, so reject it
gpDeviceInterface->SetRenderState(D3DRS_STENCILZFAIL, D3DSTENCILOP_ZERO);

// Draw the terrain and reset D3DRS_STENCILENABLE...

I don't think I'm grokking Luna, because at this point the only terrain that renders is what's under the box "lid" (i.e. the stencil mask).  Can anyone spot what might be going on?

atan2 inconsistencies

19 March 2014 - 12:19 PM



I'm using atan2 from math.h and I'm getting weird results.  I call it once using a set of parameters and I get the result of 0 degrees (which is correct), but when I call it again with the exact same parameters I get the result of -180 or +180 degrees.  Is this a known issue and are there work arounds?

XMVector3Project implementation

30 December 2013 - 04:24 PM



I need to transform an object's world coordinate vertex into screen coordinates.  It seems like XMVector3Project would do this, but unfortunately I can't use DXMath so I have to come up with the implementation by hand.


My linear-algebra-mojo isn't that strong, so I'm having a difficult time figuring out exactly what this function does.  Anyone know for sure?


Handrolled mipmaps

30 December 2013 - 10:53 AM



I have terrain that I want to display with a grid.  I thought multitexturing would be a good way to apply the grid (basically, have one texture that is a square outline), but I run into problems when zooming the camera out because of minification.  I want the grid lines to be a consistent width no matter the zoom.


I thought the solution would be to build my own mipmaps in a graphics editor so the texture contains each LOD.  I did something similar in OpenGL a while ago, but I'm not having any luck finding out how to do it in DX.  Can someone point me to where I can (if it's possible) implement this?


Also, if this is possible:  My base texture is 256x256 and when the camera is fully zoomed out the texture size would be 16x16.  Would I just stub out the remaining LODs or just leave them out entirely?

Rendering text without D3DX

06 December 2013 - 12:30 PM



I'm trying to figure out how to render text to a D3D9 scene without using D3DXFont or any other extensions.  Unfortunately, all of the googleverse examples seem to rely on that.  What would be a good strategy for doing so?


I thought about rendering to a texture with DrawText, but that seems a little clunky.  Not sure if there is something better.