0

I've got colors in GL_UNSIGNED_BYTE r,g,b but I want to use the alpha channel to put a custom value that is going to be used inside the pixel shader to color the geometry differently. There are two possible values 0 and 127 now my problem is that when I do this in the vertex shader :

[vertex]

varying float factor;

factor = gl_Color.w

it seems that the factor is always 1.0 because if I do this:

[fragment]

varying float factor;

factor = factor;

gl_FragColor = vec4(factor, 0.0, 0.0, 1.0)

The output is always red why I would expect two different colors, one when the factor is zero and one when the factor is 127.

So if I assign two values 0 and 127 I should get in the vertex shader 0/0.5? is this correct?

[Edit] Ok I see now two different values but I don't know why I get them, there s any operation the GPU does in the gl_Colow.w component I am not aware of?

[Edit2] As Nicholas has pointed out I am using glColorPointer(4...);

user1583007
  • 399
  • 1
  • 5
  • 17
  • possible solutions http://stackoverflow.com/questions/4191310/what-does-gl-unsigned-byte-mean-for-glteximage2d http://www.gamedev.net/topic/260318-unsigned-char-vs-gl_unsigned_byte/ – user827992 Aug 16 '12 at 17:06

1 Answers1

2

Since you are using the gl_Color input, and you make reference to GL_UNSIGNED_BYTE, I surmise that you are using glColorPointer to specify these color values. So in your code, you're calling something to the effect of:

glColorPointer(4, GL_UNSIGNED_BYTE, ...);

(BTW, in the future, it would be best if you actually provide this information, rather than forcing us to deduce it)

So, first issue: are you actually using 4 for the number of color components? You should be, but you don't say one way or the other.

Now that this has been corrected, let's get to the real issue (or at least it was in the original form of the question):

factor = factor/127.0;(to normalize)

OpenGL already normalized that for you. If you use any integer type with glColorPointer, the values you get in gl_Color will be normalized, either [0, 1] for UNSIGNED types, or [-1, 1] for non-unsigned types.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982