简体   繁体   English

无法在OpenGL中绘制三角形,但其他图元可以很好地渲染

[英]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. 我正在尝试学习使用使用OpenGL的图形库。 I can draw 2D primitives (text and lines) but 3D triangles don't render. 我可以绘制2D基本体(文本和线条),但3D三角形不渲染。 I've tried everything I can think of but as an OpenGL newcomer I've probably missed something obvious. 我已经尝试了所有可以想到的方法,但是作为OpenGL新手,我可能错过了一些显而易见的东西。

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. 原来是多个OpenGL上下文。 It wasn't setting things back before trying to draw. 尝试绘画之前并没有让事情退缩。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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