Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 6 of 6

Thread: Gradient line

  1. #1
    Junior Member Newbie
    Join Date
    Feb 2012
    Posts
    4

    Gradient line

    I am trying to draw a gradient line.

    This does not work:

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

    Any other method for getting a smooth gradient?

  2. #2
    Junior Member Regular Contributor
    Join Date
    Apr 2012
    Location
    Los Angeles
    Posts
    186
    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?

  3. #3
    Junior Member Newbie
    Join Date
    Jul 2013
    Posts
    12
    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

    Code :
    glBegin(GL_QUADS)
    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
    glEnd()

    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.

  4. #4
    Junior Member Newbie
    Join Date
    Feb 2012
    Posts
    4
    When using both my example code and hugo_the_dwarfs the line/quad is drawn solid in the last set glColor3f...

  5. #5
    Member Regular Contributor
    Join Date
    Jun 2013
    Posts
    490
    Quote Originally Posted by paleajed View Post
    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.

  6. #6
    Junior Member Newbie
    Join Date
    Feb 2012
    Posts
    4
    Thank you very much, this was indeed the problem!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •