当我使用 GL_HALF_FLOAT_OES 纹理时;片段着色器似乎可以很好地写入它,当我在 xCode 中使用 gpuTrace 时,我可以观察到它具有正确的值。但是当我使用
读回它时glReadPixels( 0, 0, self.imageSize->v[0], self.imageSize->v[1], GL_RGBA, type, pixels );
像素分量要么 float -0 要么 float 1。这是因为 float 值被意外剪切了吗?有没有办法读回最初写入的浮点值?如果我使用 GL_RGBA16F_EXT
或 GL_RGBA32F_EXT
而不是 GL_RGBA
我会得到无效的枚举。
基于 Rabbid76 的反馈
int formatExt, typeExt;
glGetIntegerv( GL_IMPLEMENTATION_COLOR_READ_FORMAT, &formatExt );
glGetIntegerv( GL_IMPLEMENTATION_COLOR_READ_TYPE, &typeExt );
[ self traceLog:[ NSString stringWithFormat:@"Implementation format is %@, type is %@", [ TextureImage strForFormat:formatExt ], [ TextureImage strForType:typeExt ] ] ];
返回值是“实现格式为 GL_RGBA,类型为 GL_HALF_FLOAT_OES”。这可能意味着 HALF_FLOAT 是它能够读回的唯一值。我仍在调查它是否有可能读回 GL_FLOAT,因为尽管 glGetIntegerv 返回了什么,glReadPixels 不会用无效枚举标记 GL_FLOAT;它接受并处理它,但 float 似乎不是正确的 float ;所以它似乎在进行一些意想不到的转换。
最佳答案
请参阅 glReadPixels
(OpenGL ES 3.1) 的文档其中说:
void glReadPixels( GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, GLvoid * data);
format
Specifies the format of the pixel data. The following symbolic values are accepted:GL_RGBA
, andGL_RGBA_INTEGER
. An implementation-chosen format will also be accepted. This can be queried withglGet
andGL_IMPLEMENTATION_COLOR_READ_FORMAT
.
type
Specifies the data type of the pixel data. Must be one ofGL_UNSIGNED_BYTE
,GL_UNSIGNED_INT
,GL_UNSIGNED_INT_2_10_10_10_REV
,GL_INT
, orGL_FLOAT
. An implementation-chosen type will also be accepted. This can be queried withglGet
andGL_IMPLEMENTATION_COLOR_READ_TYPE
.
这意味着您必须读取实现选择的 format
和 type
,以读回最初写入的浮点值:
int format = glGet(GL_IMPLEMENTATION_COLOR_READ_FORMAT);
int type = glGet(GL_IMPLEMENTATION_COLOR_READ_TYPE);
glReadPixels(
0, 0, self.imageSize->v[0], self.imageSize->v[1],
format, type, pixels );
请参阅 glGet
(OpenGL ES 3.1) 的文档:
GL_IMPLEMENTATION_COLOR_READ_FORMAT
params returns one value, the format chosen by the implementation in which pixels may be read from the color buffer of the currently bound framebuffer.
GL_IMPLEMENTATION_COLOR_READ_TYPE
params returns one value, the type chosen by the implementation with which pixels may be read from the color buffer of the currently bound framebuffer.
关于ios - GL_HALF_FLOAT_OES 和 glReadPixels,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46626336/