Genpfault极大地帮助了我,使我的代码得以编译。下面的代码是我目前正在使用的简化版本。但是,Glut不允许我绘制形状。编译后运行可执行文件时,唯一发生的是我看到了桌面的屏幕截图。
如何解决此问题,以便实际上可以在黑屏上绘制彩色三角形和多边形? Screenshot of what the program looks like when it runs
#include <iostream>
#include <GL/freeglut.h>
namespace GameBoxes
{
template<class T>
class Box
{
public:
void display( void );
};
} //GameBoxes
namespace GameBoxes
{
template <class T>
void Box<T>::display( void )
{
glClearColor( 0.0, 0.0, 0.0, 0.0 ); // black background
glClear( GL_COLOR_BUFFER_BIT );
glColor3f( 0.2, 0.2, 0.2 );
glBegin( GL_TRIANGLES );
glVertex2f( -0.5, -0.5 );
glVertex2f( 0.5, -0.5 );
glVertex2f( 0.0, 0.5 );
glutSwapBuffers();
}
} // GameBoxes
int main( int argc, char **argv )
{
glutInit( &argc, argv );
int windowPos1 = 0, windowPos2 = 0, windowSize1 = 400, windowSize2 = 400;
glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH );
glutInitWindowPosition( windowPos1, windowPos2 );
glutInitWindowSize( windowSize1, windowSize2 );
glutCreateWindow( "square" );
GameBoxes::Box<double> square;
glutSetWindowData( &square );
glutDisplayFunc( []()
{
auto instance = static_cast< GameBoxes::Box<double>* >( glutGetWindowData() );
instance->display();
} );
glutMainLoop();
return 0;
};
答案 0 :(得分:1)
您错过了用glEnd
完成Declaring OS-level libraries序列
template <class T>
void Box<T>::display( void )
{
glClearColor( 0.0, 0.0, 0.0, 0.0 ); // black background
glClear( GL_COLOR_BUFFER_BIT );
glColor3f( 0.2, 0.2, 0.2 );
glBegin( GL_TRIANGLES );
glVertex2f( -0.5, -0.5 );
glVertex2f( 0.5, -0.5 );
glVertex2f( 0.0, 0.5 );
glEnd(); // <-------------- add glEnd
glutSwapBuffers();
}
请注意,自几十年来,不赞成使用glBegin
/glEnd
序列进行绘制。
阅读有关glBegin
/glEnd
的信息,并参阅Fixed Function Pipeline和Vertex Specification了解最新的渲染方式。