The triangle doesn't appear on the screen. I am using Dev c++ 4.9.9.2(i know that you don't like it, but for me it's still the best :) with free glut.
Here's the code:
#include <GL/glut.h>
void display(){
glClear ( GL_COLOR_BUFFER_BIT );
glutSwapBuffers();
glBegin ( GL_TRIANGLES );
glColor3f ( 0.0, 1.0, 0.0);
glVertex2f (-0.5,-0.5);
glVertex2f (0.5,-0.5);
glVertex2f (0.0, 0.5);
glEnd();
}
void reshape ( int width, int height ){
glViewport ( 0, 0, width, height );
}
void initOpenGL(){
glClearColor ( 1.0, 0.1, 0.0, 1.0 );
}
int main (int argc, char **argv){
glutInit ( &argc , argv );
glutInitDisplayMode ( GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH );
glutInitWindowSize ( 500, 500 );
glutInitWindowPosition ( 100, 100 );
glutCreateWindow ( "OpenGl" );
initOpenGL();
glutDisplayFunc ( display );
glutIdleFunc ( display );
glutReshapeFunc ( reshape );
glutMainLoop ();
return 0;
}
Let's take a closer look at your display
function:
display()
{
glClear();
glutSwapBuffers();
drawTriangle();
}
glClear()
clears the backbuffer to a constant color. glutSwapBuffers()
swaps frontbuffer and backbuffer. This will show the current content of the backbuffer (which is just the clear color) on the screen. The content of the new backbuffer will be undefined. Every draw call will draw to the backbuffer.
As you see, the program has never a chance to display something other than the clear color on screen. Just move glutSwapBuffers()
to the end of the function:
display()
{
glClear();
drawTriangle();
glutSwapBuffers();
}
And please pick a more recent tutorial. You are using the fixed function pipeline, which has been deprecated for about 10 years. You can recognize it by calls like glBegin()
or glVertex3f()
. There is no reason to learn something that is dying (or already dead on some platforms).