Gradient background

Hi,

I’m trying to draw a horizontal gradient background of an OpenGL scene and I did not find any suitable idea. Does anyone did something similar ?

Thanks a lot !!

glMatrixMode(GL_PROJECTION);
glLoadIdentity();

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

glBegin(GL_QUADS);
//red color
glColor3f(1.0,0.0,0.0);
glVertex2f(-1.0, 1.0);
glVertex2f(-1.0,-1.0);
//blue color
glColor3f(0.0,0.0,1.0);
glVertex2f(1.0,-1.0);
glVertex2f(1.0, 1.0);
glEnd();

Thanks a lot Nico !

Works like a charm but the gradient is vertical. Do you know how to draw it horizontally ?

Thanks again !

Try to swap around randomly the glVertex coords until you get it right :wink:

Just adapt the vertex parameters to this:

glBegin(GL_QUADS);
//red color
glColor3f(1.0,0.0,0.0);
glVertex2f(-1.0,-1.0);
glVertex2f(1.0,-1.0);
//blue color
glColor3f(0.0,0.0,1.0);
glVertex2f(1.0, 1.0);
glVertex2f(-1.0, 1.0);
glEnd();

But since you had to ask this question I suggest you take a look at the NeHe tutorials to get you started on OpenGL programming. :wink:

I was just started to play around with the glVertex coords but this one definitely solved my problem.

Thanks a lot guys !!

Guys,

I’m facing with a weird issue with the linear gradient fade-out.
The code that works is the following:

glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glDisable(GL_LIGHTING);
glBegin(GL_QUADS);
//red color
glColor3f(1.0,0.0,0.0);
glVertex2f(-1.0,-1.0);
glVertex2f(1.0,-1.0);
//blue color
glColor3f(0.0,0.0,1.0);
glVertex2f(1.0, 1.0);
glVertex2f(-1.0, 1.0);
glEnd();

glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);

However adding a torus on the scene, only the portion on top of the gradient will be displayed. The portion behind the gradient is not visible. It looks like the depth of field of the scene is smaller than before drawing the gradient.

Is there any state I should save before drawing the gradient and retore it after ?

Thanks a lot !!

  1. call glDisable(GL_DEPTH_TEST);
  2. draw background
  3. call glEnable(GL_DEPTH_TEST);
  4. draw torus

Thanks Overlay ! That did the trick !!