PDA

View Full Version : Gradient background



gourou
03-27-2008, 10:08 AM
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 !!

-NiCo-
03-27-2008, 10:19 AM
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();

gourou
03-27-2008, 10:30 AM
Thanks a lot Nico !

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

Thanks again !

ZbuffeR
03-27-2008, 10:35 AM
Try to swap around randomly the glVertex coords until you get it right ;)

-NiCo-
03-27-2008, 10:36 AM
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 (http://nehe.gamedev.net/) tutorials to get you started on OpenGL programming. ;)

gourou
03-27-2008, 10:43 AM
I was just started to play around with the glVertex coords but this one definitely solved my problem.

Thanks a lot guys !!

gourou
03-27-2008, 11:33 AM
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 !!

overlay
03-27-2008, 12:57 PM
1. call glDisable(GL_DEPTH_TEST);
2. draw background
3. call glEnable(GL_DEPTH_TEST);
4. draw torus

gourou
03-27-2008, 01:41 PM
Thanks Overlay ! That did the trick !!