Gradient line

I am trying to draw a gradient line.

This does not work:

    glColor3f(r, g, b)
    glVertex2f(x, y)
    glColor3f(r2, g2, b2)
    glVertex2f(x2, y2)

Any other method for getting a smooth gradient?

Need more info. ‘This does not work’ is a little vague.
What happens when you run this? Do you see anything?
If so, what do you see? Can you post a picture?

well you’re just drawing a line from left to right, hard to see a 1 pixel long line. try a GL_TRIANGLE or GL_QUADS instead of GL_LINES so

glColor3f(r, g, b)
glVertex2f(x, y) //Top Left
glColor3f(r2, g2, b2)
glVertex2f(x2, y2)//Top Right
glColor3f(r2, g2, b2)
glVertex2f(x2, y + 20) //Bottom right
glColor3f(r, g, b)
glVertex2f(x, y2 + 20)//Bottom Left

if the gradient works, then you can go back to GL_LINES (since now you know it work, just hard to see)
Note I did not test this code. So take it with a grain of salt.

When using both my example code and hugo_the_dwarfs the line/quad is drawn solid in the last set glColor3f…

That suggests that glShadeModel(GL_FLAT) is in effect. Use glShadeModel(GL_SMOOTH) to change it back.

Thank you very much, this was indeed the problem!