Jump to content
  • Advertisement
Sign in to follow this  
Lode

OpenGL gradient

This topic is 5030 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm drawing a rectangle like this:
    glBegin(GL_QUADS);
      glColor4f(color1.r / 255.0, color1.g / 255.0, color1.b / 255.0, color1.a / 255.0);
      glVertex3d(x2, h - y1, 1);
      glColor4f(color2.r / 255.0, color2.g / 255.0, color2.b / 255.0, color2.a / 255.0);
      glVertex3d(x1, h - y1, 1);
      glColor4f(color3.r / 255.0, color3.g / 255.0, color3.b / 255.0, color3.a / 255.0);
      glVertex3d(x1, h - y2, 1);
      glColor4f(color4.r / 255.0, color4.g / 255.0, color4.b / 255.0, color4.a / 255.0);
      glVertex3d(x2, h - y2, 1);
    glEnd();
I expected every corner of the rectangle would have another color (see, there's color1, color2, color3 and color4), but it doesn't work: instead, the whole rectangle has color4. What is it supposed to do? In the nehe tutorials, it works for the triangle with blue, red and green corners... Before drawing the rectangle, I already do the following OpenGL commands (It's a 2D engine btw):
    glShadeModel(GL_FLAT);
    glCullFace(GL_BACK);
    glFrontFace(GL_CCW);
    glClearColor(0, 0, 0, 0);
    glViewport(0, 0, screenWidth, screenHeight);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0, screenWidth, 0, screenHeight, -1024, 1024);   

    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    glEnable(GL_BLEND);
    glDisable(GL_TEXTURE_2D);

Share this post


Link to post
Share on other sites
Advertisement
Quote:

glShadeModel(GL_FLAT);

Should be GL_SMOOTH if you want smooth shading. When using flat shading, the color that was current when the primitive was completed is the color that is used for rendering. In your case, color4 was the current color when the fourth vertex formed a complete quad.

Share this post


Link to post
Share on other sites
Quote:
Original post by Lode
glShadeModel(GL_FLAT);


You'll want to use GL_SMOOTH, since the flat shading model doesn't use interpolated colours.

Edit: Beaten by 31 seconds. Grah. ;)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!