opengl纹理无法正确渲染

时间:2012-12-11 00:53:02

标签: c++ opengl textures

我正在尝试渲染一个狼模型。我有正确渲染的模型。然而,纹理我尚未开始工作。我正在使用OpenGL和AssImp。

这是狼的样子:

what my model looks like

这就是狼的样子:

properly textured

以下是加载模型的代码:

IModel* ModelManager::loadModel(const std::string filename) {

    if (models_[filename] != 0) {
        BOOST_LOG_TRIVIAL(debug) << "Model found.";
        return models_[filename].get();
    }

    const aiScene* scene = aiImportFile(filename.c_str(), aiProcessPreset_TargetRealtime_MaxQuality);

    if (scene == 0) {
        BOOST_LOG_TRIVIAL(debug) << "Unable to load model...";
        return 0;       
    }

    models_[filename] = std::unique_ptr<Model>( new Model(scene) );

    aiReleaseImport(scene);

    BOOST_LOG_TRIVIAL(debug) << "Done loading model '" << filename << "'.";

    return models_[filename].get();
}

以下是为模型加载网格的代码:

Mesh::Mesh(const aiMesh* mesh) {
uint32 currentIndex = 0;

    for (uint32 t = 0; t < mesh->mNumFaces; ++t) {
        const aiFace* face = &mesh->mFaces[t];
        GLenum face_mode;

        switch(face->mNumIndices) {
            case 1: face_mode = GL_POINTS; break;
            case 2: face_mode = GL_LINES; break;
            case 3: face_mode = GL_TRIANGLES; break;
            default: face_mode = GL_POLYGON; break;
        }

        uint32 numIndices = face->mNumIndices;

        vertices_.resize( currentIndex + numIndices );
        normals_.resize( currentIndex + numIndices );
        textureCoordinates_.resize( currentIndex + numIndices );
        colors_.resize( currentIndex + numIndices );

        for(uint32 i = 0; i < numIndices; i++) {
            // get group index for current index
            int vertexIndex = face->mIndices[i];

            if (mesh->mNormals != 0) {
                vertices_[currentIndex + i]     = glm::vec3( mesh->mVertices[vertexIndex].x, mesh->mVertices[vertexIndex].y, mesh->mVertices[vertexIndex].z );
                normals_[currentIndex + i]      = glm::vec3( mesh->mNormals[vertexIndex].x, mesh->mNormals[vertexIndex].y, mesh->mNormals[vertexIndex].z );
            }

            if (mesh->HasTextureCoords(0)) {
                textureCoordinates_[currentIndex + i] = glm::vec2( mesh->mTextureCoords[0][i].x, mesh->mTextureCoords[0][i].y );
            }

            //utilities::AssImpUtilities::color4_to_vec4(&mesh->mColors[0][vertexIndex], colors_[colors_.size() + i]);
            if (mesh->mColors[0] != 0) {
                colors_[currentIndex + i]           = glm::vec4(
                                                        (float)mesh->mColors[0][vertexIndex].a,
                                                        (float)mesh->mColors[0][vertexIndex].b,
                                                        (float)mesh->mColors[0][vertexIndex].g,
                                                        (float)mesh->mColors[0][vertexIndex].r
                                                    );
            }           
        }

        currentIndex += 3;
    }
}

这是加载纹理的代码:

void Texture::loadTexture(utilities::Image* image) {
glGenTextures(1, &textureId_);

    glBindTexture(GL_TEXTURE_2D, textureId_);

    //glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    //glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    glTexParameteri( GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER, GL_LINEAR );
    glTexParameteri( GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER, GL_LINEAR );

    // i don't combine the color with the original surface color, use only the texture map.
    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);

    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, image->width, image->height, 0, GL_RGB, GL_UNSIGNED_BYTE, image->data);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
}

以下是模型的渲染代码:

for (uint32 i=0; i < meshes_.size(); i++) {
        textures_[i]->bind();
        meshes_[i]->render();
    }
}

这是纹理的'bind'代码:

void Texture::bind() {
glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    glEnable( GL_TEXTURE_2D );
    glBindTexture(GL_TEXTURE_2D, textureId_);
}

最后,这是Mesh的渲染代码:

void Mesh::render() {
glBegin( GL_TRIANGLES );

    for (uint32 i = 0; i < vertices_.size(); i++) {
        glTexCoord2fv( &textureCoordinates_[i].x );
        glVertex3fv( &vertices_[i].x );
        glNormal3fv( &normals_[i].x );
    }

    glEnd();
}

我没有看到任何明显的错误。有没有人看到我做错了什么?

1 个答案:

答案 0 :(得分:2)

GAH,我明白了......加载网格时遇到了问题:

for(uint32 i = 0; i < numIndices; i++) {
            // get group index for current index
            int vertexIndex = face->mIndices[i];

            ...

            if (mesh->HasTextureCoords(0)) {
                textureCoordinates_[currentIndex + i] = glm::vec2( mesh->mTextureCoords[0][i].x, mesh->mTextureCoords[0][i].y );
            }

            ...
            }           
        }

应该使用mesh->mTextureCoords[0][vertexIndex].x代替mesh->mTextureCoords[0][i].x(对于y索引也是如此)......

所以,每个循环只使用了错误的纹理坐标索引,因此每个渲染三角形的纹理坐标是相同的3个坐标:S

感谢所有关注此问题的人!