Genpfault helped me tremendously is getting my code to compile. The code below is a reduced version of what I'm currently working with. However, Glut is not letting me draw shapes. The only thing that happens when I run the executable after compiling is that I see a screenshot of my desktop.
How can I remedy this so that I can actually draw colorful triangles and polygons on a black screen? 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;
};
You missed to finish the glBegin
/glEnd
sequence with glEnd
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();
}
Note, that drawing by glBegin
/glEnd
sequences is deprecated since decades.
Read about Fixed Function Pipeline and see Vertex Specification and Shader for a state of the art way of rendering.