opengl - 使用 assimp 加载不同的纹理类型

标签 opengl assimp

默认情况下我会加载这样的所有内容

for(int g = 0; g < faces.size(); g++)
{

    glMaterialfv(GL_FRONT, GL_SPECULAR, materials[g].Ks);
    glMaterialfv(GL_FRONT, GL_DIFFUSE, materials[g].Kd);
    glMaterialfv(GL_FRONT, GL_AMBIENT, materials[g].Ka);
    glMaterialf(GL_FRONT, GL_SHININESS, materials[g].Ns);

    Texture t;
    getTexture(&t, materials[g].pict);

    glBindTexture(GL_TEXTURE_2D, t.texID);

    glBegin(GL_TRIANGLES);

    for(int f = 0; f < faces[g].size(); f++)
    {
        glNormal3f(normals[faces[g][f].vn1 - 1].x, normals[faces[g][f].vn1 - 1].y, normals[faces[g][f].vn1 - 1].z);
        glTexCoord2f(texCoords[faces[g][f].vt1 - 1].u, texCoords[faces[g][f].vt1 - 1].v);
        glVertex3f(vertices[faces[g][f].v1 - 1].x, vertices[faces[g][f].v1 - 1].y, vertices[faces[g][f].v1 - 1].z);

        glNormal3f(normals[faces[g][f].vn2 - 1].x, normals[faces[g][f].vn2 - 1].y, normals[faces[g][f].vn2 - 1].z);
        glTexCoord2f(texCoords[faces[g][f].vt2 - 1].u, texCoords[faces[g][f].vt2 - 1].v);
        glVertex3f(vertices[faces[g][f].v2 - 1].x, vertices[faces[g][f].v2 - 1].y, vertices[faces[g][f].v2 - 1].z);

        glNormal3f(normals[faces[g][f].vn3 - 1].x, normals[faces[g][f].vn3 - 1].y, normals[faces[g][f].vn3 - 1].z);
        glTexCoord2f(texCoords[faces[g][f].vt3 - 1].u, texCoords[faces[g][f].vt3 - 1].v);
        glVertex3f(vertices[faces[g][f].v3 - 1].x, vertices[faces[g][f].v3 - 1].y, vertices[faces[g][f].v3 - 1].z);
    }

    glEnd();
}

它非常非常慢,所以我决定使用 assimp。 模型本身加载良好。但它们根本没有任何纹理(以及存在诸如颜色之类的东西) enter image description here

我加载这样的 Material

 for (unsigned int i = 0 ; i < pScene->mNumMaterials ; i++) {
    const aiMaterial* pMaterial = pScene->mMaterials[i];

    m_Textures[i] = NULL;
    //GL_SHININESS GL_SPECULAR GL_DIFFUSE GL_AMBIENT
    if (pMaterial->GetTextureCount(aiTextureType_DIFFUSE) > 0) {
        aiString Path;

        if (pMaterial->GetTexture(aiTextureType_DIFFUSE, 0, &Path, NULL, NULL, NULL, NULL, NULL) == AI_SUCCESS) {
            std::string FullPath = Dir + "/" + Path.data;
            m_Textures[i] = new Texture();
            if (!LoadTGA(m_Textures[i], const_cast<char*>(FullPath.c_str())))
            {
                printf("Error loading texture '%s'\n", FullPath.c_str());
                delete m_Textures[i];
                m_Textures[i] = NULL;
                Ret = false;
            }
            else {
                printf("Loaded texture '%s'\n", FullPath.c_str());
                glGenTextures(1, &m_Textures[i]->texID);
                glBindTexture(GL_TEXTURE_2D, m_Textures[i]->texID);
                glTexImage2D(GL_TEXTURE_2D, 
                                0, 
                                m_Textures[i]->bpp / 8, 
                                m_Textures[i]->width, 
                                m_Textures[i]->height, 
                                0, 
                                m_Textures[i]->type, 
                                GL_UNSIGNED_BYTE, 
                                m_Textures[i]->imageData);
                glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
                glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
            }
        }
    }
}

我将aiTextureType设置为漫反射,但在我的.obj文件中还有光泽、镜面反射、环境 Material 。

我画的都是这样的

glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glEnableVertexAttribArray(2);

for (unsigned int i = 0 ; i < m_Entries.size() ; i++) {
    glBindBuffer(GL_ARRAY_BUFFER, m_Entries[i].VB);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(TVertex), 0);
    glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(TVertex), (const GLvoid*)12);
    glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, sizeof(TVertex), (const GLvoid*)20);

    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_Entries[i].IB);

    const unsigned int MaterialIndex = m_Entries[i].MaterialIndex;

    if (MaterialIndex < m_Textures.size() && m_Textures[MaterialIndex]) {
        glClientActiveTexture(GL_TEXTURE0);
        glBindTexture(GL_TEXTURE_2D, m_Textures[MaterialIndex]->texID);
    }

    glDrawElements(GL_TRIANGLES, m_Entries[i].NumIndices, GL_UNSIGNED_INT, 0);
}

glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
glDisableVertexAttribArray(2);

那么如何加载其他类型的素材呢?

最佳答案

由于这里没有答案,

http://assimp.sourceforge.net/lib_html/material_8h.html#a7dd415ff703a2cc53d1c22ddbbd7dde0

Enumerator:

aiTextureType_NONE
Dummy value.

No texture, but the value to be used as 'texture semantic' (#aiMaterialProperty::mSemantic) for all material properties not related to textures.

aiTextureType_DIFFUSE
The texture is combined with the result of the diffuse lighting equation.

aiTextureType_SPECULAR
The texture is combined with the result of the specular lighting equation.

aiTextureType_AMBIENT
The texture is combined with the result of the ambient lighting equation.

aiTextureType_EMISSIVE
The texture is added to the result of the lighting calculation.

It isn't influenced by incoming light.

aiTextureType_HEIGHT The texture is a height map.

By convention, higher gray-scale values stand for higher elevations from the base height.

aiTextureType_NORMALS
The texture is a (tangent space) normal-map.

Again, there are several conventions for tangent-space normal maps. Assimp does (intentionally) not distinguish here.

aiTextureType_SHININESS
The texture defines the glossiness of the material.

The glossiness is in fact the exponent of the specular (phong) lighting equation. Usually there is a conversion function defined to map the linear color values in the texture to a suitable exponent. Have fun.

aiTextureType_OPACITY
The texture defines per-pixel opacity.

Usually 'white' means opaque and 'black' means 'transparency'. Or quite the opposite. Have fun.

aiTextureType_DISPLACEMENT
Displacement texture.

The exact purpose and format is application-dependent. Higher color values stand for higher vertex displacements.

aiTextureType_LIGHTMAP
Lightmap texture (aka Ambient Occlusion)

Both 'Lightmaps' and dedicated 'ambient occlusion maps' are covered by this material property. The texture contains a scaling value for the final color value of a pixel. Its intensity is not affected by incoming light.

aiTextureType_REFLECTION
Reflection texture.

Contains the color of a perfect mirror reflection. Rarely used, almost never for real-time applications.

aiTextureType_UNKNOWN
Unknown texture.

A texture reference that does not match any of the definitions above is considered to be 'unknown'. It is still imported, but is excluded from any further postprocessing.

关于opengl - 使用 assimp 加载不同的纹理类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20478501/

相关文章:

c++ - QOpenGLWidget 纹理映射导致黑屏

c++ - glDrawElements 可以独立于多边形类型使用吗?

c++ - 使用 Assimp 在运行时更改 .OBJ 的内存纹理

c++ - 是什么导致我的光线追踪器中出现伪影?

opengl - 开始 3D 编程/OpenGL

c - 使用 4.2 OpenGL 时,"glGenTextures"返回 1282 错误

opengl - 使用 Assimp 加载 OBJ 模型时的视觉伪像

c++ - Assimp 对现有 aiVector3D 的访问冲突

c++ - Assimp 链接器错误 - undefined reference

c++ - TrueType 字体缩放会导致文本模糊?