I'm attempting to convert my program from GLfloat to GLshort for vertex positions and I'm not sure how to represent this in the shader. I was using a vec3 datatype in the shader but vec3 represents 3 floats. Now I need to represent 3 shorts. As far as I can tell OpenGL doesn't have a vector for shorts so what am I supposed to do in this case?
问题:
回答1:
I'm not sure how to represent this in the shader.
That's because this information doesn't live in the shader.
All values provided by glVertexAttribPointer
will be converted to floating-point GLSL values (if they're not floats already). This conversion is essentially free. So you can use any of these glVertexAttribPointer
definitions with a vec4
attribute type:
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, ...);
glVertexAttribPointer(0, 4, GL_UNSIGNED_SHORT, GL_TRUE, ...);
glVertexAttribPointer(0, 4, GL_SHORT, GL_TRUE, ...);
glVertexAttribPointer(0, 4, GL_SHORT, GL_FALSE, ...);
All of these will be converted into a vec4
automatically. Your shader doesn't have to know or care that it's being fed shorts, bytes, integers, floats, etc.
The first one will be used as is. The second one will convert the unsigned short range [0, 65535] to the [0.0, 1.0] floating-point range. The third will convert the signed short range [-32768, 32767] to the [-1.0, 1.0] range (though the conversion is a bit odd and differs for certain OpenGL versions, so as to allow the integer 0 to map to the floating point 0.0). The fourth will convert [-32768, 32767] to [-32768.0, 32767.0], as a floating-point range.
The GLSL type you use for attributes only changes if you use glVertexAttribIPointer
or glVertexAttribLPointer
, neither of which is available in OpenGL ES 2.0.
In short: you should always use float GLSL types for attributes. OpenGL will do the conversion for you.