簡體   English   中英

無法在OpenGL中繪制三角形,但其他圖元可以很好地渲染

[英]Can't draw triangles in OpenGL but other primitives render fine

下午好,

我正在嘗試學習使用使用OpenGL的圖形庫。 我可以繪制2D基本體(文本和線條),但3D三角形不渲染。 我已經嘗試了所有可以想到的方法,但是作為OpenGL新手,我可能錯過了一些顯而易見的東西。

此代碼並非旨在提高效率。 我正在嘗試使其首先工作。

這是啟動時的設置:

  // 800 by 600 windows 32 bit depth
  Driver->setDisplay( UDriver::CMode( ScreenWidth, ScreenHeight, 32 ) );
  NL3D::CViewport viewport;
  viewport.initFullScreen();
  Driver->setViewport( viewport );

  NLMISC::CMatrix mtx;
  mtx.identity();
  Driver->setViewMatrix( mtx );
  Driver->setModelMatrix( mtx );

  // screen size is same as pixel resolution
  // CFrustum(float left, float right, float bottom, float top, float znear, float zfar, bool perspective= true)
  Driver->setMatrixMode2D( CFrustum( 0.0f, ScreenWidth, 0.0f, ScreenHeight, -2.0f, 10000.0f, false ) );

這是渲染循環中的代碼:

static NL3D::CMaterial mat;
mat.initUnlit();
mat.setColor( CRGBA( 255, 255, 0, 128 ) );

float x = 200.0f;
float y = 200.0f;
float width = 200.0f; // (float)ScreenWidth * 0.125f;
float height = 200.0f; // (float)ScreenHeight * 0.125f;
static NL3D::CVertexBuffer vb;
if ( vb.getName().empty() )
   vb.setName("drawBitmap");
vb.setVertexFormat( NL3D::CVertexBuffer::PositionFlag | NL3D::CVertexBuffer::TexCoord0Flag );
vb.setNumVertices( 4 );
{
   NL3D::CVertexBufferReadWrite vba;
   vb.lock( vba );
   vba.setVertexCoord( 0, NLMISC::CVector( x, 0, y ) );
   vba.setVertexCoord( 1, NLMISC::CVector( x + width, 0, y ) );
   vba.setVertexCoord( 2, NLMISC::CVector( x + width, 0, y + height ) );
   vba.setVertexCoord( 3, NLMISC::CVector( x, 0, y + height ) );
   vba.setTexCoord( 0, 0, 0.f, 1.f );
   vba.setTexCoord( 1, 0, 1.f, 1.f );
   vba.setTexCoord( 2, 0, 1.f, 0.f );
   vba.setTexCoord( 3, 0, 0.f, 0.f );
}
dynamic_cast<NL3D::CDriverUser*>(Driver)->getDriver()->activeVertexBuffer( vb );

static NL3D::CIndexBuffer pb;
if ( pb.getName().empty() )
   pb.setName("drawBitmap");
pb.setFormat( NL_DEFAULT_INDEX_BUFFER_FORMAT );
pb.setNumIndexes( 6 );
{
   CIndexBufferReadWrite iba;
   pb.lock( iba );
   iba.setTri( 0, 0, 1, 2 );
   iba.setTri( 3, 2, 3, 0 );
}

dynamic_cast<NL3D::CDriverUser*>(Driver)->getDriver()->activeIndexBuffer( pb );
dynamic_cast<NL3D::CDriverUser*>(Driver)->getDriver()->renderTriangles( mat, 0, 2 );

有什么建議么?

謝謝

原來是多個OpenGL上下文。 嘗試繪畫之前並沒有讓事情退縮。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM