【问题标题】:glTexImage2D returns invalid valueglTexImage2D 返回无效值
【发布时间】:2018-01-07 18:18:25
【问题描述】:

我找不到原因。我是这样称呼它的

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, surf->w, surf->h, 0, data_fmt, GL_UNSIGNED_BYTE, surf->pixels)

width 和 height 参数为 872 639 内部格式为 GL_RGBA

可能是什么问题?我检查了https://www.khronos.org/registry/OpenGL-Refpages/es1.1/xhtml/glTexImage2D.xml,它看起来很好(除非宽度或高度大于我怀疑的最大纹理大小,或者不支持内部格式 RGBA,这也不太可能)

这是完整的代码

out << "loading " << fileName << "\n";
dimensions.setZero();
name = fileName;
this->repeat = repeat;

SDL_Surface* surf = IMG_Load(fileName.c_str());
if (surf == NULL)
{
    out << "surface_error: " << fileName << " " << SDL_GetError() << "\n";
    hasError = true;
    return;
}

GLenum data_fmt;
if (surf->format->BytesPerPixel == 4)
{
    data_fmt = GL_RGBA;
}
else if (surf->format->BytesPerPixel == 3)
{
    data_fmt = GL_RGB;
}
else if (surf->format->BytesPerPixel == 1)
{
    data_fmt = GL_RED;
}
else
{
    assert(false);
}
GL_CALL(glGenTextures(1, &gTexture));
GL_CALL(glBindTexture(GL_TEXTURE_2D, gTexture));
GL_CALL(glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, surf->w, surf->h, 0, data_fmt, GL_UNSIGNED_BYTE, surf->pixels));
if (hadGLError)
{
    out << data_fmt << " " << surf->w << " " << surf->h << "\n";
}
GL_CALL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR));
GL_CALL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR));
dimensions = IntVec2(surf->w, surf->h);

glGenerateMipmap(GL_TEXTURE_2D);
GL_CALL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, repeat ? GL_REPEAT : GL_CLAMP_TO_EDGE));
GL_CALL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, repeat ? GL_REPEAT : GL_CLAMP_TO_EDGE));
GL_CALL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR));
GL_CALL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR));

SDL_FreeSurface(surf);

GL_CALL 检查 glGetError 并将其设置为 GL_INVALID_VALUEglTexImage2D 之后的 0x0501。尝试设置换行参数时游戏崩溃

【问题讨论】:

  • 您实际使用的是哪个 GL 版本?另外,“他的游戏在尝试设置包装参数时崩溃”应该是什么意思? glTexParameteri 的崩溃会很奇怪。
  • @derhass 如果您要问的话,我使用 SDL_GL_SetAttribute 将 GL 版本设置为 3.2。遗憾的是,在客户端能够打印其实际的 GL 版本之前,游戏就崩溃了。设置GL_TEXTURE_WRAP_S的游戏在线崩溃,我不知道如何。这是调用堆栈justpaste.it/1feu9 和源代码github.com/shultays/bloodworks/blob/master/game/source/…
  • 在您的 glTexImage2D 调用中,您为什么不使用 data_fmt 来处理内部格式和格式?如果它们不同怎么办?另外,我认为您不能为不是 2 次方的纹理生成 mipmap。 stackoverflow.com/questions/45430608/…
  • @Rabbid76,很高兴知道。谢谢。
  • 不,你不请求 3.2 上下文。您正在使用旧上下文并在之后设置这些 SDL 属性,这不会做任何事情。

标签: c++ opengl sdl


【解决方案1】:

我认为问题是由其他原因引起的,opengl 没有正确启动。

【讨论】:

    猜你喜欢
    • 2023-03-08
    • 2017-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多