【问题标题】:glTexSubImage leads to black texture, while glTexImage works fineglTexSubImage3d 导致黑色纹理,而 glTexImage3d 工作正常
【发布时间】:2013-09-24 21:25:01
【问题描述】:

当我使用glTexImage2D 绑定我的图像时,它呈现得很好。

首先,片段着色器中的代码:

uniform sampler2D tex;
in vec2 tex_coord;
// main:
fragment_out = mix(
                   texture(tex, tex_coord),
                   vec4(tex_coord.x, tex_coord.y, 0.0, 1.0),
                   0.5
                   );

使用此代码绘制图像:

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 
             0, GL_RGBA, GL_UNSIGNED_BYTE, image);

但如果我通过 glTexSubImage2D 绑定它,纹理将被绘制为黑色。

glTexStorage2D(GL_TEXTURE_2D,
               1,
               GL_RGBA,
               width, height);
glTexSubImage2D(GL_TEXTURE_2D,
                0,
                0, 0,
                width, height,
                GL_RGBA,
                GL_UNSIGNED_BYTE,
                image);

当我在第二个代码中将GL_RGBA 替换为GL_RGBA8GL_RGBA16 时,呈现会失真。

这是整个纹理加载和绑定代码:

GLuint texture; 
glGenTextures(1, &texture);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture);
int width, height;
unsigned char *image = SOIL_load_image(Resource::getPath("Images/2hehwdv.jpg").c_str(), &width, &height, 0, SOIL_LOAD_RGBA);
std::cout << SOIL_last_result() << std::endl;
std::cout << width << "x" << height << std::endl;
glTexStorage2D(GL_TEXTURE_2D,
               1,
               GL_RGBA,
               width, height);
glTexSubImage2D(GL_TEXTURE_2D,
                0,
                0, 0,
                width, height,
                GL_RGBA,
                GL_UNSIGNED_BYTE,
                image);

//glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, image);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glGenerateMipmap(GL_TEXTURE_2D);

有人可以向我解释这种行为吗?

【问题讨论】:

  • 在您的 glTexStorage2D() 呼叫中,GL_RGBA 看起来不是有效大小的 internalFormat。试试GL_RGBA8
  • @genpfault 你说得对,我只需要在glTexStorage2D 中将GL_RGBA 替换为GL_RGBA8(而不是在glTexSubImage2D 中)。您可以将此作为答案添加一些解释吗?

标签: c++ image opengl glsl


【解决方案1】:
glTexStorage2D( GL_TEXTURE_2D, 1, GL_RGBA, width, height );
                                  ^^^^^^^ not sized

GL_RGBA 是一种未调整大小的格式。

glTexStorage2D()internalFormat 参数必须 传递sized internal format

glGetError()glTexStorage2D() would have returned GL_INVALID_ENUM 之后调用:

如果internalformat 不是有效大小的内部格式,则生成GL_INVALID_ENUM

尝试从Table 1 调整大小的格式,例如GL_RGBA8

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多