Jump to content
  • Advertisement
Sign in to follow this  
mattropo1is

Backfacing lines

This topic is 2606 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

I'm drawing a lot of lines (think a vector-based type drawing of objects):

m_pDevice->IASetVertexBuffers( 0, 1, &pMesh->m_pVertexBuffer, &stride, &offset );
m_pDevice->IASetIndexBuffer( pMesh->m_pIndexBuffer, DXGI_FORMAT_R32_UINT, 0 );
m_pDevice->IASetPrimitiveTopology( D3D10_PRIMITIVE_TOPOLOGY_LINELIST );

The thing is, I'd like to be able to draw the 'backfacing' line segments in a different/muted color. I know the obvious solution would be to sort the list and build up two index buffers - one for one color, and another index buffer for the second color. However, that requires re-building the index lists every frame. Is there a better way?

Thanks in advance

Share this post


Link to post
Share on other sites
Advertisement
I know this might not be the answer to your question, but I'm pretty sure lines don't actually have a "backface", correct me if i'm wrong, but doesn't backface culling do nothing for lines, since the "backface" is determined by the order in which you draw the vertices compared to the camera, so only triangles really have a backface.

I suppose a solution would be to determine the order that your entire object, or sections of it, are drawn relative to the camera, then just change the color of your lines depending on whether the object is "facing" towards or away from the camera. This might be a bad way to do it, but for example, if you were drawing a grid of lines, you could find the normal of the grid which points in the direction the front of the grid is facing by using three of the corner vertices of the grid. Then find if the the normal is pointing away or towards the camera to determine the side of the grid the camera sees. However, this wouldn't work too well with rounded objects. So for rounded objects, you could take smaller sections of the entire object and find their normals to determine which each section faces. Other than that, the only other thing i can thing of is you could find the front side of every line by finding its normal, which should point in the direction the line "faces".

hope that gives a little help

Share this post


Link to post
Share on other sites

The thing is, I'd like to be able to draw the 'backfacing' line segments in a different/muted color. I know the obvious solution would be to sort the list and build up two index buffers - one for one color, and another index buffer for the second color. However, that requires re-building the index lists every frame. Is there a better way?


If you converted a triangle list of an object to a line list, to render a wireframe display of the same object, you can simply use the vertex normals from the triangle list for the lines as well, and use a (vertex normal) dot (camera direction) >= 0 test to determine which color to pass out from the vertex shader to pixel shader for drawing.


If that was not the case, and your line lists come from somewhere else (and you can't build a notion of front-facing vs back-facing with normals like that), you can give each vertex in the line list a vertex color to specify what color to draw it in. This is probably better than maintaining two sets of index buffers, though if the colors dynamically change, in both cases (e.g. using an index buffer, or using a vertex color) you will have to discard-lock and reupdate the full vertex/index buffer.

For best performance, the desired method is to try to provide the vertex shader with the appropriate data to make the color choice, so you do not need to lock vertex or index buffers at render time each frame (only small-sized constant buffers). Line lists are not any special compared to triangle lists, so you can feed in as per-vertex attributes whatever parameters you find to be relevant for the computation.

Share this post


Link to post
Share on other sites

If you converted a triangle list of an object to a line list, to render a wireframe display of the same object, you can simply use the vertex normals from the triangle list for the lines as well, and use a (vertex normal) dot (camera direction) >= 0 test to determine which color to pass out from the vertex shader to pixel shader for drawing.


Duh - surprised I didn't think of this. Great answer. I'll give it a try when I get a chance as the points of the line list have normals that are VERY easy to calculate.

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!