下午好,
我正在尝试学习使用使用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 );
有什么建议吗?
由于
答案 0 :(得分:1)
原来是多个OpenGL上下文。在尝试绘画之前,它没有回头。