At the moment, when I need to process colour attributes, I pass them to the shader as vec4, and for glVertexAttribPointer() I use the GL_FLOAT type. Eg.

C code:
glVertexAttribPointer(index, 4, GL_FLOAT, GL_FALSE, stride, colour_data);

attribute vec4 colour;

I’ve read that glVertexAttribPointer() can also accept GL_UNSIGNED_BYTE as attribute type. However, for the life of me, I cannot figure out how to let the GLSL shader know about these attributes, since the language only specifies float, int and bool as supported vector/scalar types. There is no support for GLubyte variables. So how do I specify that the attributes are GLubyte if I wanted to save bandwidth and use 4 bytes for colour instead of 4 floats.

PS. I do know that internally everything gets converted to floats in the shader, I just want to learn how I’m supposed to access non float/int/bool variables (eg. how to access short or bytes, since it is a supported argument for glVertexAttribPointer)

Edit OK, I’ve figured it out. The normalized flag in glVertexAttribPointer() indicates whether to convert bytes/shorts/etc to floats. So the GLSL would access the attribute as a vec4. I’m leaving this post here so that others can also read about the problem/solution. The only question I have is performance - is it really worth while? Only benchmarking can answer that question …

If you use GL_UNSIGNED_BYTE in the vertex attribute and specify it as normalized (parameter #4 is GL_TRUE) then it will treat it as unsigned normalized number that you can use as vec4 in the shader as usual. The conversion is done automatically.

I would suggest you to use GL_UNSIGNED_BYTE as for color you most probably can go with 32 bit RGBA representation and it will spare you a lot of bandwidth if you not use floats for color (unless you really need the additional precision).

Thanks for the reply. I figured it out, and was editing my post as you submitted yours. Thanks again for the help. You’re from Hungary, I’m in Vienna, if you ever come over, I’ll treat you for a beer :slight_smile:

Yeah, I just realized it right after I posted. And thanks for the beer proposal :smiley:

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.