Custom color interpolation within a polygon

In smooth shading (RGBA mode), if I provide separate colors for every vertex in a polygon, OpenGL performs linear color interpolation between vertices. E.g., if two adjacent vertices in quadrangle are red and two others are blue, there is a smooth transition from red to blue through the RGB color (0.5, 0, 0.5).
I would like OpenGL to produce a smooth transition between red and blue through well-known color scale (roughly, red-orange-yellow-green-cyan-blue).
One possible way I know is to use color index mode. But I’d like to make sure that it’s impossible - or how is it possible - to do the things in RGBA mode.
Could anybody give any hint?

Use a 1D texture, or use some nifty fragment program if you can.

Thanks! I expected something like this in fact. Don’t you know where I could find a code sample for this particular task?

Interesting how this problem is the same as LERP vs SLERP.

A fragment program would have to do this for each fragment - convert RGB to HLV, interpolate, convert HLV back to RGB. It might be slow. You could speed it up by specifying the vertex colors in HLVA, avoiding the initial conversion.

Thanks to all! I had some fun playing with textures last night I don’t want to deal with fragmenting since now I’ve got what I need.

Long live OpenGL ! ))

I have detected quite strange behaviour of OpenGL when using 1D textures for drawing color contours on quadrangles.
It looks like OpenGL interpolates 1D texture coordinate within a quadrangle in two steps:

  1. splitting a quadrangle into two triangles;
  2. interpolating texture coordinates for each triangle independently.
    The result is that very often there is a clearly visible diagonal on each quadrangle so that color contours have a sharp bend lying on that diagonal.
    I don’t know how to fight this. Does anybody?

Implementations do the same for colors. It is pretty fundamental although there is nothing in OpenGL that says implementations MUST do this. They just all do.

Draw a quad like this:

Black +---------+ White
      |         |
      |         |
      |         |
      |         |
      |         |
      |         |
White +---------+ Black

You will find that the diagonal will either be shaded white or black depending on the triangulation.

One possible solution is to further subdivide your model, with an interpolated color (or 1D coordinate in your case).

Black +---------+ White
      |\       /|
      | \     / |
      |  \   /  |
      Gray>+<   |
      |  /   \  |
      | /     \ |
      |/       \|
White +---------+ Black

[This message has been edited by dorbie (edited 04-06-2003).]

Hmm, your proposal will make quadrangles look a bit nicer, but…still there are sharp bends and moreover there are four ones per quadrangle instead of one by default.

I tried to investogate another alternative: creating an individual 2D texture for each quadrangle. Each pixel’s color is calculated as linear combinations of all four quadrangle corner colors. For me, this is the right solution from the presentation viewpoint, but I still don’t have enough information on the performance. I guess it will be very low if several thousands quadrangles are rendered…