6

次の問題があります。たとえば、オブジェクトの上にワイヤーフレームのアウトラインをglPolygonOffset描画しようとしている場合など、メッシュでは完全に機能しますが、単純な線では機能しません。

メッシュでの動作は次のとおりです。

// draw object  
mTexture.enableAndBind();
gl::color( Colorf( 1, 1, 1 ) );
gl::draw( mVboMesh );
mTexture.unbind();

// overlay wireframe
gl::enableWireframe();
glLineWidth(1);
glEnable( GL_POLYGON_OFFSET_LINE );
glPolygonOffset( -1, -1 );
glColor3f( 0, 0, 1 );
gl::draw( mVboMesh );
glDisable( GL_POLYGON_OFFSET_LINE );
gl::disableWireframe();

メッシュ

For some reason it doesn't work for lines. What I'm trying to achieve is to draw a coordinate frame's arrows over a grid. I'm using the very same GL_POLYGON_OFFSET_LINE mode as when I was drawing lines, just like I was doing for the wireframe over the object. However in this case glPolygonOffset( -1, -1 ); makes absolutely no difference. I've tried it with huge values like 100 and it's the same. Absolutely no effect. Here is what I'm doing:

// enable 3D rendering
gl::enable( GL_CULL_FACE );
gl::enableDepthRead();
gl::enableDepthWrite();

// drawing the grid
int size = 2000;
int step = 25;
gl::color( Colorf( 0.2f, 0.2f, 0.2f ) );
for( float i = -size; i <= size; i += step )
{
    glBegin( GL_LINES );
    glVertex3f( i, 0, -size );
    glVertex3f( i, 0, size );
    glVertex3f( -size, 0, i );
    glVertex3f( size, 0, i );
    glEnd( );
}

// drawing the arrows
glEnable( GL_POLYGON_OFFSET_LINE );
glPolygonOffset( -1, -1 );

glBegin( GL_LINES );
gl::color( Colorf( 1, 0, 0 ) );
glVertex3f( 0, 0, 0 );
glVertex3f( 100, 0, 0 );
gl::color( Colorf( 0, 1, 0 ) );
glVertex3f( 0, 0, 0 );
glVertex3f( 0, 100, 0 );
gl::color( Colorf( 0, 0, 1 ) );
glVertex3f( 0, 0, 0 );
glVertex3f( 0, 0, 100 );
glEnd( );

glDisable( GL_POLYGON_OFFSET_LINE );

// disable 3D rendering
gl::disableDepthWrite();
gl::disableDepthRead();
gl::disable( GL_CULL_FACE );

and an example of the Z-fighting I get:

行

One hack I've tried and what worked perfectly is:

  1. disable depth read, enable depth write
  2. draw grid
  3. draw arrows
  4. enable depth read
  5. draw other objects

However this is a very special case and while it works for a flat grid and arrows, it wouldn't work for pretty much anything else with a complex shape.

My questions are:

  1. glPolygonOffset がライン オン ポリゴンでは機能するのに、ライン オン ラインでは機能しないのはなぜですか?
  2. 上記のハックに頼らずに、非常に特定の場合にのみ機能するものを修正するにはどうすればよいですか?

// Cinder をフレームワークとして使用していますが、未加工の OpenGL コマンドを使用しているので問題ありません

アップデート

最初のコメントで答えを確認し、その方法も試しましたが、結果はカメラからの距離に依存するため、どちらも機能しません。

//// draw coordinate frame and grid
glDepthRange (0.01, 1.0);
drawGrid( 2000.0f, 25.0f );
glDepthRange (0.0, 0.99);   
gl::drawCoordinateFrame( 100.0f, 5.0f, 2.0f );

glDepthRange (0.0, 1.0);

// draw object  

ミックス

4

1 に答える 1

3

1 つのハックは、視点に少しだけ近い線を引くことだと思います (0.1 近いとしましょう)。これにより、Z ファイティングが回避されます。

そのためには、ポイントから POV 位置までの正規化された方向ベクトルを計算します。次に、小さな係数でスケーリングし、ラインポイント座標に追加します

于 2012-12-06T15:12:16.860 に答える