c++ - OpenGL 纹理未在模型上正确渲染

标签 c++ opengl graphics rendering assimp

在我的 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 只是我在渲染器中的一个调试宏)

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

最佳答案

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

关于c++ - OpenGL 纹理未在模型上正确渲染,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57044792/

相关文章:

c++ - 显示不在模态模式下的对话框

c++ - glutStrokeCharacter 绘制一个 int 变量

multithreading - 使用 OpenGL 加载 GLFW 3.0 资源

opengl - 使用 OpenGL 混合音频

c++ - SDL_SetVideoMode挂起进程

c++ - 切片网格的算法或软件

c++ - boost 序列化,按基类型加载归档类会给出错误的数据

c++ - 我应该选择哪个 C++ 信号/槽库?

java - Windows 在生成许多图像的 Java 代码执行期间(或之后)挂起

c++ - BOOST_THROW_EXCEPTION 导致无限递归