使用opencv为opengl提供纹理图片

5 篇文章 0 订阅
3 篇文章 0 订阅


    class Texture
    {
    public:
      GLuint m_ID=0;
      GLuint m_Slot=0;
      std::string m_FilePath;
      int m_W=0, m_H=0;

      Texture(const std::string &FilePath, unsigned slot) { Create(FilePath, slot); }
      void Create(const std::string& FilePath, unsigned slot)
      {
        m_FilePath = FilePath; m_Slot = slot;

        cv::Mat I = cv::imread(FilePath, cv::ImreadModes::IMREAD_UNCHANGED);    // 按照图片的原本信息读取
        cv::flip(I, I, 0); // 上下翻转, opengl store image from bottom to top while opencv store image from top to bottom
        m_W = I.cols; m_H = I.rows;   // 图片的长宽尺寸
        int channels = I.channels(); // int pixel_size = I.elemSize(); auto channel_size = I.elemSize1(); auto row_size = I.step;

        // 因为opengl存储像素数据的alignment问题,需要进行如下设置才能正常显示图片
        //use fast 4-byte alignment (default anyway) if possible
        glPixelStorei(GL_UNPACK_ALIGNMENT, (I.step & 3) ? 1 : 4);
        //set length of one complete row in data (doesn't need to equal image.cols)
        glPixelStorei(GL_UNPACK_ROW_LENGTH, I.step / I.elemSize());


        glGenTextures(1, &m_ID); Bind();

        // filter settings: GL_NEAREST  GL_LINEAR
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); // when tex is scale down
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); // when tex is scale up

        // mipmap settings:  GL_NEAREST_MIPMAP_NEAREST GL_LINEAR_MIPMAP_NEAREST GL_NEAREST_MIPMAP_LINEAR GL_LINEAR_MIPMAP_LINEAR
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);

        // wrap settings : GL_REPEAT, GL_MIRRORED_REPEAT  GL_CLAMP_TO_EDGE  GL_CLAMP_TO_BORDER
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
        // for GL_CLAMP_TO_BORDER
        // float borderColor[] = { 1.0f, 1.0f, 0.0f, 1.0f }; glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, borderColor); 

        glTexImage2D(GL_TEXTURE_2D,     // Type of texture
          0,                            // Pyramid level (for mip-mapping) - 0 is the top level
          channels==3? GL_RGB : GL_RGBA,     // Internal colour format to convert to
          m_W,                          // Image width  i.e. 640 for Kinect in standard mode
          m_H,                          // Image height i.e. 480 for Kinect in standard mode
          0,                            // Border width in pixels (can either be 1 or 0)
          channels == 3 ? GL_BGR : GL_BGRA,  // Input image format
          GL_UNSIGNED_BYTE,             // Image data type
          I.ptr());

        glGenerateMipmap(GL_TEXTURE_2D);

        Unbind();
      }

      void Bind() const  { glActiveTexture(GL_TEXTURE0 + m_Slot); glBindTexture(GL_TEXTURE_2D, m_ID); }
      void Unbind() const  { glBindTexture(GL_TEXTURE_2D, 0);  }
    };

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qiuzen

您的资助将帮助我创作更好的作品

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值