简体   繁体   中英

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

Good afternoon,

I'm trying to learn to use a graphics library that uses OpenGL. I can draw 2D primitives (text and lines) but 3D triangles don't render. I've tried everything I can think of but as an OpenGL newcomer I've probably missed something obvious.

This code is not intended to be efficient. I'm trying to get it to work first.

Here's the setup at startup:

  // 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 ) );

Here's the code in my rendering loop:

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 );

Any suggestions?

Thanks

It turned out to be multiple OpenGL contexts. It wasn't setting things back before trying to draw.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM