c++ - OpenGL(核心配置文件)纹理缺失或黑色

标签 c++ opengl textures assimp

我正在使用模型的 .obj 附带的 .mtl 文件中的引用来加载纹理。 为此,我正在使用 Assimp 库。

问题是,如标题所述,模型上没有显示纹理,或者(我无法相信)它是黑色的。

这是加载纹理的代码:

std::vector<texture> model::loadMaterialTextures(aiMaterial * mat, aiTextureType type, std::string typeName)
{
    std::vector<texture> textures;

    for (GLuint i = 0; i < mat->GetTextureCount(type); i++)
    {
        aiString str;
        mat->GetTexture(type, i, &str);

        GLboolean skip = false;

        for (GLuint j = 0; j < textures_loaded.size(); j++)
        {
            if (textures_loaded[j].path == str)
            {
                skip = true;
                break;
            }
        }

        if (!skip)
        {
            texture tex;
            tex.id = textureFromFile(str.C_Str(), this->directory);
            tex.type = typeName;
            tex.path = str;
            textures.push_back(tex);
            this->textures_loaded.push_back(tex);
        }
    }

    return textures;
}

GLint model::textureFromFile(const char* path, std::string directory)
{
    //Generate texture ID and load texture data 
    std::string filename = std::string(path);
    filename = directory + '\\' + filename;

    GLuint textureID;
    glGenTextures(1, &textureID);
    glBindTexture(GL_TEXTURE_2D, textureID);

    int width, height;
    unsigned char* image = SOIL_load_image(filename.c_str(), &width, &height, 0, SOIL_LOAD_RGB);

    std::cout << SOIL_last_result() << std::endl;

    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
    glGenerateMipmap(GL_TEXTURE_2D);

    //auto e = glGetError();

    // Parameters
    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_LINEAR_MIPMAP_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

    glBindTexture(GL_TEXTURE_2D, 0);
    SOIL_free_image_data(image);

    return textureID;
}

好的,这两个函数读取 .mtl 中的纹理名称并从同一目录加载它们。我已经使用调试器逐步执行了代码,宽度和高度设置正确,图像指针指向字符数组。这让我觉得这部分很好。

进一步返回的纹理结构进入我的网格对象的构造函数:

mesh::mesh(std::vector<vertex> vertices, std::vector<GLuint> indices, std::vector<texture> textures)
    : vertices(vertices), textures(textures), indices(indices)
{
    this->setupMesh();
}

在 setupMesh 中,我执行基本属性设置、绑定(bind)到缓冲区等。 在渲染循环中,我调用了如下所示的 mesh->draw() 方法:

void mesh::draw(shader* shader)
{
    GLuint tex_diffNumber = 1;
    GLuint tex_specNumber = 1;

    for (GLuint i = 0; i < this->textures.size() ; i++)
    {
        //load target texture
        glActiveTexture(GL_TEXTURE0 + 1);

        std::stringstream sstream;
        std::string number;
        std::string name = this->textures[i].type;

        if (name == TEX_DIFF_NAME) 
            sstream << tex_diffNumber++;
        else if (name == TEX_SPEC_NAME) 
            sstream << tex_specNumber++;

        number = sstream.str();

        glBindTexture(GL_TEXTURE_2D, this->textures[i].id);
        glUniform1i(glGetUniformLocation(shader->shaderProgID, (name + number).c_str()), i);

    }

    //set shininess
    //glUniform1f(glGetUniformLocation(shader->shaderProgID, "material.shininess"), 16.0f);

    //draw
    glBindVertexArray(this->vao);
    glDrawElements(GL_TRIANGLES, this->indices.size(), GL_UNSIGNED_INT, 0);
    glBindVertexArray(0);

    //release
    for (GLuint i = 0; i < this->textures.size(); i++)
    {
        glActiveTexture(GL_TEXTURE0 + i);
        glBindTexture(GL_TEXTURE_2D, 0);
    }
}

这里重要的一点(恕我直言)是对统一更新的调用:

glUniform1i(glGetUniformLocation(shader->shaderProgID, (name + number).c_str()), i);

再一次,这里没有问题,如果我输入错误,着色器将无法编译,这是着色器:

#version 330 core

in vec2 TexCoords;
in vec4 customColor;

uniform sampler2D tex_diff1;

void main()
{    
    gl_FragColor = vec4(texture(tex_diff1, TexCoords));
}

名称“tex_diff1”是调用 draw() 期间使用的确切成员名称。 此外,我测试了纹理坐标是否良好,我根据 texCoords 的值在片段着色器中输出颜色。结果我得到了彩色模型,这意味着一切都很好。

那么问题来了,少了什么?

有什么想法可以在哪里挖掘和摇晃什么吗?

附言。

Problem image

Texcoords as color

最佳答案

在 for 循环中,您不断地将事件纹理设置为 GL_TEXTURE0 + 1,但您告诉着色器使用纹理单元 i。

很可能你想要这样的东西:

for (GLuint i = 0; i < this->textures.size() ; i++)
{
    glActiveTexture(GL_TEXTURE0 + i);
    glBindTexture(GL_TEXTURE_2D, this->textures[i].id);
    glUniform1i(glGetUniformLocation(shader->shaderProgID, (name + number).c_str()), i);
}

关于c++ - OpenGL(核心配置文件)纹理缺失或黑色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38373342/

相关文章:

c++ - 静态库头文件包括其他头文件。我应该需要那些头文件的实现/源文件吗?

c++ - 在 C++11 中有没有一种好方法可以将对其他对象的引用存储为成员?

c++ - 在类 Base 中实现函数的最有效方法

c++ - OpenGL:在帧中回收帧缓冲区会影响性能吗?

c - WinAPI - 同步交换缓冲区

opengl - 使用 GLSL 对顶点数组进行广告牌?

java - 加载图像 libgdx

C++ - 对象的平滑加速和减速

javascript - 更新纹理的正确方法(THREE.JS)

android - 使用 libgdx 绘制大背景图像 - 最佳实践?