问题描述
我正在尝试将我的程序从 GLfloat 转换为 GLshort 以获得顶点位置,但我不确定如何在着色器中表示它.我在着色器中使用了 vec3 数据类型,但 vec3 代表 3 个浮点数.现在我需要代表 3 条短裤.据我所知,OpenGL 没有用于短裤的向量,那么在这种情况下我应该怎么做?
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?
推荐答案
这是因为这些信息并不存在于着色器中.
That's because this information doesn't live in the shader.
glVertexAttribPointer
提供的所有值都将转换为浮点 GLSL 值(如果它们还不是浮点数).这种转换基本上是免费的.因此,您可以将这些 glVertexAttribPointer
定义中的任何一个与 vec4
属性类型一起使用:
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, ...);
所有这些都会自动转换成vec4
.您的着色器不必知道或关心它正在被输入short、字节、整数、浮点数等.
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.
第一个将按原样使用.第二个将无符号短范围 [0, 65535] 转换为 [0.0, 1.0] 浮点范围.第三个将签名的短范围 [-32768, 32767] 转换为 [-1.0, 1.0] 范围(虽然转换有点奇怪,并且对于某些 OpenGL 版本有所不同,以便允许整数 0 映射到浮点 0.0).第四个将 [-32768, 32767] 转换为 [-32768.0, 32767.0],作为浮点范围.
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.
仅当您使用 glVertexAttribIPointer
或 glVertexAttribLPointer
时,您用于属性的 GLSL 类型才会发生变化,这两种方法在 OpenGL ES 2.0 中均不可用.
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.
简而言之:您应该始终对属性使用浮点 GLSL 类型.OpenGL 将为您完成转换.
In short: you should always use float GLSL types for attributes. OpenGL will do the conversion for you.
这篇关于对顶点使用 GLshort 而不是 GLfloat的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!