glTexImage2D 空数据指针和内存初始化

glTexImage2D null data pointer and memory initialization

让我们假设 2D 纹理是使用 glTexImage2D 和空数据指针定义的。传递空数据指针是否会使内存归零,或者当片段着色器对该纹理进行采样时我可能会得到一些垃圾数据?

vec3 color = texture(texture0, texCoord).xyz;

documentation clearly states:

data may be a null pointer. In this case, texture memory is allocated to accommodate a texture of width width and height height. ... The image is undefined if the user tries to apply an uninitialized portion of the texture image to a primitive.

所以是的——你会得到垃圾。请记住,零也是有效的垃圾,因此不要将其用作它“有效”的证据。

将纹理清除为全零的最简单方法是 with glClearTexImage:

glClearTexImage(texture, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);

对于这个函数 data == 0 实际上意味着“全零”。