【问题标题】:OpenGL Texture DSA not showing textureOpenGL 纹理 DSA 不显示纹理
【发布时间】:2021-03-10 23:25:35
【问题描述】:

我正在尝试使用较新的 DSA 函数来显示纹理,但它根本不起作用。

这是使用旧方法的代码。

unsigned int containerTexture = 0;
glGenTextures(1, &containerTexture);
glBindTexture(GL_TEXTURE_2D, containerTexture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

int width = 0, height = 0, channelCount = 0;
stbi_set_flip_vertically_on_load(true);
unsigned char* pixels = stbi_load("res/textures/container.jpg", &width, &height, &channelCount, 0);
if (pixels) {
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, pixels);
    glGenerateMipmap(GL_TEXTURE_2D);
} else {
    cerr << "Failed to load texture! \n";
}
stbi_image_free(pixels);

这是 DSA 版本。

unsigned int containerTexture = 0;

int width = 0, height = 0, channelCount = 0;
stbi_set_flip_vertically_on_load(true);
unsigned char* pixels = stbi_load("res/textures/container.jpg", &width, &height, &channelCount, 0);
if (pixels) {
    glCreateTextures(GL_TEXTURE_2D, 1, &containerTexture);

    glTextureParameteri(containerTexture, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTextureParameteri(containerTexture, GL_TEXTURE_WRAP_T, GL_REPEAT);
    glTextureParameteri(containerTexture, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTextureParameteri(containerTexture, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

    glTextureStorage2D(containerTexture, 1, GL_RGB8, width, height);
    glTextureSubImage2D(containerTexture, 1, 0, 0, width, height, GL_RGB, GL_UNSIGNED_BYTE, pixels);
    glGenerateTextureMipmap(containerTexture);

    glBindTextureUnit(0, containerTexture);
} else {
    cerr << "Failed to load texture! \n";
}
stbi_image_free(pixels);

【问题讨论】:

    标签: c++ opengl textures opengl-4


    【解决方案1】:

    glTextureSubImage2D 的第二个参数是要设置的level。 Mipmap 级别是从零开始的,因此基本级别是 0 而不是 1,如您的代码中所示:

    glTextureSubImage2D(containerTexture, 0, 0, 0, width, height, GL_RGB, GL_UNSIGNED_BYTE, pixels);
    

    【讨论】:

      猜你喜欢
      • 2014-07-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多