object outline

本文介绍了一种在游戏开发中实现准确且线条均匀的模型轮廓渲染技术。通过使用OpenGL的stencil buffer特性,在渲染物体的同时勾勒出其轮廓,解决传统方法中轮廓线粗细不一或无法精确反映物体轮廓的问题。
Object Outlining
by Max McGuire (06 May 2002)


Return to The Archives
Introduction
31225512_mbF9.png

I was recently charged with the task of rendering outlines around models in the game I am working on to show when they are selected. There are a number of techniques available for accomplishing this, however they typically produce outlines of inconsistent line weight or fail to accurately represent the silhouette of the object.

In this short article I'll present my solution to the problem of producing an accurate, constant weight outline around a polygonal mesh. It recently came to my attention that a similar method was originally published by Rossignac and van Emmerik [1] in 1992.

31225512_VTqY.pngThe technique is very simple:

When rendering the object, write a constant value into the stencil buffer. Then to draw the outline, render the object in wireframe using thick lines wherever the stencil buffer is unset. This will leave a outline around the border of the mesh which is one half the wireframe line thickness.

This can be implemented in OpenGL as shown in the following code snippet.

 glClearStencil(0);
     glClear(GL_STENCIL_BUFFER_BIT); // Render the mesh into the stencil buffer.  glEnable(GL_STENCIL_TEST);

     glStencilFunc(GL_ALWAYS, 1, -1);
     glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);

     RenderMesh(); // Render the thick wireframe version.  glStencilFunc(GL_NOTEQUAL, 1, -1);
     glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);

     glLineWidth(3);
     glPolygonMode(GL_FRONT, GL_LINE);
	
     RenderMesh(); 


One additional detail demonstrated in the sample code is that the stencil buffer can be reset to zero as the wireframe mesh is being rendered. This is done so that each pixel in the outline is rendered only once. When drawing the outline using alpha blending this can be essential to ensure a uniform opacity along the entire outline.


Additional Notes
31225512_mbF9.png

If rendering the wireframe version of the mesh is a performance bottleneck, then a simple geometric silhouette detection method can be used to reduce the number of lines which need to be drawn. This algorithm works by identifying edges in the mesh which are bordered by a 'front facing' face and a 'back facing' face. In addition to including all of the lines which make up the true silhouette of the mesh, this set of edges may also include lines which will fall in the interior of the mesh when they are rendered, however these lines will be discarded by the stencil operation described in the first section.

Direct3D does not support thick line rendering, however the same result can be achieved by drawing the lines using a camera-facing quad. Pierre Terdiman presents source code [2] to accomplish this.

Finally, it may be prohibitive for an application to require a stencil buffer just to achieve the outline effect. In the abscense of a stencil buffer, the depth buffer can be used in a similar manner as descibed by Rossignac and van Emmerik [1].


References
31225512_mbF9.png

[1] J. Rossignac and M. van Emmerik. Hidden contours on a framebuffer. In Proceedings of the 7th Workshop on Computer Graphics Hardware, Eurographics, September 1992.

[2] Pierre Terdiman. Textured Lines In D3D

转载于:https://my.oschina.net/lyr/blog/118960

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值