Search code examples
copenglglut

my open gl doesn't work properly?


i was trying to setup opengl libraries on visual studio 2013 this code for drawing a triangle i don't have error in error list which relate to include files but it dosen't work this is the code:

#include<glut.h>

static void redraw();

void main()
{
    glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
    glutInitWindowPosition(100, 100); 
    glutInitWindowSize(400, 300); 
    glutCreateWindow("Application11");
    glutDisplayFunc(redraw);
    glutMainLoop();
}

static void redraw()
{
    glBegin(GL_TRIANGLES);
    glVertex2f(0,1);
    glVertex2f(0.5,0);
    glVertex2f(0.5,0);
    glColor3f(1,0,0);
    glEnd();
    glFlush();
}

Solution

  • You have to initialize the GLUT library (see glutInit):

    Either:

    int main()
    {
        int cnt = 0;
        glutInit(&cnt, NULL);
        .....
    

    or

    int main(int argc, char** argv)
    {
        glutInit(&argc, argv);
        .....
    

    Further 2 points of your triangle are equal. Change it for example to:

     glBegin(GL_TRIANGLES);
     glColor3f(1.0f, 0.0f, 0.0f);
     glVertex2f( 0.0f, 1.0f );
     glVertex2f( 0.5f, 0.0f );
     glVertex2f( 0.5f, 1.0f );
     glEnd();
    


    See further Undefined reference when using glew and mingw?