Using GLshort instead of GLfloat for vertices

I am trying to convert my program from GLfloat to GLshort for vertex positions, and I'm not sure how to present this in a shader. I used the vec3 data type in the shader, but vec3 represents 3 floats. Now I need to submit 3 shorts. As far as I can tell, OpenGL does not have a vector for shorts, so what should I do in this case?

+6
source share
1 answer

I'm not sure how to present this in a shader.

This is because this information does not live in the shader.

All values ​​provided by glVertexAttribPointer will be converted to floating-point GLSL values ​​(if they are no longer a float). This conversion is essentially free. Therefore, you can use any of these glVertexAttribPointer definitions with the vec4 attribute vec4 :

 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 them will be automatically converted to vec4 . Your shader should not know or care about being fed shorts, bytes, integers, floats, etc.

The first will be used as is. The second converts the unsigned short range [0, 65535] to the range [0.0, 1.0] with a floating point. The third converts the signed short range [-32768, 32767] to the range [-1.0, 1.0] ( although the conversion is a bit odd and differs for some versions of OpenGL to allow integer 0 to match floating point 0.0 ). The fourth converts [-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 , none of which are available in OpenGL ES 2.0.

In short: you should always use GLSL floating point types for attributes. OpenGL will do the conversion for you.

+16
source

Source: https://habr.com/ru/post/921436/


All Articles