Decide a pixel's color based on its position?

Hi,

I am trying to coloring a 3D tall cube with two different colors.

With one color starting from the top and the other starting from the bottom.

I want to do something like the original fixed pipeline does for the color interpolation.

Except

  1. I don’t need color interpolation. So there are two distinct parts with different colors.
  2. I’d like to be able to specify where the colors intersect with each other. For example, a tall cube starting from (0,0,0) to (0, 10, 0), I want to paint it green from (0,0,0) to (0,x,0) , and red from (0, 10-x,0 ) to (0,10,0).

It hits me that I could probably do it by defining some user defined varying variable in shader. But I have no idea how to do it. Please help me. Thanks.

In the fragment shader you can get the fragment position interpolated from the vertex shader. Then if fragment y component is below x draw it green and otherwise, red.

You can also split the cube in two parts and draw each part with specific color.

It works.

I have to use the interpolated texture coordinates instead vertex position.

The coordinates are ranging from 0.0 to 1.0.
So I just have to decide at which place those two colors are separated.

If Texcoord.t is less than the level, draw fragments as one color.
If Texcoord.t is greater than the level, draw fragments as the other color.