Drawing a line using openGL does not work
        Posted  
        
            by 
                vikasm
            
        on Game Development
        
        See other posts from Game Development
        
            or by vikasm
        
        
        
        Published on 2014-08-20T08:34:49Z
        Indexed on 
            2014/08/20
            10:35 UTC
        
        
        Read the original article
        Hit count: 280
        
opengl
I am a beginner in OpenGL and tried to write my first program to draw some points and a line. I can see that the window opens with white background but no line is drawn. I was expecting to see red colored (because glColor3f(1.0, 0.0, 0.0);) dots (pixels) and line. But nothing is seen. Here is my code.
void init2D(float r, float g, float b)
{
    glClearColor(r,g,b,0.0);
    glMatrixMode(GL_PROJECTION);
    gluOrtho2D(0.0, 200.0, 0.0, 150.0);
}
void display()
{
    glClear(GL_COLOR_BUFFER_BIT);
    glColor3f(1.0, 0.0, 0.0);
    glBegin(GL_POINTS);
    for(int i = 0; i < 10; i++)
    {
        glVertex2i(10+5*i, 110);
    }
    glEnd();
    //draw a line
    glBegin(GL_LINES);
    glVertex2i(10,10);
    glVertex2i(100,100);
    glEnd();
    glFlush();
}
int main(int argc, char** argv)
{
    //Initialize Glut
    glutInit(&argc, argv);
    //setup some memory buffers for our display
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
    //set the window size
    glutInitWindowSize(500, 500);
    //create the window with the title 'points and lines'
    glutCreateWindow("Points and Lines");
    init2D(0.0, 0.0, 0.0);
    glutDisplayFunc(display);
    glutMainLoop();
}
I wanted to verify that the glcontext was opening properly and used this code:
int main(int argc, char **argv)
{
    glutInit(&argc, argv);
    //setup some memory buffers for our display
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
    //set the window size
    glutInitWindowSize(500, 500);
    //create the window with the title 'points and lines'
    glutCreateWindow("Points and Lines");
    char *GL_version=(char *)glGetString(GL_VERSION);
    puts(GL_version);
    char *GL_vendor=(char *)glGetString(GL_VENDOR);
    puts(GL_vendor);
    char *GL_renderer=(char *)glGetString(GL_RENDERER);
    puts(GL_renderer);
    getchar();
    return 0;
}
And the ouput I got was:
- 3.1.0 - Build 8.15.10.2345
 - Intel
 - Intel(R) HD Graphics Family
 
Can someone point out what I am doing wrong ? Thanks.
© Game Development or respective owner