We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpOpenGL and 3D Libraries › Efficient way to draw large gradient
Page Index Toggle Pages: 1
Efficient way to draw large gradient? (Read 3032 times)
Efficient way to draw large gradient?
Mar 21st, 2007, 2:00pm
 
Hello,

I'd like to use a gradient background for my sketch using Processing/OpenGL. The sketch can be large, 1000x1000 or so.  The gradient is 1-dimensional; i.e. the color changes along the vertical axis but not along the horizontal axis.  I'm very familiar with Java2D programming but not with Processing or OpenGL.

One possible solution is to render a 1-pixel-wide image and use that as a texture.  The image would need to be re-rendered when the sketch is resized so that the gradient is the correct height.

Specifically, I'm wondering:

- Is there a more efficient way to do this?

- If the dynamically-rendered texture image is the way to go, exactly how do I do this in Processing (i.e. how to render into a PImage)?

Thanks in advance for any tips!
Chris
Re: Efficient way to draw large gradient?
Reply #1 - Mar 21st, 2007, 6:44pm
 
In GL, the solution would be to draw a QUAD and color the vertices. Here's some example code.

Code:
beginShape(QUADS);
fill(255,255,0);
vertex(0,0);
vertex(width,0);
fill(0,0,255);
vertex(width,height);
vertex(0,height);
endShape();

That will create a vertical gradient from yellow to blue. Just adjust the verts to suit your needs.

ryan
Re: Efficient way to draw large gradient?
Reply #2 - Mar 21st, 2007, 8:48pm
 
Thanks, that works great.  I knew about filling in vertices in this way, but for some reason I had assumed that it wouldn't work efficiently.  Go, hardware acceleration.

A minor correction, just in case anyone else reads this: The second and third lines should be "fill(255,255,0); vertex(0,0);".

Thanks again,
Chris
Re: Efficient way to draw large gradient?
Reply #3 - Mar 26th, 2007, 9:58am
 
haha oops Tongue i fixed it
Re: Efficient way to draw large gradient?
Reply #4 - Apr 3rd, 2007, 12:31am
 
But the quad will be just like any other 3D object, so you have to clear the depth buffer or far-away objects will be obscured by your background..

Re: Efficient way to draw large gradient?
Reply #5 - Apr 23rd, 2007, 1:24am
 
you can use a sphere to do more complex gradients too.
just put a big sphere and "spot-light" it with the color you want.
Page Index Toggle Pages: 1