【问题标题】:OpenGL texture not rendered properly on modelOpenGL 纹理未在模型上正确渲染
【发布时间】:2019-11-24 10:54:07
【问题描述】:

在我的 opengl 应用程序中,模型上的纹理未正确渲染。

这是结果的截图:

这是兔子的样子: expected result

这是加载纹理的代码。

stbi_set_flip_vertically_on_load(1); 
m_LocalBuffer = stbi_load(path.c_str(), &m_Width, &m_Height, &m_BPP, 0); 

GLCall(glGenTextures(1, &m_RendererID));
GLCall(glBindTexture(GL_TEXTURE_2D, m_RendererID));
GLCall(glGenerateMipmap(GL_TEXTURE_2D));
GLenum format = GL_RGBA;

//..switching on m_BPP to set format, omitted here

GLCall(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR));
GLCall(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR));
GLCall(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE));
GLCall(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE));

GLCall(glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, m_Width, m_Height, 0, format, GL_UNSIGNED_BYTE, m_LocalBuffer));

GLCall(glBindTexture(GL_TEXTURE_2D, 0));

if (m_LocalBuffer) {
    stbi_image_free(m_LocalBuffer);
}

这是我正在使用的纹理文件 Texture File

我从https://blenderartists.org/t/uv-unwrapped-stanford-bunny-happy-spring-equinox/1101297(3.3Mb 链接)下载了资产

这是我在 texCoords 中阅读的代码

for (size_t i = 0; i < mesh->mNumVertices; i++) {
    //..read in positions and normals

    if (mesh->mTextureCoords[0]) {
        vertex.TexCoords.x = mesh->mTextureCoords[0][i].x;
        vertex.TexCoords.y = mesh->mTextureCoords[0][i].y;
    }        
} 

我正在使用 assimp 将模型作为 obj 文件加载。我只是从结果中读取纹理坐标并将其传递给着色器。 (GLCall 只是我在渲染器中的一个调试宏)

这可能是什么原因?如果需要更多信息,请告诉我。非常感谢!

【问题讨论】:

  • 可以提供纹理文件吗?兔子应该长什么样子? uv 检查器是另一种纹理吗?
  • @AdrienGivry 感谢您的回复!我已经更新了这个问题。而且我相信提供的纹理是整个事物的纹理。有什么我想念的吗?
  • @JoyceChen 纹理是否沿y轴(v)翻转?
  • @Rabbid76 谢谢!是的,事实证明我不应该在加载图像时垂直翻转它(在使用 png 文件之前需要)。现在可以使用了!

标签: c++ opengl graphics rendering assimp


【解决方案1】:

图像似乎垂直翻转(围绕 x 轴)。为了弥补这一点,您必须在加载后手动翻转图像。或者,如果您翻转了图像,那么您必须省略它。图片是否需要翻转,取决于图片格式。

【讨论】: