opengl复习之mesh中的纹理绑定代码顺序以及函数

mesh.h当中的纹理绘制;纹理绑定函数glBindTexture(GL_TEXTURE_2D,textures[i].id)

  • main函数在补全shader和mesh的函数之后,添加绑定skybox用于进行反射渲染的值。
    注意之前用的纹理获取是什么样的,这里注意mesh.H中的纹理单元先激活后绑定glBindTexture(GL_TEXTURE_2D, textures[i].id)
    // render the mesh
    void Draw(Shader shader)
    {
        // bind appropriate textures
        unsigned int diffuseNr = 1;
        unsigned int specularNr = 1;
        unsigned int normalNr = 1;
        unsigned int heightNr = 1;
        for (unsigned int i = 0; i < textures.size(); i++)
        {
            glActiveTexture(GL_TEXTURE0 + i); // active proper texture unit before binding
            // retrieve texture number (the N in diffuse_textureN)
            string number;
            string name = textures[i].type;
            if (name == "texture_diffuse")
                number = std::to_string(static_cast<long long>(diffuseNr++));
            else if (name == "texture_specular")
                number = std::to_string(static_cast<long long>(specularNr++)); // transfer unsigned int to stream
            else if (name == "texture_normal")
                number = std::to_string(static_cast<long long>(normalNr++)); // transfer unsigned int to stream
            else if (name == "texture_height")
                number = std::to_string(static_cast<long long>(heightNr++)); // transfer unsigned int to stream

            // now set the sampler to the correct texture unit
            glUniform1i(glGetUniformLocation(shader.ID, (name + number).c_str()), i);
            // and finally bind the texture
            glBindTexture(GL_TEXTURE_2D, textures[i].id);
        }

        // draw mesh
        glBindVertexArray(VAO);
        glDrawElements(GL_TRIANGLES, indices.size(), GL_UNSIGNED_INT, 0);
        glBindVertexArray(0);

        // always good practice to set everything back to defaults once configured.
        glActiveTexture(GL_TEXTURE0);
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值