コード例 #1
0
ファイル: AssimpLoader.cpp プロジェクト: TheCrafter/Elesword
//--------------------------------------------------
// AssimpPainter
//--------------------------------------------------
void AssimpPainter::DrawMesh(
    const Shader& shader,
    GLuint vao,
    const Mesh& mesh) const
{
    shader.Use();

    // Bind appropriate textures
    GLuint diffuseNr = 1;
    GLuint specularNr = 1;

    // Bind textures
    std::vector<Texture>::size_type index2 = 0;
    for(Texture texture : mesh.textures)
    {
        glActiveTexture(GL_TEXTURE0 + (GLuint)index2);

        // Retrieve texture number (the N in diffuse_textureN)
        std::string number;
        TextureType type = texture.type;

        // Transfer GLuint to stream
        switch(type)
        {
            case TextureType::DIFFUSE:
                number += (char)diffuseNr++;
                break;

            case TextureType::SPECULAR:
                number += (char)specularNr++;
                break;
            default:
                break;
        }

        // Now set the sampler to the correct texture unit
        glUniform1i(
            glGetUniformLocation(shader.GetProgID(), ("material." + TextureTypeNames[(size_t)type] + number).c_str()),
            (GLuint)index2);

        // And finally bind the texture
        glBindTexture(GL_TEXTURE_2D, texture.id);

        index2++;
    }

    // Also set each mesh's shininess property to a default value
    // (if you want you could extend this to another mesh property and possibly change this value)
    glUniform1f(glGetUniformLocation(shader.GetProgID(), "material.shininess"), 16.0f);

    // Draw mesh
    glBindVertexArray(vao);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mesh.ebo);
    glDrawElements(
        GL_TRIANGLES,
        (GLsizei)mesh.indices.size(),
        GL_UNSIGNED_INT,
        0);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
    glBindVertexArray(0);

    // "Unbind" textures
    index2 = 0;
    for(Texture texture : mesh.textures)
    {
        glActiveTexture(GL_TEXTURE0 + (GLuint)index2);
        glBindTexture(GL_TEXTURE_2D, 0);
    }
}